mongoid-fixture_kit 0.1.2 → 0.2.1
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 +4 -3
- data/lib/mongoid/fixture_kit/file.rb +11 -5
- data/lib/mongoid/fixture_kit/fixture.rb +4 -5
- data/lib/mongoid/fixture_kit/render_context.rb +9 -3
- data/lib/mongoid/fixture_kit/test_helper.rb +19 -18
- data/lib/mongoid/fixture_kit/util.rb +63 -31
- data/lib/mongoid/fixture_kit/version.rb +3 -3
- data/lib/mongoid/fixture_kit.rb +16 -14
- metadata +9 -8
- /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: 6f4ecbf0b4f66f508d2ed208682a25be26cf7a44fda95ad7500cd9ca8c40fbaf
|
4
|
+
data.tar.gz: eafd73f1f80d1d7c70092b03dd431d302cfa22a7dc552e617e770d707fbdb0e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00ba5f0bfd92afe5ef79e78525bf96ff03b455d7f945e9e32f1358e60fd29eb2deb4246abd3b9275c5a3a0925f9b489a1d9249962f69fd1a0b7815edcc519458
|
7
|
+
data.tar.gz: bdcf36fc63f8ae538d8a0d158af9af1e1fcc893b534c8189330ef10ddf8e05e15d29712b2edb86e5e072d54a78c7b1d6654b6dc5ec2ec26168a3976d305175ff
|
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|
|
@@ -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,22 +23,28 @@ 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
|
33
38
|
|
34
39
|
def render(content)
|
35
|
-
context = Mongoid::FixtureKit::RenderContext.new
|
40
|
+
context = Mongoid::FixtureKit::RenderContext.create_subclass.new
|
36
41
|
ERB.new(content).result(context.binder)
|
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,14 @@
|
|
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
|
8
|
+
def binder
|
9
|
+
binding
|
10
|
+
end
|
11
|
+
end
|
6
12
|
end
|
7
13
|
end
|
8
14
|
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,28 @@ 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
|
+
define_method(accessor_name) do |*fixture_names|
|
52
|
+
force_reload = false
|
53
|
+
force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload
|
54
|
+
@fixture_cache[fs_name] ||= {}
|
55
|
+
instances =
|
56
|
+
fixture_names.map do |f_name|
|
57
|
+
f_name = f_name.to_s
|
58
|
+
@fixture_cache[fs_name].delete(f_name) if force_reload
|
59
|
+
raise(FixtureNotFound, "No fixture named '#{f_name}' found for fixture set '#{fs_name}'") unless @loaded_fixtures[fs_name] && @loaded_fixtures[fs_name][f_name]
|
60
|
+
|
61
|
+
@fixture_cache[fs_name][f_name] ||= @loaded_fixtures[fs_name][f_name].find
|
62
|
+
end
|
63
|
+
instances.length == 1 ? instances.first : instances
|
61
64
|
end
|
62
|
-
instances.length == 1 ? instances.first : instances
|
63
65
|
end
|
64
66
|
end
|
65
|
-
|
66
|
-
include methods
|
67
|
+
include(methods)
|
67
68
|
end
|
68
69
|
end
|
69
70
|
|
@@ -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,6 +80,8 @@ module Mongoid
|
|
76
80
|
value = attributes[key] || document[key]
|
77
81
|
if key.include?('_translations')
|
78
82
|
document.public_send("#{key}=", value)
|
83
|
+
elsif attributes[key].instance_of?(Array) || document[key].instance_of?(Array)
|
84
|
+
document[key] = Array(attributes[key]) + Array(document[key])
|
79
85
|
else
|
80
86
|
document[key] = value
|
81
87
|
end
|
@@ -90,18 +96,31 @@ module Mongoid
|
|
90
96
|
document.relations.each do |name, relation|
|
91
97
|
case macro_from_relation(relation)
|
92
98
|
when :embeds_one
|
93
|
-
|
99
|
+
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
100
|
+
(is_new && document[name])
|
101
|
+
|
102
|
+
embedded_document = document.public_send(relation.name)
|
103
|
+
embedded_document_set_default_values(embedded_document, document[name])
|
104
|
+
end
|
94
105
|
when :embeds_many
|
95
|
-
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
106
|
+
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
107
|
+
(is_new && document[name])
|
108
|
+
|
96
109
|
embedded_documents = document.public_send(relation.name)
|
97
|
-
embedded_documents.each_with_index do |
|
98
|
-
embedded_document_set_default_values(
|
110
|
+
embedded_documents.each_with_index do |embedded_document, i|
|
111
|
+
embedded_document_set_default_values(embedded_document, document[name][i])
|
99
112
|
end
|
100
113
|
end
|
101
114
|
when :belongs_to
|
102
115
|
if is_new && document.attributes[name]
|
103
116
|
value = document.attributes.delete(name)
|
104
|
-
|
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
|
+
|
105
124
|
doc = find_or_create_document(relation.class_name, value)
|
106
125
|
document.attributes[relation.foreign_key] = doc.id
|
107
126
|
end
|
@@ -114,23 +133,27 @@ module Mongoid
|
|
114
133
|
def embedded_document_set_default_values(document, attributes)
|
115
134
|
sanitize_new_embedded_documents(document, is_new: true)
|
116
135
|
attributes.delete('_id')
|
117
|
-
|
118
|
-
|
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|
|
141
|
+
attributes.delete(k)
|
119
142
|
end
|
120
143
|
end
|
121
144
|
|
122
145
|
def find_or_create_document(model, fixture_name)
|
123
|
-
model = model.constantize if model.is_a?
|
146
|
+
model = model.constantize if model.is_a?(String)
|
124
147
|
|
125
|
-
document = model.where(
|
148
|
+
document = model.where(__fixture_name: fixture_name).first
|
126
149
|
if document.nil?
|
127
150
|
document = model.new
|
128
151
|
document['__fixture_name'] = fixture_name
|
129
152
|
begin
|
130
153
|
save_document(document)
|
131
154
|
rescue StandardError => e
|
132
|
-
Rails.logger.debug
|
133
|
-
Rails.logger.debug
|
155
|
+
Rails.logger.debug(document.attributes)
|
156
|
+
Rails.logger.debug(e)
|
134
157
|
Rails.logger.debug { "Backtrace:\n\t#{e.backtrace.join("\n\t")}" }
|
135
158
|
end
|
136
159
|
end
|
@@ -139,6 +162,7 @@ module Mongoid
|
|
139
162
|
|
140
163
|
def macro_from_relation(relation)
|
141
164
|
return relation.macro if defined?(Mongoid::Relations) && relation.instance_of?(Mongoid::Relations::Metadata)
|
165
|
+
|
142
166
|
relation.class.name.split('::').last.underscore.to_sym
|
143
167
|
end
|
144
168
|
|
@@ -148,9 +172,10 @@ module Mongoid
|
|
148
172
|
|
149
173
|
# track any join collection we need to insert later
|
150
174
|
documents = {}
|
151
|
-
documents[fixture_kit.class_name] =
|
152
|
-
|
153
|
-
|
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
|
154
179
|
documents
|
155
180
|
end
|
156
181
|
|
@@ -161,7 +186,7 @@ module Mongoid
|
|
161
186
|
end
|
162
187
|
|
163
188
|
def unmarshall_fixture(label, attributes, model_class)
|
164
|
-
model_class = model_class.constantize if model_class.is_a?
|
189
|
+
model_class = model_class.constantize if model_class.is_a?(String)
|
165
190
|
attributes = attributes.to_hash
|
166
191
|
|
167
192
|
if label
|
@@ -176,11 +201,12 @@ module Mongoid
|
|
176
201
|
return attributes if model_class.nil?
|
177
202
|
|
178
203
|
unless attributes.key?('_id')
|
179
|
-
document =
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
204
|
+
document =
|
205
|
+
if label
|
206
|
+
find_or_create_document(model_class, label)
|
207
|
+
else
|
208
|
+
model_class.new
|
209
|
+
end
|
184
210
|
attributes['_id'] = document.id
|
185
211
|
end
|
186
212
|
|
@@ -206,8 +232,14 @@ module Mongoid
|
|
206
232
|
value = attributes.delete(relation.name.to_s)
|
207
233
|
return if value.nil?
|
208
234
|
|
209
|
-
if value.is_a?
|
210
|
-
|
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
|
+
|
211
243
|
document = relation.class_name.constantize.new
|
212
244
|
value = unmarshall_fixture(nil, value, relation.class_name)
|
213
245
|
document = update_document(document, value)
|
@@ -229,7 +261,7 @@ module Mongoid
|
|
229
261
|
return if values.nil?
|
230
262
|
|
231
263
|
values.each do |value|
|
232
|
-
if value.is_a?
|
264
|
+
if value.is_a?(Hash)
|
233
265
|
document = relation.class_name.constantize.new
|
234
266
|
value[relation.foreign_key] = attributes['_id']
|
235
267
|
value[relation.type] = model_class.name if relation.polymorphic?
|
@@ -255,7 +287,7 @@ module Mongoid
|
|
255
287
|
attributes[key] = []
|
256
288
|
|
257
289
|
values.each do |value|
|
258
|
-
if value.is_a?
|
290
|
+
if value.is_a?(Hash)
|
259
291
|
document = relation.class_name.constantize.new
|
260
292
|
value[relation.inverse_foreign_key] = Array(attributes['_id'])
|
261
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,11 +24,12 @@ 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
|
@@ -38,9 +39,10 @@ module Mongoid
|
|
38
39
|
private
|
39
40
|
|
40
41
|
def read_fixture_files
|
41
|
-
files =
|
42
|
-
|
43
|
-
|
42
|
+
files =
|
43
|
+
Dir["#{path}/{**,*}/*.yml"].select do |f|
|
44
|
+
::File.file?(f)
|
45
|
+
end
|
44
46
|
yaml_files = files.push("#{path}.yml")
|
45
47
|
|
46
48
|
yaml_files.each_with_object({}) do |file, fixtures|
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-fixture_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dániel Sipos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
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,7 +25,7 @@ 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
|
- - "~>"
|
@@ -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,10 +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
|
62
|
+
- lib/mongoid_fixture_kit.rb
|
63
63
|
homepage: https://github.com/siposdani87/mongoid-fixture-kit
|
64
64
|
licenses:
|
65
65
|
- MIT
|
66
|
-
metadata:
|
66
|
+
metadata:
|
67
|
+
rubygems_mfa_required: 'true'
|
67
68
|
post_install_message:
|
68
69
|
rdoc_options: []
|
69
70
|
require_paths:
|
@@ -72,14 +73,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
72
73
|
requirements:
|
73
74
|
- - ">="
|
74
75
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
76
|
+
version: 3.1.0
|
76
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
78
|
requirements:
|
78
79
|
- - ">="
|
79
80
|
- !ruby/object:Gem::Version
|
80
81
|
version: '0'
|
81
82
|
requirements: []
|
82
|
-
rubygems_version: 3.
|
83
|
+
rubygems_version: 3.4.13
|
83
84
|
signing_key:
|
84
85
|
specification_version: 4
|
85
86
|
summary: Fixtures for Rails Mongoid
|
File without changes
|