mongoid-fixture_kit 0.1.2 → 0.2.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/lib/mongoid/fixture_kit/file.rb +1 -1
- data/lib/mongoid/fixture_kit/fixture.rb +1 -1
- data/lib/mongoid/fixture_kit/render_context.rb +6 -2
- data/lib/mongoid/fixture_kit/util.rb +13 -4
- data/lib/mongoid/fixture_kit/version.rb +1 -1
- data/lib/mongoid/fixture_kit.rb +4 -0
- data/test/fixtures/groups.yml +12 -0
- data/test/fixtures/not_models.yml +2 -0
- data/test/fixtures/organisations.yml +4 -0
- data/test/fixtures/schools.yml +8 -0
- data/test/fixtures/users/family.yml +4 -0
- data/test/fixtures/users.yml +34 -0
- data/test/load_once_fixtures/tests.yml +2 -0
- data/test/models/address.rb +11 -0
- data/test/models/group.rb +13 -0
- data/test/models/home.rb +8 -0
- data/test/models/item.rb +8 -0
- data/test/models/organisation.rb +9 -0
- data/test/models/school.rb +9 -0
- data/test/models/test.rb +6 -0
- data/test/models/user.rb +20 -0
- data/test/mongoid/fixture_kit_test.rb +95 -0
- data/test/mongoid/fixtures_test.rb +16 -0
- data/test/mongoid/load_once_test.rb +35 -0
- data/test/mongoid.yml +6 -0
- data/test/nested_polymorphic_relation_fixtures/groups.yml +4 -0
- data/test/test_helper.rb +23 -0
- metadata +46 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6f576e8f49dc8f69d5fc2d69f2c2aa4d910bb5121bc4c4b6592e619ff6b3204
|
4
|
+
data.tar.gz: 6a67780022ef088022a121af8dfe51c691acac4d08a855d7ffcf8c71f606e974
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97455d54e77ba5795e6001b8cacbab5a3517335c66f26766c1a09ad1a7ae6ffd5cd33a706ba402b6813116cafe871835228c500d9f46bc0c28eb75e615a23c13
|
7
|
+
data.tar.gz: 973f909842955d39a1728962ab25e0dbd4a812cedb4b947aa5b34a9b6953744930d714461d277f01c0f42186752f4b51435d4505c0d0e9b8e36e098ab99e241b
|
@@ -26,7 +26,7 @@ module Mongoid
|
|
26
26
|
alias to_hash fixture
|
27
27
|
|
28
28
|
def find
|
29
|
-
raise FixtureClassNotFound,
|
29
|
+
raise FixtureClassNotFound, 'No class attached to find.' unless model_class
|
30
30
|
model_class.unscoped do
|
31
31
|
model_class.find_by('__fixture_name' => name)
|
32
32
|
end
|
@@ -76,6 +76,8 @@ module Mongoid
|
|
76
76
|
value = attributes[key] || document[key]
|
77
77
|
if key.include?('_translations')
|
78
78
|
document.public_send("#{key}=", value)
|
79
|
+
elsif attributes[key].is_a?(Array) || document[key].is_a?(Array)
|
80
|
+
document[key] = Array(attributes[key]) + Array(document[key])
|
79
81
|
else
|
80
82
|
document[key] = value
|
81
83
|
end
|
@@ -90,12 +92,17 @@ module Mongoid
|
|
90
92
|
document.relations.each do |name, relation|
|
91
93
|
case macro_from_relation(relation)
|
92
94
|
when :embeds_one
|
93
|
-
|
95
|
+
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
96
|
+
(is_new && document[name])
|
97
|
+
embedded_document = document.public_send(relation.name)
|
98
|
+
embedded_document_set_default_values(embedded_document, document[name])
|
99
|
+
end
|
94
100
|
when :embeds_many
|
95
|
-
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
101
|
+
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
102
|
+
(is_new && document[name])
|
96
103
|
embedded_documents = document.public_send(relation.name)
|
97
|
-
embedded_documents.each_with_index do |
|
98
|
-
embedded_document_set_default_values(
|
104
|
+
embedded_documents.each_with_index do |embedded_document, i|
|
105
|
+
embedded_document_set_default_values(embedded_document, document[name][i])
|
99
106
|
end
|
100
107
|
end
|
101
108
|
when :belongs_to
|
@@ -116,6 +123,8 @@ module Mongoid
|
|
116
123
|
attributes.delete('_id')
|
117
124
|
document.fields.select do |k, v|
|
118
125
|
k != '_id' && !v.default_val.nil? && attributes[k] == document[k]
|
126
|
+
end.each do |k, _v|
|
127
|
+
attributes.delete(k)
|
119
128
|
end
|
120
129
|
end
|
121
130
|
|
data/lib/mongoid/fixture_kit.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
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?
|
@@ -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/home.rb
ADDED
data/test/models/item.rb
ADDED
data/test/models/test.rb
ADDED
data/test/models/user.rb
ADDED
@@ -0,0 +1,20 @@
|
|
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
|
+
|
@@ -0,0 +1,95 @@
|
|
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
|
+
|
@@ -0,0 +1,16 @@
|
|
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
|
@@ -0,0 +1,35 @@
|
|
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
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
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
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.2.0
|
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
14
|
name: mongoid
|
@@ -60,6 +60,27 @@ files:
|
|
60
60
|
- lib/mongoid/fixture_kit/test_helper.rb
|
61
61
|
- lib/mongoid/fixture_kit/util.rb
|
62
62
|
- lib/mongoid/fixture_kit/version.rb
|
63
|
+
- test/fixtures/groups.yml
|
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
|
63
84
|
homepage: https://github.com/siposdani87/mongoid-fixture-kit
|
64
85
|
licenses:
|
65
86
|
- MIT
|
@@ -79,8 +100,29 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
100
|
- !ruby/object:Gem::Version
|
80
101
|
version: '0'
|
81
102
|
requirements: []
|
82
|
-
rubygems_version: 3.
|
103
|
+
rubygems_version: 3.4.13
|
83
104
|
signing_key:
|
84
105
|
specification_version: 4
|
85
106
|
summary: Fixtures for Rails Mongoid
|
86
|
-
test_files:
|
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
|