mongoid-fixture_kit 0.1.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/LICENSE +21 -0
- data/Rakefile +14 -0
- data/lib/mongoid/fixture_kit/class_cache.rb +25 -0
- data/lib/mongoid/fixture_kit/file.rb +44 -0
- data/lib/mongoid/fixture_kit/fixture.rb +36 -0
- data/lib/mongoid/fixture_kit/fixture_class_not_found.rb +6 -0
- data/lib/mongoid/fixture_kit/fixture_error.rb +6 -0
- data/lib/mongoid/fixture_kit/fixture_not_found.rb +6 -0
- data/lib/mongoid/fixture_kit/format_error.rb +6 -0
- data/lib/mongoid/fixture_kit/render_context.rb +9 -0
- data/lib/mongoid/fixture_kit/test_helper.rb +101 -0
- data/lib/mongoid/fixture_kit/util.rb +285 -0
- data/lib/mongoid/fixture_kit/version.rb +5 -0
- data/lib/mongoid/fixture_kit.rb +47 -0
- data/lib/mongoid-fixture_kit.rb +7 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b8f375c33dca26d8a7405665619d2b3967fcafb8283fa3620218e430ffdbff4c
|
4
|
+
data.tar.gz: 07ea12bbfa497ff03a7adfb138cb844302c3d6147713f7835e61c1ae4e410b7e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45eb57698392535c3a60b8a7d0b077945bb5b5e6ebf7808eacfade96f59a50498f5314b9180a38a0dd160891c633c4a60560e5bd697e8f0ebe1fa182eefed4c3
|
7
|
+
data.tar.gz: db7d777fec746e3521631449856d3baaf208cf80849e77f5c82458e5c8def1b9db9f2be8a47001dfc96ccf3893715975230b039a5c2b2f7c786fac30e248d1b8
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Dániel Sipos
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
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
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class FixtureKit
|
3
|
+
class ClassCache
|
4
|
+
def initialize(class_names)
|
5
|
+
@class_names = class_names.stringify_keys
|
6
|
+
# Remove string values that aren't constants or subclasses of Mongoid::Document
|
7
|
+
@class_names.delete_if { |klass_name, klass| !insert_class(@class_names, klass_name, klass) }
|
8
|
+
end
|
9
|
+
|
10
|
+
def [](fs_name)
|
11
|
+
@class_names.fetch(fs_name) do
|
12
|
+
klass = fs_name.singularize.camelize.safe_constantize
|
13
|
+
insert_class(@class_names, fs_name, klass)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def insert_class(class_names, name, klass)
|
20
|
+
# We only want to deal with Mongoid objects.
|
21
|
+
class_names[name] = (klass if klass && klass < Mongoid::Document)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class FixtureKit
|
3
|
+
class File
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
def self.open(file)
|
7
|
+
x = new file
|
8
|
+
block_given? ? yield(x) : x
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(file)
|
12
|
+
@file = file
|
13
|
+
@rows = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def each(&)
|
17
|
+
rows.each(&)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def rows
|
23
|
+
return @rows if @rows
|
24
|
+
begin
|
25
|
+
data = YAML.safe_load(render(::File.read(@file)))
|
26
|
+
rescue ArgumentError, Psych::SyntaxError => e
|
27
|
+
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 #{e.class}: #{e}", e.backtrace
|
28
|
+
end
|
29
|
+
@rows = data ? validate(data).to_a : []
|
30
|
+
end
|
31
|
+
|
32
|
+
def render(content)
|
33
|
+
context = RenderContext.new
|
34
|
+
ERB.new(content).result(context.binder)
|
35
|
+
end
|
36
|
+
|
37
|
+
def validate(data)
|
38
|
+
raise FormatError, 'fixture is not a hash' unless data.is_a?(Hash) || data.is_a?(YAML::Omap)
|
39
|
+
raise FormatError unless data.all? { |_name, row| row.is_a?(Hash) }
|
40
|
+
data
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class FixtureKit
|
3
|
+
class Fixture
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
attr_reader :name
|
7
|
+
attr_reader :fixture
|
8
|
+
attr_reader :model_class
|
9
|
+
|
10
|
+
def initialize(name, fixture, model_class)
|
11
|
+
@name = name
|
12
|
+
@fixture = fixture
|
13
|
+
@model_class = model_class
|
14
|
+
end
|
15
|
+
|
16
|
+
def class_name
|
17
|
+
model_class&.name
|
18
|
+
end
|
19
|
+
|
20
|
+
def each(&)
|
21
|
+
fixture.each(&)
|
22
|
+
end
|
23
|
+
|
24
|
+
delegate :[], to: :fixture
|
25
|
+
|
26
|
+
alias to_hash fixture
|
27
|
+
|
28
|
+
def find
|
29
|
+
raise FixtureClassNotFound, _('No class attached to find.') unless model_class
|
30
|
+
model_class.unscoped do
|
31
|
+
model_class.find_by('__fixture_name' => name)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class FixtureKit
|
3
|
+
module TestHelper
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def before_setup
|
7
|
+
super
|
8
|
+
setup_fixtures
|
9
|
+
end
|
10
|
+
|
11
|
+
def after_teardown
|
12
|
+
super
|
13
|
+
teardown_fixtures
|
14
|
+
end
|
15
|
+
|
16
|
+
included do
|
17
|
+
# rubocop:disable ThreadSafety/ClassAndModuleAttributes
|
18
|
+
class_attribute :fixture_path
|
19
|
+
class_attribute :fixture_kit_names
|
20
|
+
class_attribute :load_fixtures_once
|
21
|
+
class_attribute :cached_fixtures
|
22
|
+
class_attribute :util
|
23
|
+
# rubocop:enable ThreadSafety/ClassAndModuleAttributes
|
24
|
+
|
25
|
+
self.fixture_path = nil
|
26
|
+
self.fixture_kit_names = [].freeze
|
27
|
+
self.load_fixtures_once = false
|
28
|
+
self.cached_fixtures = nil
|
29
|
+
self.util = Mongoid::FixtureKit::Util.new
|
30
|
+
end
|
31
|
+
|
32
|
+
class_methods do
|
33
|
+
def fixtures(*fixture_kit_names)
|
34
|
+
if fixture_kit_names.first == :all
|
35
|
+
fixture_kit_names = Dir["#{fixture_path}/{**,*}/*.{yml}"]
|
36
|
+
fixture_kit_names.map! { |f| f[(fixture_path.to_s.length + 1)..-5] }
|
37
|
+
else
|
38
|
+
fixture_kit_names = fixture_kit_names.flatten.map(&:to_s)
|
39
|
+
end
|
40
|
+
self.fixture_kit_names |= fixture_kit_names
|
41
|
+
setup_fixture_accessors(fixture_kit_names)
|
42
|
+
end
|
43
|
+
|
44
|
+
def setup_fixture_accessors(fixture_kit_names = nil)
|
45
|
+
fixture_kit_names = Array(fixture_kit_names || self.fixture_kit_names)
|
46
|
+
methods = Module.new do
|
47
|
+
fixture_kit_names.each do |fs_name|
|
48
|
+
fs_name = fs_name.to_s
|
49
|
+
accessor_name = fs_name.tr('/', '_').to_sym
|
50
|
+
define_method(accessor_name) do |*fixture_names|
|
51
|
+
force_reload = false
|
52
|
+
force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload
|
53
|
+
@fixture_cache[fs_name] ||= {}
|
54
|
+
instances = fixture_names.map do |f_name|
|
55
|
+
f_name = f_name.to_s
|
56
|
+
@fixture_cache[fs_name].delete(f_name) if force_reload
|
57
|
+
raise FixtureNotFound, "No fixture named '#{f_name}' found for fixture set '#{fs_name}'" unless @loaded_fixtures[fs_name] && @loaded_fixtures[fs_name][f_name]
|
58
|
+
@fixture_cache[fs_name][f_name] ||= @loaded_fixtures[fs_name][f_name].find
|
59
|
+
end
|
60
|
+
instances.length == 1 ? instances.first : instances
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
include methods
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def setup_fixtures
|
69
|
+
@fixture_cache = {}
|
70
|
+
|
71
|
+
if self.class.cached_fixtures && self.class.load_fixtures_once
|
72
|
+
self.class.fixtures(self.class.fixture_kit_names)
|
73
|
+
@loaded_fixtures = self.class.cached_fixtures
|
74
|
+
else
|
75
|
+
self.class.util.reset_cache
|
76
|
+
self.loaded_fixtures = load_fixtures
|
77
|
+
self.class.cached_fixtures = @loaded_fixtures
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def teardown_fixtures
|
82
|
+
self.class.util.reset_cache
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def load_fixtures
|
88
|
+
fixture_kit_names = self.class.fixture_kit_names
|
89
|
+
if fixture_kit_names.empty?
|
90
|
+
self.class.fixtures(:all)
|
91
|
+
fixture_kit_names = self.class.fixture_kit_names
|
92
|
+
end
|
93
|
+
self.class.util.create_fixtures(self.class.fixture_path, fixture_kit_names)
|
94
|
+
end
|
95
|
+
|
96
|
+
def loaded_fixtures=(fixtures)
|
97
|
+
@loaded_fixtures = fixtures.dup.index_by(&:name)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,285 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class FixtureKit
|
3
|
+
class Util
|
4
|
+
def initialize
|
5
|
+
@cached_fixtures = {}
|
6
|
+
@all_loaded_fixtures = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def cached_fixtures(keys_to_fetch = nil)
|
10
|
+
if keys_to_fetch
|
11
|
+
@cached_fixtures.values_at(*keys_to_fetch)
|
12
|
+
else
|
13
|
+
@cached_fixtures.values
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset_cache
|
18
|
+
@cached_fixtures.clear
|
19
|
+
end
|
20
|
+
|
21
|
+
def fixture_is_cached?(name)
|
22
|
+
@cached_fixtures[name]
|
23
|
+
end
|
24
|
+
|
25
|
+
def cache_fixtures(fixtures_map)
|
26
|
+
@cached_fixtures.update(fixtures_map)
|
27
|
+
end
|
28
|
+
|
29
|
+
def update_all_loaded_fixtures(fixtures_map)
|
30
|
+
@all_loaded_fixtures = fixtures_map
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_fixtures(fixtures_directory, fixture_kit_names, class_names = {})
|
34
|
+
fixture_kit_names = Array(fixture_kit_names).map(&:to_s)
|
35
|
+
class_names = Mongoid::FixtureKit::ClassCache.new(class_names)
|
36
|
+
|
37
|
+
files_to_read = fixture_kit_names.reject do |fs_name|
|
38
|
+
fixture_is_cached?(fs_name)
|
39
|
+
end
|
40
|
+
|
41
|
+
return cached_fixtures(fixture_kit_names) if files_to_read.empty?
|
42
|
+
|
43
|
+
fixtures_map = {}
|
44
|
+
fixture_kits = files_to_read.map do |fs_name|
|
45
|
+
fixtures_map[fs_name] = Mongoid::FixtureKit.new(fs_name, class_names[fs_name], ::File.join(fixtures_directory, fs_name))
|
46
|
+
end
|
47
|
+
|
48
|
+
update_all_loaded_fixtures(fixtures_map)
|
49
|
+
|
50
|
+
fixture_kits.each do |fixture_kit|
|
51
|
+
collection_documents(fixture_kit).each do |model, documents|
|
52
|
+
model = class_names[model]
|
53
|
+
next unless model
|
54
|
+
documents.each do |attributes|
|
55
|
+
create_or_update_document(model, attributes)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
cache_fixtures(fixtures_map)
|
61
|
+
cached_fixtures(fixture_kit_names)
|
62
|
+
end
|
63
|
+
|
64
|
+
def create_or_update_document(model, attributes)
|
65
|
+
model = model.constantize if model.is_a? String
|
66
|
+
|
67
|
+
document = find_or_create_document(model, attributes['__fixture_name'])
|
68
|
+
update_document(document, attributes)
|
69
|
+
end
|
70
|
+
|
71
|
+
def update_document(document, attributes)
|
72
|
+
attributes.delete('_id') if document.attributes.key?('_id')
|
73
|
+
|
74
|
+
keys = (attributes.keys + document.attributes.keys).uniq
|
75
|
+
keys.each do |key|
|
76
|
+
value = attributes[key] || document[key]
|
77
|
+
if key.include?('_translations')
|
78
|
+
document.public_send("#{key}=", value)
|
79
|
+
else
|
80
|
+
document[key] = value
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
sanitize_new_embedded_documents(document)
|
85
|
+
save_document(document)
|
86
|
+
document
|
87
|
+
end
|
88
|
+
|
89
|
+
def sanitize_new_embedded_documents(document, is_new: false)
|
90
|
+
document.relations.each do |name, relation|
|
91
|
+
case macro_from_relation(relation)
|
92
|
+
when :embeds_one
|
93
|
+
embedded_document_set_default_values(document.public_send(relation.name), document[name]) if (document.changes[name] && !document.changes[name][1].nil?) || (is_new && document[name])
|
94
|
+
when :embeds_many
|
95
|
+
if (document.changes[name] && !document.changes[name][1].nil?) || (is_new && document[name])
|
96
|
+
embedded_documents = document.public_send(relation.name)
|
97
|
+
embedded_documents.each_with_index do |embedded, i|
|
98
|
+
embedded_document_set_default_values(embedded, document[name][i])
|
99
|
+
end
|
100
|
+
end
|
101
|
+
when :belongs_to
|
102
|
+
if is_new && document.attributes[name]
|
103
|
+
value = document.attributes.delete(name)
|
104
|
+
raise Mongoid::FixtureKit::FixtureError, 'Unable to create nested document inside an embedded document' if value.is_a?(Hash)
|
105
|
+
doc = find_or_create_document(relation.class_name, value)
|
106
|
+
document.attributes[relation.foreign_key] = doc.id
|
107
|
+
end
|
108
|
+
else
|
109
|
+
# type code here
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def embedded_document_set_default_values(document, attributes)
|
115
|
+
sanitize_new_embedded_documents(document, is_new: true)
|
116
|
+
attributes.delete('_id')
|
117
|
+
document.fields.select do |k, v|
|
118
|
+
k != '_id' && !v.default_val.nil? && attributes[k] == document[k]
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def find_or_create_document(model, fixture_name)
|
123
|
+
model = model.constantize if model.is_a? String
|
124
|
+
|
125
|
+
document = model.where('__fixture_name' => fixture_name).first
|
126
|
+
if document.nil?
|
127
|
+
document = model.new
|
128
|
+
document['__fixture_name'] = fixture_name
|
129
|
+
begin
|
130
|
+
save_document(document)
|
131
|
+
rescue StandardError => e
|
132
|
+
Rails.logger.debug document.attributes
|
133
|
+
Rails.logger.debug e
|
134
|
+
Rails.logger.debug { "Backtrace:\n\t#{e.backtrace.join("\n\t")}" }
|
135
|
+
end
|
136
|
+
end
|
137
|
+
document
|
138
|
+
end
|
139
|
+
|
140
|
+
def macro_from_relation(relation)
|
141
|
+
return relation.macro if defined?(Mongoid::Relations) && relation.instance_of?(Mongoid::Relations::Metadata)
|
142
|
+
relation.class.name.split('::').last.underscore.to_sym
|
143
|
+
end
|
144
|
+
|
145
|
+
def collection_documents(fixture_kit)
|
146
|
+
# allow a standard key to be used for doing defaults in YAML
|
147
|
+
fixture_kit.fixtures.delete('DEFAULTS')
|
148
|
+
|
149
|
+
# track any join collection we need to insert later
|
150
|
+
documents = {}
|
151
|
+
documents[fixture_kit.class_name] = fixture_kit.fixtures.map do |label, fixture|
|
152
|
+
unmarshall_fixture(label, fixture, fixture_kit.model_class)
|
153
|
+
end
|
154
|
+
documents
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def save_document(doc)
|
160
|
+
doc.save({ validate: false })
|
161
|
+
end
|
162
|
+
|
163
|
+
def unmarshall_fixture(label, attributes, model_class)
|
164
|
+
model_class = model_class.constantize if model_class.is_a? String
|
165
|
+
attributes = attributes.to_hash
|
166
|
+
|
167
|
+
if label
|
168
|
+
attributes['__fixture_name'] = label
|
169
|
+
|
170
|
+
# interpolate the fixture label
|
171
|
+
attributes.each do |key, value|
|
172
|
+
attributes[key] = value.gsub('$LABEL', label) if value.is_a?(String)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
return attributes if model_class.nil?
|
177
|
+
|
178
|
+
unless attributes.key?('_id')
|
179
|
+
document = if label
|
180
|
+
find_or_create_document(model_class, label)
|
181
|
+
else
|
182
|
+
model_class.new
|
183
|
+
end
|
184
|
+
attributes['_id'] = document.id
|
185
|
+
end
|
186
|
+
|
187
|
+
set_attributes_timestamps(model_class, attributes)
|
188
|
+
|
189
|
+
model_class.relations.each_value do |relation|
|
190
|
+
case macro_from_relation(relation)
|
191
|
+
when :belongs_to
|
192
|
+
unmarshall_belongs_to(model_class, attributes, relation)
|
193
|
+
when :has_many
|
194
|
+
unmarshall_has_many(model_class, attributes, relation)
|
195
|
+
when :has_and_belongs_to_many
|
196
|
+
unmarshall_has_and_belongs_to_many(model_class, attributes, relation)
|
197
|
+
else
|
198
|
+
# type code here
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
attributes
|
203
|
+
end
|
204
|
+
|
205
|
+
def unmarshall_belongs_to(_model_class, attributes, relation)
|
206
|
+
value = attributes.delete(relation.name.to_s)
|
207
|
+
return if value.nil?
|
208
|
+
|
209
|
+
if value.is_a? Hash
|
210
|
+
raise Mongoid::FixtureKit::FixtureError, 'Unable to create document from nested attributes in a polymorphic relation' if relation.polymorphic?
|
211
|
+
document = relation.class_name.constantize.new
|
212
|
+
value = unmarshall_fixture(nil, value, relation.class_name)
|
213
|
+
document = update_document(document, value)
|
214
|
+
attributes[relation.foreign_key] = document.id
|
215
|
+
return
|
216
|
+
end
|
217
|
+
|
218
|
+
if relation.polymorphic? && value.sub!(/\s*\(([^)]*)\)\s*/, '')
|
219
|
+
type = Regexp.last_match(1)
|
220
|
+
attributes[relation.inverse_type] = type
|
221
|
+
attributes[relation.foreign_key] = find_or_create_document(type, value).id
|
222
|
+
else
|
223
|
+
attributes[relation.foreign_key] = find_or_create_document(relation.class_name, value).id
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
def unmarshall_has_many(model_class, attributes, relation)
|
228
|
+
values = attributes.delete(relation.name.to_s)
|
229
|
+
return if values.nil?
|
230
|
+
|
231
|
+
values.each do |value|
|
232
|
+
if value.is_a? Hash
|
233
|
+
document = relation.class_name.constantize.new
|
234
|
+
value[relation.foreign_key] = attributes['_id']
|
235
|
+
value[relation.type] = model_class.name if relation.polymorphic?
|
236
|
+
value = unmarshall_fixture(nil, value, relation.class_name)
|
237
|
+
update_document(document, value)
|
238
|
+
next
|
239
|
+
end
|
240
|
+
|
241
|
+
document = find_or_create_document(relation.class_name, value)
|
242
|
+
if relation.polymorphic?
|
243
|
+
update_document(document, { relation.foreign_key => attributes['_id'], relation.type => model_class.name })
|
244
|
+
else
|
245
|
+
update_document(document, { relation.foreign_key => attributes['_id'] })
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
def unmarshall_has_and_belongs_to_many(_model_class, attributes, relation)
|
251
|
+
values = attributes.delete(relation.name.to_s)
|
252
|
+
return if values.nil?
|
253
|
+
|
254
|
+
key = relation.foreign_key
|
255
|
+
attributes[key] = []
|
256
|
+
|
257
|
+
values.each do |value|
|
258
|
+
if value.is_a? Hash
|
259
|
+
document = relation.class_name.constantize.new
|
260
|
+
value[relation.inverse_foreign_key] = Array(attributes['_id'])
|
261
|
+
value = unmarshall_fixture(nil, value, relation.class_name)
|
262
|
+
update_document(document, value)
|
263
|
+
attributes[key] << document.id
|
264
|
+
next
|
265
|
+
end
|
266
|
+
|
267
|
+
document = find_or_create_document(relation.class_name, value)
|
268
|
+
attributes[key] << document.id
|
269
|
+
|
270
|
+
update_document(document, { relation.inverse_foreign_key => Array(attributes['_id']) })
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
def set_attributes_timestamps(model_class, attributes)
|
275
|
+
now = Time.now.utc
|
276
|
+
|
277
|
+
attributes['c_at'] = now if model_class < Mongoid::Timestamps::Created::Short && !attributes.key?('c_at')
|
278
|
+
attributes['created_at'] = now if model_class < Mongoid::Timestamps::Created && !attributes.key?('created_at')
|
279
|
+
|
280
|
+
attributes['u_at'] = now if model_class < Mongoid::Timestamps::Updated::Short && !attributes.key?('u_at')
|
281
|
+
attributes['updated_at'] = now if model_class < Mongoid::Timestamps::Updated && !attributes.key?('updated_at')
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class FixtureKit
|
3
|
+
attr_reader :name
|
4
|
+
attr_reader :path
|
5
|
+
attr_reader :model_class
|
6
|
+
attr_reader :class_name
|
7
|
+
attr_reader :fixtures
|
8
|
+
|
9
|
+
def initialize(name, class_name, path)
|
10
|
+
@name = name
|
11
|
+
@path = path
|
12
|
+
|
13
|
+
if class_name.is_a?(Class)
|
14
|
+
@model_class = class_name
|
15
|
+
elsif class_name
|
16
|
+
@model_class = class_name.safe_constantize
|
17
|
+
end
|
18
|
+
|
19
|
+
@class_name = if @model_class.respond_to?(:name)
|
20
|
+
@model_class.name
|
21
|
+
else
|
22
|
+
name.singularize.camelize
|
23
|
+
end
|
24
|
+
|
25
|
+
@fixtures = read_fixture_files
|
26
|
+
end
|
27
|
+
|
28
|
+
delegate :[], to: :fixtures
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def read_fixture_files
|
33
|
+
files = Dir["#{path}/{**,*}/*.yml"].select do |f|
|
34
|
+
::File.file?(f)
|
35
|
+
end
|
36
|
+
yaml_files = files.push("#{path}.yml")
|
37
|
+
|
38
|
+
yaml_files.each_with_object({}) do |file, fixtures|
|
39
|
+
Mongoid::FixtureKit::File.open(file) do |f|
|
40
|
+
f.each do |fixture_name, row|
|
41
|
+
fixtures[fixture_name] = Mongoid::FixtureKit::Fixture.new(fixture_name, row, model_class)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-fixture_kit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dániel Sipos
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-02-09 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: '7.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.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: '7.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '7.0'
|
41
|
+
description: Use fixtures with Mongoid the same way you did with ActiveRecord
|
42
|
+
email:
|
43
|
+
- siposdani87@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- LICENSE
|
49
|
+
- Rakefile
|
50
|
+
- lib/mongoid-fixture_kit.rb
|
51
|
+
- lib/mongoid/fixture_kit.rb
|
52
|
+
- lib/mongoid/fixture_kit/class_cache.rb
|
53
|
+
- lib/mongoid/fixture_kit/file.rb
|
54
|
+
- lib/mongoid/fixture_kit/fixture.rb
|
55
|
+
- lib/mongoid/fixture_kit/fixture_class_not_found.rb
|
56
|
+
- lib/mongoid/fixture_kit/fixture_error.rb
|
57
|
+
- lib/mongoid/fixture_kit/fixture_not_found.rb
|
58
|
+
- lib/mongoid/fixture_kit/format_error.rb
|
59
|
+
- lib/mongoid/fixture_kit/render_context.rb
|
60
|
+
- lib/mongoid/fixture_kit/test_helper.rb
|
61
|
+
- lib/mongoid/fixture_kit/util.rb
|
62
|
+
- lib/mongoid/fixture_kit/version.rb
|
63
|
+
homepage: https://github.com/siposdani87/mongoid-fixture-kit
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.3.26
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Fixtures for Rails Mongoid
|
86
|
+
test_files: []
|