sbf-dm-core 1.3.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +29 -0
  3. data/.document +5 -0
  4. data/.gitignore +44 -0
  5. data/.rspec +1 -0
  6. data/.rubocop.yml +468 -0
  7. data/.travis.yml +57 -0
  8. data/.yardopts +1 -0
  9. data/Gemfile +70 -0
  10. data/LICENSE +20 -0
  11. data/README.md +269 -0
  12. data/Rakefile +4 -0
  13. data/dm-core.gemspec +21 -0
  14. data/lib/dm-core/adapters/abstract_adapter.rb +233 -0
  15. data/lib/dm-core/adapters/in_memory_adapter.rb +110 -0
  16. data/lib/dm-core/adapters.rb +249 -0
  17. data/lib/dm-core/associations/many_to_many.rb +477 -0
  18. data/lib/dm-core/associations/many_to_one.rb +282 -0
  19. data/lib/dm-core/associations/one_to_many.rb +332 -0
  20. data/lib/dm-core/associations/one_to_one.rb +84 -0
  21. data/lib/dm-core/associations/relationship.rb +650 -0
  22. data/lib/dm-core/backwards.rb +11 -0
  23. data/lib/dm-core/collection.rb +1486 -0
  24. data/lib/dm-core/core_ext/kernel.rb +21 -0
  25. data/lib/dm-core/core_ext/pathname.rb +4 -0
  26. data/lib/dm-core/core_ext/symbol.rb +10 -0
  27. data/lib/dm-core/identity_map.rb +6 -0
  28. data/lib/dm-core/model/hook.rb +99 -0
  29. data/lib/dm-core/model/is.rb +30 -0
  30. data/lib/dm-core/model/property.rb +244 -0
  31. data/lib/dm-core/model/relationship.rb +366 -0
  32. data/lib/dm-core/model/scope.rb +87 -0
  33. data/lib/dm-core/model.rb +876 -0
  34. data/lib/dm-core/property/binary.rb +19 -0
  35. data/lib/dm-core/property/boolean.rb +35 -0
  36. data/lib/dm-core/property/class.rb +23 -0
  37. data/lib/dm-core/property/date.rb +45 -0
  38. data/lib/dm-core/property/date_time.rb +44 -0
  39. data/lib/dm-core/property/decimal.rb +47 -0
  40. data/lib/dm-core/property/discriminator.rb +40 -0
  41. data/lib/dm-core/property/float.rb +27 -0
  42. data/lib/dm-core/property/integer.rb +32 -0
  43. data/lib/dm-core/property/invalid_value_error.rb +17 -0
  44. data/lib/dm-core/property/lookup.rb +26 -0
  45. data/lib/dm-core/property/numeric.rb +35 -0
  46. data/lib/dm-core/property/object.rb +33 -0
  47. data/lib/dm-core/property/serial.rb +13 -0
  48. data/lib/dm-core/property/string.rb +47 -0
  49. data/lib/dm-core/property/text.rb +12 -0
  50. data/lib/dm-core/property/time.rb +46 -0
  51. data/lib/dm-core/property/typecast/numeric.rb +32 -0
  52. data/lib/dm-core/property/typecast/time.rb +33 -0
  53. data/lib/dm-core/property.rb +856 -0
  54. data/lib/dm-core/property_set.rb +177 -0
  55. data/lib/dm-core/query/conditions/comparison.rb +886 -0
  56. data/lib/dm-core/query/conditions/operation.rb +710 -0
  57. data/lib/dm-core/query/direction.rb +33 -0
  58. data/lib/dm-core/query/operator.rb +34 -0
  59. data/lib/dm-core/query/path.rb +113 -0
  60. data/lib/dm-core/query/sort.rb +38 -0
  61. data/lib/dm-core/query.rb +1352 -0
  62. data/lib/dm-core/relationship_set.rb +69 -0
  63. data/lib/dm-core/repository.rb +226 -0
  64. data/lib/dm-core/resource/persistence_state/clean.rb +36 -0
  65. data/lib/dm-core/resource/persistence_state/deleted.rb +26 -0
  66. data/lib/dm-core/resource/persistence_state/dirty.rb +91 -0
  67. data/lib/dm-core/resource/persistence_state/immutable.rb +32 -0
  68. data/lib/dm-core/resource/persistence_state/persisted.rb +25 -0
  69. data/lib/dm-core/resource/persistence_state/transient.rb +87 -0
  70. data/lib/dm-core/resource/persistence_state.rb +70 -0
  71. data/lib/dm-core/resource.rb +1220 -0
  72. data/lib/dm-core/spec/lib/adapter_helpers.rb +63 -0
  73. data/lib/dm-core/spec/lib/collection_helpers.rb +21 -0
  74. data/lib/dm-core/spec/lib/counter_adapter.rb +38 -0
  75. data/lib/dm-core/spec/lib/pending_helpers.rb +50 -0
  76. data/lib/dm-core/spec/lib/spec_helper.rb +74 -0
  77. data/lib/dm-core/spec/setup.rb +164 -0
  78. data/lib/dm-core/spec/shared/adapter_spec.rb +366 -0
  79. data/lib/dm-core/spec/shared/public/property_spec.rb +229 -0
  80. data/lib/dm-core/spec/shared/resource_spec.rb +1221 -0
  81. data/lib/dm-core/spec/shared/sel_spec.rb +111 -0
  82. data/lib/dm-core/spec/shared/semipublic/property_spec.rb +184 -0
  83. data/lib/dm-core/spec/shared/semipublic/query/conditions/abstract_comparison_spec.rb +261 -0
  84. data/lib/dm-core/support/assertions.rb +8 -0
  85. data/lib/dm-core/support/chainable.rb +18 -0
  86. data/lib/dm-core/support/deprecate.rb +12 -0
  87. data/lib/dm-core/support/descendant_set.rb +89 -0
  88. data/lib/dm-core/support/equalizer.rb +48 -0
  89. data/lib/dm-core/support/ext/array.rb +22 -0
  90. data/lib/dm-core/support/ext/blank.rb +25 -0
  91. data/lib/dm-core/support/ext/hash.rb +67 -0
  92. data/lib/dm-core/support/ext/module.rb +47 -0
  93. data/lib/dm-core/support/ext/object.rb +57 -0
  94. data/lib/dm-core/support/ext/string.rb +24 -0
  95. data/lib/dm-core/support/ext/try_dup.rb +12 -0
  96. data/lib/dm-core/support/hook.rb +388 -0
  97. data/lib/dm-core/support/inflections.rb +60 -0
  98. data/lib/dm-core/support/inflector/inflections.rb +211 -0
  99. data/lib/dm-core/support/inflector/methods.rb +151 -0
  100. data/lib/dm-core/support/lazy_array.rb +451 -0
  101. data/lib/dm-core/support/local_object_space.rb +13 -0
  102. data/lib/dm-core/support/logger.rb +201 -0
  103. data/lib/dm-core/support/mash.rb +176 -0
  104. data/lib/dm-core/support/naming_conventions.rb +109 -0
  105. data/lib/dm-core/support/ordered_set.rb +381 -0
  106. data/lib/dm-core/support/subject.rb +33 -0
  107. data/lib/dm-core/support/subject_set.rb +251 -0
  108. data/lib/dm-core/version.rb +3 -0
  109. data/lib/dm-core.rb +274 -0
  110. data/script/performance.rb +275 -0
  111. data/script/profile.rb +218 -0
  112. data/spec/lib/rspec_immediate_feedback_formatter.rb +54 -0
  113. data/spec/public/associations/many_to_many/read_multiple_join_spec.rb +69 -0
  114. data/spec/public/associations/many_to_many_spec.rb +197 -0
  115. data/spec/public/associations/many_to_one_spec.rb +83 -0
  116. data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +40 -0
  117. data/spec/public/associations/many_to_one_with_custom_fk_spec.rb +49 -0
  118. data/spec/public/associations/one_to_many_spec.rb +81 -0
  119. data/spec/public/associations/one_to_one_spec.rb +176 -0
  120. data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +46 -0
  121. data/spec/public/collection_spec.rb +69 -0
  122. data/spec/public/finalize_spec.rb +77 -0
  123. data/spec/public/model/hook_spec.rb +245 -0
  124. data/spec/public/model/property_spec.rb +91 -0
  125. data/spec/public/model/relationship_spec.rb +1040 -0
  126. data/spec/public/model_spec.rb +456 -0
  127. data/spec/public/property/binary_spec.rb +43 -0
  128. data/spec/public/property/boolean_spec.rb +21 -0
  129. data/spec/public/property/class_spec.rb +27 -0
  130. data/spec/public/property/date_spec.rb +21 -0
  131. data/spec/public/property/date_time_spec.rb +21 -0
  132. data/spec/public/property/decimal_spec.rb +23 -0
  133. data/spec/public/property/discriminator_spec.rb +134 -0
  134. data/spec/public/property/float_spec.rb +22 -0
  135. data/spec/public/property/integer_spec.rb +22 -0
  136. data/spec/public/property/object_spec.rb +117 -0
  137. data/spec/public/property/serial_spec.rb +22 -0
  138. data/spec/public/property/string_spec.rb +21 -0
  139. data/spec/public/property/text_spec.rb +62 -0
  140. data/spec/public/property/time_spec.rb +21 -0
  141. data/spec/public/property_spec.rb +333 -0
  142. data/spec/public/resource/state_spec.rb +72 -0
  143. data/spec/public/resource_spec.rb +289 -0
  144. data/spec/public/sel_spec.rb +53 -0
  145. data/spec/public/setup_spec.rb +145 -0
  146. data/spec/public/shared/association_collection_shared_spec.rb +309 -0
  147. data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
  148. data/spec/public/shared/collection_shared_spec.rb +1637 -0
  149. data/spec/public/shared/finder_shared_spec.rb +1647 -0
  150. data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
  151. data/spec/semipublic/adapters/in_memory_adapter_spec.rb +13 -0
  152. data/spec/semipublic/associations/many_to_many_spec.rb +94 -0
  153. data/spec/semipublic/associations/many_to_one_spec.rb +63 -0
  154. data/spec/semipublic/associations/one_to_many_spec.rb +55 -0
  155. data/spec/semipublic/associations/one_to_one_spec.rb +53 -0
  156. data/spec/semipublic/associations/relationship_spec.rb +200 -0
  157. data/spec/semipublic/associations_spec.rb +177 -0
  158. data/spec/semipublic/collection_spec.rb +110 -0
  159. data/spec/semipublic/model_spec.rb +96 -0
  160. data/spec/semipublic/property/binary_spec.rb +13 -0
  161. data/spec/semipublic/property/boolean_spec.rb +47 -0
  162. data/spec/semipublic/property/class_spec.rb +33 -0
  163. data/spec/semipublic/property/date_spec.rb +43 -0
  164. data/spec/semipublic/property/date_time_spec.rb +46 -0
  165. data/spec/semipublic/property/decimal_spec.rb +83 -0
  166. data/spec/semipublic/property/discriminator_spec.rb +19 -0
  167. data/spec/semipublic/property/float_spec.rb +82 -0
  168. data/spec/semipublic/property/integer_spec.rb +82 -0
  169. data/spec/semipublic/property/lookup_spec.rb +29 -0
  170. data/spec/semipublic/property/serial_spec.rb +13 -0
  171. data/spec/semipublic/property/string_spec.rb +13 -0
  172. data/spec/semipublic/property/text_spec.rb +31 -0
  173. data/spec/semipublic/property/time_spec.rb +50 -0
  174. data/spec/semipublic/property_spec.rb +114 -0
  175. data/spec/semipublic/query/conditions/comparison_spec.rb +1502 -0
  176. data/spec/semipublic/query/conditions/operation_spec.rb +1296 -0
  177. data/spec/semipublic/query/path_spec.rb +471 -0
  178. data/spec/semipublic/query_spec.rb +3665 -0
  179. data/spec/semipublic/resource/state/clean_spec.rb +89 -0
  180. data/spec/semipublic/resource/state/deleted_spec.rb +79 -0
  181. data/spec/semipublic/resource/state/dirty_spec.rb +163 -0
  182. data/spec/semipublic/resource/state/immutable_spec.rb +107 -0
  183. data/spec/semipublic/resource/state/transient_spec.rb +163 -0
  184. data/spec/semipublic/resource/state_spec.rb +230 -0
  185. data/spec/semipublic/resource_spec.rb +23 -0
  186. data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
  187. data/spec/semipublic/shared/resource_shared_spec.rb +198 -0
  188. data/spec/semipublic/shared/resource_state_shared_spec.rb +91 -0
  189. data/spec/semipublic/shared/subject_shared_spec.rb +79 -0
  190. data/spec/spec_helper.rb +34 -0
  191. data/spec/support/core_ext/hash.rb +10 -0
  192. data/spec/support/core_ext/inheritable_attributes.rb +46 -0
  193. data/spec/support/properties/huge_integer.rb +17 -0
  194. data/spec/unit/array_spec.rb +23 -0
  195. data/spec/unit/blank_spec.rb +73 -0
  196. data/spec/unit/data_mapper/ordered_set/append_spec.rb +26 -0
  197. data/spec/unit/data_mapper/ordered_set/clear_spec.rb +24 -0
  198. data/spec/unit/data_mapper/ordered_set/delete_spec.rb +28 -0
  199. data/spec/unit/data_mapper/ordered_set/each_spec.rb +19 -0
  200. data/spec/unit/data_mapper/ordered_set/empty_spec.rb +20 -0
  201. data/spec/unit/data_mapper/ordered_set/entries_spec.rb +22 -0
  202. data/spec/unit/data_mapper/ordered_set/eql_spec.rb +51 -0
  203. data/spec/unit/data_mapper/ordered_set/equal_value_spec.rb +84 -0
  204. data/spec/unit/data_mapper/ordered_set/hash_spec.rb +12 -0
  205. data/spec/unit/data_mapper/ordered_set/include_spec.rb +23 -0
  206. data/spec/unit/data_mapper/ordered_set/index_spec.rb +28 -0
  207. data/spec/unit/data_mapper/ordered_set/initialize_spec.rb +32 -0
  208. data/spec/unit/data_mapper/ordered_set/merge_spec.rb +36 -0
  209. data/spec/unit/data_mapper/ordered_set/shared/append_spec.rb +24 -0
  210. data/spec/unit/data_mapper/ordered_set/shared/clear_spec.rb +9 -0
  211. data/spec/unit/data_mapper/ordered_set/shared/delete_spec.rb +25 -0
  212. data/spec/unit/data_mapper/ordered_set/shared/each_spec.rb +17 -0
  213. data/spec/unit/data_mapper/ordered_set/shared/empty_spec.rb +9 -0
  214. data/spec/unit/data_mapper/ordered_set/shared/entries_spec.rb +9 -0
  215. data/spec/unit/data_mapper/ordered_set/shared/include_spec.rb +9 -0
  216. data/spec/unit/data_mapper/ordered_set/shared/index_spec.rb +13 -0
  217. data/spec/unit/data_mapper/ordered_set/shared/initialize_spec.rb +28 -0
  218. data/spec/unit/data_mapper/ordered_set/shared/merge_spec.rb +28 -0
  219. data/spec/unit/data_mapper/ordered_set/shared/size_spec.rb +13 -0
  220. data/spec/unit/data_mapper/ordered_set/shared/to_ary_spec.rb +11 -0
  221. data/spec/unit/data_mapper/ordered_set/size_spec.rb +27 -0
  222. data/spec/unit/data_mapper/ordered_set/to_ary_spec.rb +23 -0
  223. data/spec/unit/data_mapper/subject_set/append_spec.rb +47 -0
  224. data/spec/unit/data_mapper/subject_set/clear_spec.rb +34 -0
  225. data/spec/unit/data_mapper/subject_set/delete_spec.rb +40 -0
  226. data/spec/unit/data_mapper/subject_set/each_spec.rb +30 -0
  227. data/spec/unit/data_mapper/subject_set/empty_spec.rb +31 -0
  228. data/spec/unit/data_mapper/subject_set/entries_spec.rb +31 -0
  229. data/spec/unit/data_mapper/subject_set/get_spec.rb +34 -0
  230. data/spec/unit/data_mapper/subject_set/include_spec.rb +32 -0
  231. data/spec/unit/data_mapper/subject_set/named_spec.rb +33 -0
  232. data/spec/unit/data_mapper/subject_set/shared/append_spec.rb +18 -0
  233. data/spec/unit/data_mapper/subject_set/shared/clear_spec.rb +9 -0
  234. data/spec/unit/data_mapper/subject_set/shared/delete_spec.rb +9 -0
  235. data/spec/unit/data_mapper/subject_set/shared/each_spec.rb +9 -0
  236. data/spec/unit/data_mapper/subject_set/shared/empty_spec.rb +9 -0
  237. data/spec/unit/data_mapper/subject_set/shared/entries_spec.rb +9 -0
  238. data/spec/unit/data_mapper/subject_set/shared/get_spec.rb +9 -0
  239. data/spec/unit/data_mapper/subject_set/shared/include_spec.rb +9 -0
  240. data/spec/unit/data_mapper/subject_set/shared/named_spec.rb +9 -0
  241. data/spec/unit/data_mapper/subject_set/shared/size_spec.rb +13 -0
  242. data/spec/unit/data_mapper/subject_set/shared/to_ary_spec.rb +9 -0
  243. data/spec/unit/data_mapper/subject_set/shared/values_at_spec.rb +44 -0
  244. data/spec/unit/data_mapper/subject_set/size_spec.rb +42 -0
  245. data/spec/unit/data_mapper/subject_set/to_ary_spec.rb +34 -0
  246. data/spec/unit/data_mapper/subject_set/values_at_spec.rb +57 -0
  247. data/spec/unit/hash_spec.rb +27 -0
  248. data/spec/unit/hook_spec.rb +1216 -0
  249. data/spec/unit/inflections_spec.rb +14 -0
  250. data/spec/unit/lazy_array_spec.rb +1949 -0
  251. data/spec/unit/mash_spec.rb +289 -0
  252. data/spec/unit/module_spec.rb +70 -0
  253. data/spec/unit/object_spec.rb +38 -0
  254. data/spec/unit/try_dup_spec.rb +46 -0
  255. data/tasks/ci.rake +1 -0
  256. data/tasks/spec.rake +18 -0
  257. data/tasks/yard.rake +9 -0
  258. data/tasks/yardstick.rake +19 -0
  259. metadata +323 -0
