hydra_attribute 0.4.2 → 0.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/.gitignore +2 -1
  2. data/.travis.yml +6 -5
  3. data/CHANGELOG.md +6 -0
  4. data/Gemfile +1 -1
  5. data/README.md +3 -3
  6. data/Rakefile +2 -7
  7. data/gemfiles/activerecord-3.2.gemfile +5 -0
  8. data/hydra_attribute.gemspec +6 -7
  9. data/lib/hydra_attribute.rb +17 -18
  10. data/lib/hydra_attribute/active_record.rb +34 -13
  11. data/lib/hydra_attribute/active_record/association_preloader.rb +47 -28
  12. data/lib/hydra_attribute/active_record/attribute_methods.rb +29 -140
  13. data/lib/hydra_attribute/active_record/mass_assignment_security.rb +39 -0
  14. data/lib/hydra_attribute/active_record/migration.rb +4 -4
  15. data/lib/hydra_attribute/active_record/relation.rb +6 -7
  16. data/lib/hydra_attribute/active_record/relation/query_methods.rb +28 -18
  17. data/lib/hydra_attribute/hydra_attribute.rb +12 -49
  18. data/lib/hydra_attribute/hydra_attribute_set.rb +67 -0
  19. data/lib/hydra_attribute/hydra_entity.rb +110 -0
  20. data/lib/hydra_attribute/hydra_entity_attribute_association.rb +155 -0
  21. data/lib/hydra_attribute/hydra_set.rb +24 -26
  22. data/lib/hydra_attribute/hydra_value.rb +210 -0
  23. data/lib/hydra_attribute/identity_map.rb +18 -0
  24. data/lib/hydra_attribute/middleware/identity_map.rb +15 -0
  25. data/lib/hydra_attribute/migrator.rb +24 -21
  26. data/lib/hydra_attribute/model.rb +47 -0
  27. data/lib/hydra_attribute/model/cacheable.rb +207 -0
  28. data/lib/hydra_attribute/model/dirty.rb +39 -0
  29. data/lib/hydra_attribute/model/has_many_through.rb +168 -0
  30. data/lib/hydra_attribute/model/identity_map.rb +59 -0
  31. data/lib/hydra_attribute/model/mediator.rb +89 -0
  32. data/lib/hydra_attribute/model/notifiable.rb +23 -0
  33. data/lib/hydra_attribute/model/persistence.rb +424 -0
  34. data/lib/hydra_attribute/model/validations.rb +40 -0
  35. data/lib/hydra_attribute/version.rb +1 -1
  36. data/spec/environments/mysql.rb +23 -0
  37. data/spec/environments/postgresql.rb +23 -0
  38. data/spec/environments/sqlite.rb +12 -0
  39. data/spec/fixtures/category.rb +8 -0
  40. data/spec/fixtures/product.rb +8 -0
  41. data/spec/fixtures/product_black_list.rb +13 -0
  42. data/spec/fixtures/product_white_list.rb +13 -0
  43. data/spec/hydra_attribute/active_record/attribute_methods_spec.rb +23 -28
  44. data/spec/hydra_attribute/active_record/mass_assignment_security_spec.rb +41 -0
  45. data/spec/hydra_attribute/active_record_spec.rb +577 -0
  46. data/spec/hydra_attribute/hydra_attribute_set_spec.rb +651 -0
  47. data/spec/hydra_attribute/hydra_attribute_spec.rb +208 -10
  48. data/spec/hydra_attribute/hydra_entity_attribute_association_spec.rb +216 -0
  49. data/spec/hydra_attribute/hydra_entity_spec.rb +71 -0
  50. data/spec/hydra_attribute/hydra_set_spec.rb +51 -10
  51. data/spec/hydra_attribute/hydra_value_spec.rb +286 -0
  52. data/spec/hydra_attribute/identity_map_spec.rb +47 -0
  53. data/spec/hydra_attribute/migrator_spec.rb +411 -0
  54. data/spec/hydra_attribute/model/cacheable_spec.rb +106 -0
  55. data/spec/hydra_attribute/model/has_many_through_spec.rb +132 -0
  56. data/spec/hydra_attribute/model/identity_map_spec.rb +39 -0
  57. data/spec/hydra_attribute/model/mediator_spec.rb +62 -0
  58. data/spec/hydra_attribute/model/persistence_spec.rb +550 -0
  59. data/spec/hydra_attribute/model_spec.rb +39 -0
  60. data/spec/hydra_attribute_spec.rb +36 -0
  61. data/spec/spec_helper.rb +10 -42
  62. metadata +76 -100
  63. data/Appraisals +0 -7
  64. data/cucumber.yml +0 -1
  65. data/features/entity/create.feature +0 -145
  66. data/features/entity/destroy.feature +0 -111
  67. data/features/entity/new.feature +0 -121
  68. data/features/entity/update.feature +0 -147
  69. data/features/hydra_attribute/create.feature +0 -30
  70. data/features/hydra_attribute/destroy.feature +0 -26
  71. data/features/hydra_attribute/update.feature +0 -36
  72. data/features/hydra_set/destroy.feature +0 -31
  73. data/features/migrations/create_and_drop.feature +0 -165
  74. data/features/migrations/migrate_and_rollback.feature +0 -211
  75. data/features/relation/query_methods/group.feature +0 -42
  76. data/features/relation/query_methods/order.feature +0 -67
  77. data/features/relation/query_methods/reorder.feature +0 -29
  78. data/features/relation/query_methods/reverse_order.feature +0 -29
  79. data/features/relation/query_methods/select.feature +0 -50
  80. data/features/relation/query_methods/where.feature +0 -115
  81. data/features/step_definitions/connections.rb +0 -65
  82. data/features/step_definitions/model_steps.rb +0 -136
  83. data/features/step_definitions/query_methods.rb +0 -48
  84. data/features/step_definitions/record_steps.rb +0 -93
  85. data/features/support/env.rb +0 -38
  86. data/features/support/world.rb +0 -61
  87. data/lib/hydra_attribute/active_record/association.rb +0 -113
  88. data/lib/hydra_attribute/active_record/reflection.rb +0 -16
  89. data/lib/hydra_attribute/association_builder.rb +0 -69
  90. data/lib/hydra_attribute/builder.rb +0 -37
  91. data/lib/hydra_attribute/entity_callbacks.rb +0 -26
  92. data/lib/hydra_attribute/hydra_attribute_methods.rb +0 -226
  93. data/lib/hydra_attribute/hydra_methods.rb +0 -528
  94. data/lib/hydra_attribute/hydra_set_methods.rb +0 -95
  95. data/lib/hydra_attribute/hydra_value_methods.rb +0 -21
  96. data/lib/hydra_attribute/memoizable.rb +0 -37
  97. data/spec/hydra_attribute/active_record/relation/query_methods_spec.rb +0 -31
  98. data/spec/hydra_attribute/hydra_attribute_methods_spec.rb +0 -458
  99. data/spec/hydra_attribute/hydra_methods_spec.rb +0 -456
  100. data/spec/hydra_attribute/hydra_set_methods_spec.rb +0 -203
  101. data/spec/hydra_attribute/memoizable_spec.rb +0 -95
