mongoid-fixture_kit 0.2.0 → 0.3.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 +4 -4
- data/Rakefile +5 -3
- data/lib/mongoid/fixture_kit/file.rb +10 -4
- data/lib/mongoid/fixture_kit/fixture.rb +4 -5
- data/lib/mongoid/fixture_kit/render_context.rb +5 -3
- data/lib/mongoid/fixture_kit/test_helper.rb +22 -18
- data/lib/mongoid/fixture_kit/util.rb +54 -31
- data/lib/mongoid/fixture_kit/version.rb +3 -3
- data/lib/mongoid/fixture_kit.rb +16 -18
- metadata +10 -51
- data/test/fixtures/groups.yml +0 -12
- data/test/fixtures/not_models.yml +0 -2
- data/test/fixtures/organisations.yml +0 -4
- data/test/fixtures/schools.yml +0 -8
- data/test/fixtures/users/family.yml +0 -4
- data/test/fixtures/users.yml +0 -34
- data/test/load_once_fixtures/tests.yml +0 -2
- data/test/models/address.rb +0 -11
- data/test/models/group.rb +0 -13
- data/test/models/home.rb +0 -8
- data/test/models/item.rb +0 -8
- data/test/models/organisation.rb +0 -9
- data/test/models/school.rb +0 -9
- data/test/models/test.rb +0 -6
- data/test/models/user.rb +0 -20
- data/test/mongoid/fixture_kit_test.rb +0 -95
- data/test/mongoid/fixtures_test.rb +0 -16
- data/test/mongoid/load_once_test.rb +0 -35
- data/test/mongoid.yml +0 -6
- data/test/nested_polymorphic_relation_fixtures/groups.yml +0 -4
- data/test/test_helper.rb +0 -23
- /data/lib/{mongoid-fixture_kit.rb → mongoid_fixture_kit.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb2ea82f5d61f73bdc02a669c3634fa16e9f429afc7501d0ad34c3605e918839
|
4
|
+
data.tar.gz: 6bff571782d48beec751eb1e858cb833a859dbf9fcbef990150044f842e988a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a575c320dbff26fbf10561fd687d44d82548004321675b94bb3aeb3471722a2a9751ed3ebe98592e2be5cbbd788eea6c184a0c9d2c4e3749876740590dd2e5e
|
7
|
+
data.tar.gz: 1efb6c2f93d31b02b84e722b6f951f2cd4d2a682765576c362f331d0d93bf51c02885892762c1a7ed409c4e4cba4e083cd54ce795dd6a53290198bf6f1ecb6b7
|
data/Rakefile
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
-
|
3
|
-
require
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
4
5
|
|
5
6
|
desc 'Default: run unit tests'
|
6
|
-
task :
|
7
|
+
task default: :test
|
7
8
|
|
8
9
|
desc 'Run tests'
|
9
10
|
Rake::TestTask.new(:test) do |t|
|
@@ -11,4 +12,5 @@ Rake::TestTask.new(:test) do |t|
|
|
11
12
|
t.libs << 'test'
|
12
13
|
t.pattern = 'test/**/*_test.rb'
|
13
14
|
t.verbose = true
|
15
|
+
t.warning = false
|
14
16
|
end
|
@@ -6,7 +6,7 @@ module Mongoid
|
|
6
6
|
include Enumerable
|
7
7
|
|
8
8
|
def self.open(file)
|
9
|
-
x = new
|
9
|
+
x = new(file)
|
10
10
|
block_given? ? yield(x) : x
|
11
11
|
end
|
12
12
|
|
@@ -23,10 +23,15 @@ module Mongoid
|
|
23
23
|
|
24
24
|
def rows
|
25
25
|
return @rows if @rows
|
26
|
+
|
26
27
|
begin
|
27
28
|
data = YAML.safe_load(render(::File.read(@file)))
|
28
29
|
rescue ArgumentError, Psych::SyntaxError => e
|
29
|
-
raise
|
30
|
+
raise(
|
31
|
+
FormatError,
|
32
|
+
"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 #{e.class}: #{e}",
|
33
|
+
e.backtrace
|
34
|
+
)
|
30
35
|
end
|
31
36
|
@rows = data ? validate(data).to_a : []
|
32
37
|
end
|
@@ -37,8 +42,9 @@ module Mongoid
|
|
37
42
|
end
|
38
43
|
|
39
44
|
def validate(data)
|
40
|
-
raise
|
41
|
-
raise
|
45
|
+
raise(FormatError, 'fixture is not a hash') unless data.is_a?(Hash) || data.is_a?(YAML::Omap)
|
46
|
+
raise(FormatError) unless data.all? { |_name, row| row.is_a?(Hash) }
|
47
|
+
|
42
48
|
data
|
43
49
|
end
|
44
50
|
end
|
@@ -3,9 +3,7 @@ module Mongoid
|
|
3
3
|
class Fixture
|
4
4
|
include Enumerable
|
5
5
|
|
6
|
-
attr_reader :name
|
7
|
-
attr_reader :fixture
|
8
|
-
attr_reader :model_class
|
6
|
+
attr_reader :name, :fixture, :model_class
|
9
7
|
|
10
8
|
def initialize(name, fixture, model_class)
|
11
9
|
@name = name
|
@@ -26,9 +24,10 @@ module Mongoid
|
|
26
24
|
alias to_hash fixture
|
27
25
|
|
28
26
|
def find
|
29
|
-
raise
|
27
|
+
raise(FixtureClassNotFound, 'No class attached to find.') unless model_class
|
28
|
+
|
30
29
|
model_class.unscoped do
|
31
|
-
model_class.find_by(
|
30
|
+
model_class.find_by(__fixture_name: name)
|
32
31
|
end
|
33
32
|
end
|
34
33
|
end
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module Mongoid
|
2
2
|
class FixtureKit
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
module RenderContext
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def create_subclass
|
7
|
+
Class.new(Mongoid::FixtureKit.context_class) do
|
6
8
|
def binder
|
7
9
|
binding
|
8
10
|
end
|
@@ -16,13 +16,11 @@ module Mongoid
|
|
16
16
|
end
|
17
17
|
|
18
18
|
included do
|
19
|
-
# rubocop:disable ThreadSafety/ClassAndModuleAttributes
|
20
19
|
class_attribute :fixture_path
|
21
20
|
class_attribute :fixture_kit_names
|
22
21
|
class_attribute :load_fixtures_once
|
23
22
|
class_attribute :cached_fixtures
|
24
23
|
class_attribute :util
|
25
|
-
# rubocop:enable ThreadSafety/ClassAndModuleAttributes
|
26
24
|
|
27
25
|
self.fixture_path = nil
|
28
26
|
self.fixture_kit_names = [].freeze
|
@@ -45,25 +43,31 @@ module Mongoid
|
|
45
43
|
|
46
44
|
def setup_fixture_accessors(fixture_kit_names = nil)
|
47
45
|
fixture_kit_names = Array(fixture_kit_names || self.fixture_kit_names)
|
48
|
-
methods =
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
46
|
+
methods =
|
47
|
+
Module.new do
|
48
|
+
fixture_kit_names.each do |fs_name|
|
49
|
+
fs_name = fs_name.to_s
|
50
|
+
accessor_name = fs_name.tr('/', '_').to_sym
|
51
|
+
|
52
|
+
next if method_defined?(accessor_name)
|
53
|
+
|
54
|
+
define_method(accessor_name) do |*fixture_names|
|
55
|
+
force_reload = false
|
56
|
+
force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload
|
57
|
+
@fixture_cache[fs_name] ||= {}
|
58
|
+
instances =
|
59
|
+
fixture_names.map do |f_name|
|
60
|
+
f_name = f_name.to_s
|
61
|
+
@fixture_cache[fs_name].delete(f_name) if force_reload
|
62
|
+
raise(FixtureNotFound, "No fixture named '#{f_name}' found for fixture set '#{fs_name}'") unless @loaded_fixtures[fs_name] && @loaded_fixtures[fs_name][f_name]
|
63
|
+
|
64
|
+
@fixture_cache[fs_name][f_name] ||= @loaded_fixtures[fs_name][f_name].find
|
65
|
+
end
|
66
|
+
instances.length == 1 ? instances.first : instances
|
61
67
|
end
|
62
|
-
instances.length == 1 ? instances.first : instances
|
63
68
|
end
|
64
69
|
end
|
65
|
-
|
66
|
-
include methods
|
70
|
+
include(methods)
|
67
71
|
end
|
68
72
|
end
|
69
73
|
|
@@ -34,16 +34,19 @@ module Mongoid
|
|
34
34
|
fixture_kit_names = Array(fixture_kit_names).map(&:to_s)
|
35
35
|
class_names = Mongoid::FixtureKit::ClassCache.new(class_names)
|
36
36
|
|
37
|
-
files_to_read =
|
38
|
-
|
39
|
-
|
37
|
+
files_to_read =
|
38
|
+
fixture_kit_names.reject do |fs_name|
|
39
|
+
fixture_is_cached?(fs_name)
|
40
|
+
end
|
40
41
|
|
41
42
|
return cached_fixtures(fixture_kit_names) if files_to_read.empty?
|
42
43
|
|
43
44
|
fixtures_map = {}
|
44
|
-
fixture_kits =
|
45
|
-
|
46
|
-
|
45
|
+
fixture_kits =
|
46
|
+
files_to_read.map do |fs_name|
|
47
|
+
fixtures_map[fs_name] =
|
48
|
+
Mongoid::FixtureKit.new(fs_name, class_names[fs_name], ::File.join(fixtures_directory, fs_name))
|
49
|
+
end
|
47
50
|
|
48
51
|
update_all_loaded_fixtures(fixtures_map)
|
49
52
|
|
@@ -51,6 +54,7 @@ module Mongoid
|
|
51
54
|
collection_documents(fixture_kit).each do |model, documents|
|
52
55
|
model = class_names[model]
|
53
56
|
next unless model
|
57
|
+
|
54
58
|
documents.each do |attributes|
|
55
59
|
create_or_update_document(model, attributes)
|
56
60
|
end
|
@@ -62,7 +66,7 @@ module Mongoid
|
|
62
66
|
end
|
63
67
|
|
64
68
|
def create_or_update_document(model, attributes)
|
65
|
-
model = model.constantize if model.is_a?
|
69
|
+
model = model.constantize if model.is_a?(String)
|
66
70
|
|
67
71
|
document = find_or_create_document(model, attributes['__fixture_name'])
|
68
72
|
update_document(document, attributes)
|
@@ -76,7 +80,7 @@ module Mongoid
|
|
76
80
|
value = attributes[key] || document[key]
|
77
81
|
if key.include?('_translations')
|
78
82
|
document.public_send("#{key}=", value)
|
79
|
-
elsif attributes[key].
|
83
|
+
elsif attributes[key].instance_of?(Array) || document[key].instance_of?(Array)
|
80
84
|
document[key] = Array(attributes[key]) + Array(document[key])
|
81
85
|
else
|
82
86
|
document[key] = value
|
@@ -93,13 +97,15 @@ module Mongoid
|
|
93
97
|
case macro_from_relation(relation)
|
94
98
|
when :embeds_one
|
95
99
|
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
96
|
-
|
100
|
+
(is_new && document[name])
|
101
|
+
|
97
102
|
embedded_document = document.public_send(relation.name)
|
98
103
|
embedded_document_set_default_values(embedded_document, document[name])
|
99
104
|
end
|
100
105
|
when :embeds_many
|
101
106
|
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
102
|
-
|
107
|
+
(is_new && document[name])
|
108
|
+
|
103
109
|
embedded_documents = document.public_send(relation.name)
|
104
110
|
embedded_documents.each_with_index do |embedded_document, i|
|
105
111
|
embedded_document_set_default_values(embedded_document, document[name][i])
|
@@ -108,7 +114,13 @@ module Mongoid
|
|
108
114
|
when :belongs_to
|
109
115
|
if is_new && document.attributes[name]
|
110
116
|
value = document.attributes.delete(name)
|
111
|
-
|
117
|
+
if value.is_a?(Hash)
|
118
|
+
raise(
|
119
|
+
Mongoid::FixtureKit::FixtureError,
|
120
|
+
'Unable to create nested document inside an embedded document'
|
121
|
+
)
|
122
|
+
end
|
123
|
+
|
112
124
|
doc = find_or_create_document(relation.class_name, value)
|
113
125
|
document.attributes[relation.foreign_key] = doc.id
|
114
126
|
end
|
@@ -121,25 +133,27 @@ module Mongoid
|
|
121
133
|
def embedded_document_set_default_values(document, attributes)
|
122
134
|
sanitize_new_embedded_documents(document, is_new: true)
|
123
135
|
attributes.delete('_id')
|
124
|
-
|
125
|
-
|
126
|
-
|
136
|
+
removable_fields =
|
137
|
+
document.fields.select do |k, v|
|
138
|
+
k != '_id' && v.default_val.present? && attributes[k] == document[k]
|
139
|
+
end
|
140
|
+
removable_fields.each do |k, _v|
|
127
141
|
attributes.delete(k)
|
128
142
|
end
|
129
143
|
end
|
130
144
|
|
131
145
|
def find_or_create_document(model, fixture_name)
|
132
|
-
model = model.constantize if model.is_a?
|
146
|
+
model = model.constantize if model.is_a?(String)
|
133
147
|
|
134
|
-
document = model.where(
|
148
|
+
document = model.where(__fixture_name: fixture_name).first
|
135
149
|
if document.nil?
|
136
150
|
document = model.new
|
137
151
|
document['__fixture_name'] = fixture_name
|
138
152
|
begin
|
139
153
|
save_document(document)
|
140
154
|
rescue StandardError => e
|
141
|
-
Rails.logger.debug
|
142
|
-
Rails.logger.debug
|
155
|
+
Rails.logger.debug(document.attributes)
|
156
|
+
Rails.logger.debug(e)
|
143
157
|
Rails.logger.debug { "Backtrace:\n\t#{e.backtrace.join("\n\t")}" }
|
144
158
|
end
|
145
159
|
end
|
@@ -148,6 +162,7 @@ module Mongoid
|
|
148
162
|
|
149
163
|
def macro_from_relation(relation)
|
150
164
|
return relation.macro if defined?(Mongoid::Relations) && relation.instance_of?(Mongoid::Relations::Metadata)
|
165
|
+
|
151
166
|
relation.class.name.split('::').last.underscore.to_sym
|
152
167
|
end
|
153
168
|
|
@@ -157,9 +172,10 @@ module Mongoid
|
|
157
172
|
|
158
173
|
# track any join collection we need to insert later
|
159
174
|
documents = {}
|
160
|
-
documents[fixture_kit.class_name] =
|
161
|
-
|
162
|
-
|
175
|
+
documents[fixture_kit.class_name] =
|
176
|
+
fixture_kit.fixtures.map do |label, fixture|
|
177
|
+
unmarshall_fixture(label, fixture, fixture_kit.model_class)
|
178
|
+
end
|
163
179
|
documents
|
164
180
|
end
|
165
181
|
|
@@ -170,7 +186,7 @@ module Mongoid
|
|
170
186
|
end
|
171
187
|
|
172
188
|
def unmarshall_fixture(label, attributes, model_class)
|
173
|
-
model_class = model_class.constantize if model_class.is_a?
|
189
|
+
model_class = model_class.constantize if model_class.is_a?(String)
|
174
190
|
attributes = attributes.to_hash
|
175
191
|
|
176
192
|
if label
|
@@ -185,11 +201,12 @@ module Mongoid
|
|
185
201
|
return attributes if model_class.nil?
|
186
202
|
|
187
203
|
unless attributes.key?('_id')
|
188
|
-
document =
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
204
|
+
document =
|
205
|
+
if label
|
206
|
+
find_or_create_document(model_class, label)
|
207
|
+
else
|
208
|
+
model_class.new
|
209
|
+
end
|
193
210
|
attributes['_id'] = document.id
|
194
211
|
end
|
195
212
|
|
@@ -215,8 +232,14 @@ module Mongoid
|
|
215
232
|
value = attributes.delete(relation.name.to_s)
|
216
233
|
return if value.nil?
|
217
234
|
|
218
|
-
if value.is_a?
|
219
|
-
|
235
|
+
if value.is_a?(Hash)
|
236
|
+
if relation.polymorphic?
|
237
|
+
raise(
|
238
|
+
Mongoid::FixtureKit::FixtureError,
|
239
|
+
'Unable to create document from nested attributes in a polymorphic relation'
|
240
|
+
)
|
241
|
+
end
|
242
|
+
|
220
243
|
document = relation.class_name.constantize.new
|
221
244
|
value = unmarshall_fixture(nil, value, relation.class_name)
|
222
245
|
document = update_document(document, value)
|
@@ -238,7 +261,7 @@ module Mongoid
|
|
238
261
|
return if values.nil?
|
239
262
|
|
240
263
|
values.each do |value|
|
241
|
-
if value.is_a?
|
264
|
+
if value.is_a?(Hash)
|
242
265
|
document = relation.class_name.constantize.new
|
243
266
|
value[relation.foreign_key] = attributes['_id']
|
244
267
|
value[relation.type] = model_class.name if relation.polymorphic?
|
@@ -264,7 +287,7 @@ module Mongoid
|
|
264
287
|
attributes[key] = []
|
265
288
|
|
266
289
|
values.each do |value|
|
267
|
-
if value.is_a?
|
290
|
+
if value.is_a?(Hash)
|
268
291
|
document = relation.class_name.constantize.new
|
269
292
|
value[relation.inverse_foreign_key] = Array(attributes['_id'])
|
270
293
|
value = unmarshall_fixture(nil, value, relation.class_name)
|
data/lib/mongoid/fixture_kit.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
require 'mongoid/fixture_kit/class_cache'
|
2
2
|
require 'mongoid/fixture_kit/file'
|
3
|
+
require 'mongoid/fixture_kit/fixture'
|
3
4
|
require 'mongoid/fixture_kit/fixture_class_not_found'
|
4
5
|
require 'mongoid/fixture_kit/fixture_not_found'
|
5
|
-
require 'mongoid/fixture_kit/fixture'
|
6
6
|
require 'mongoid/fixture_kit/format_error'
|
7
7
|
require 'mongoid/fixture_kit/test_helper'
|
8
8
|
|
9
9
|
module Mongoid
|
10
10
|
class FixtureKit
|
11
|
-
attr_reader :name
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
attr_reader :name, :path, :model_class, :class_name, :fixtures
|
12
|
+
|
13
|
+
def self.context_class
|
14
|
+
@context_class ||= Class.new
|
15
|
+
end
|
16
16
|
|
17
17
|
def initialize(name, class_name, path)
|
18
18
|
@name = name
|
@@ -24,27 +24,25 @@ module Mongoid
|
|
24
24
|
@model_class = class_name.safe_constantize
|
25
25
|
end
|
26
26
|
|
27
|
-
@class_name =
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
@class_name =
|
28
|
+
if @model_class.respond_to?(:name)
|
29
|
+
@model_class.name
|
30
|
+
else
|
31
|
+
name.singularize.camelize
|
32
|
+
end
|
32
33
|
|
33
34
|
@fixtures = read_fixture_files
|
34
35
|
end
|
35
36
|
|
36
|
-
def self.context_class
|
37
|
-
@context_class ||= Class.new
|
38
|
-
end
|
39
|
-
|
40
37
|
delegate :[], to: :fixtures
|
41
38
|
|
42
39
|
private
|
43
40
|
|
44
41
|
def read_fixture_files
|
45
|
-
files =
|
46
|
-
|
47
|
-
|
42
|
+
files =
|
43
|
+
Dir["#{path}/{**,*}/*.yml"].select do |f|
|
44
|
+
::File.file?(f)
|
45
|
+
end
|
48
46
|
yaml_files = files.push("#{path}.yml")
|
49
47
|
|
50
48
|
yaml_files.each_with_object({}) do |file, fixtures|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-fixture_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dániel Sipos
|
@@ -11,7 +11,7 @@ cert_chain: []
|
|
11
11
|
date: 2023-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
@@ -25,17 +25,17 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '7.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: mongoid
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '7.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '7.0'
|
41
41
|
description: Use fixtures with Mongoid the same way you did with ActiveRecord
|
@@ -47,7 +47,6 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- LICENSE
|
49
49
|
- Rakefile
|
50
|
-
- lib/mongoid-fixture_kit.rb
|
51
50
|
- lib/mongoid/fixture_kit.rb
|
52
51
|
- lib/mongoid/fixture_kit/class_cache.rb
|
53
52
|
- lib/mongoid/fixture_kit/file.rb
|
@@ -60,31 +59,12 @@ files:
|
|
60
59
|
- lib/mongoid/fixture_kit/test_helper.rb
|
61
60
|
- lib/mongoid/fixture_kit/util.rb
|
62
61
|
- lib/mongoid/fixture_kit/version.rb
|
63
|
-
-
|
64
|
-
- test/fixtures/not_models.yml
|
65
|
-
- test/fixtures/organisations.yml
|
66
|
-
- test/fixtures/schools.yml
|
67
|
-
- test/fixtures/users.yml
|
68
|
-
- test/fixtures/users/family.yml
|
69
|
-
- test/load_once_fixtures/tests.yml
|
70
|
-
- test/models/address.rb
|
71
|
-
- test/models/group.rb
|
72
|
-
- test/models/home.rb
|
73
|
-
- test/models/item.rb
|
74
|
-
- test/models/organisation.rb
|
75
|
-
- test/models/school.rb
|
76
|
-
- test/models/test.rb
|
77
|
-
- test/models/user.rb
|
78
|
-
- test/mongoid.yml
|
79
|
-
- test/mongoid/fixture_kit_test.rb
|
80
|
-
- test/mongoid/fixtures_test.rb
|
81
|
-
- test/mongoid/load_once_test.rb
|
82
|
-
- test/nested_polymorphic_relation_fixtures/groups.yml
|
83
|
-
- test/test_helper.rb
|
62
|
+
- lib/mongoid_fixture_kit.rb
|
84
63
|
homepage: https://github.com/siposdani87/mongoid-fixture-kit
|
85
64
|
licenses:
|
86
65
|
- MIT
|
87
|
-
metadata:
|
66
|
+
metadata:
|
67
|
+
rubygems_mfa_required: 'true'
|
88
68
|
post_install_message:
|
89
69
|
rdoc_options: []
|
90
70
|
require_paths:
|
@@ -93,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
73
|
requirements:
|
94
74
|
- - ">="
|
95
75
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
76
|
+
version: 3.1.0
|
97
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
78
|
requirements:
|
99
79
|
- - ">="
|
@@ -104,25 +84,4 @@ rubygems_version: 3.4.13
|
|
104
84
|
signing_key:
|
105
85
|
specification_version: 4
|
106
86
|
summary: Fixtures for Rails Mongoid
|
107
|
-
test_files:
|
108
|
-
- test/fixtures/groups.yml
|
109
|
-
- test/fixtures/not_models.yml
|
110
|
-
- test/fixtures/organisations.yml
|
111
|
-
- test/fixtures/schools.yml
|
112
|
-
- test/fixtures/users/family.yml
|
113
|
-
- test/fixtures/users.yml
|
114
|
-
- test/load_once_fixtures/tests.yml
|
115
|
-
- test/models/address.rb
|
116
|
-
- test/models/group.rb
|
117
|
-
- test/models/home.rb
|
118
|
-
- test/models/item.rb
|
119
|
-
- test/models/organisation.rb
|
120
|
-
- test/models/school.rb
|
121
|
-
- test/models/test.rb
|
122
|
-
- test/models/user.rb
|
123
|
-
- test/mongoid/fixture_kit_test.rb
|
124
|
-
- test/mongoid/fixtures_test.rb
|
125
|
-
- test/mongoid/load_once_test.rb
|
126
|
-
- test/mongoid.yml
|
127
|
-
- test/nested_polymorphic_relation_fixtures/groups.yml
|
128
|
-
- test/test_helper.rb
|
87
|
+
test_files: []
|
data/test/fixtures/groups.yml
DELETED
data/test/fixtures/schools.yml
DELETED
data/test/fixtures/users.yml
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
geoffroy:
|
2
|
-
firstname: Geoffroy
|
3
|
-
lastname: Planquart
|
4
|
-
main_group: sudoers
|
5
|
-
groups:
|
6
|
-
- print
|
7
|
-
- !ruby/hash
|
8
|
-
name: Test nested polymorphic belongs_to
|
9
|
-
something: organization1 (Organisation)
|
10
|
-
- !ruby/hash
|
11
|
-
name: Test nested has_many creation
|
12
|
-
main_users:
|
13
|
-
- {
|
14
|
-
firstname: 'Created in nested group'
|
15
|
-
}
|
16
|
-
items:
|
17
|
-
- {
|
18
|
-
name: Test
|
19
|
-
}
|
20
|
-
user1:
|
21
|
-
firstname: Margot
|
22
|
-
lastname: Last
|
23
|
-
address:
|
24
|
-
city: Strasbourg
|
25
|
-
organisation: organization1
|
26
|
-
homes:
|
27
|
-
- name: Home
|
28
|
-
address:
|
29
|
-
city: Strasbourg
|
30
|
-
real: true
|
31
|
-
user 2é:
|
32
|
-
firstname: user2
|
33
|
-
main_group:
|
34
|
-
name: Win?
|
data/test/models/address.rb
DELETED
data/test/models/group.rb
DELETED
@@ -1,13 +0,0 @@
|
|
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/home.rb
DELETED
data/test/models/item.rb
DELETED
data/test/models/organisation.rb
DELETED
data/test/models/school.rb
DELETED
data/test/models/test.rb
DELETED
data/test/models/user.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
class User
|
2
|
-
include Mongoid::Document
|
3
|
-
include Mongoid::Timestamps::Updated::Short
|
4
|
-
|
5
|
-
embeds_one :address, as: :place
|
6
|
-
embeds_many :homes
|
7
|
-
|
8
|
-
field :firstname
|
9
|
-
field :lastname
|
10
|
-
|
11
|
-
field :default, type: Boolean, default: true
|
12
|
-
|
13
|
-
belongs_to :main_group, class_name: 'Group', inverse_of: :main_users
|
14
|
-
|
15
|
-
has_and_belongs_to_many :groups
|
16
|
-
|
17
|
-
has_many :items
|
18
|
-
accepts_nested_attributes_for :items
|
19
|
-
end
|
20
|
-
|
@@ -1,95 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'pry'
|
3
|
-
|
4
|
-
module Mongoid
|
5
|
-
class FixtureKitTest < BaseTest
|
6
|
-
def test_should_initialize_fixture_kit
|
7
|
-
fs = Mongoid::FixtureKit.new('users', 'User', 'test/fixtures/users')
|
8
|
-
assert_equal User, fs.model_class
|
9
|
-
fixture = fs['geoffroy']
|
10
|
-
assert_equal 'User', fixture.class_name
|
11
|
-
assert_equal 'Geoffroy', fixture['firstname']
|
12
|
-
assert_equal 'Planquart', fixture['lastname']
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_should_not_create_fixtures
|
16
|
-
util = Mongoid::FixtureKit::Util.new
|
17
|
-
util.reset_cache
|
18
|
-
fs = util.create_fixtures('test/fixtures/', [])
|
19
|
-
assert_equal 0, fs.count
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_should_create_not_model_fixture
|
23
|
-
util = Mongoid::FixtureKit::Util.new
|
24
|
-
util.reset_cache
|
25
|
-
fs = util.create_fixtures('test/fixtures', %w(not_models))
|
26
|
-
fs = fs.first
|
27
|
-
fixture = fs['error']
|
28
|
-
|
29
|
-
begin
|
30
|
-
fixture.find
|
31
|
-
assert false, 'No exception has been raised'
|
32
|
-
rescue Mongoid::FixtureKit::FixtureClassNotFound
|
33
|
-
assert true
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
test 'should raised if nested polymorphic relation' do
|
38
|
-
util = Mongoid::FixtureKit::Util.new
|
39
|
-
util.reset_cache
|
40
|
-
begin
|
41
|
-
_fs = util.create_fixtures('test/nested_polymorphic_relation_fixtures', %w(groups))
|
42
|
-
assert false
|
43
|
-
rescue Mongoid::FixtureKit::FixtureError
|
44
|
-
assert true
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_should_create_fixtures
|
49
|
-
util = Mongoid::FixtureKit::Util.new
|
50
|
-
util.reset_cache
|
51
|
-
fs = util.create_fixtures('test/fixtures/', %w(users groups schools organisations))
|
52
|
-
|
53
|
-
users = fs.find{|x| x.model_class == User}
|
54
|
-
f_geoffroy = users['geoffroy']
|
55
|
-
|
56
|
-
assert_equal 6, School.count
|
57
|
-
assert_equal 6, User.count
|
58
|
-
|
59
|
-
geoffroy = User.find_by(firstname: 'Geoffroy')
|
60
|
-
user1 = User.find_by(firstname: 'Margot')
|
61
|
-
sudoers = Group.find_by(name: 'Sudoers!')
|
62
|
-
print = Group.find_by(name: 'Print')
|
63
|
-
group1 = Group.find_by(name: 'Margot')
|
64
|
-
organization1 = Organisation.find_by(name: '1 Organisation')
|
65
|
-
school = School.find_by(name: 'School')
|
66
|
-
test_item = Item.find_by(name: 'Test')
|
67
|
-
user2 = User.find_by(firstname: 'user2')
|
68
|
-
win_group = Group.find_by(name: 'Win?')
|
69
|
-
_test_nested_polymorphic_belongs_to = Group.find_by(name: 'Test nested polymorphic belongs_to')
|
70
|
-
_test_nested_has_many_creation = Group.find_by(name: 'Test nested has_many creation')
|
71
|
-
User.find_by(firstname: 'Created in nested group')
|
72
|
-
|
73
|
-
assert_equal user1.address.organisation, organization1
|
74
|
-
assert_equal 1, user1.homes.count
|
75
|
-
assert_equal geoffroy, f_geoffroy.find
|
76
|
-
assert_equal 3, print.users.count
|
77
|
-
assert print.users.include?(geoffroy)
|
78
|
-
assert print.users.include?(user1)
|
79
|
-
assert sudoers.main_users.include?(geoffroy)
|
80
|
-
assert_equal group1, user1.main_group
|
81
|
-
assert_equal print, user1.groups.first
|
82
|
-
assert_equal geoffroy, test_item.user
|
83
|
-
assert win_group.main_users.include?(user2)
|
84
|
-
|
85
|
-
assert_equal 1, school.groups.count
|
86
|
-
assert_equal group1, school.groups.first
|
87
|
-
assert_equal school, group1.something
|
88
|
-
|
89
|
-
assert_equal 3, organization1.groups.count
|
90
|
-
assert organization1.groups.include?(sudoers)
|
91
|
-
assert_equal organization1, sudoers.something
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class FixturesTest < BaseTest
|
4
|
-
include Mongoid::FixtureKit::TestHelper
|
5
|
-
self.fixture_path = 'test/fixtures'
|
6
|
-
|
7
|
-
def test_should_access_fixtures
|
8
|
-
begin
|
9
|
-
_geoffroy = users(:geoffroy)
|
10
|
-
assert true
|
11
|
-
rescue StandardError => e
|
12
|
-
puts e
|
13
|
-
assert false, 'An exception was thrown'
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class LoadOnceTest < BaseTest
|
4
|
-
include Mongoid::FixtureKit::TestHelper
|
5
|
-
self.fixture_path = 'test/load_once_fixtures'
|
6
|
-
self.load_fixtures_once = true
|
7
|
-
|
8
|
-
class_attribute :count
|
9
|
-
self.count = 0
|
10
|
-
|
11
|
-
module FixtureLoadCount
|
12
|
-
def count
|
13
|
-
LoadOnceTest.count += 1
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
Mongoid::FixtureKit.context_class.send :include, FixtureLoadCount
|
18
|
-
|
19
|
-
def teardown
|
20
|
-
end
|
21
|
-
|
22
|
-
def count_equal_1
|
23
|
-
assert_equal 1, self.class.count
|
24
|
-
begin
|
25
|
-
tests(:test1)
|
26
|
-
assert true
|
27
|
-
rescue => e
|
28
|
-
assert false, "#{e}\n#{e.backtrace.join("\n")}"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
5.times do |i|
|
33
|
-
alias_method "test_load_once_#{i}", :count_equal_1
|
34
|
-
end
|
35
|
-
end
|
data/test/mongoid.yml
DELETED
data/test/test_helper.rb
DELETED
@@ -1,23 +0,0 @@
|
|
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_kit", __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
|
-
ActiveSupport::TestCase.test_order = :random
|
18
|
-
|
19
|
-
class BaseTest < ActiveSupport::TestCase
|
20
|
-
def teardown
|
21
|
-
Mongoid::Clients.default.use('mongoid_fixture_kit_test').database.drop
|
22
|
-
end
|
23
|
-
end
|
File without changes
|