@@ -0,0 +1,1502 @@
1
+ require_relative '../../../spec_helper'
2
+
3
+ describe DataMapper::Query::Conditions::Comparison do
4
+ before :all do
5
+ module ::Blog
6
+ class Article
7
+ include DataMapper::Resource
8
+
9
+ property :id, Serial
10
+ property :title, String, required: true
11
+ end
12
+ end
13
+ DataMapper.finalize
14
+
15
+ @model = Blog::Article
16
+ end
17
+
18
+ before :all do
19
+ @property = @model.properties[:id]
20
+ end
21
+
22
+ it { expect(DataMapper::Query::Conditions::Comparison).to respond_to(:new) }
23
+
24
+ describe '.new' do
25
+ {
26
+ eql: DataMapper::Query::Conditions::EqualToComparison,
27
+ in: DataMapper::Query::Conditions::InclusionComparison,
28
+ regexp: DataMapper::Query::Conditions::RegexpComparison,
29
+ like: DataMapper::Query::Conditions::LikeComparison,
30
+ gt: DataMapper::Query::Conditions::GreaterThanComparison,
31
+ lt: DataMapper::Query::Conditions::LessThanComparison,
32
+ gte: DataMapper::Query::Conditions::GreaterThanOrEqualToComparison,
33
+ lte: DataMapper::Query::Conditions::LessThanOrEqualToComparison,
34
+ }.each do |slug, klass|
35
+ describe "with a slug #{slug.inspect}" do
36
+ subject { DataMapper::Query::Conditions::Comparison.new(slug, @property, @value) }
37
+
38
+ it { is_expected.to be_kind_of(klass) }
39
+ end
40
+ end
41
+
42
+ describe 'with an invalid slug' do
43
+ subject { DataMapper::Query::Conditions::Comparison.new(:invalid, @property, @value) }
44
+
45
+ it { expect { method(:subject) }.to raise_error(ArgumentError, 'No Comparison class for :invalid has been defined') }
46
+ end
47
+ end
48
+ end
49
+
50
+ describe DataMapper::Query::Conditions::EqualToComparison do
51
+ it_behaves_like 'DataMapper::Query::Conditions::AbstractComparison'
52
+
53
+ before do
54
+ @property = @model.properties[:id]
55
+ @other_property = @model.properties[:title]
56
+ @value = 1
57
+ @other_value = 2
58
+ @slug = :eql
59
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
60
+ @other = ::OtherComparison.new(@property, @value)
61
+ end
62
+
63
+ subject { @comparison }
64
+
65
+ it { is_expected.to respond_to(:foreign_key_mapping) }
66
+
67
+ describe '#foreign_key_mapping' do
68
+ supported_by :all do
69
+ before do
70
+ @parent = @model.create(title: 'Parent')
71
+ @child = @parent.children.create(title: 'Child')
72
+
73
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @parent)
74
+ end
75
+
76
+ it 'returns criteria that matches the record' do
77
+ expect(@model.all(conditions: @comparison.foreign_key_mapping)).to eq [ @child ]
78
+ end
79
+ end
80
+ end
81
+
82
+ it { is_expected.to respond_to(:inspect) }
83
+
84
+ describe '#inspect' do
85
+ subject { @comparison.inspect }
86
+
87
+ it { is_expected.to eq '#<DataMapper::Query::Conditions::EqualToComparison @subject=#<DataMapper::Property::Serial @model=Blog::Article @name=:id> @dumped_value=1 @loaded_value=1>' }
88
+ end
89
+
90
+ it { is_expected.to respond_to(:matches?) }
91
+
92
+ describe '#matches?' do
93
+ supported_by :all do
94
+ describe 'with a Property subject' do
95
+ describe 'with an Integer value' do
96
+ describe 'with a matching Hash' do
97
+ subject { @comparison.matches?(@property.field => 1) }
98
+
99
+ it { is_expected.to be(true) }
100
+ end
101
+
102
+ describe 'with a not matching Hash' do
103
+ subject { @comparison.matches?(@property.field => 2) }
104
+
105
+ it { is_expected.to be(false) }
106
+ end
107
+
108
+ describe 'with a matching Resource' do
109
+ subject { @comparison.matches?(@model.new(@property => 1)) }
110
+
111
+ it { is_expected.to be(true) }
112
+ end
113
+
114
+ describe 'with a not matching Resource' do
115
+ subject { @comparison.matches?(@model.new(@property => 2)) }
116
+
117
+ it { is_expected.to be(false) }
118
+ end
119
+ end
120
+
121
+ describe 'with a nil value' do
122
+ before do
123
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
124
+ end
125
+
126
+ describe 'with a matching Hash' do
127
+ subject { @comparison.matches?(@property.field => nil) }
128
+
129
+ it { is_expected.to be(true) }
130
+ end
131
+
132
+ describe 'with a not matching Hash' do
133
+ subject { @comparison.matches?(@property.field => 1) }
134
+
135
+ it { is_expected.to be(false) }
136
+ end
137
+
138
+ describe 'with a matching Resource' do
139
+ subject { @comparison.matches?(@model.new(@property => nil)) }
140
+
141
+ it { is_expected.to be(true) }
142
+ end
143
+
144
+ describe 'with a not matching Resource' do
145
+ subject { @comparison.matches?(@model.new(@property => 1)) }
146
+
147
+ it { is_expected.to be(false) }
148
+ end
149
+ end
150
+ end
151
+
152
+ describe 'with a Relationship subject' do
153
+ describe 'with a nil value' do
154
+ before do
155
+ @parent = @model.create(title: 'Parent')
156
+ @child = @parent.children.create(title: 'Child')
157
+
158
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, nil)
159
+ end
160
+
161
+ describe 'with a matching Hash' do
162
+ subject { @comparison.matches?({ @relationship.field => nil }) }
163
+
164
+ it { is_expected.to be(true) }
165
+ end
166
+
167
+ describe 'with a not matching Hash' do
168
+ subject { @comparison.matches?({ @relationship.field => {} }) }
169
+
170
+ it { pending { is_expected.to be(false) } }
171
+ end
172
+
173
+ describe 'with a matching Resource' do
174
+ subject { @comparison.matches?(@parent) }
175
+
176
+ it { is_expected.to be(true) }
177
+ end
178
+
179
+ describe 'with a not matching Resource' do
180
+ subject { @comparison.matches?(@child) }
181
+
182
+ it { is_expected.to be(false) }
183
+ end
184
+ end
185
+
186
+ describe 'with a Hash value' do
187
+ before do
188
+ @parent = @model.create(title: 'Parent')
189
+ @child = @parent.children.create(title: 'Child')
190
+
191
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @parent.attributes.except(:id))
192
+ end
193
+
194
+ describe 'with a matching Hash' do
195
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
196
+
197
+ it { is_expected.to be(true) }
198
+ end
199
+
200
+ describe 'with a not matching Hash' do
201
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
202
+
203
+ it { pending { is_expected.to be(false) } }
204
+ end
205
+
206
+ describe 'with a matching Resource' do
207
+ subject { @comparison.matches?(@child) }
208
+
209
+ it { pending { is_expected.to be(true) } }
210
+ end
211
+
212
+ describe 'with a not matching Resource' do
213
+ subject { @comparison.matches?(@parent) }
214
+
215
+ it { pending { is_expected.to be(false) } }
216
+ end
217
+ end
218
+
219
+ describe 'with new Resource value' do
220
+ before do
221
+ @parent = @model.create(title: 'Parent')
222
+ @child = @parent.children.create(title: 'Child')
223
+
224
+ new_resource = @model.new(@parent.attributes.except(:id))
225
+
226
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, new_resource)
227
+ end
228
+
229
+ describe 'with a matching Hash' do
230
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
231
+
232
+ it { is_expected.to be(true) }
233
+ end
234
+
235
+ describe 'with a not matching Hash' do
236
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
237
+
238
+ it { pending { is_expected.to be(false) } }
239
+ end
240
+
241
+ describe 'with a matching Resource' do
242
+ subject { @comparison.matches?(@child) }
243
+
244
+ it { pending { is_expected.to be(true) } }
245
+ end
246
+
247
+ describe 'with a not matching Resource' do
248
+ subject { @comparison.matches?(@parent) }
249
+
250
+ it { pending { is_expected.to be(false) } }
251
+ end
252
+ end
253
+
254
+ describe 'with a saved Resource value' do
255
+ before do
256
+ @parent = @model.create(title: 'Parent')
257
+ @child = @parent.children.create(title: 'Child')
258
+
259
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @parent)
260
+ end
261
+
262
+ describe 'with a matching Hash' do
263
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
264
+
265
+ it { pending { is_expected.to be(true) } }
266
+ end
267
+
268
+ describe 'with a not matching Hash' do
269
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
270
+
271
+ it { is_expected.to be(false) }
272
+ end
273
+
274
+ describe 'with a matching Resource' do
275
+ subject { @comparison.matches?(@child) }
276
+
277
+ it { is_expected.to be(true) }
278
+ end
279
+
280
+ describe 'with a not matching Resource' do
281
+ subject { @comparison.matches?(@parent) }
282
+
283
+ it { is_expected.to be(false) }
284
+ end
285
+ end
286
+ end
287
+ end
288
+ end
289
+
290
+ it { is_expected.to respond_to(:relationship?) }
291
+
292
+ describe '#relationship?' do
293
+ subject { @comparison.relationship? }
294
+
295
+ it { is_expected.to be(false) }
296
+ end
297
+
298
+ it { is_expected.to respond_to(:to_s) }
299
+
300
+ describe '#to_s' do
301
+ subject { @comparison.to_s }
302
+
303
+ it { is_expected.to eq 'id = 1' }
304
+ end
305
+
306
+ it { is_expected.to respond_to(:value) }
307
+
308
+ describe '#value' do
309
+ supported_by :all do
310
+ subject { @comparison.value }
311
+
312
+ describe 'with a Property subject' do
313
+ describe 'with an Integer value' do
314
+ it { is_expected.to eq @value }
315
+ end
316
+
317
+ describe 'with a nil value' do
318
+ before do
319
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
320
+ end
321
+
322
+ it { is_expected.to be_nil }
323
+ end
324
+ end
325
+
326
+ describe 'with a Relationship subject' do
327
+ before :all do
328
+ @parent = @model.create(title: 'Parent')
329
+ @child = @parent.children.create(title: 'Child')
330
+ end
331
+
332
+ describe 'with an Hash value' do
333
+ before do
334
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, id: 1)
335
+ end
336
+
337
+ it { is_expected.to eq @model.new(id: 1) }
338
+ end
339
+
340
+ describe 'with a Resource value' do
341
+ before do
342
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @parent)
343
+ end
344
+
345
+ it { is_expected.to eq @parent }
346
+ end
347
+ end
348
+ end
349
+ end
350
+ end
351
+
352
+ describe DataMapper::Query::Conditions::InclusionComparison do
353
+ it_behaves_like 'DataMapper::Query::Conditions::AbstractComparison'
354
+
355
+ before do
356
+ @property = @model.properties[:id]
357
+ @other_property = @model.properties[:title]
358
+ @value = [ 1 ]
359
+ @other_value = [ 2 ]
360
+ @slug = :in
361
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
362
+ @other = ::OtherComparison.new(@property, @value)
363
+ end
364
+
365
+ subject { @comparison }
366
+
367
+ it { is_expected.to respond_to(:foreign_key_mapping) }
368
+
369
+ describe '#foreign_key_mapping' do
370
+ supported_by :all do
371
+ before do
372
+ @parent = @model.create(title: 'Parent')
373
+ @child = @parent.children.create(title: 'Child')
374
+
375
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @model.all)
376
+ end
377
+
378
+ it 'returns criteria that matches the record' do
379
+ expect(@model.all(conditions: @comparison.foreign_key_mapping)).to eq [ @child ]
380
+ end
381
+ end
382
+ end
383
+
384
+ it { is_expected.to respond_to(:inspect) }
385
+
386
+ describe '#inspect' do
387
+ subject { @comparison.inspect }
388
+
389
+ it { is_expected.to eq '#<DataMapper::Query::Conditions::InclusionComparison @subject=#<DataMapper::Property::Serial @model=Blog::Article @name=:id> @dumped_value=[1] @loaded_value=[1]>' }
390
+ end
391
+
392
+ it { is_expected.to respond_to(:matches?) }
393
+
394
+ describe '#matches?' do
395
+ supported_by :all do
396
+ describe 'with a Property subject' do
397
+ describe 'with an Array value' do
398
+ describe 'with a matching Hash' do
399
+ subject { @comparison.matches?(@property.field => 1) }
400
+
401
+ it { is_expected.to be(true) }
402
+ end
403
+
404
+ describe 'with a not matching Hash' do
405
+ subject { @comparison.matches?(@property.field => 2) }
406
+
407
+ it { is_expected.to be(false) }
408
+ end
409
+
410
+ describe 'with a matching Resource' do
411
+ subject { @comparison.matches?(@model.new(@property => 1)) }
412
+
413
+ it { is_expected.to be(true) }
414
+ end
415
+
416
+ describe 'with a not matching Resource' do
417
+ subject { @comparison.matches?(@model.new(@property => 2)) }
418
+
419
+ it { is_expected.to be(false) }
420
+ end
421
+ end
422
+
423
+ describe 'with an Array value that needs typecasting' do
424
+ before do
425
+ @value = [ '1' ]
426
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
427
+ end
428
+
429
+ describe 'with a matching Hash' do
430
+ subject { @comparison.matches?(@property.field => 1) }
431
+
432
+ it { is_expected.to be(true) }
433
+ end
434
+
435
+ describe 'with a not matching Hash' do
436
+ subject { @comparison.matches?(@property.field => 2) }
437
+
438
+ it { is_expected.to be(false) }
439
+ end
440
+
441
+ describe 'with a matching Resource' do
442
+ subject { @comparison.matches?(@model.new(@property => 1)) }
443
+
444
+ it { is_expected.to be(true) }
445
+ end
446
+
447
+ describe 'with a not matching Resource' do
448
+ subject { @comparison.matches?(@model.new(@property => 2)) }
449
+
450
+ it { is_expected.to be(false) }
451
+ end
452
+ end
453
+
454
+ describe 'with a Range value' do
455
+ before do
456
+ @value = 1..2
457
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
458
+ end
459
+
460
+ describe 'with a matching Hash' do
461
+ subject { @comparison.matches?(@property.field => 1) }
462
+
463
+ it { is_expected.to be(true) }
464
+ end
465
+
466
+ describe 'with a not matching Hash' do
467
+ subject { @comparison.matches?(@property.field => 0) }
468
+
469
+ it { is_expected.to be(false) }
470
+ end
471
+
472
+ describe 'with a matching Resource' do
473
+ subject { @comparison.matches?(@model.new(@property => 1)) }
474
+
475
+ it { is_expected.to be(true) }
476
+ end
477
+
478
+ describe 'with a not matching Resource' do
479
+ subject { @comparison.matches?(@model.new(@property => 0)) }
480
+
481
+ it { is_expected.to be(false) }
482
+ end
483
+ end
484
+
485
+ describe 'with a Range value that needs typecasting' do
486
+ before do
487
+ @value = '1'...'2'
488
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
489
+ end
490
+
491
+ describe 'with a matching Hash' do
492
+ subject { @comparison.matches?(@property.field => 1) }
493
+
494
+ it { is_expected.to be(true) }
495
+ end
496
+
497
+ describe 'with a not matching Hash' do
498
+ subject { @comparison.matches?(@property.field => 2) }
499
+
500
+ it { is_expected.to be(false) }
501
+ end
502
+
503
+ describe 'with a matching Resource' do
504
+ subject { @comparison.matches?(@model.new(@property => 1)) }
505
+
506
+ it { is_expected.to be(true) }
507
+ end
508
+
509
+ describe 'with a not matching Resource' do
510
+ subject { @comparison.matches?(@model.new(@property => 2)) }
511
+
512
+ it { is_expected.to be(false) }
513
+ end
514
+ end
515
+ end
516
+
517
+ describe 'with a Relationship subject' do
518
+ describe 'with a Hash value' do
519
+ before do
520
+ @parent = @model.create(title: 'Parent')
521
+ @child = @parent.children.create(title: 'Child')
522
+
523
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @parent.attributes.except(:id))
524
+ end
525
+
526
+ describe 'with a matching Hash' do
527
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
528
+
529
+ it { pending { is_expected.to be(true) } }
530
+ end
531
+
532
+ describe 'with a not matching Hash' do
533
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
534
+
535
+ it { is_expected.to be(false) }
536
+ end
537
+
538
+ describe 'with a matching Resource' do
539
+ subject { @comparison.matches?(@child) }
540
+
541
+ it { is_expected.to be(true) }
542
+ end
543
+
544
+ describe 'with a not matching Resource' do
545
+ subject { @comparison.matches?(@parent) }
546
+
547
+ it { is_expected.to be(false) }
548
+ end
549
+ end
550
+
551
+ describe 'with a new Resource value' do
552
+ before do
553
+ @parent = @model.create(title: 'Parent')
554
+ @child = @parent.children.create(title: 'Child')
555
+
556
+ new_resource = @model.new(@parent.attributes.except(:id))
557
+
558
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, new_resource)
559
+ end
560
+
561
+ describe 'with a matching Hash' do
562
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
563
+
564
+ it { is_expected.to be(true) }
565
+ end
566
+
567
+ describe 'with a not matching Hash' do
568
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
569
+
570
+ it { pending { is_expected.to be(false) } }
571
+ end
572
+
573
+ describe 'with a matching Resource' do
574
+ subject { @comparison.matches?(@child) }
575
+
576
+ it { pending { is_expected.to be(true) } }
577
+ end
578
+
579
+ describe 'with a not matching Resource' do
580
+ subject { @comparison.matches?(@parent) }
581
+
582
+ it { pending { is_expected.to be(false) } }
583
+ end
584
+ end
585
+
586
+ describe 'with a saved Resource value' do
587
+ before do
588
+ @parent = @model.create(title: 'Parent')
589
+ @child = @parent.children.create(title: 'Child')
590
+
591
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @parent)
592
+ end
593
+
594
+ describe 'with a matching Hash' do
595
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
596
+
597
+ it { pending { is_expected.to be(true) } }
598
+ end
599
+
600
+ describe 'with a not matching Hash' do
601
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
602
+
603
+ it { is_expected.to be(false) }
604
+ end
605
+
606
+ describe 'with a matching Resource' do
607
+ subject { @comparison.matches?(@child) }
608
+
609
+ it { is_expected.to be(true) }
610
+ end
611
+
612
+ describe 'with a not matching Resource' do
613
+ subject { @comparison.matches?(@parent) }
614
+
615
+ it { is_expected.to be(false) }
616
+ end
617
+ end
618
+
619
+ describe 'with a Collection value' do
620
+ before do
621
+ @parent = @model.create(title: 'Parent')
622
+ @child = @parent.children.create(title: 'Child')
623
+
624
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @model.all(title: 'Parent'))
625
+ end
626
+
627
+ describe 'with a matching Hash' do
628
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
629
+
630
+ it { pending { is_expected.to be(true) } }
631
+ end
632
+
633
+ describe 'with a not matching Hash' do
634
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
635
+
636
+ it { is_expected.to be(false) }
637
+ end
638
+
639
+ describe 'with a matching Resource' do
640
+ subject { @comparison.matches?(@child) }
641
+
642
+ it { is_expected.to be(true) }
643
+ end
644
+
645
+ describe 'with a not matching Resource' do
646
+ subject { @comparison.matches?(@parent) }
647
+
648
+ it { is_expected.to be(false) }
649
+ end
650
+ end
651
+
652
+ describe 'with an Enumerable value containing a Hash' do
653
+ before do
654
+ @parent = @model.create(title: 'Parent')
655
+ @child = @parent.children.create(title: 'Child')
656
+
657
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, [ @parent.attributes.except(:id), { title: 'Other' } ])
658
+ end
659
+
660
+ describe 'with a matching Hash' do
661
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
662
+
663
+ it { pending { is_expected.to be(true) } }
664
+ end
665
+
666
+ describe 'with a not matching Hash' do
667
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
668
+
669
+ it { is_expected.to be(false) }
670
+ end
671
+
672
+ describe 'with a matching Resource' do
673
+ subject { @comparison.matches?(@child) }
674
+
675
+ it { is_expected.to be(true) }
676
+ end
677
+
678
+ describe 'with a not matching Resource' do
679
+ subject { @comparison.matches?(@parent) }
680
+
681
+ it { is_expected.to be(false) }
682
+ end
683
+ end
684
+
685
+ describe 'with an Enumerable value containing a new Resource' do
686
+ before do
687
+ @parent = @model.create(title: 'Parent')
688
+ @child = @parent.children.create(title: 'Child')
689
+
690
+ new_resource = @model.new(@parent.attributes.except(:id))
691
+
692
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, [ new_resource ])
693
+ end
694
+
695
+ describe 'with a matching Hash' do
696
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
697
+
698
+ it { is_expected.to be(true) }
699
+ end
700
+
701
+ describe 'with a not matching Hash' do
702
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
703
+
704
+ it { pending { is_expected.to be(false) } }
705
+ end
706
+
707
+ describe 'with a matching Resource' do
708
+ subject { @comparison.matches?(@child) }
709
+
710
+ it { pending { is_expected.to be(true) } }
711
+ end
712
+
713
+ describe 'with a not matching Resource' do
714
+ subject { @comparison.matches?(@parent) }
715
+
716
+ it { pending { is_expected.to be(false) } }
717
+ end
718
+ end
719
+
720
+ describe 'with an Enumerable value containing a saved Resource' do
721
+ before do
722
+ @parent = @model.create(title: 'Parent')
723
+ @child = @parent.children.create(title: 'Child')
724
+
725
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, [ @parent ])
726
+ end
727
+
728
+ describe 'with a matching Hash' do
729
+ subject { @comparison.matches?({ @relationship.field => @parent.attributes(:field) }) }
730
+
731
+ it { pending { is_expected.to be(true) } }
732
+ end
733
+
734
+ describe 'with a not matching Hash' do
735
+ subject { @comparison.matches?({ @relationship.field => @child.attributes(:field) }) }
736
+
737
+ it { is_expected.to be(false) }
738
+ end
739
+
740
+ describe 'with a matching Resource' do
741
+ subject { @comparison.matches?(@child) }
742
+
743
+ it { is_expected.to be(true) }
744
+ end
745
+
746
+ describe 'with a not matching Resource' do
747
+ subject { @comparison.matches?(@parent) }
748
+
749
+ it { is_expected.to be(false) }
750
+ end
751
+ end
752
+ end
753
+ end
754
+ end
755
+
756
+ it { is_expected.to respond_to(:relationship?) }
757
+
758
+ describe '#relationship?' do
759
+ subject { @comparison.relationship? }
760
+
761
+ it { is_expected.to be(false) }
762
+ end
763
+
764
+ it { is_expected.to respond_to(:to_s) }
765
+
766
+ describe '#to_s' do
767
+ subject { @comparison.to_s }
768
+
769
+ it { is_expected.to eq 'id IN [1]' }
770
+ end
771
+
772
+ it { is_expected.to respond_to(:valid?) }
773
+
774
+ describe '#valid?' do
775
+ subject { @comparison.valid? }
776
+
777
+ describe 'with a Property subject' do
778
+ describe 'with a valid Array value' do
779
+ it { is_expected.to be(true) }
780
+ end
781
+
782
+ describe 'with a valid Array value that needs typecasting' do
783
+ before do
784
+ @value = [ '1' ]
785
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
786
+ end
787
+
788
+ it { is_expected.to be(true) }
789
+ end
790
+
791
+ describe 'with an invalid Array value' do
792
+ before do
793
+ @value = [ 'invalid' ]
794
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
795
+ end
796
+
797
+ it { is_expected.to be(false) }
798
+ end
799
+
800
+ describe 'with an empty Array value' do
801
+ before do
802
+ @value = []
803
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
804
+ end
805
+
806
+ it { is_expected.to be(false) }
807
+ end
808
+
809
+ describe 'with a valid Range value' do
810
+ before do
811
+ @value = 1..1
812
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
813
+ end
814
+
815
+ it { is_expected.to be(true) }
816
+ end
817
+
818
+ describe 'with a valid Range value that needs typecasting' do
819
+ before do
820
+ @value = '1'...'2'
821
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
822
+ end
823
+
824
+ it { is_expected.to be(true) }
825
+ end
826
+
827
+ describe 'with an invalid Range value' do
828
+ before do
829
+ @value = 'a'..'z'
830
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
831
+ end
832
+
833
+ it { is_expected.to be(false) }
834
+ end
835
+
836
+ describe 'with an empty Range value' do
837
+ before do
838
+ @value = 1..0
839
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
840
+ end
841
+
842
+ it { is_expected.to be(false) }
843
+ end
844
+ end
845
+
846
+ describe 'with a Relationship subject' do
847
+ supported_by :all do
848
+ describe 'with a valid Array value' do
849
+ before do
850
+ @value = [ @model.new(id: 1) ]
851
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @value)
852
+ end
853
+
854
+ it { is_expected.to be(true) }
855
+ end
856
+
857
+ describe 'with an invalid Array value' do
858
+ before do
859
+ @value = [ @model.new ]
860
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @value)
861
+ end
862
+
863
+ it { is_expected.to be(false) }
864
+ end
865
+
866
+ describe 'with an empty Array value' do
867
+ before do
868
+ @value = []
869
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @value)
870
+ end
871
+
872
+ it { is_expected.to be(false) }
873
+ end
874
+
875
+ describe 'with a valid Collection' do
876
+ before do
877
+ @value = @model.all
878
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @value)
879
+ end
880
+
881
+ it { is_expected.to be(true) }
882
+ end
883
+ end
884
+ end
885
+ end
886
+
887
+ it { is_expected.to respond_to(:value) }
888
+
889
+ describe '#value' do
890
+ supported_by :all do
891
+ subject { @comparison.value }
892
+
893
+ describe 'with a Property subject' do
894
+ describe 'with an Array value' do
895
+ before do
896
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, [ 1, 1 ])
897
+ end
898
+
899
+ it { is_expected.to be_kind_of(Array) }
900
+
901
+ it { is_expected.to eq @value }
902
+ end
903
+
904
+ describe 'with a Range value' do
905
+ before do
906
+ @value = 1..1
907
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
908
+ end
909
+
910
+ it { is_expected.to be_kind_of(Range) }
911
+
912
+ it { is_expected.to eq @value }
913
+ end
914
+ end
915
+
916
+ describe 'with a Relationship subject' do
917
+ before :all do
918
+ @parent = @model.create(title: 'Parent')
919
+ @child = @parent.children.create(title: 'Child')
920
+ end
921
+
922
+ describe 'with an Hash value' do
923
+ before do
924
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, id: @parent.id)
925
+ end
926
+
927
+ it { is_expected.to be_kind_of(DataMapper::Collection) }
928
+
929
+ it { is_expected.to eq [ @parent ] }
930
+ end
931
+
932
+ describe 'with an Array value' do
933
+ before do
934
+ @value = [ @parent ]
935
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @value)
936
+ end
937
+
938
+ it { is_expected.to be_kind_of(DataMapper::Collection) }
939
+
940
+ it { is_expected.to eq @value }
941
+ end
942
+
943
+ describe 'with a Resource value' do
944
+ before do
945
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @parent)
946
+ end
947
+
948
+ it { is_expected.to be_kind_of(DataMapper::Collection) }
949
+
950
+ it { is_expected.to eq [ @parent ] }
951
+ end
952
+
953
+ describe 'with a Collection value' do
954
+ before do
955
+ @value = @model.all
956
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @relationship, @value)
957
+ end
958
+
959
+ it 'is not a kicker' do
960
+ expect(@value).not_to be_loaded
961
+ end
962
+
963
+ it { is_expected.to be_kind_of(DataMapper::Collection) }
964
+
965
+ it { is_expected.to eq @value }
966
+ end
967
+ end
968
+ end
969
+ end
970
+ end
971
+
972
+ describe DataMapper::Query::Conditions::RegexpComparison do
973
+ it_behaves_like 'DataMapper::Query::Conditions::AbstractComparison'
974
+
975
+ before do
976
+ @property = @model.properties[:title]
977
+ @other_property = @model.properties[:id]
978
+ @value = /Title/
979
+ @other_value = /Other Title/
980
+ @slug = :regexp
981
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
982
+ @other = ::OtherComparison.new(@property, @value)
983
+ end
984
+
985
+ subject { @comparison }
986
+
987
+ it { is_expected.to respond_to(:inspect) }
988
+
989
+ describe '#inspect' do
990
+ subject { @comparison.inspect }
991
+
992
+ it { is_expected.to eq '#<DataMapper::Query::Conditions::RegexpComparison @subject=#<DataMapper::Property::String @model=Blog::Article @name=:title> @dumped_value=/Title/ @loaded_value=/Title/>' }
993
+ end
994
+
995
+ it { is_expected.to respond_to(:matches?) }
996
+
997
+ describe '#matches?' do
998
+ supported_by :all do
999
+ describe 'with a matching Hash' do
1000
+ subject { @comparison.matches?(@property.field => 'Title') }
1001
+
1002
+ it { is_expected.to be(true) }
1003
+ end
1004
+
1005
+ describe 'with a not matching Hash' do
1006
+ subject { @comparison.matches?(@property.field => 'Other') }
1007
+
1008
+ it { is_expected.to be(false) }
1009
+ end
1010
+
1011
+ describe 'with a matching Resource' do
1012
+ subject { @comparison.matches?(@model.new(@property => 'Title')) }
1013
+
1014
+ it { is_expected.to be(true) }
1015
+ end
1016
+
1017
+ describe 'with a not matching Resource' do
1018
+ subject { @comparison.matches?(@model.new(@property => 'Other')) }
1019
+
1020
+ it { is_expected.to be(false) }
1021
+ end
1022
+
1023
+ describe 'with a not matching nil field' do
1024
+ subject { @comparison.matches?(@property.field => nil) }
1025
+
1026
+ it { is_expected.to be(false) }
1027
+ end
1028
+
1029
+ describe 'with a not matching nil attribute' do
1030
+ subject { @comparison.matches?(@model.new(@property => nil)) }
1031
+
1032
+ it { is_expected.to be(false) }
1033
+ end
1034
+ end
1035
+ end
1036
+
1037
+ it { is_expected.to respond_to(:relationship?) }
1038
+
1039
+ describe '#relationship?' do
1040
+ subject { @comparison.relationship? }
1041
+
1042
+ it { is_expected.to be(false) }
1043
+ end
1044
+
1045
+ it { is_expected.to respond_to(:to_s) }
1046
+
1047
+ describe '#to_s' do
1048
+ subject { @comparison.to_s }
1049
+
1050
+ it { is_expected.to eq 'title =~ /Title/' }
1051
+ end
1052
+ end
1053
+
1054
+ describe DataMapper::Query::Conditions::LikeComparison do
1055
+ it_behaves_like 'DataMapper::Query::Conditions::AbstractComparison'
1056
+
1057
+ before do
1058
+ @property = @model.properties[:title]
1059
+ @other_property = @model.properties[:id]
1060
+ @value = '_it%'
1061
+ @other_value = 'Other Title'
1062
+ @slug = :like
1063
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
1064
+ @other = ::OtherComparison.new(@property, @value)
1065
+ end
1066
+
1067
+ subject { @comparison }
1068
+
1069
+ it { is_expected.to respond_to(:inspect) }
1070
+
1071
+ describe '#inspect' do
1072
+ subject { @comparison.inspect }
1073
+
1074
+ it { is_expected.to eq '#<DataMapper::Query::Conditions::LikeComparison @subject=#<DataMapper::Property::String @model=Blog::Article @name=:title> @dumped_value="_it%" @loaded_value="_it%">' }
1075
+ end
1076
+
1077
+ it { is_expected.to respond_to(:matches?) }
1078
+
1079
+ describe '#matches?' do
1080
+ supported_by :all do
1081
+ describe 'with a matching Hash' do
1082
+ subject { @comparison.matches?(@property.field => 'Title') }
1083
+
1084
+ it { is_expected.to be(true) }
1085
+ end
1086
+
1087
+ describe 'with a not matching Hash' do
1088
+ subject { @comparison.matches?(@property.field => 'Other Title') }
1089
+
1090
+ it { is_expected.to be(false) }
1091
+ end
1092
+
1093
+ describe 'with a matching Resource' do
1094
+ subject { @comparison.matches?(@model.new(@property => 'Title')) }
1095
+
1096
+ it { is_expected.to be(true) }
1097
+ end
1098
+
1099
+ describe 'with a not matching Resource' do
1100
+ subject { @comparison.matches?(@model.new(@property => 'Other Title')) }
1101
+
1102
+ it { is_expected.to be(false) }
1103
+ end
1104
+
1105
+ describe 'with a not matching nil field' do
1106
+ subject { @comparison.matches?(@property.field => nil) }
1107
+
1108
+ it { is_expected.to be(false) }
1109
+ end
1110
+
1111
+ describe 'with a not matching nil attribute' do
1112
+ subject { @comparison.matches?(@model.new(@property => nil)) }
1113
+
1114
+ it { is_expected.to be(false) }
1115
+ end
1116
+ end
1117
+ end
1118
+
1119
+ it { is_expected.to respond_to(:relationship?) }
1120
+
1121
+ describe '#relationship?' do
1122
+ subject { @comparison.relationship? }
1123
+
1124
+ it { is_expected.to be(false) }
1125
+ end
1126
+
1127
+ it { is_expected.to respond_to(:to_s) }
1128
+
1129
+ describe '#to_s' do
1130
+ subject { @comparison.to_s }
1131
+
1132
+ it { is_expected.to eq 'title LIKE "_it%"' }
1133
+ end
1134
+ end
1135
+
1136
+ describe DataMapper::Query::Conditions::GreaterThanComparison do
1137
+ it_behaves_like 'DataMapper::Query::Conditions::AbstractComparison'
1138
+
1139
+ before do
1140
+ @property = @model.properties[:id]
1141
+ @other_property = @model.properties[:title]
1142
+ @value = 1
1143
+ @other_value = 2
1144
+ @slug = :gt
1145
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
1146
+ @other = ::OtherComparison.new(@property, @value)
1147
+ end
1148
+
1149
+ subject { @comparison }
1150
+
1151
+ it { is_expected.to respond_to(:inspect) }
1152
+
1153
+ describe '#inspect' do
1154
+ subject { @comparison.inspect }
1155
+
1156
+ it { is_expected.to eq '#<DataMapper::Query::Conditions::GreaterThanComparison @subject=#<DataMapper::Property::Serial @model=Blog::Article @name=:id> @dumped_value=1 @loaded_value=1>' }
1157
+ end
1158
+
1159
+ it { is_expected.to respond_to(:matches?) }
1160
+
1161
+ describe '#matches?' do
1162
+ supported_by :all do
1163
+ describe 'with a matching Hash' do
1164
+ subject { @comparison.matches?(@property.field => 2) }
1165
+
1166
+ it { is_expected.to be(true) }
1167
+ end
1168
+
1169
+ describe 'with a not matching Hash' do
1170
+ subject { @comparison.matches?(@property.field => 1) }
1171
+
1172
+ it { is_expected.to be(false) }
1173
+ end
1174
+
1175
+ describe 'with a matching Resource' do
1176
+ subject { @comparison.matches?(@model.new(@property => 2)) }
1177
+
1178
+ it { is_expected.to be(true) }
1179
+ end
1180
+
1181
+ describe 'with a not matching Resource' do
1182
+ subject { @comparison.matches?(@model.new(@property => 1)) }
1183
+
1184
+ it { is_expected.to be(false) }
1185
+ end
1186
+
1187
+ describe 'with a not matching nil field' do
1188
+ subject { @comparison.matches?(@property.field => nil) }
1189
+
1190
+ it { is_expected.to be(false) }
1191
+ end
1192
+
1193
+ describe 'with a not matching nil attribute' do
1194
+ subject { @comparison.matches?(@model.new(@property => nil)) }
1195
+
1196
+ it { is_expected.to be(false) }
1197
+ end
1198
+
1199
+ describe 'with an expected value of nil' do
1200
+ subject { @comparison.matches?(@model.new(@property => 2)) }
1201
+
1202
+ before do
1203
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
1204
+ end
1205
+
1206
+ it { is_expected.to be(false) }
1207
+ end
1208
+ end
1209
+ end
1210
+
1211
+ it { is_expected.to respond_to(:relationship?) }
1212
+
1213
+ describe '#relationship?' do
1214
+ subject { @comparison.relationship? }
1215
+
1216
+ it { is_expected.to be(false) }
1217
+ end
1218
+
1219
+ it { is_expected.to respond_to(:to_s) }
1220
+
1221
+ describe '#to_s' do
1222
+ subject { @comparison.to_s }
1223
+
1224
+ it { is_expected.to eq 'id > 1' }
1225
+ end
1226
+ end
1227
+
1228
+ describe DataMapper::Query::Conditions::LessThanComparison do
1229
+ it_behaves_like 'DataMapper::Query::Conditions::AbstractComparison'
1230
+
1231
+ before do
1232
+ @property = @model.properties[:id]
1233
+ @other_property = @model.properties[:title]
1234
+ @value = 1
1235
+ @other_value = 2
1236
+ @slug = :lt
1237
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
1238
+ @other = ::OtherComparison.new(@property, @value)
1239
+ end
1240
+
1241
+ subject { @comparison }
1242
+
1243
+ it { is_expected.to respond_to(:inspect) }
1244
+
1245
+ describe '#inspect' do
1246
+ subject { @comparison.inspect }
1247
+
1248
+ it { is_expected.to eq '#<DataMapper::Query::Conditions::LessThanComparison @subject=#<DataMapper::Property::Serial @model=Blog::Article @name=:id> @dumped_value=1 @loaded_value=1>' }
1249
+ end
1250
+
1251
+ it { is_expected.to respond_to(:matches?) }
1252
+
1253
+ describe '#matches?' do
1254
+ supported_by :all do
1255
+ describe 'with a matching Hash' do
1256
+ subject { @comparison.matches?(@property.field => 0) }
1257
+
1258
+ it { is_expected.to be(true) }
1259
+ end
1260
+
1261
+ describe 'with a not matching Hash' do
1262
+ subject { @comparison.matches?(@property.field => 1) }
1263
+
1264
+ it { is_expected.to be(false) }
1265
+ end
1266
+
1267
+ describe 'with a matching Resource' do
1268
+ subject { @comparison.matches?(@model.new(@property => 0)) }
1269
+
1270
+ it { is_expected.to be(true) }
1271
+ end
1272
+
1273
+ describe 'with a not matching Resource' do
1274
+ subject { @comparison.matches?(@model.new(@property => 1)) }
1275
+
1276
+ it { is_expected.to be(false) }
1277
+ end
1278
+
1279
+ describe 'with a not matching nil field' do
1280
+ subject { @comparison.matches?(@property.field => nil) }
1281
+
1282
+ it { is_expected.to be(false) }
1283
+ end
1284
+
1285
+ describe 'with a not matching nil attribute' do
1286
+ subject { @comparison.matches?(@model.new(@property => nil)) }
1287
+
1288
+ it { is_expected.to be(false) }
1289
+ end
1290
+
1291
+ describe 'with an expected value of nil' do
1292
+ subject { @comparison.matches?(@model.new(@property => 0)) }
1293
+
1294
+ before do
1295
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
1296
+ end
1297
+
1298
+ it { is_expected.to be(false) }
1299
+ end
1300
+ end
1301
+ end
1302
+
1303
+ it { is_expected.to respond_to(:relationship?) }
1304
+
1305
+ describe '#relationship?' do
1306
+ subject { @comparison.relationship? }
1307
+
1308
+ it { is_expected.to be(false) }
1309
+ end
1310
+
1311
+ it { is_expected.to respond_to(:to_s) }
1312
+
1313
+ describe '#to_s' do
1314
+ subject { @comparison.to_s }
1315
+
1316
+ it { is_expected.to eq 'id < 1' }
1317
+ end
1318
+ end
1319
+
1320
+ describe DataMapper::Query::Conditions::GreaterThanOrEqualToComparison do
1321
+ it_behaves_like 'DataMapper::Query::Conditions::AbstractComparison'
1322
+
1323
+ before do
1324
+ @property = @model.properties[:id]
1325
+ @other_property = @model.properties[:title]
1326
+ @value = 1
1327
+ @other_value = 2
1328
+ @slug = :gte
1329
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
1330
+ @other = ::OtherComparison.new(@property, @value)
1331
+ end
1332
+
1333
+ subject { @comparison }
1334
+
1335
+ it { is_expected.to respond_to(:inspect) }
1336
+
1337
+ describe '#inspect' do
1338
+ subject { @comparison.inspect }
1339
+
1340
+ it { is_expected.to eq '#<DataMapper::Query::Conditions::GreaterThanOrEqualToComparison @subject=#<DataMapper::Property::Serial @model=Blog::Article @name=:id> @dumped_value=1 @loaded_value=1>' }
1341
+ end
1342
+
1343
+ it { is_expected.to respond_to(:matches?) }
1344
+
1345
+ describe '#matches?' do
1346
+ supported_by :all do
1347
+ describe 'with a matching Hash' do
1348
+ subject { @comparison.matches?(@property.field => 1) }
1349
+
1350
+ it { is_expected.to be(true) }
1351
+ end
1352
+
1353
+ describe 'with a not matching Hash' do
1354
+ subject { @comparison.matches?(@property.field => 0) }
1355
+
1356
+ it { is_expected.to be(false) }
1357
+ end
1358
+
1359
+ describe 'with a matching Resource' do
1360
+ subject { @comparison.matches?(@model.new(@property => 1)) }
1361
+
1362
+ it { is_expected.to be(true) }
1363
+ end
1364
+
1365
+ describe 'with a not matching Resource' do
1366
+ subject { @comparison.matches?(@model.new(@property => 0)) }
1367
+
1368
+ it { is_expected.to be(false) }
1369
+ end
1370
+
1371
+ describe 'with a not matching nil field' do
1372
+ subject { @comparison.matches?(@property.field => nil) }
1373
+
1374
+ it { is_expected.to be(false) }
1375
+ end
1376
+
1377
+ describe 'with a not matching nil attribute' do
1378
+ subject { @comparison.matches?(@model.new(@property => nil)) }
1379
+
1380
+ it { is_expected.to be(false) }
1381
+ end
1382
+
1383
+ describe 'with an expected value of nil' do
1384
+ subject { @comparison.matches?(@model.new(@property => 1)) }
1385
+
1386
+ before do
1387
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
1388
+ end
1389
+
1390
+ it { is_expected.to be(false) }
1391
+ end
1392
+ end
1393
+ end
1394
+
1395
+ it { is_expected.to respond_to(:relationship?) }
1396
+
1397
+ describe '#relationship?' do
1398
+ subject { @comparison.relationship? }
1399
+
1400
+ it { is_expected.to be(false) }
1401
+ end
1402
+
1403
+ it { is_expected.to respond_to(:to_s) }
1404
+
1405
+ describe '#to_s' do
1406
+ subject { @comparison.to_s }
1407
+
1408
+ it { is_expected.to eq 'id >= 1' }
1409
+ end
1410
+ end
1411
+
1412
+ describe DataMapper::Query::Conditions::LessThanOrEqualToComparison do
1413
+ it_behaves_like 'DataMapper::Query::Conditions::AbstractComparison'
1414
+
1415
+ before do
1416
+ @property = @model.properties[:id]
1417
+ @other_property = @model.properties[:title]
1418
+ @value = 1
1419
+ @other_value = 2
1420
+ @slug = :lte
1421
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, @value)
1422
+ @other = ::OtherComparison.new(@property, @value)
1423
+ end
1424
+
1425
+ subject { @comparison }
1426
+
1427
+ it { is_expected.to respond_to(:inspect) }
1428
+
1429
+ describe '#inspect' do
1430
+ subject { @comparison.inspect }
1431
+
1432
+ it { is_expected.to eq '#<DataMapper::Query::Conditions::LessThanOrEqualToComparison @subject=#<DataMapper::Property::Serial @model=Blog::Article @name=:id> @dumped_value=1 @loaded_value=1>' }
1433
+ end
1434
+
1435
+ it { is_expected.to respond_to(:matches?) }
1436
+
1437
+ describe '#matches?' do
1438
+ supported_by :all do
1439
+ describe 'with a matching Hash' do
1440
+ subject { @comparison.matches?(@property.field => 1) }
1441
+
1442
+ it { is_expected.to be(true) }
1443
+ end
1444
+
1445
+ describe 'with a not matching Hash' do
1446
+ subject { @comparison.matches?(@property.field => 2) }
1447
+
1448
+ it { is_expected.to be(false) }
1449
+ end
1450
+
1451
+ describe 'with a matching Resource' do
1452
+ subject { @comparison.matches?(@model.new(@property => 1)) }
1453
+
1454
+ it { is_expected.to be(true) }
1455
+ end
1456
+
1457
+ describe 'with a not matching Resource' do
1458
+ subject { @comparison.matches?(@model.new(@property => 2)) }
1459
+
1460
+ it { is_expected.to be(false) }
1461
+ end
1462
+
1463
+ describe 'with a not matching nil field' do
1464
+ subject { @comparison.matches?(@property.field => nil) }
1465
+
1466
+ it { is_expected.to be(false) }
1467
+ end
1468
+
1469
+ describe 'with a not matching nil attribute' do
1470
+ subject { @comparison.matches?(@model.new(@property => nil)) }
1471
+
1472
+ it { is_expected.to be(false) }
1473
+ end
1474
+
1475
+ describe 'with an expected value of nil' do
1476
+ subject { @comparison.matches?(@model.new(@property => 1)) }
1477
+
1478
+ before do
1479
+ @comparison = DataMapper::Query::Conditions::Comparison.new(@slug, @property, nil)
1480
+ end
1481
+
1482
+ it { is_expected.to be(false) }
1483
+ end
1484
+ end
1485
+ end
1486
+
1487
+ it { is_expected.to respond_to(:relationship?) }
1488
+
1489
+ describe '#relationship?' do
1490
+ subject { @comparison.relationship? }
1491
+
1492
+ it { is_expected.to be(false) }
1493
+ end
1494
+
1495
+ it { is_expected.to respond_to(:to_s) }
1496
+
1497
+ describe '#to_s' do
1498
+ subject { @comparison.to_s }
1499
+
1500
+ it { is_expected.to eq 'id <= 1' }
1501
+ end
1502
+ end