@@ -0,0 +1,651 @@
1
+ require 'spec_helper'
2
+
3
+ describe HydraAttribute::HydraAttributeSet do
4
+ describe '.all_by_hydra_attribute_id' do
5
+ describe 'hydra_attribute_sets table has several records' do
6
+ let(:hydra_attribute_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'name', 'string', '2012-12-12', '2012-12-12')]).to_i }
7
+ let(:hydra_attribute_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'code', 'string', '2012-12-12', '2012-12-12')]).to_i }
8
+
9
+ let(:hydra_set_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'one', '2012-12-12', '2012-12-12')]).to_i }
10
+ let(:hydra_set_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'two', '2012-12-12', '2012-12-12')]).to_i }
11
+
12
+ let!(:id1) { ::ActiveRecord::Base.connection.insert(%[INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id1}, '2012-12-12', '2012-12-12')]).to_i }
13
+ let!(:id2) { ::ActiveRecord::Base.connection.insert(%[INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')]).to_i }
14
+ let!(:id3) { ::ActiveRecord::Base.connection.insert(%[INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id2}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')]).to_i }
15
+
16
+ it 'should return models which have a correct hydra_attribute_id' do
17
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id1)
18
+
19
+ hydra_attribute_sets.should have(2).models
20
+ hydra_attribute_sets.map(&:hydra_set_id).should =~ [hydra_set_id1, hydra_set_id2]
21
+
22
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id2)
23
+ hydra_attribute_sets.should have(1).model
24
+ hydra_attribute_sets.map(&:hydra_set_id).should == [hydra_set_id2]
25
+
26
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(0)
27
+ hydra_attribute_sets.should == []
28
+ end
29
+
30
+ it 'should return model which was created in runtime and has a correct hydra_attribute_id' do
31
+ hydra_set_id = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'set3').id
32
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id1, hydra_set_id: hydra_set_id)
33
+
34
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id1).should have(3).models
35
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id1).should include(hydra_attribute_set)
36
+
37
+ hydra_set_id = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'set4').id
38
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id1, hydra_set_id: hydra_set_id)
39
+
40
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id1).should have(4).models
41
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id1).should include(hydra_attribute_set)
42
+ end
43
+
44
+ it 'should not return model which was removed' do
45
+ id = ::ActiveRecord::Base.connection.select_value(%[SELECT id FROM hydra_attribute_sets WHERE hydra_attribute_id=#{hydra_attribute_id1} LIMIT 1])
46
+ HydraAttribute::HydraAttributeSet.find(id).destroy
47
+
48
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id1)
49
+ hydra_attribute_sets.should have(1).item
50
+ end
51
+
52
+ it 'should not return model if hydra_attribute_id was updated in runtime' do
53
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(id1)
54
+ hydra_attribute_set.hydra_attribute_id = hydra_attribute_id2
55
+ hydra_attribute_set.save
56
+
57
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id1)
58
+ hydra_attribute_sets.map(&:hydra_set_id).should =~ [hydra_set_id2]
59
+
60
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id2)
61
+ hydra_attribute_sets.map(&:hydra_set_id).should =~ [hydra_set_id1, hydra_set_id2]
62
+ end
63
+ end
64
+
65
+ describe 'hydra_attribute_sets table is blank' do
66
+ it 'should return blank collection' do
67
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(1).should == []
68
+ end
69
+
70
+ it 'should return model which was created in runtime and has a correct hydra_set_id' do
71
+ hydra_attribute_id = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'attr1', backend_type: 'string').id
72
+ hydra_set_id = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'set1').id
73
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id, hydra_set_id: hydra_set_id)
74
+
75
+
76
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id).should have(1).model
77
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id).should include(hydra_attribute_set)
78
+
79
+ hydra_set_id = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'set2').id
80
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id, hydra_set_id: hydra_set_id)
81
+
82
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id).should have(2).models
83
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute_id).should include(hydra_attribute_set)
84
+ end
85
+ end
86
+ end
87
+
88
+ describe '.all_by_hydra_set_id' do
89
+ describe 'hydra_attribute_sets table has several records' do
90
+ let(:hydra_attribute_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'name', 'string', '2012-12-12', '2012-12-12')]).to_i }
91
+ let(:hydra_attribute_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'code', 'string', '2012-12-12', '2012-12-12')]).to_i }
92
+
93
+ let(:hydra_set_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'one', '2012-12-12', '2012-12-12')]).to_i }
94
+ let(:hydra_set_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'two', '2012-12-12', '2012-12-12')]).to_i }
95
+
96
+ let!(:id1) { ::ActiveRecord::Base.connection.insert(%[INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id1}, '2012-12-12', '2012-12-12')]).to_i }
97
+ let!(:id2) { ::ActiveRecord::Base.connection.insert(%[INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id2}, #{hydra_set_id1}, '2012-12-12', '2012-12-12')]).to_i }
98
+ let!(:id3) { ::ActiveRecord::Base.connection.insert(%[INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id2}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')]).to_i }
99
+
100
+ it 'should return models which have a correct hydra_set_id' do
101
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id1)
102
+ hydra_attribute_sets.should have(2).models
103
+ hydra_attribute_sets.map(&:hydra_attribute_id).should == [hydra_attribute_id1, hydra_attribute_id2]
104
+
105
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id2)
106
+ hydra_attribute_sets.should have(1).model
107
+ hydra_attribute_sets.map(&:hydra_attribute_id).should == [hydra_attribute_id2]
108
+
109
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(0)
110
+ hydra_attribute_sets.should == []
111
+ end
112
+
113
+ it 'should return model which was created in runtime and has a correct hydra_set_id' do
114
+ hydra_attribute_id = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a3', backend_type: 'string').id
115
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id, hydra_set_id: hydra_set_id1)
116
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id1).should have(3).models
117
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id1).should include(hydra_attribute_set)
118
+
119
+ hydra_attribute_id = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a4', backend_type: 'string').id
120
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id, hydra_set_id: hydra_set_id1)
121
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id1).should have(4).models
122
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id1).should include(hydra_attribute_set)
123
+ end
124
+
125
+ it 'should not return model which was removed' do
126
+ id = ::ActiveRecord::Base.connection.select_value(%[SELECT id FROM hydra_attribute_sets WHERE hydra_set_id=#{hydra_set_id1} LIMIT 1])
127
+ HydraAttribute::HydraAttributeSet.find(id).destroy
128
+
129
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id1)
130
+ hydra_attribute_sets.should have(1).item
131
+ end
132
+
133
+ it 'should not return model if hydra_set_id was updated in runtime' do
134
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(id1)
135
+ hydra_attribute_set.hydra_set_id = hydra_set_id2
136
+ hydra_attribute_set.save
137
+
138
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id1)
139
+ hydra_attribute_sets.map(&:hydra_attribute_id).should =~ [hydra_attribute_id2]
140
+
141
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id2)
142
+ hydra_attribute_sets.map(&:hydra_attribute_id).should =~ [hydra_attribute_id1, hydra_attribute_id2]
143
+ end
144
+ end
145
+
146
+ describe 'hydra_attribute_sets table is blank' do
147
+ it 'should return blank collection' do
148
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(1).should == []
149
+ end
150
+
151
+ it 'should return model which was created in runtime and has a correct hydra_set_id' do
152
+ hydra_set_id = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 's1').id
153
+ hydra_attribute_id = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a1', backend_type: 'string').id
154
+
155
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id, hydra_set_id: hydra_set_id)
156
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id).should have(1).model
157
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id).should include(hydra_attribute_set)
158
+
159
+ hydra_attribute_id = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a2', backend_type: 'string').id
160
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id, hydra_set_id: hydra_set_id)
161
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id).should have(2).models
162
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set_id).should include(hydra_attribute_set)
163
+ end
164
+ end
165
+ end
166
+
167
+ describe '.hydra_attributes_by_hydra_set_id' do
168
+ describe 'hydra_attribute_sets table has several records' do
169
+ let(:hydra_set_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'one', '2012-12-12', '2012-12-12')]).to_i }
170
+ let(:hydra_set_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'two', '2012-12-12', '2012-12-12')]).to_i }
171
+
172
+ let(:hydra_attribute_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'name', 'string', '2012-12-12', '2012-12-12')]).to_i }
173
+ let(:hydra_attribute_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'code', 'string', '2012-12-12', '2012-12-12')]).to_i }
174
+
175
+ let!(:id1) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id1}, '2012-12-12', '2012-12-12')").to_i }
176
+ let!(:id2) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id2}, #{hydra_set_id1}, '2012-12-12', '2012-12-12')").to_i }
177
+ let!(:id3) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id2}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')").to_i }
178
+
179
+ it 'should return hydra_attribute models which assigned to the correct hydra_set_id' do
180
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(hydra_set_id1)
181
+ hydra_attributes.should have(2).models
182
+ hydra_attributes.map(&:name).should =~ %w[name code]
183
+
184
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(hydra_set_id2)
185
+ hydra_attributes.should have(1).model
186
+ hydra_attributes.map(&:name).should =~ %w[code]
187
+
188
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(0)
189
+ hydra_attributes.should == []
190
+ end
191
+
192
+ it 'should return hydra_attribute models which were created in runtime and are assigned to the correct hydra_set_id' do
193
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'title', backend_type: 'string')
194
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute.id, hydra_set_id: hydra_set_id1)
195
+
196
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(hydra_set_id1)
197
+ hydra_attributes.should have(3).models
198
+ hydra_attributes.map(&:name).should =~ %w[name code title]
199
+
200
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'quantity', backend_type: 'integer')
201
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute.id, hydra_set_id: hydra_set_id1)
202
+
203
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(hydra_set_id1)
204
+ hydra_attributes.should have(4).models
205
+ hydra_attributes.map(&:name).should =~ %w[name code title quantity]
206
+ end
207
+
208
+ it 'should not return model which was removed in runtime' do
209
+ HydraAttribute::HydraAttributeSet.find(id1).destroy
210
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(hydra_set_id1)
211
+ hydra_attributes.should have(1).item
212
+ hydra_attributes[0].name.should == 'code'
213
+ end
214
+
215
+ it 'should not return model if hydra_set_id was changed in runtime' do
216
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(id1)
217
+ hydra_attribute_set.hydra_set_id = hydra_set_id2
218
+ hydra_attribute_set.save
219
+
220
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(hydra_set_id1)
221
+ hydra_attributes.map(&:name).should =~ %w[code]
222
+
223
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(hydra_set_id2)
224
+ hydra_attributes.map(&:name).should =~ %w[name code]
225
+ end
226
+ end
227
+
228
+ describe 'hydra_attribute_sets table is blank' do
229
+ it 'should return blank collection' do
230
+ HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(1).should == []
231
+ end
232
+
233
+ it 'should return hydra_attribute models which were created in runtime and are assigned to the correct hydra_set_id' do
234
+ hydra_set_id = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'set1').id
235
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'title', backend_type: 'string')
236
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute.id, hydra_set_id: hydra_set_id)
237
+
238
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(hydra_set_id)
239
+ hydra_attributes.should have(1).model
240
+ hydra_attributes.map(&:name).should =~ %w[title]
241
+
242
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'quantity', backend_type: 'integer')
243
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute.id, hydra_set_id: hydra_set_id)
244
+
245
+ hydra_attributes = HydraAttribute::HydraAttributeSet.hydra_attributes_by_hydra_set_id(hydra_set_id)
246
+ hydra_attributes.should have(2).models
247
+ hydra_attributes.map(&:name).should =~ %w[title quantity]
248
+ end
249
+ end
250
+ end
251
+
252
+ describe '.hydra_sets_by_hydra_attribute_id' do
253
+ describe 'hydra_attribute_sets table has several records' do
254
+ let(:hydra_set_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'one', '2012-12-12', '2012-12-12')]).to_i }
255
+ let(:hydra_set_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'two', '2012-12-12', '2012-12-12')]).to_i }
256
+
257
+ let(:hydra_attribute_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'name', 'string', '2012-12-12', '2012-12-12')]).to_i }
258
+ let(:hydra_attribute_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'code', 'string', '2012-12-12', '2012-12-12')]).to_i }
259
+
260
+ let!(:id1) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id1}, '2012-12-12', '2012-12-12')").to_i }
261
+ let!(:id2) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')").to_i }
262
+ let!(:id3) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id2}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')").to_i }
263
+
264
+ it 'should return hydra_set models which assigned to the correct hydra_attribute_id' do
265
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(hydra_attribute_id1)
266
+ hydra_sets.should have(2).models
267
+ hydra_sets.map(&:name).should =~ %w[one two]
268
+
269
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(hydra_attribute_id2)
270
+ hydra_sets.should have(1).model
271
+ hydra_sets.map(&:name).should =~ %w[two]
272
+
273
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(0)
274
+ hydra_sets.should == []
275
+ end
276
+
277
+ it 'should return hydra_set models which were created in runtime and are assigned to the correct hydra_attribute_id' do
278
+ hydra_set = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'three')
279
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id1, hydra_set_id: hydra_set.id)
280
+
281
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(hydra_attribute_id1)
282
+ hydra_sets.should have(3).models
283
+ hydra_sets.map(&:name).should =~ %w[one two three]
284
+
285
+ hydra_set = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'four')
286
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute_id1, hydra_set_id: hydra_set.id)
287
+
288
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(hydra_attribute_id1)
289
+ hydra_sets.should have(4).models
290
+ hydra_sets.map(&:name).should =~ %w[one two three four]
291
+ end
292
+
293
+ it 'should not return model which was removed in runtime' do
294
+ HydraAttribute::HydraAttributeSet.find(id1).destroy
295
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(hydra_attribute_id1)
296
+ hydra_sets.should have(1).item
297
+ hydra_sets[0].name.should == 'two'
298
+ end
299
+
300
+ it 'should not return model if hydra_attribute_id was updated in runtime' do
301
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(id1)
302
+ hydra_attribute_set.hydra_attribute_id = hydra_attribute_id2
303
+ hydra_attribute_set.save
304
+
305
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(hydra_attribute_id1)
306
+ hydra_sets.map(&:name).should =~ %w[two]
307
+
308
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(hydra_attribute_id2)
309
+ hydra_sets.map(&:name).should =~ %w[one two]
310
+ end
311
+ end
312
+
313
+ describe 'hydra_attribute_sets table is blank' do
314
+ it 'should return blank collection' do
315
+ HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(1).should == []
316
+ end
317
+
318
+ it 'should return hydra_set models which were created in runtime and are assigned to the correct hydra_attribute_id' do
319
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a1', backend_type: 'string')
320
+ hydra_set = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'three')
321
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute.id, hydra_set_id: hydra_set.id)
322
+
323
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(hydra_attribute.id)
324
+ hydra_sets.should have(1).models
325
+ hydra_sets.map(&:name).should =~ %w[three]
326
+
327
+ hydra_set = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'four')
328
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute.id, hydra_set_id: hydra_set.id)
329
+
330
+ hydra_sets = HydraAttribute::HydraAttributeSet.hydra_sets_by_hydra_attribute_id(hydra_attribute.id)
331
+ hydra_sets.should have(2).models
332
+ hydra_sets.map(&:name).should =~ %w[three four]
333
+ end
334
+ end
335
+ end
336
+
337
+ describe '.hydra_attribute_ids_by_hydra_set_id' do
338
+ describe 'hydra_attribute_sets table has several records' do
339
+ let(:hydra_set_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'one', '2012-12-12', '2012-12-12')]).to_i }
340
+ let(:hydra_set_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'two', '2012-12-12', '2012-12-12')]).to_i }
341
+
342
+ let(:hydra_attribute_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'name', 'string', '2012-12-12', '2012-12-12')]).to_i }
343
+ let(:hydra_attribute_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'code', 'string', '2012-12-12', '2012-12-12')]).to_i }
344
+
345
+ let!(:id1) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id1}, '2012-12-12', '2012-12-12')").to_i }
346
+ let!(:id2) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')").to_i }
347
+ let!(:id3) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id2}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')").to_i }
348
+
349
+ it 'should return collection of hydra_attribute_id which are assigned to the correct hydra_set_id' do
350
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id1).should =~ [hydra_attribute_id1]
351
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id2).should =~ [hydra_attribute_id1, hydra_attribute_id2]
352
+ end
353
+
354
+ it 'should return hydra_attribute_ids which were created in runtime and are assigned to the correct hydra_set_id' do
355
+ hydra_attribute_id3 = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a1', backend_type: 'string').id
356
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set_id2, hydra_attribute_id: hydra_attribute_id3)
357
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id2).should =~ [hydra_attribute_id1, hydra_attribute_id2, hydra_attribute_id3]
358
+
359
+ hydra_attribute_id4 = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a2', backend_type: 'string').id
360
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set_id2, hydra_attribute_id: hydra_attribute_id4)
361
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id2).should =~ [hydra_attribute_id1, hydra_attribute_id2, hydra_attribute_id3, hydra_attribute_id4]
362
+ end
363
+
364
+ it 'should not return hydra_attribute_id if hydra_attribute_set was removed' do
365
+ HydraAttribute::HydraAttributeSet.find(id3).destroy
366
+ hydra_attribute_ids = HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id2)
367
+ hydra_attribute_ids.should == [hydra_attribute_id1]
368
+ end
369
+
370
+ it 'should not return hydra_attribute_id if hydra_set_id was updated in runtime' do
371
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(id3)
372
+ hydra_attribute_set.hydra_set_id = hydra_set_id1
373
+ hydra_attribute_set.save
374
+
375
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id1).should =~ [hydra_attribute_id1, hydra_attribute_id2]
376
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id2).should =~ [hydra_attribute_id1]
377
+ end
378
+
379
+ it 'should not return hydra_attribute_id if hydra_attribute_id was updated in runtime' do
380
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(id1)
381
+ hydra_attribute_set.hydra_attribute_id = hydra_attribute_id2
382
+ hydra_attribute_set.save
383
+
384
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id1).should =~ [hydra_attribute_id2]
385
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id2).should =~ [hydra_attribute_id1, hydra_attribute_id2]
386
+ end
387
+ end
388
+
389
+ describe 'hydra_attribute_sets table is blank' do
390
+ it 'should return blank collection' do
391
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(1).should == []
392
+ end
393
+
394
+ it 'should return hydra_attribute_ids which were created in runtime and are assigned to the correct hydra_set_id' do
395
+ hydra_set_id = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 's1').id
396
+
397
+ hydra_attribute_id1 = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a1', backend_type: 'string').id
398
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set_id, hydra_attribute_id: hydra_attribute_id1)
399
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id).should =~ [hydra_attribute_id1]
400
+
401
+ hydra_attribute_id2 = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a2', backend_type: 'string').id
402
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set_id, hydra_attribute_id: hydra_attribute_id2)
403
+ HydraAttribute::HydraAttributeSet.hydra_attribute_ids_by_hydra_set_id(hydra_set_id).should =~ [hydra_attribute_id1, hydra_attribute_id2]
404
+ end
405
+ end
406
+ end
407
+
408
+ describe '.hydra_set_ids_by_hydra_attribute_id' do
409
+ describe 'hydra_attribute_sets table has several records' do
410
+ let(:hydra_set_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'one', '2012-12-12', '2012-12-12')]).to_i }
411
+ let(:hydra_set_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'two', '2012-12-12', '2012-12-12')]).to_i }
412
+
413
+ let(:hydra_attribute_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'name', 'string', '2012-12-12', '2012-12-12')]).to_i }
414
+ let(:hydra_attribute_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'code', 'string', '2012-12-12', '2012-12-12')]).to_i }
415
+
416
+ let!(:id1) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id1}, '2012-12-12', '2012-12-12')").to_i }
417
+ let!(:id2) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id1}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')").to_i }
418
+ let!(:id3) { ::ActiveRecord::Base.connection.insert("INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id2}, #{hydra_set_id2}, '2012-12-12', '2012-12-12')").to_i }
419
+
420
+ it 'should return collection of hydra_set_id which are assigned to the correct hydra_attribute_id' do
421
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id1).should =~ [hydra_set_id1, hydra_set_id2]
422
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id2).should =~ [hydra_set_id2]
423
+ end
424
+
425
+ it 'should return hydra_set_ids which were created in runtime and are assigned to the correct hydra_attribute_id' do
426
+ hydra_set_id3 = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 's1').id
427
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set_id3, hydra_attribute_id: hydra_attribute_id1)
428
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id1).should =~ [hydra_set_id1, hydra_set_id2, hydra_set_id3]
429
+
430
+ hydra_set_id4 = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 's2').id
431
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set_id4, hydra_attribute_id: hydra_attribute_id1)
432
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id1).should =~ [hydra_set_id1, hydra_set_id2, hydra_set_id3, hydra_set_id4]
433
+ end
434
+
435
+ it 'should not return hydra_set_id if hydra_attribute_set was removed' do
436
+ HydraAttribute::HydraAttributeSet.find(id1).destroy
437
+ hydra_set_ids = HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id1)
438
+ hydra_set_ids.should == [hydra_set_id2]
439
+ end
440
+
441
+ it 'should not return hydra_set_id if hydra_attribute_id was updated in runtime' do
442
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(id1)
443
+ hydra_attribute_set.hydra_attribute_id = hydra_attribute_id2
444
+ hydra_attribute_set.save
445
+
446
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id1).should =~ [hydra_set_id2]
447
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id2).should =~ [hydra_set_id1, hydra_set_id2]
448
+ end
449
+
450
+ it 'should not return hydra_set_id if hydra_set_id was updated in runtime' do
451
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(id3)
452
+ hydra_attribute_set.hydra_set_id = hydra_set_id1
453
+ hydra_attribute_set.save
454
+
455
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id1).should =~ [hydra_set_id1, hydra_set_id2]
456
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id2).should =~ [hydra_set_id1]
457
+ end
458
+ end
459
+
460
+ describe 'hydra_attribute_sets table is blank' do
461
+ it 'should return blank collection' do
462
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(1).should == []
463
+ end
464
+
465
+ it 'should return hydra_set_ids which were created in runtime and are assigned to the correct hydra_attribute_id' do
466
+ hydra_set_id1 = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 's1').id
467
+ hydra_attribute_id = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'a1', backend_type: 'string').id
468
+
469
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set_id1, hydra_attribute_id: hydra_attribute_id)
470
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id).should =~ [hydra_set_id1]
471
+
472
+ hydra_set_id2 = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 's2').id
473
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set_id2, hydra_attribute_id: hydra_attribute_id)
474
+ HydraAttribute::HydraAttributeSet.hydra_set_ids_by_hydra_attribute_id(hydra_attribute_id).should =~ [hydra_set_id1, hydra_set_id2]
475
+ end
476
+ end
477
+ end
478
+
479
+ describe '.has_hydra_attribute_id_in_hydra_set_id?' do
480
+ let(:hydra_attribute_id) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'attr', 'string', '2012-12-12', '2012-12-12')]).to_i }
481
+ let(:hydra_set_id) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'set', '2012-12-12', '2012-12-12')]).to_i }
482
+ let!(:hydra_attribute_set_id) { ::ActiveRecord::Base.connection.insert(%[INSERT INTO hydra_attribute_sets(hydra_attribute_id, hydra_set_id, created_at, updated_at) VALUES(#{hydra_attribute_id}, #{hydra_set_id}, '2012-12-12', '2012-12-12')]).to_i }
483
+
484
+ it 'should return true if hydra_attribute_id is assigned to hydra_set_id' do
485
+ HydraAttribute::HydraAttributeSet.should have_hydra_attribute_id_in_hydra_set_id(hydra_attribute_id, hydra_set_id)
486
+ end
487
+
488
+ it 'should return true if hydra_attribute_id is assigned to hydra_set_id in runtime' do
489
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'attr1', backend_type: 'string')
490
+ hydra_set = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'set1')
491
+
492
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: hydra_attribute.id, hydra_set_id: hydra_set.id)
493
+ HydraAttribute::HydraAttributeSet.should have_hydra_attribute_id_in_hydra_set_id(hydra_attribute.id, hydra_set.id)
494
+ end
495
+
496
+ it 'should return false if hydra_attribute_id is not assigned to hydra_set_id' do
497
+ HydraAttribute::HydraAttributeSet.should_not have_hydra_attribute_id_in_hydra_set_id(0, 0)
498
+ end
499
+
500
+ it 'should return false if hydra_attribute_set was destroyed in runtime' do
501
+ HydraAttribute::HydraAttributeSet.find(hydra_attribute_set_id).destroy
502
+ HydraAttribute::HydraAttributeSet.should_not have_hydra_attribute_id_in_hydra_set_id(hydra_attribute_id, hydra_set_id)
503
+ end
504
+
505
+ it 'should return false if hydra_attribute_set was updated in runtime' do
506
+ hydra_attribute_id2 = ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'attr2', 'string', '2012-12-12', '2012-12-12')]).to_i
507
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(hydra_attribute_set_id)
508
+ hydra_attribute_set.hydra_attribute_id = hydra_attribute_id2
509
+ hydra_attribute_set.save
510
+
511
+ HydraAttribute::HydraAttributeSet.should_not have_hydra_attribute_id_in_hydra_set_id(hydra_attribute_id, hydra_set_id)
512
+ end
513
+
514
+ it 'should return false if hydra_set_id was updated in runtime' do
515
+ hydra_set_id2 = ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_sets(entity_type, name, created_at, updated_at) VALUES('Product', 'set2', '2012-12-12', '2012-12-12')]).to_i
516
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.find(hydra_attribute_set_id)
517
+ hydra_attribute_set.hydra_set_id = hydra_set_id2
518
+ hydra_attribute_set.save
519
+
520
+ HydraAttribute::HydraAttributeSet.should_not have_hydra_attribute_id_in_hydra_set_id(hydra_attribute_id, hydra_set_id)
521
+ end
522
+ end
523
+
524
+ describe '#hydra_set' do
525
+ it 'should return HydraSet model if this model is persisted' do
526
+ hydra_set = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'default')
527
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'attr', backend_type: 'string')
528
+
529
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set.id, hydra_attribute_id: hydra_attribute.id)
530
+ hydra_attribute_set.hydra_set.should be(hydra_set)
531
+ end
532
+
533
+ it 'should return nil if this model is not persisted' do
534
+ HydraAttribute::HydraAttributeSet.new.hydra_set.should be_nil
535
+ end
536
+ end
537
+
538
+ describe '#hydra_attribute' do
539
+ it 'should return HydraAttribute model if this model is persisted' do
540
+ hydra_set = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'default')
541
+ hydra_attribute = HydraAttribute::HydraAttribute.create(name: 'title', entity_type: 'Product', backend_type: 'string')
542
+
543
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set.id, hydra_attribute_id: hydra_attribute.id)
544
+ hydra_attribute_set.hydra_attribute.should be(hydra_attribute)
545
+ end
546
+
547
+ it 'should return nil if this model is not persisted' do
548
+ HydraAttribute::HydraAttributeSet.new.hydra_attribute.should be_nil
549
+ end
550
+ end
551
+
552
+ describe 'callbacks' do
553
+ describe 'hydra_set destroyed' do
554
+ let!(:hydra_set) { HydraAttribute::HydraSet.create(name: 'default', entity_type: 'Product') }
555
+ let!(:hydra_attribute) { HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'title', backend_type: 'string') }
556
+ let!(:hydra_attribute_set) { HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set.id, hydra_attribute_id: hydra_attribute.id) }
557
+
558
+ it 'should delete hydra_attribute_set relation from database' do
559
+ hydra_set.destroy
560
+ ::ActiveRecord::Base.connection.select_value(%[SELECT COUNT(*) FROM hydra_attribute_sets WHERE id=#{hydra_attribute_set.id}]).to_i.should be(0)
561
+ end
562
+
563
+ it 'should delete hydra_attribute_set relation from cache' do
564
+ hydra_set.destroy
565
+ lambda do
566
+ HydraAttribute::HydraAttributeSet.find(hydra_attribute_set.id)
567
+ end.should raise_error(HydraAttribute::RecordNotFound, "Couldn't find HydraAttribute::HydraAttributeSet with id=#{hydra_attribute_set.id}")
568
+ end
569
+
570
+ it 'should delete hydra_set cache' do
571
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set.id).should include(hydra_attribute_set)
572
+ hydra_set.destroy
573
+ HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set.id).should == []
574
+ end
575
+
576
+ it 'should delete hydra_set from hydra_attribute cache' do
577
+ hydra_set2 = HydraAttribute::HydraSet.create(name: 'second', entity_type: 'Product')
578
+ hydra_attribute_set2 = HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set2.id, hydra_attribute_id: hydra_attribute.id)
579
+
580
+ hydra_set.destroy
581
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute.id)
582
+ hydra_attribute_sets.should_not include(hydra_attribute_set)
583
+ hydra_attribute_sets.should include(hydra_attribute_set2)
584
+ end
585
+ end
586
+
587
+ describe 'hydra_attribute destroyed' do
588
+ let!(:hydra_set) { HydraAttribute::HydraSet.create(name: 'default', entity_type: 'Product') }
589
+ let!(:hydra_attribute) { HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'title', backend_type: 'string') }
590
+ let!(:hydra_attribute_set) { HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set.id, hydra_attribute_id: hydra_attribute.id) }
591
+
592
+ it 'should delete hydra_attribute_set relation from database' do
593
+ hydra_attribute.destroy
594
+ ::ActiveRecord::Base.connection.select_value(%[SELECT COUNT(*) FROM hydra_attribute_sets WHERE id=#{hydra_attribute_set.id}]).to_i.should be(0)
595
+ end
596
+
597
+ it 'should delete hydra_attribute_set relation from cache' do
598
+ hydra_set.destroy
599
+ lambda do
600
+ HydraAttribute::HydraAttributeSet.find(hydra_attribute_set.id)
601
+ end.should raise_error(HydraAttribute::RecordNotFound, "Couldn't find HydraAttribute::HydraAttributeSet with id=#{hydra_attribute_set.id}")
602
+ end
603
+
604
+ it 'should delete hydra_attribute cache' do
605
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute.id).should include(hydra_attribute_set)
606
+ hydra_attribute.destroy
607
+ HydraAttribute::HydraAttributeSet.all_by_hydra_attribute_id(hydra_attribute.id).should == []
608
+ end
609
+
610
+ it 'should delete hydra_attribute from hydra_set cache' do
611
+ hydra_attribute2 = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'code', backend_type: 'string')
612
+ hydra_attribute_set2 = HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set.id, hydra_attribute_id: hydra_attribute2.id)
613
+
614
+ hydra_attribute.destroy
615
+ hydra_attribute_sets = HydraAttribute::HydraAttributeSet.all_by_hydra_set_id(hydra_set.id)
616
+ hydra_attribute_sets.should_not include(hydra_attribute_set)
617
+ hydra_attribute_sets.should include(hydra_attribute_set2)
618
+ end
619
+ end
620
+ end
621
+
622
+ describe 'validations' do
623
+ it 'should require hydra_set_id' do
624
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.new
625
+ hydra_attribute_set.valid?
626
+ hydra_attribute_set.errors.should include(:hydra_set_id)
627
+
628
+ hydra_attribute_set.hydra_set_id = 1
629
+ hydra_attribute_set.valid?
630
+ hydra_attribute_set.errors.should_not include(:hydra_set_id)
631
+ end
632
+
633
+ it 'should require hydra_attribute_id' do
634
+ hydra_attribute_set = HydraAttribute::HydraAttributeSet.new
635
+ hydra_attribute_set.valid?
636
+ hydra_attribute_set.errors.should include(:hydra_attribute_id)
637
+
638
+ hydra_attribute_set.hydra_attribute_id = 1
639
+ hydra_attribute_set.valid?
640
+ hydra_attribute_set.errors.should_not include(:hydra_attribute_id)
641
+ end
642
+
643
+ it 'should have unique hydra_set_id with hydra_attribute_id' do
644
+ hydra_set = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'default')
645
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'attr', backend_type: 'string')
646
+
647
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set.id, hydra_attribute_id: hydra_attribute.id).should be_persisted
648
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set.id, hydra_attribute_id: hydra_attribute.id).should_not be_persisted
649
+ end
650
+ end
651
+ end