mongoid_rails_fixtures 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Mongoid #:nodoc
4
+ class Config #:nodoc
5
+ # current rails behavior is to not migrate before tests
6
+ attr_accessor :migrate_before_tests
7
+ # current rails behavior is to delete existing fixtures before create
8
+ attr_accessor :delete_existing_fixtures_before_create
9
+ end
10
+
11
+ Mongoid.configure.delete_existing_fixtures_before_create = true
12
+
13
+ # originally in AR/database_statements
14
+ # Inserts the given fixture into the table. Overridden in adapters that require
15
+ # something beyond a simple insert (eg. Oracle).
16
+ def self.insert_fixture(fixture, table_name)
17
+ klass = table_name.singularize.capitalize.constantize
18
+ klass.create fixture.instance_variable_get(:@fixture)
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ if defined?(Rails::Railtie)
4
+ module Rails #:nodoc:
5
+ module Mongoid #:nodoc:
6
+ class Railtie < Rails::Railtie
7
+
8
+ rake_tasks do
9
+ load "mongoid_rails_fixtures/tasks/database.rake"
10
+ load "mongoid_rails_fixtures/tasks/test.rake"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ module Mongoid
2
+ class TestCase < ActiveSupport::TestCase #:nodoc:
3
+ def assert_date_from_db(expected, actual, message = nil)
4
+ assert_equal expected.to_s, actual.to_s, message
5
+ end
6
+
7
+ def with_kcode(kcode)
8
+ if RUBY_VERSION < '1.9'
9
+ orig_kcode, $KCODE = $KCODE, kcode
10
+ begin
11
+ yield
12
+ ensure
13
+ $KCODE = orig_kcode
14
+ end
15
+ else
16
+ yield
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ if defined?(Mongoid)
2
+ # TODO: change path after gemify
3
+ require File.dirname(__FILE__) + '/test_case'
4
+
5
+ class ActiveSupport::TestCase
6
+ include Mongoid::TestFixtures
7
+ self.fixture_path = "#{Rails.root}/test/fixtures/"
8
+ end
9
+
10
+ ActiveSupport::TestCase.send :define_method, :fixture_path do
11
+ "#{Rails.root}/test/fixtures/"
12
+ end
13
+
14
+ ActionController::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
15
+
16
+ def create_fixtures(*table_names, &block)
17
+ Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block)
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Timestamps
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ field :created_at, :type => Time
7
+ field :updated_at, :type => Time
8
+ set_callback :create, :before, :set_created_at
9
+ set_callback :save, :before, :set_updated_at
10
+
11
+ class_inheritable_accessor :record_timestamps, :instance_writer => false
12
+ self.record_timestamps = true
13
+ end
14
+
15
+ # Update the created_at field on the Document to the current time. This is
16
+ # only called on create.
17
+ def set_created_at
18
+ self.created_at = Time.now.utc if !created_at
19
+ end
20
+
21
+ # Update the updated_at field on the Document to the current time.
22
+ # This is only called on create and on save.
23
+ def set_updated_at
24
+ self.updated_at = Time.now.utc
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,34 @@
1
+ namespace :db do
2
+ namespace :fixtures do
3
+ desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
4
+ task :load => :environment do
5
+ base_dir = ENV['FIXTURES_PATH'] ? File.join(Rails.root, ENV['FIXTURES_PATH']) : File.join(Rails.root, 'test', 'fixtures')
6
+ fixtures_dir = ENV['FIXTURES_DIR'] ? File.join(base_dir, ENV['FIXTURES_DIR']) : base_dir
7
+
8
+ (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/).map {|f| File.join(fixtures_dir, f) } : Dir.glob(File.join(fixtures_dir, '*.{yml,csv}'))).each do |fixture_file|
9
+ Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*'))
10
+ end
11
+ end
12
+
13
+ desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
14
+ task :identify => :environment do
15
+ label, id = ENV["LABEL"], ENV["ID"]
16
+ raise "LABEL or ID required" if label.blank? && id.blank?
17
+
18
+ puts %Q(The fixture ID for "#{label}" is #{Fixtures.identify(label)}.) if label
19
+
20
+ base_dir = ENV['FIXTURES_PATH'] ? File.join(Rails.root, ENV['FIXTURES_PATH']) : File.join(Rails.root, 'test', 'fixtures')
21
+ Dir["#{base_dir}/**/*.yml"].each do |file|
22
+ if data = YAML::load(ERB.new(IO.read(file)).result)
23
+ data.keys.each do |key|
24
+ key_id = Fixtures.identify(key)
25
+
26
+ if key == label || key_id == id.to_i
27
+ puts "#{file}: #{key} (#{key_id})"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,8 @@
1
+ namespace :test do
2
+ task :prepare do
3
+ if Mongoid.configure.migrate_before_tests
4
+ puts "Migrating db"
5
+ Rake::Task['db:migrate'].invoke
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.platform = Gem::Platform::RUBY
3
+ s.name = 'mongoid_rails_fixtures'
4
+ s.version = '0.0.1'
5
+ s.summary = 'Fixtures for Mongoid in classic rails style.'
6
+ s.description = 'Retain your old testing workflow with Mongodb power'
7
+
8
+ # only tested with 1.9.1
9
+ s.required_ruby_version = '>= 1.8.6'
10
+ s.required_rubygems_version = ">= 1.3.6"
11
+
12
+ s.author = 'Alan Da Costa'
13
+ s.email = 'alandacosta@gmail.com.com'
14
+ s.date = %q{2010-06-29}
15
+ s.homepage = "http://github.com/adacosta/#{s.name}"
16
+
17
+ s.require_paths = ["lib"]
18
+ s.files = Dir['.gitignore', 'Gemfile', 'Rakefile', 'README.rdoc', "#{s.name}.gemspec", 'lib/**/*']
19
+ s.test_files = Dir['test/**/*']
20
+ s.has_rdoc = false
21
+
22
+ rails_version = '>= 3.0.0.rc'
23
+
24
+ s.add_dependency('bundler', '>=0.9.26')
25
+ s.add_dependency('mongoid', '>=2.0.0.beta.7')
26
+ s.add_dependency('rails', rails_version)
27
+ s.add_dependency('railties', rails_version)
28
+ s.add_dependency('activesupport', rails_version)
29
+ end
data/test/config.rb ADDED
@@ -0,0 +1,43 @@
1
+ # we have to beat the main lib to the bundle load in order to have Rails present for testing
2
+ require 'rubygems'
3
+ # Set up gems listed in the Gemfile.
4
+ if File.exist?( File.dirname(__FILE__) + "/../Gemfile")
5
+ require 'bundler'
6
+ Bundler.setup
7
+ end
8
+
9
+ Bundler.require(:default) if defined?(Bundler)
10
+
11
+ require 'test/unit'
12
+
13
+ require File.dirname(__FILE__) + '/../lib/mongoid_rails_fixtures'
14
+
15
+ # require all models
16
+ Dir[File.dirname(__FILE__) + "/models/*.rb"].each {|file| require file }
17
+
18
+ Rake.application.add_import File.dirname(__FILE__) + '/test_helper.rake'
19
+ Rake.application.add_import File.dirname(__FILE__) + '/../lib/mongoid_rails_fixtures/tasks/database.rake'
20
+ Rake.application.load_imports
21
+
22
+ module MongoidRailsFixtures
23
+ class TestRailsApp < Rails::Application; end
24
+ end
25
+ MongoidRailsFixtures::TestRailsApp.initialize!
26
+
27
+ # connect to our local mongodb
28
+ Mongoid.configure do |config|
29
+ config.master = Mongo::Connection.new.db("mongoid_test")
30
+ end
31
+
32
+ # borrowed from http://thinkingdigitally.com/archive/capturing-output-from-puts-in-ruby/
33
+ require 'stringio'
34
+ module Kernel
35
+ def capture_stdout
36
+ out = StringIO.new
37
+ $stdout = out
38
+ yield
39
+ return out
40
+ ensure
41
+ $stdout = STDOUT
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/config'
2
+
3
+ module Mongoid
4
+ class TestCase < ActiveSupport::TestCase #:nodoc:
5
+
6
+ def setup
7
+ drop_collections
8
+ end
9
+
10
+ def teardown; end
11
+
12
+ def test_fixture_invoke
13
+ # verify db is empty
14
+ bob = User.where(:username => 'bob').first
15
+ assert_equal 0, User.count, "Users should be empty loaded"
16
+
17
+ # load and verify fixture
18
+ Rake::Task['db:fixtures:load'].invoke
19
+ bob = User.where(:username => 'bob').first
20
+ assert (User.count > 0), "Users should have loaded"
21
+ assert bob.respond_to?(:username), "Bob should have a username"
22
+ end
23
+
24
+ def test_identify_fixture
25
+ ENV["LABEL"] = 'users'
26
+ output = capture_stdout do
27
+ Rake::Task['db:fixtures:identify'].invoke
28
+ end
29
+ assert_equal 'The fixture ID for "users" is 344172009.', output.string.chomp, "Identity should have ID for users"
30
+ end
31
+
32
+ private
33
+
34
+ def drop_collections
35
+ (Mongoid.master.collections.reject { |c| c.name == 'system.indexes' }).map(&:drop)
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ user_1:
2
+ username: bob
3
+ is_active: true
4
+ roles:
5
+ - user
@@ -0,0 +1,10 @@
1
+ class User
2
+ include Mongoid::Document
3
+
4
+ field :username
5
+ field :is_active, :type => Boolean
6
+ field :roles, :type => Array
7
+
8
+ validates_presence_of :username
9
+ validates_uniqueness_of :username
10
+ end
@@ -0,0 +1,9 @@
1
+ # stub for rails environment rake task
2
+ unless Rake.application.tasks.include? 'db:environment'
3
+ namespace :db do
4
+ desc "stub environment"
5
+ task :environment do
6
+ 'test'
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_rails_fixtures
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Alan Da Costa
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-29 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 0
32
+ - 9
33
+ - 26
34
+ version: 0.9.26
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: mongoid
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 4246949665
46
+ segments:
47
+ - 2
48
+ - 0
49
+ - 0
50
+ - beta
51
+ - 7
52
+ version: 2.0.0.beta.7
53
+ type: :runtime
54
+ version_requirements: *id002
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ prerelease: false
58
+ requirement: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 1005551048
64
+ segments:
65
+ - 3
66
+ - 0
67
+ - 0
68
+ - rc
69
+ version: 3.0.0.rc
70
+ type: :runtime
71
+ version_requirements: *id003
72
+ - !ruby/object:Gem::Dependency
73
+ name: railties
74
+ prerelease: false
75
+ requirement: &id004 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 1005551048
81
+ segments:
82
+ - 3
83
+ - 0
84
+ - 0
85
+ - rc
86
+ version: 3.0.0.rc
87
+ type: :runtime
88
+ version_requirements: *id004
89
+ - !ruby/object:Gem::Dependency
90
+ name: activesupport
91
+ prerelease: false
92
+ requirement: &id005 !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 1005551048
98
+ segments:
99
+ - 3
100
+ - 0
101
+ - 0
102
+ - rc
103
+ version: 3.0.0.rc
104
+ type: :runtime
105
+ version_requirements: *id005
106
+ description: Retain your old testing workflow with Mongodb power
107
+ email: alandacosta@gmail.com.com
108
+ executables: []
109
+
110
+ extensions: []
111
+
112
+ extra_rdoc_files: []
113
+
114
+ files:
115
+ - .gitignore
116
+ - Gemfile
117
+ - Rakefile
118
+ - README.rdoc
119
+ - mongoid_rails_fixtures.gemspec
120
+ - lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb
121
+ - lib/mongoid_rails_fixtures/mongoid_ext/mongoid_ext.rb
122
+ - lib/mongoid_rails_fixtures/mongoid_ext/railtie_ext.rb
123
+ - lib/mongoid_rails_fixtures/mongoid_ext/test_case.rb
124
+ - lib/mongoid_rails_fixtures/mongoid_ext/test_help.rb
125
+ - lib/mongoid_rails_fixtures/mongoid_ext/timestamps_ext.rb
126
+ - lib/mongoid_rails_fixtures/tasks/database.rake
127
+ - lib/mongoid_rails_fixtures/tasks/test.rake
128
+ - lib/mongoid_rails_fixtures.rb
129
+ - test/config.rb
130
+ - test/fixture_test.rb
131
+ - test/fixtures/users.yml
132
+ - test/models/user.rb
133
+ - test/test_helper.rake
134
+ has_rdoc: true
135
+ homepage: http://github.com/adacosta/mongoid_rails_fixtures
136
+ licenses: []
137
+
138
+ post_install_message:
139
+ rdoc_options: []
140
+
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 59
149
+ segments:
150
+ - 1
151
+ - 8
152
+ - 6
153
+ version: 1.8.6
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ none: false
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ hash: 23
160
+ segments:
161
+ - 1
162
+ - 3
163
+ - 6
164
+ version: 1.3.6
165
+ requirements: []
166
+
167
+ rubyforge_project:
168
+ rubygems_version: 1.3.7
169
+ signing_key:
170
+ specification_version: 3
171
+ summary: Fixtures for Mongoid in classic rails style.
172
+ test_files:
173
+ - test/config.rb
174
+ - test/fixture_test.rb
175
+ - test/fixtures/users.yml
176
+ - test/models/user.rb
177
+ - test/test_helper.rake