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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8891f4eae6a846883d879c031d00ab00ffb858a525fddb783936197f8f3b71b
4
- data.tar.gz: 4d805da0f74023dae73a0d39b26eab714cebbd0418ab0d52d535f0ff51eea487
3
+ metadata.gz: b6f576e8f49dc8f69d5fc2d69f2c2aa4d910bb5121bc4c4b6592e619ff6b3204
4
+ data.tar.gz: 6a67780022ef088022a121af8dfe51c691acac4d08a855d7ffcf8c71f606e974
5
5
  SHA512:
6
- metadata.gz: 34c3d210776f1237146e3d9fc572a4aa9c206e504a7e8e63684038151b415ae73935acba42be3f100f93cb6f093b71dc6ac07feaf19965ae10977abf3393bc37
7
- data.tar.gz: 339860729e0dc898226bcec88cd322aaebcbc5ee32b14b90fff5763f87cf9bf795f85cbc3045b6660fb21f98ae8a7ac2781d3a65845efc7a7ffc674d4d5cd920
6
+ metadata.gz: 97455d54e77ba5795e6001b8cacbab5a3517335c66f26766c1a09ad1a7ae6ffd5cd33a706ba402b6813116cafe871835228c500d9f46bc0c28eb75e615a23c13
7
+ data.tar.gz: 973f909842955d39a1728962ab25e0dbd4a812cedb4b947aa5b34a9b6953744930d714461d277f01c0f42186752f4b51435d4505c0d0e9b8e36e098ab99e241b
@@ -32,7 +32,7 @@ module Mongoid
32
32
  end
33
33
 
34
34
  def render(content)
35
- context = Mongoid::FixtureKit::RenderContext.new
35
+ context = Mongoid::FixtureKit::RenderContext.create_subclass.new
36
36
  ERB.new(content).result(context.binder)
37
37
  end
38
38
 
@@ -26,7 +26,7 @@ module Mongoid
26
26
  alias to_hash fixture
27
27
 
28
28
  def find
29
- raise FixtureClassNotFound, _('No class attached to find.') unless model_class
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
@@ -1,8 +1,12 @@
1
1
  module Mongoid
2
2
  class FixtureKit
3
3
  class RenderContext
4
- def binder
5
- binding
4
+ def self.create_subclass
5
+ Class.new Mongoid::FixtureKit.context_class do
6
+ def binder
7
+ binding
8
+ end
9
+ end
6
10
  end
7
11
  end
8
12
  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
- 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])
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?) || (is_new && document[name])
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 |embedded, i|
98
- embedded_document_set_default_values(embedded, document[name][i])
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
 
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  class FixtureKit
3
- VERSION = '0.1.2'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -33,6 +33,10 @@ module Mongoid
33
33
  @fixtures = read_fixture_files
34
34
  end
35
35
 
36
+ def self.context_class
37
+ @context_class ||= Class.new
38
+ end
39
+
36
40
  delegate :[], to: :fixtures
37
41
 
38
42
  private
@@ -0,0 +1,12 @@
1
+ sudoers:
2
+ name: Sudoers!
3
+ something: organization1 (Organisation)
4
+ print:
5
+ name: Print
6
+ users:
7
+ - user1
8
+ - user 2é
9
+ group1:
10
+ name: Margot
11
+ main_users:
12
+ - user1
@@ -0,0 +1,2 @@
1
+ error:
2
+ name: Does not exist?
@@ -0,0 +1,4 @@
1
+ organization1:
2
+ name: <%= '1 Organisation' %>
3
+ groups:
4
+ - print
@@ -0,0 +1,8 @@
1
+ school:
2
+ name: School
3
+ groups:
4
+ - group1
5
+ <% 5.times do |i| %>
6
+ school<%= i %>:
7
+ name: School <%= i %>
8
+ <% end %>
@@ -0,0 +1,4 @@
1
+ dad:
2
+ firstname: Dad
3
+ mom:
4
+ firstname: Mom
@@ -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,2 @@
1
+ test1:
2
+ value: <%= count.to_s %>
@@ -0,0 +1,11 @@
1
+ class Address
2
+ include Mongoid::Document
3
+
4
+ embedded_in :place, polymorphic: true
5
+
6
+ field :city
7
+ field :real, type: Boolean, default: true
8
+
9
+ belongs_to :organisation
10
+ end
11
+
@@ -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
+
@@ -0,0 +1,8 @@
1
+ class Home
2
+ include Mongoid::Document
3
+
4
+ field :name
5
+
6
+ embeds_one :address, as: :place
7
+ end
8
+
@@ -0,0 +1,8 @@
1
+ class Item
2
+ include Mongoid::Document
3
+
4
+ belongs_to :user
5
+
6
+ field :name
7
+ end
8
+
@@ -0,0 +1,9 @@
1
+ class Organisation
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps::Created
4
+
5
+ field :name
6
+
7
+ has_many :groups, as: :something
8
+ end
9
+
@@ -0,0 +1,9 @@
1
+ class School
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps::Updated
4
+
5
+ field :name
6
+
7
+ has_many :groups, as: :something
8
+ end
9
+
@@ -0,0 +1,6 @@
1
+ class Test
2
+ include Mongoid::Document
3
+
4
+ field :value
5
+ end
6
+
@@ -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
@@ -0,0 +1,6 @@
1
+ test:
2
+ clients:
3
+ default:
4
+ database: mongoid_fixture_kit_test
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,4 @@
1
+ will_fail:
2
+ name: will_fail
3
+ something:
4
+ name: NOK
@@ -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.1.2
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-02-09 00:00:00.000000000 Z
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.3.26
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