mongoid-fixture_set 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Rakefile +15 -0
- data/lib/mongoid-fixture_set.rb +2 -0
- data/lib/mongoid/fixture_set.rb +242 -0
- data/lib/mongoid/fixture_set/class_cache.rb +29 -0
- data/lib/mongoid/fixture_set/errors.rb +13 -0
- data/lib/mongoid/fixture_set/file.rb +61 -0
- data/lib/mongoid/fixture_set/fixture.rb +41 -0
- data/lib/mongoid/fixture_set/test_helper.rb +88 -0
- data/lib/mongoid/fixture_set/version.rb +6 -0
- data/test/fixtures/groups.yml +11 -0
- data/test/fixtures/not_models.yml +2 -0
- data/test/fixtures/organisations.yml +4 -0
- data/test/fixtures/schools.yml +9 -0
- data/test/fixtures/users.yml +9 -0
- data/test/fixtures/users/family.yml +4 -0
- data/test/models/group.rb +13 -0
- data/test/models/organisation.rb +9 -0
- data/test/models/school.rb +9 -0
- data/test/models/user.rb +12 -0
- data/test/mongoid.yml +6 -0
- data/test/mongoid/fixture_set_test.rb +72 -0
- data/test/mongoid/fixtures_test.rb +15 -0
- data/test/test_helper.rb +22 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 297ec45702b1e85a83fda02e8be090acaac190ea
|
4
|
+
data.tar.gz: d6d94d95f55e846592e4db664076c0b9ab0003b5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e6f4172a8e2718ff390cab895326a909d9cf3d04b15a087118ccb416a91ad18ad3a80c2efae8173729fbed1390fb69fc171466d12482bb75fca06e68b5975694
|
7
|
+
data.tar.gz: cda8caed33dd8e1362a503f59e62d249a46e65547c9b987f1455e76438713642c4ea0be9dfaa2348280d7700898fd2e68a8027a3512296ed99fdc0e6c60653ca
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rake/testtask"
|
4
|
+
|
5
|
+
desc 'Default: run unit tests'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Run tests'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,242 @@
|
|
1
|
+
require 'mongoid/fixture_set/fixture'
|
2
|
+
require 'mongoid/fixture_set/file'
|
3
|
+
require 'mongoid/fixture_set/class_cache'
|
4
|
+
require 'mongoid/fixture_set/test_helper'
|
5
|
+
|
6
|
+
require 'active_support/core_ext/digest/uuid'
|
7
|
+
|
8
|
+
module Mongoid
|
9
|
+
class FixtureSet
|
10
|
+
@@cached_fixtures = Hash.new
|
11
|
+
|
12
|
+
cattr_accessor :all_loaded_fixtures
|
13
|
+
self.all_loaded_fixtures = {}
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def context_class
|
17
|
+
@context_class ||= Class.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def cached_fixtures(keys_to_fetch = nil)
|
21
|
+
if keys_to_fetch
|
22
|
+
@@cached_fixtures.values_at(*keys_to_fetch)
|
23
|
+
else
|
24
|
+
@@cached_fixtures.values
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def reset_cache
|
29
|
+
@@cached_fixtures.clear
|
30
|
+
end
|
31
|
+
|
32
|
+
def fixture_is_cached?(name)
|
33
|
+
@@cached_fixtures[name]
|
34
|
+
end
|
35
|
+
|
36
|
+
def cache_fixtures(fixtures_map)
|
37
|
+
@@cached_fixtures.update(fixtures_map)
|
38
|
+
end
|
39
|
+
|
40
|
+
def default_fixture_model_name(fixture_set_name)
|
41
|
+
fixture_set_name.singularize.camelize
|
42
|
+
end
|
43
|
+
|
44
|
+
def update_all_loaded_fixtures(fixtures_map)
|
45
|
+
all_loaded_fixtures.update(fixtures_map)
|
46
|
+
end
|
47
|
+
|
48
|
+
def create_fixtures(fixtures_directory, fixture_set_names, class_names = {})
|
49
|
+
fixture_set_names = Array(fixture_set_names).map(&:to_s)
|
50
|
+
class_names = ClassCache.new(class_names)
|
51
|
+
|
52
|
+
files_to_read = fixture_set_names.reject { |fs_name|
|
53
|
+
fixture_is_cached?(fs_name)
|
54
|
+
}
|
55
|
+
|
56
|
+
if files_to_read.empty?
|
57
|
+
return cached_fixtures(fixture_set_names)
|
58
|
+
end
|
59
|
+
|
60
|
+
fixtures_map = {}
|
61
|
+
fixture_sets = files_to_read.map do |fs_name|
|
62
|
+
klass = class_names[fs_name]
|
63
|
+
fixtures_map[fs_name] = Mongoid::FixtureSet.new(
|
64
|
+
fs_name,
|
65
|
+
klass,
|
66
|
+
::File.join(fixtures_directory, fs_name))
|
67
|
+
end
|
68
|
+
|
69
|
+
update_all_loaded_fixtures fixtures_map
|
70
|
+
|
71
|
+
fixture_sets.each do |fs|
|
72
|
+
fs.collection_documents.each do |model, documents|
|
73
|
+
model = class_names[model]
|
74
|
+
if model
|
75
|
+
documents.each do |attributes|
|
76
|
+
create_or_update_document(model, attributes)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
cache_fixtures(fixtures_map)
|
83
|
+
|
84
|
+
return cached_fixtures(fixture_set_names)
|
85
|
+
end
|
86
|
+
|
87
|
+
def create_or_update_document(model, attributes)
|
88
|
+
document = model.find_or_initialize_by('_id' => attributes['_id'])
|
89
|
+
keys = (attributes.keys + document.attributes.keys).uniq
|
90
|
+
attrs = Hash[keys.collect do |key|
|
91
|
+
if attributes[key].is_a?(Array) || document.attributes[key].is_a?(Array)
|
92
|
+
value = [attributes[key], document.attributes[key]].flatten(1).compact
|
93
|
+
else
|
94
|
+
value = attributes[key] || document.attributes[key]
|
95
|
+
end
|
96
|
+
[key, value]
|
97
|
+
end]
|
98
|
+
document.assign_attributes(attrs)
|
99
|
+
document.save(validate: false)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Returns a consistent, platform-independent identifier for +label+.
|
103
|
+
# UUIDs are RFC 4122 version 5 SHA-1 hashes.
|
104
|
+
def identify(model, label)
|
105
|
+
Digest::UUID.uuid_v5(Digest::UUID::OID_NAMESPACE, "#{model}##{label}")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
attr_reader :name, :path, :model_class, :class_name
|
110
|
+
attr_reader :fixtures
|
111
|
+
|
112
|
+
def initialize(name, class_name, path)
|
113
|
+
@name = name
|
114
|
+
@path = path
|
115
|
+
|
116
|
+
if class_name.is_a?(Class)
|
117
|
+
@model_class = class_name
|
118
|
+
elsif class_name
|
119
|
+
@model_class = class_name.safe_constantize
|
120
|
+
end
|
121
|
+
|
122
|
+
@class_name = @model_class.respond_to?(:name) ?
|
123
|
+
@model_class.name :
|
124
|
+
self.class.default_fixture_model_name(name)
|
125
|
+
|
126
|
+
@fixtures = read_fixture_files
|
127
|
+
end
|
128
|
+
|
129
|
+
def [](x)
|
130
|
+
fixtures[x]
|
131
|
+
end
|
132
|
+
|
133
|
+
# Returns a hash of documents to be inserted. The key is the model class, the value is
|
134
|
+
# a list of documents to insert in th relative collection.
|
135
|
+
def collection_documents
|
136
|
+
# allow a standard key to be used for doing defaults in YAML
|
137
|
+
fixtures.delete('DEFAULTS')
|
138
|
+
|
139
|
+
# track any join collection we need to insert later
|
140
|
+
documents = Hash.new
|
141
|
+
|
142
|
+
documents[class_name] = fixtures.map do |label, fixture|
|
143
|
+
attributes = fixture.to_hash
|
144
|
+
|
145
|
+
next attributes if model_class.nil?
|
146
|
+
|
147
|
+
set_attributes_timestamps(attributes, model_class)
|
148
|
+
|
149
|
+
# interpolate the fixture label
|
150
|
+
attributes.each do |key, value|
|
151
|
+
attributes[key] = value.gsub("$LABEL", label) if value.is_a?(String)
|
152
|
+
end
|
153
|
+
|
154
|
+
attributes['_id'] = self.class.identify(class_name, label)
|
155
|
+
|
156
|
+
model_class.relations.each do |name, relation|
|
157
|
+
case relation.macro
|
158
|
+
when :belongs_to
|
159
|
+
if value = attributes.delete(relation.name.to_s)
|
160
|
+
id = self.class.identify(relation.class_name, value)
|
161
|
+
if relation.polymorphic? && value.sub!(/\s*\(([^)]*)\)\s*/, '')
|
162
|
+
type = $1
|
163
|
+
attributes[relation.foreign_key.sub(/_id$/, '_type')] = type
|
164
|
+
id = self.class.identify(type, value)
|
165
|
+
end
|
166
|
+
attributes[relation.foreign_key] = id
|
167
|
+
end
|
168
|
+
when :has_many
|
169
|
+
if values = attributes.delete(relation.name.to_s)
|
170
|
+
values.each do |value|
|
171
|
+
id = self.class.identify(relation.class_name, value)
|
172
|
+
if relation.polymorphic?
|
173
|
+
self.class.create_or_update_document(relation.class_name.constantize, {
|
174
|
+
'_id' => id,
|
175
|
+
"#{relation.as}_id" => attributes['_id'],
|
176
|
+
"#{relation.as}_type" => model_class.name,
|
177
|
+
})
|
178
|
+
else
|
179
|
+
self.class.create_or_update_document(relation.class_name.constantize, {
|
180
|
+
'_id' => id,
|
181
|
+
relation.foreign_key => attributes['_id'],
|
182
|
+
})
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
when :has_and_belongs_to_many
|
187
|
+
if values = attributes.delete(relation.name.to_s)
|
188
|
+
key = "#{relation.name.to_s.singularize}_ids"
|
189
|
+
attributes[key] = []
|
190
|
+
|
191
|
+
values.each do |value|
|
192
|
+
id = self.class.identify(relation.class_name, value)
|
193
|
+
attributes[key] << id
|
194
|
+
|
195
|
+
self.class.create_or_update_document(relation.class_name.constantize, {
|
196
|
+
'_id' => id,
|
197
|
+
relation.inverse_foreign_key => [attributes['_id']]
|
198
|
+
})
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
attributes
|
205
|
+
end
|
206
|
+
|
207
|
+
return documents
|
208
|
+
end
|
209
|
+
|
210
|
+
private
|
211
|
+
def set_attributes_timestamps(attributes, model_class)
|
212
|
+
now = Time.now.utc
|
213
|
+
|
214
|
+
if model_class < Mongoid::Timestamps::Created::Short
|
215
|
+
attributes['c_at'] = now unless attributes.has_key?('c_at')
|
216
|
+
elsif model_class < Mongoid::Timestamps::Created
|
217
|
+
attributes['created_at'] = now unless attributes.has_key?('created_at')
|
218
|
+
end
|
219
|
+
|
220
|
+
if model_class < Mongoid::Timestamps::Updated::Short
|
221
|
+
attributes['u_at'] = now unless attributes.has_key?('u_at')
|
222
|
+
elsif model_class < Mongoid::Timestamps::Updated
|
223
|
+
attributes['updated_at'] = now unless attributes.has_key?('updated_at')
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
def read_fixture_files
|
228
|
+
yaml_files = Dir["#{path}/{**,*}/*.yml"].select { |f|
|
229
|
+
::File.file?(f)
|
230
|
+
} + ["#{path}.yml"]
|
231
|
+
|
232
|
+
yaml_files.each_with_object({}) do |file, fixtures|
|
233
|
+
Mongoid::FixtureSet::File.open(file) do |fh|
|
234
|
+
fh.each do |fixture_name, row|
|
235
|
+
fixtures[fixture_name] = Mongoid::FixtureSet::Fixture.new(fixture_name, row, model_class)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class ClassCache
|
2
|
+
def initialize(class_names)
|
3
|
+
@class_names = class_names.stringify_keys
|
4
|
+
# Remove string values that aren't constants or subclasses of Mongoid::Document
|
5
|
+
@class_names.delete_if { |klass_name, klass| !insert_class(@class_names, klass_name, klass) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def [](fs_name)
|
9
|
+
@class_names.fetch(fs_name) {
|
10
|
+
klass = default_fixture_model(fs_name).safe_constantize
|
11
|
+
insert_class(@class_names, fs_name, klass)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def insert_class(class_names, name, klass)
|
17
|
+
# We only want to deal with Mongoid objects.
|
18
|
+
if klass && klass < Mongoid::Document
|
19
|
+
class_names[name] = klass
|
20
|
+
else
|
21
|
+
class_names[name] = nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def default_fixture_model(fs_name)
|
26
|
+
Mongoid::FixtureSet.default_fixture_model_name(fs_name)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module Mongoid
|
5
|
+
class FixtureSet
|
6
|
+
|
7
|
+
class RenderContext
|
8
|
+
def self.create_subclass
|
9
|
+
Class.new Mongoid::FixtureSet.context_class do
|
10
|
+
def get_binding
|
11
|
+
binding()
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Mongoid::FixtureSet::File # :nodoc:
|
18
|
+
include Enumerable
|
19
|
+
|
20
|
+
def self.open(file)
|
21
|
+
x = new file
|
22
|
+
block_given? ? yield(x) : x
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(file)
|
26
|
+
@file = file
|
27
|
+
@rows = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
def each(&block)
|
31
|
+
rows.each(&block)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def rows
|
36
|
+
return @rows if @rows
|
37
|
+
begin
|
38
|
+
data = YAML.load(render(IO.read(@file)))
|
39
|
+
rescue ArgumentError, Psych::SyntaxError => error
|
40
|
+
raise FormatError, "a YAML error occurred parsing #{@file}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace
|
41
|
+
end
|
42
|
+
@rows = data ? validate(data).to_a : []
|
43
|
+
end
|
44
|
+
|
45
|
+
def render(content)
|
46
|
+
context = RenderContext.create_subclass.new
|
47
|
+
ERB.new(content).result(context.get_binding)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Validate our unmarshalled data.
|
51
|
+
def validate(data)
|
52
|
+
unless Hash === data || YAML::Omap === data
|
53
|
+
raise FormatError, 'fixture is not a hash'
|
54
|
+
end
|
55
|
+
raise FormatError unless data.all? { |name, row| Hash === row }
|
56
|
+
data
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'mongoid/fixture_set/errors'
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
class FixtureSet
|
5
|
+
class Fixture
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
attr_reader :name, :fixture, :model_class
|
9
|
+
def initialize(name, fixture, model_class)
|
10
|
+
@name = name
|
11
|
+
@fixture = fixture
|
12
|
+
@model_class = model_class
|
13
|
+
end
|
14
|
+
|
15
|
+
def class_name
|
16
|
+
model_class.name if model_class
|
17
|
+
end
|
18
|
+
|
19
|
+
def each
|
20
|
+
fixture.each { |item| yield item }
|
21
|
+
end
|
22
|
+
|
23
|
+
def [](key)
|
24
|
+
fixture[key]
|
25
|
+
end
|
26
|
+
|
27
|
+
alias :to_hash :fixture
|
28
|
+
|
29
|
+
def find
|
30
|
+
if model_class
|
31
|
+
model_class.unscoped do
|
32
|
+
model_class.find(Mongoid::FixtureSet.identify(model_class, name))
|
33
|
+
end
|
34
|
+
else
|
35
|
+
raise FixtureClassNotFound, "No class attached to find."
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
class FixtureSet
|
5
|
+
module TestHelper
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def before_setup
|
9
|
+
setup_fixtures
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def after_teardown
|
14
|
+
super
|
15
|
+
teardown_fixtures
|
16
|
+
end
|
17
|
+
|
18
|
+
included do
|
19
|
+
class_attribute :fixture_path, :instance_writer => false
|
20
|
+
class_attribute :fixture_set_names
|
21
|
+
|
22
|
+
self.fixture_set_names = []
|
23
|
+
end
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
def fixtures(*fixture_set_names)
|
27
|
+
if fixture_set_names.first == :all
|
28
|
+
fixture_set_names = Dir["#{fixture_path}/{**,*}/*.{yml}"]
|
29
|
+
fixture_set_names.map! { |f| f[(fixture_path.to_s.size + 1)..-5] }
|
30
|
+
else
|
31
|
+
fixture_set_names = fixture_set_names.flatten.map(&:to_s)
|
32
|
+
end
|
33
|
+
self.fixture_set_names |= fixture_set_names
|
34
|
+
setup_fixture_accessors(fixture_set_names)
|
35
|
+
end
|
36
|
+
|
37
|
+
def setup_fixture_accessors(fixture_set_names = nil)
|
38
|
+
fixture_set_names = Array(fixture_set_names || self.fixture_set_names)
|
39
|
+
methods = Module.new do
|
40
|
+
fixture_set_names.each do |fs_name|
|
41
|
+
fs_name = fs_name.to_s
|
42
|
+
accessor_name = fs_name.tr('/', '_').to_sym
|
43
|
+
define_method(accessor_name) do |*fixture_names|
|
44
|
+
force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload
|
45
|
+
@fixture_cache[fs_name] ||= {}
|
46
|
+
instances = fixture_names.map do |f_name|
|
47
|
+
f_name = f_name.to_s
|
48
|
+
@fixture_cache[fs_name].delete(f_name) if force_reload
|
49
|
+
if @loaded_fixtures[fs_name] && @loaded_fixtures[fs_name][f_name]
|
50
|
+
@fixture_cache[fs_name][f_name] ||= @loaded_fixtures[fs_name][f_name].find
|
51
|
+
else
|
52
|
+
raise StandardError, "No fixture named '#{f_name}' found for fixture set '#{fs_name}'"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
instances.size == 1 ? instances.first : instances
|
56
|
+
end
|
57
|
+
private accessor_name
|
58
|
+
end
|
59
|
+
end
|
60
|
+
include methods
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def setup_fixtures
|
65
|
+
@fixture_cache = {}
|
66
|
+
|
67
|
+
Mongoid::FixtureSet.reset_cache
|
68
|
+
@loaded_fixtures = load_fixtures
|
69
|
+
end
|
70
|
+
|
71
|
+
def teardown_fixtures
|
72
|
+
Mongoid::FixtureSet.reset_cache
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
def load_fixtures
|
77
|
+
fixture_set_names = self.class.fixture_set_names
|
78
|
+
if fixture_set_names.empty?
|
79
|
+
self.class.fixtures(:all)
|
80
|
+
fixture_set_names = self.class.fixture_set_names
|
81
|
+
end
|
82
|
+
fixtures = Mongoid::FixtureSet.create_fixtures(fixture_path, fixture_set_names)
|
83
|
+
Hash[fixtures.map { |f| [f.name, f] }]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Group
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Timestamps::Created::Short
|
4
|
+
|
5
|
+
field :name
|
6
|
+
|
7
|
+
has_many :main_users, class_name: 'User', inverse_of: :main_group
|
8
|
+
|
9
|
+
has_and_belongs_to_many :users
|
10
|
+
|
11
|
+
belongs_to :something, polymorphic: true
|
12
|
+
end
|
13
|
+
|
data/test/models/user.rb
ADDED
data/test/mongoid.yml
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
module Mongoid
|
5
|
+
class FixtureSetTest < BaseTest
|
6
|
+
def test_should_initialize_fixture_set
|
7
|
+
Mongoid::FixtureSet.reset_cache
|
8
|
+
fs = Mongoid::FixtureSet.new('users', 'User', 'test/fixtures/users')
|
9
|
+
assert_equal User, fs.model_class
|
10
|
+
fixture = fs['geoffroy']
|
11
|
+
assert_equal 'User', fixture.class_name
|
12
|
+
assert_equal 'Geoffroy', fixture['firstname']
|
13
|
+
assert_equal 'Planquart', fixture['lastname']
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_not_create_fixtures
|
17
|
+
Mongoid::FixtureSet.reset_cache
|
18
|
+
fs = Mongoid::FixtureSet.create_fixtures('test/fixtures/', [])
|
19
|
+
assert_equal 0, fs.count
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_should_create_not_model_fixture
|
23
|
+
Mongoid::FixtureSet.reset_cache
|
24
|
+
fs = Mongoid::FixtureSet.create_fixtures('test/fixtures', %w(not_models))
|
25
|
+
fs = fs.first
|
26
|
+
fixture = fs['error']
|
27
|
+
|
28
|
+
begin
|
29
|
+
fixture.find
|
30
|
+
assert false, 'No exception has been raised'
|
31
|
+
rescue Mongoid::FixtureSet::FixtureClassNotFound
|
32
|
+
assert true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_should_create_fixtures
|
37
|
+
Mongoid::FixtureSet.reset_cache
|
38
|
+
fs = Mongoid::FixtureSet.create_fixtures('test/fixtures/', %w(users groups schools organisations))
|
39
|
+
|
40
|
+
users = fs.find{|x| x.model_class == User}
|
41
|
+
f_geoffroy = users['geoffroy']
|
42
|
+
|
43
|
+
assert_equal 6, School.count
|
44
|
+
assert_equal 4, User.count
|
45
|
+
|
46
|
+
geoffroy = User.find_by(firstname: 'Geoffroy')
|
47
|
+
user1 = User.find_by(firstname: 'Margot')
|
48
|
+
sudoers = Group.find_by(name: 'Sudoers!')
|
49
|
+
print = Group.find_by(name: 'Print')
|
50
|
+
group1 = Group.find_by(name: 'Margot')
|
51
|
+
orga1 = Organisation.find_by(name: '1 Organisation')
|
52
|
+
school = School.find_by(name: 'School')
|
53
|
+
|
54
|
+
assert_equal geoffroy, f_geoffroy.find
|
55
|
+
assert_equal 2, print.users.count
|
56
|
+
assert print.users.include?(geoffroy)
|
57
|
+
assert print.users.include?(user1)
|
58
|
+
assert sudoers.main_users.include?(geoffroy)
|
59
|
+
assert_equal group1, user1.main_group
|
60
|
+
assert_equal print, user1.groups.first
|
61
|
+
|
62
|
+
assert_equal 1, school.groups.count
|
63
|
+
assert_equal group1, school.groups.first
|
64
|
+
assert_equal school, group1.something
|
65
|
+
|
66
|
+
assert_equal 2, orga1.groups.count
|
67
|
+
assert orga1.groups.include?(sudoers)
|
68
|
+
assert_equal orga1, sudoers.something
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class FixturesTest < BaseTest
|
2
|
+
include Mongoid::FixtureSet::TestHelper
|
3
|
+
self.fixture_path = 'test/fixtures'
|
4
|
+
|
5
|
+
def test_should_access_fixtures
|
6
|
+
begin
|
7
|
+
geoffroy = users(:geoffroy)
|
8
|
+
assert true
|
9
|
+
rescue
|
10
|
+
assert false, 'An exception was thrown'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.configure do
|
4
|
+
add_filter '/test/'
|
5
|
+
end
|
6
|
+
SimpleCov.start if ENV['COVERAGE']
|
7
|
+
|
8
|
+
require 'minitest/autorun'
|
9
|
+
require 'mongoid'
|
10
|
+
|
11
|
+
require File.expand_path("../../lib/mongoid-fixture_set", __FILE__)
|
12
|
+
|
13
|
+
Mongoid.load!("#{File.dirname(__FILE__)}/mongoid.yml", "test")
|
14
|
+
|
15
|
+
Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
|
16
|
+
|
17
|
+
class BaseTest < ActiveSupport::TestCase
|
18
|
+
def teardown
|
19
|
+
Mongoid::Sessions.default.use('mongoid_fixture_set_test').drop
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-fixture_set
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Geoffroy Planquart
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
description: Let you use fixtures with Mongoid the same way you did with ActiveRecord
|
56
|
+
email:
|
57
|
+
- geoffroy@planquart.fr
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- lib/mongoid/fixture_set/version.rb
|
63
|
+
- lib/mongoid/fixture_set/errors.rb
|
64
|
+
- lib/mongoid/fixture_set/file.rb
|
65
|
+
- lib/mongoid/fixture_set/class_cache.rb
|
66
|
+
- lib/mongoid/fixture_set/test_helper.rb
|
67
|
+
- lib/mongoid/fixture_set/fixture.rb
|
68
|
+
- lib/mongoid/fixture_set.rb
|
69
|
+
- lib/mongoid-fixture_set.rb
|
70
|
+
- Rakefile
|
71
|
+
- test/mongoid/fixtures_test.rb
|
72
|
+
- test/mongoid/fixture_set_test.rb
|
73
|
+
- test/models/group.rb
|
74
|
+
- test/models/organisation.rb
|
75
|
+
- test/models/school.rb
|
76
|
+
- test/models/user.rb
|
77
|
+
- test/mongoid.yml
|
78
|
+
- test/fixtures/users.yml
|
79
|
+
- test/fixtures/groups.yml
|
80
|
+
- test/fixtures/schools.yml
|
81
|
+
- test/fixtures/organisations.yml
|
82
|
+
- test/fixtures/users/family.yml
|
83
|
+
- test/fixtures/not_models.yml
|
84
|
+
- test/test_helper.rb
|
85
|
+
homepage: https://github.com/Aethelflaed/mongoid-fixture_set
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.0.3
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Fixtures for Mongoid
|
109
|
+
test_files:
|
110
|
+
- test/mongoid/fixtures_test.rb
|
111
|
+
- test/mongoid/fixture_set_test.rb
|
112
|
+
- test/models/group.rb
|
113
|
+
- test/models/organisation.rb
|
114
|
+
- test/models/school.rb
|
115
|
+
- test/models/user.rb
|
116
|
+
- test/mongoid.yml
|
117
|
+
- test/fixtures/users.yml
|
118
|
+
- test/fixtures/groups.yml
|
119
|
+
- test/fixtures/schools.yml
|
120
|
+
- test/fixtures/organisations.yml
|
121
|
+
- test/fixtures/users/family.yml
|
122
|
+
- test/fixtures/not_models.yml
|
123
|
+
- test/test_helper.rb
|
124
|
+
has_rdoc:
|