alf-core 0.13.1 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. data/CHANGELOG.md +77 -0
  2. data/examples/operators/page.alf +4 -0
  3. data/lib/alf-adapter-fs/alf/adapter/folder.rb +4 -0
  4. data/lib/alf-adapter-fs/alf/adapter/folder/connection.rb +3 -2
  5. data/lib/alf-adapter/alf/adapter/connection.rb +1 -1
  6. data/lib/alf-algebra/alf/algebra/operand.rb +2 -2
  7. data/lib/alf-algebra/alf/algebra/operand/fake.rb +3 -3
  8. data/lib/alf-algebra/alf/algebra/operand/named.rb +1 -1
  9. data/lib/alf-algebra/alf/algebra/operand/proxy.rb +14 -0
  10. data/lib/alf-algebra/alf/algebra/operator.rb +4 -1
  11. data/lib/alf-algebra/alf/algebra/operator/clip.rb +7 -4
  12. data/lib/alf-algebra/alf/algebra/operator/frame.rb +22 -0
  13. data/lib/alf-algebra/alf/algebra/operator/hierarchize.rb +22 -0
  14. data/lib/alf-algebra/alf/algebra/operator/page.rb +22 -0
  15. data/lib/alf-algebra/alf/algebra/operator/project.rb +4 -9
  16. data/lib/alf-algebra/alf/algebra/operator/rank.rb +2 -2
  17. data/lib/alf-algebra/alf/algebra/support.rb +2 -0
  18. data/lib/alf-algebra/alf/algebra/support/signature.rb +2 -1
  19. data/lib/alf-algebra/alf/algebra/support/with_ordering.rb +11 -0
  20. data/lib/alf-core/ext/domain/heading_based.rb +16 -4
  21. data/lib/alf-core/facade.rb +5 -1
  22. data/lib/alf-core/version.rb +2 -2
  23. data/lib/alf-database/alf/database/connection.rb +2 -2
  24. data/lib/alf-database/alf/database/options.rb +0 -3
  25. data/lib/alf-engine/alf/engine.rb +4 -1
  26. data/lib/alf-engine/alf/engine/aggregate.rb +2 -1
  27. data/lib/alf-engine/alf/engine/autonum.rb +2 -1
  28. data/lib/alf-engine/alf/engine/clip.rb +2 -1
  29. data/lib/alf-engine/alf/engine/coerce.rb +2 -1
  30. data/lib/alf-engine/alf/engine/cog.rb +23 -0
  31. data/lib/alf-engine/alf/engine/compact.rb +3 -2
  32. data/lib/alf-engine/alf/engine/compact/set.rb +2 -1
  33. data/lib/alf-engine/alf/engine/compact/uniq.rb +2 -1
  34. data/lib/alf-engine/alf/engine/compiler.rb +60 -33
  35. data/lib/alf-engine/alf/engine/concat.rb +2 -1
  36. data/lib/alf-engine/alf/engine/defaults.rb +2 -1
  37. data/lib/alf-engine/alf/engine/filter.rb +4 -2
  38. data/lib/alf-engine/alf/engine/generator.rb +2 -1
  39. data/lib/alf-engine/alf/engine/group/hash.rb +4 -3
  40. data/lib/alf-engine/alf/engine/hierarchize.rb +87 -0
  41. data/lib/alf-engine/alf/engine/infer_heading.rb +2 -1
  42. data/lib/alf-engine/alf/engine/join.rb +5 -1
  43. data/lib/alf-engine/alf/engine/join/hash.rb +9 -2
  44. data/lib/alf-engine/alf/engine/leaf.rb +6 -1
  45. data/lib/alf-engine/alf/engine/materialize/array.rb +2 -1
  46. data/lib/alf-engine/alf/engine/materialize/hash.rb +2 -1
  47. data/lib/alf-engine/alf/engine/quota/cesure.rb +2 -1
  48. data/lib/alf-engine/alf/engine/rank/cesure.rb +2 -1
  49. data/lib/alf-engine/alf/engine/rename.rb +2 -1
  50. data/lib/alf-engine/alf/engine/semi/hash.rb +7 -1
  51. data/lib/alf-engine/alf/engine/set_attr.rb +4 -3
  52. data/lib/alf-engine/alf/engine/sort.rb +3 -2
  53. data/lib/alf-engine/alf/engine/sort/in_memory.rb +2 -1
  54. data/lib/alf-engine/alf/engine/summarize/cesure.rb +2 -1
  55. data/lib/alf-engine/alf/engine/summarize/hash.rb +3 -2
  56. data/lib/alf-engine/alf/engine/{cesure.rb → support/cesure.rb} +0 -0
  57. data/lib/alf-engine/alf/engine/take.rb +53 -0
  58. data/lib/alf-engine/alf/engine/to_array.rb +6 -5
  59. data/lib/alf-engine/alf/engine/type_safe.rb +2 -1
  60. data/lib/alf-engine/alf/engine/ungroup.rb +2 -1
  61. data/lib/alf-engine/alf/engine/unwrap.rb +2 -1
  62. data/lib/alf-engine/alf/engine/wrap.rb +2 -1
  63. data/lib/alf-io/alf/reader.rb +1 -1
  64. data/lib/alf-io/alf/reader/ruby.rb +23 -0
  65. data/lib/alf-io/alf/renderer/text.rb +22 -9
  66. data/lib/alf-lang/alf/lang/lispy.rb +41 -29
  67. data/lib/alf-lang/alf/lang/oo/algebra_methods.rb +17 -1
  68. data/lib/alf-optimizer/alf/optimizer/restrict.rb +7 -1
  69. data/lib/alf-predicate/alf/predicate.rb +11 -2
  70. data/lib/alf-relation/alf/relation.rb +21 -1
  71. data/lib/alf-relvar/alf/relvar.rb +21 -1
  72. data/lib/alf-relvar/alf/relvar/base.rb +11 -6
  73. data/lib/alf-relvar/alf/relvar/fake.rb +3 -2
  74. data/lib/alf-relvar/alf/relvar/virtual.rb +1 -2
  75. data/lib/alf-support/alf/support/bindable.rb +1 -1
  76. data/lib/alf-support/alf/support/config.rb +91 -18
  77. data/lib/alf-support/alf/support/tuple_scope.rb +8 -4
  78. data/lib/alf-types/alf/types.rb +2 -0
  79. data/lib/alf-types/alf/types/attr_list.rb +3 -1
  80. data/lib/alf-types/alf/types/heading.rb +4 -1
  81. data/lib/alf-types/alf/types/keys.rb +0 -4
  82. data/lib/alf-types/alf/types/ordering.rb +104 -18
  83. data/lib/alf-types/alf/types/selection.rb +18 -0
  84. data/lib/alf-types/alf/types/selector.rb +59 -0
  85. data/lib/alf-types/alf/types/tuple_computation.rb +2 -2
  86. data/lib/alf-types/alf/types/tuple_expression.rb +18 -2
  87. data/lib/alf-viewpoint/alf/viewpoint.rb +23 -8
  88. data/lib/alf-viewpoint/alf/viewpoint/metadata.rb +73 -0
  89. data/spec/integration/relation/test_extend.rb +20 -0
  90. data/spec/integration/relation/test_to_array.rb +1 -1
  91. data/spec/integration/test_examples.rb +18 -2
  92. data/spec/shared/a_cog.rb +36 -0
  93. data/spec/spec_helper.rb +8 -0
  94. data/spec/unit/alf-algebra/operand/test_proxy.rb +54 -0
  95. data/spec/unit/alf-algebra/operator/hierarchize/test_heading.rb +23 -0
  96. data/spec/unit/alf-algebra/operator/project/test_key_preserving.rb +3 -0
  97. data/spec/unit/alf-algebra/operator/project/test_keys.rb +54 -17
  98. data/spec/unit/alf-algebra/operator/rank/test_keys.rb +2 -2
  99. data/spec/unit/alf-algebra/operator/restrict/test_keys.rb +2 -2
  100. data/spec/unit/alf-algebra/operator/test_page.rb +30 -0
  101. data/spec/unit/alf-algebra/support/test_relational.rb +3 -0
  102. data/spec/unit/alf-algebra/support/with_ordering/test_total_ordering.rb +61 -0
  103. data/spec/unit/alf-database/connection/test_reconnect.rb +1 -1
  104. data/spec/unit/alf-database/options/test_new.rb +4 -4
  105. data/spec/unit/alf-database/shared_examples/a_facade_on_database_options.rb +1 -2
  106. data/spec/unit/alf-engine/cog/test_to_cog.rb +14 -0
  107. data/spec/unit/alf-engine/compiler/test_on_frame.rb +70 -0
  108. data/spec/unit/alf-engine/compiler/test_on_hierarchize.rb +33 -0
  109. data/spec/unit/alf-engine/compiler/test_on_page.rb +128 -0
  110. data/spec/unit/alf-engine/compiler/test_unsupported.rb +48 -0
  111. data/spec/unit/alf-engine/hierarchize/test_renamer.rb +18 -0
  112. data/spec/unit/alf-engine/test_filter.rb +43 -11
  113. data/spec/unit/alf-engine/test_hierarchize.rb +71 -0
  114. data/spec/unit/alf-engine/test_set_attr.rb +13 -0
  115. data/spec/unit/alf-engine/test_take.rb +46 -0
  116. data/spec/unit/alf-engine/test_to_array.rb +45 -28
  117. data/spec/unit/alf-io/reader/{input.rb → input.rash} +0 -0
  118. data/spec/unit/alf-io/reader/input.ruby +4 -0
  119. data/spec/unit/alf-io/reader/test_rash.rb +1 -1
  120. data/spec/unit/alf-io/reader/test_ruby.rb +25 -0
  121. data/spec/unit/alf-predicate/predicate/test_evaluate.rb +51 -13
  122. data/spec/unit/alf-relation/relation/class/test_recursive_type.rb +42 -0
  123. data/spec/unit/alf-relation/relation/test_equality.rb +19 -1
  124. data/spec/unit/alf-relation/relation/test_relops.rb +14 -2
  125. data/spec/unit/alf-relation/relation/test_to_hash.rb +31 -0
  126. data/spec/unit/alf-relvar/base/test_connection.rb +8 -2
  127. data/spec/unit/alf-relvar/base/test_delete.rb +2 -2
  128. data/spec/unit/alf-relvar/base/test_heading.rb +2 -2
  129. data/spec/unit/alf-relvar/base/test_insert.rb +3 -3
  130. data/spec/unit/alf-relvar/base/test_keys.rb +2 -2
  131. data/spec/unit/alf-relvar/base/test_lock.rb +2 -2
  132. data/spec/unit/alf-relvar/base/test_name.rb +3 -2
  133. data/spec/unit/alf-relvar/base/test_oo_lang.rb +2 -1
  134. data/spec/unit/alf-relvar/base/test_to_cog.rb +3 -3
  135. data/spec/unit/alf-relvar/base/test_to_lispy.rb +3 -2
  136. data/spec/unit/alf-relvar/base/test_to_relvar.rb +2 -1
  137. data/spec/unit/alf-relvar/base/test_to_s.rb +3 -2
  138. data/spec/unit/alf-relvar/base/test_update.rb +5 -4
  139. data/spec/unit/alf-relvar/virtual/test_connection.rb +3 -2
  140. data/spec/unit/alf-relvar/virtual/test_delete.rb +3 -4
  141. data/spec/unit/alf-relvar/virtual/test_expr.rb +3 -2
  142. data/spec/unit/alf-relvar/virtual/test_heading.rb +5 -4
  143. data/spec/unit/alf-relvar/virtual/test_insert.rb +2 -2
  144. data/spec/unit/alf-relvar/virtual/test_keys.rb +5 -4
  145. data/spec/unit/alf-relvar/virtual/test_oo_lang.rb +7 -2
  146. data/spec/unit/alf-relvar/virtual/test_to_cog.rb +4 -3
  147. data/spec/unit/alf-relvar/virtual/test_to_lispy.rb +3 -6
  148. data/spec/unit/alf-relvar/virtual/test_to_relvar.rb +2 -1
  149. data/spec/unit/alf-relvar/virtual/test_to_s.rb +3 -3
  150. data/spec/unit/alf-relvar/virtual/test_update.rb +4 -5
  151. data/spec/unit/alf-support/config/test_dup.rb +58 -0
  152. data/spec/unit/alf-support/config/test_freeze.rb +37 -0
  153. data/spec/unit/alf-support/config/test_hash_get.rb +41 -0
  154. data/spec/unit/alf-support/config/test_hash_set.rb +61 -0
  155. data/spec/unit/alf-support/config/test_new.rb +10 -0
  156. data/spec/unit/alf-support/config/test_option.rb +15 -0
  157. data/spec/unit/alf-support/config/test_option_setter.rb +72 -0
  158. data/spec/unit/alf-support/config/test_options.rb +22 -0
  159. data/spec/unit/alf-support/tuple_scope/test_hash_get.rb +20 -0
  160. data/spec/unit/alf-support/tuple_scope/test_respond_to.rb +6 -0
  161. data/spec/unit/alf-support/tuple_scope/test_to_s.rb +22 -0
  162. data/spec/unit/alf-types/keys/test_select.rb +57 -8
  163. data/spec/unit/alf-types/ordering/test_coerce.rb +46 -35
  164. data/spec/unit/alf-types/ordering/test_dive.rb +41 -0
  165. data/spec/unit/alf-types/ordering/test_hash_get.rb +20 -0
  166. data/spec/unit/alf-types/ordering/test_merge.rb +61 -0
  167. data/spec/unit/alf-types/ordering/test_reverse.rb +40 -0
  168. data/spec/unit/alf-types/ordering/test_selectors.rb +24 -0
  169. data/spec/unit/alf-types/ordering/test_sorter.rb +62 -8
  170. data/spec/unit/alf-types/ordering/test_to_attr_list.rb +16 -4
  171. data/spec/unit/alf-types/ordering/test_total.rb +56 -0
  172. data/spec/unit/alf-types/selection/test_coerce.rb +38 -0
  173. data/spec/unit/alf-types/selection/test_select.rb +34 -0
  174. data/spec/unit/alf-types/selector/test_coerce.rb +36 -0
  175. data/spec/unit/alf-types/selector/test_composite_q.rb +20 -0
  176. data/spec/unit/alf-types/selector/test_dive.rb +32 -0
  177. data/spec/unit/alf-types/selector/test_select.rb +40 -0
  178. data/spec/unit/alf-types/selector/test_simple_q.rb +20 -0
  179. data/spec/unit/alf-types/selector/test_to_a.rb +20 -0
  180. data/spec/unit/alf-types/selector/test_to_lispy.rb +20 -0
  181. data/spec/unit/alf-types/selector/test_to_ruby_literal.rb +20 -0
  182. data/spec/unit/alf-types/tuple_expression/test_evaluate.rb +38 -0
  183. data/spec/unit/alf-viewpoint/metadata/test_add_members.rb +38 -0
  184. data/spec/unit/alf-viewpoint/metadata/test_all_members.rb +33 -0
  185. data/spec/unit/alf-viewpoint/metadata/test_depends.rb +50 -0
  186. data/spec/unit/alf-viewpoint/metadata/test_dup.rb +32 -0
  187. data/spec/unit/alf-viewpoint/metadata/test_expand.rb +31 -0
  188. data/spec/unit/alf-viewpoint/metadata/test_expects.rb +38 -0
  189. data/spec/unit/alf-viewpoint/metadata/test_initialize.rb +40 -0
  190. data/spec/unit/alf-viewpoint/metadata/test_to_module.rb +37 -0
  191. data/spec/unit/alf-viewpoint/test_depends.rb +18 -0
  192. data/spec/unit/alf-viewpoint/test_expects.rb +18 -0
  193. data/spec/unit/alf-viewpoint/test_members.rb +33 -0
  194. data/spec/unit/alf-viewpoint/test_metadata.rb +27 -0
  195. data/spec/unit/alf-viewpoint/test_native_const.rb +6 -0
  196. metadata +125 -14
  197. data/spec/unit/alf-database/options/test_default_viewpoint.rb +0 -25
  198. data/spec/unit/alf-types/keys/test_compact.rb +0 -21
  199. data/spec/unit/alf-types/ordering/test_plus.rb +0 -27
  200. data/spec/unit/alf-viewpoint/test_namespace.rb +0 -23
@@ -7,6 +7,12 @@ module Alf
7
7
  let(:scope){ TupleScope.new }
8
8
 
9
9
  it_behaves_like "A scope"
10
+
11
+ it 'responds to [], to_s and inspect' do
12
+ scope.respond_to?(:[]).should be_true
13
+ scope.respond_to?(:to_s).should be_true
14
+ scope.respond_to?(:inspect).should be_true
15
+ end
10
16
  end
11
17
 
12
18
  context 'on a TupleScope that decorates a tuple' do
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ module Alf
3
+ module Support
4
+ describe TupleScope, "to_s and inspect" do
5
+
6
+ let(:tuple){ {:a => 1, :b => 2} }
7
+
8
+ context 'with a simple TupleScope' do
9
+ let(:scope){ TupleScope.new(tuple) }
10
+
11
+ it "delegates to_s to the tuple" do
12
+ scope.to_s.should eq(tuple.to_s)
13
+ end
14
+
15
+ it "delegates to the tuple" do
16
+ scope.inspect.should eq(tuple.inspect)
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -2,16 +2,65 @@ require 'spec_helper'
2
2
  module Alf
3
3
  describe Keys, "select" do
4
4
 
5
- subject{ keys.select{|k| (k & [:name]).empty? } }
5
+ context 'when checking for intersection' do
6
+ subject{ keys.select{|k| (k & [:name]).empty? } }
6
7
 
7
- let(:keys) {
8
- Keys[ [:a], [:name], [:last, :name] ]
9
- }
10
- let(:expected) {
11
- Keys[ [:a] ]
12
- }
8
+ let(:keys) {
9
+ Keys[ [:a], [:name], [:last, :name] ]
10
+ }
13
11
 
14
- it{ should eq(expected) }
12
+ let(:expected) {
13
+ Keys[ [:a] ]
14
+ }
15
+
16
+ it{ should eq(expected) }
17
+ end
18
+
19
+ context 'when taking only empty keys' do
20
+ subject{ keys.select{|k| k.empty?} }
21
+
22
+ let(:keys) {
23
+ Keys[ [:a], [], [], [:a, :b] ]
24
+ }
25
+
26
+ let(:expected) {
27
+ Keys[ [] ]
28
+ }
29
+
30
+ it{ should eq(expected) }
31
+ end
32
+
33
+ context 'when checking projections' do
34
+ subject{
35
+ keys.select{|k|
36
+ k.project([:a], false) == k
37
+ }
38
+ }
39
+
40
+ context 'when keys are not empty' do
41
+ let(:keys){
42
+ Keys[ [:a], [:a, :name], [] ]
43
+ }
44
+
45
+ let(:expected){
46
+ Keys[ [:a], [] ]
47
+ }
48
+
49
+ it{ should eq(expected) }
50
+ end
51
+
52
+ context 'when keys contains only the empty key' do
53
+ let(:keys){
54
+ Keys[ [] ]
55
+ }
56
+
57
+ let(:expected){
58
+ Keys[ [] ]
59
+ }
60
+
61
+ it{ should eq(expected) }
62
+ end
63
+ end
15
64
 
16
65
  end
17
66
  end
@@ -2,51 +2,62 @@ require 'spec_helper'
2
2
  module Alf
3
3
  describe Ordering, "coerce" do
4
4
 
5
- let(:expected){ Ordering.new([[:a, :asc], [:b, :asc]]) }
5
+ context 'with single attribute names' do
6
+ let(:expected){ Ordering.new([[:a, :asc], [:b, :asc]]) }
6
7
 
7
- it "should work with an Ordering" do
8
- key = Ordering.coerce [:a, :b]
9
- key.should eq(expected)
10
- end
8
+ it "should work with an Ordering" do
9
+ key = Ordering.coerce [:a, :b]
10
+ key.should eq(expected)
11
+ end
11
12
 
12
- it "should work with an empty array" do
13
- key = Ordering.coerce []
14
- key.should eq(Ordering.new([]))
15
- end
13
+ it "should work with an empty array" do
14
+ key = Ordering.coerce []
15
+ key.should eq(Ordering.new([]))
16
+ end
16
17
 
17
- it "should work with a normalized array" do
18
- key = Ordering.coerce [[:a, :asc], [:b, :asc]]
19
- key.should eq(expected)
20
- end
18
+ it "should work with a normalized array" do
19
+ key = Ordering.coerce [[:a, :asc], [:b, :asc]]
20
+ key.should eq(expected)
21
+ end
21
22
 
22
- it "should work with an array of AttrName" do
23
- key = Ordering.coerce [:a, :b]
24
- key.should eq(expected)
25
- end
23
+ it "should work with an array of AttrName" do
24
+ key = Ordering.coerce [:a, :b]
25
+ key.should eq(expected)
26
+ end
26
27
 
27
- it "should work with an array of Strings" do
28
- key = Ordering.coerce ["a", "b"]
29
- key.should eq(expected)
30
- end
28
+ it "should work with an array of Strings" do
29
+ key = Ordering.coerce ["a", "b"]
30
+ key.should eq(expected)
31
+ end
31
32
 
32
- it "should work with a single array with explicit asc/desc (1)" do
33
- key = Ordering.coerce [:a, :asc]
34
- key.should eq(Ordering.new([[:a, :asc]]))
35
- end
33
+ it "should work with a single array with explicit asc/desc (1)" do
34
+ key = Ordering.coerce [:a, :asc]
35
+ key.should eq(Ordering.new([[:a, :asc]]))
36
+ end
36
37
 
37
- it "should work with a single array with explicit asc/desc (2)" do
38
- key = Ordering.coerce [:a, :asc, :b, :desc]
39
- key.should eq(Ordering.new([[:a, :asc], [:b, :desc]]))
40
- end
38
+ it "should work with a single array with explicit asc/desc (2)" do
39
+ key = Ordering.coerce [:a, :asc, :b, :desc]
40
+ key.should eq(Ordering.new([[:a, :asc], [:b, :desc]]))
41
+ end
41
42
 
42
- it "should work with a single array with explicit asc/desc (3)" do
43
- key = Ordering.coerce ["a", "asc", "b", "desc"]
44
- key.should eq(Ordering.new([[:a, :asc], [:b, :desc]]))
43
+ it "should work with a single array with explicit asc/desc (3)" do
44
+ key = Ordering.coerce ["a", "asc", "b", "desc"]
45
+ key.should eq(Ordering.new([[:a, :asc], [:b, :desc]]))
46
+ end
47
+
48
+ it "should work with an AttrList" do
49
+ key = Ordering.coerce(AttrList.new([:a, :b]))
50
+ key.should eq(expected)
51
+ end
45
52
  end
46
53
 
47
- it "should work with an AttrList" do
48
- key = Ordering.coerce(AttrList.new([:a, :b]))
49
- key.should eq(expected)
54
+ context 'with complex attribute names' do
55
+ let(:expected){ Ordering.new([[:a, :asc], [[:b, :name], :asc]]) }
56
+
57
+ it "should work with an array of Strings" do
58
+ key = Ordering.coerce ["a", "b.name"]
59
+ key.should eq(expected)
60
+ end
50
61
  end
51
62
 
52
63
  end # Ordering
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ module Alf
3
+ describe Ordering, 'dive' do
4
+
5
+ subject{ ordering.dive(:b) }
6
+
7
+ context 'when the ordering has :b' do
8
+ let(:ordering){
9
+ Ordering.new([[:a, :asc], [[:b, :x], :asc], [[:b, :y], :asc]])
10
+ }
11
+ let(:expected){
12
+ Ordering.new([[:x, :asc], [:y, :asc]])
13
+ }
14
+
15
+ it{ should eq(expected) }
16
+ end
17
+
18
+ context 'when the ordering has no :b' do
19
+ let(:ordering){
20
+ Ordering.new([[:a, :asc]])
21
+ }
22
+ let(:expected){
23
+ Ordering::EMPTY
24
+ }
25
+
26
+ it{ should eq(expected) }
27
+ end
28
+
29
+ context 'when the ordering has a single :b' do
30
+ let(:ordering){
31
+ Ordering.new([[:b, :asc]])
32
+ }
33
+ let(:expected){
34
+ Ordering::EMPTY
35
+ }
36
+
37
+ it{ should eq(expected) }
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ module Alf
3
+ describe Ordering, "[]" do
4
+
5
+ let(:ordering){ Ordering.new([[:a, :asc], [:b, :desc], [[:c, :d], :asc]]) }
6
+
7
+ it 'return the direction when an attribute' do
8
+ ordering[:a].should eq(:asc)
9
+ ordering[:b].should eq(:desc)
10
+ ordering[Selector[:a]].should eq(:asc)
11
+ ordering[Selector[:b]].should eq(:desc)
12
+ ordering[Selector[[:c, :d]]].should eq(:asc)
13
+ end
14
+
15
+ it 'return nil when not an attribute' do
16
+ ordering[:c].should be_nil
17
+ end
18
+
19
+ end # Ordering
20
+ end # Alf
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+ module Alf
3
+ describe Ordering, "merge" do
4
+
5
+ subject{ Ordering.coerce(left) + right }
6
+
7
+ let(:expected){ Ordering.new([[:a, :asc], [:b, :desc]]) }
8
+
9
+ describe "with another Ordering" do
10
+ let(:left){ [:a] }
11
+ let(:right){ Ordering.new([[:b, :desc]]) }
12
+
13
+ it{ should eq(expected) }
14
+ end
15
+
16
+ describe "with another array" do
17
+ let(:left){ [:a] }
18
+ let(:right){ [[:b, :desc]] }
19
+
20
+ it{ should eq(expected) }
21
+ end
22
+
23
+ describe "with another array (2)" do
24
+ let(:left){ [:a] }
25
+ let(:right){ [:b, :desc] }
26
+
27
+ it{ should eq(expected) }
28
+ end
29
+
30
+ describe "with redundancy" do
31
+ let(:left) { [[:a, :asc], [:b, :desc]] }
32
+ let(:right){ [[:a, :asc], [:c, :desc]] }
33
+
34
+ it{ should eq(Ordering.new([[:a, :asc], [:b, :desc], [:c, :desc]])) }
35
+ end
36
+
37
+ describe "with conflict and no block" do
38
+ let(:left) { [[:a, :asc], [:b, :desc]] }
39
+ let(:right){ [[:a, :desc], [:c, :desc]] }
40
+
41
+ it{ should eq(Ordering.new([[:a, :desc], [:b, :desc], [:c, :desc]])) }
42
+ end
43
+
44
+ describe "with conflict and a block" do
45
+ let(:left) { [[:a, :asc], [:b, :desc]] }
46
+ let(:right){ [[:a, :desc], [:c, :desc]] }
47
+
48
+ subject{
49
+ Ordering.coerce(left).merge(right){|attr,d1,d2|
50
+ attr.should eq(Selector[:a])
51
+ d1.should eq(:asc)
52
+ d2.should eq(:desc)
53
+ d1
54
+ }
55
+ }
56
+
57
+ it{ should eq(Ordering.new([[:a, :asc], [:b, :desc], [:c, :desc]])) }
58
+ end
59
+
60
+ end # Ordering
61
+ end # Alf
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ module Alf
3
+ describe Ordering, "reverse" do
4
+
5
+ subject{
6
+ ordering.reverse
7
+ }
8
+
9
+ context 'on an empty ordering' do
10
+ let(:ordering){ Ordering.new([]) }
11
+
12
+ it{ should eq(ordering) }
13
+ end
14
+
15
+ context 'on a singleton asc ordering' do
16
+ let(:ordering){ Ordering.new([[:name, :asc]]) }
17
+
18
+ it{ should eq(Ordering.new([[:name, :desc]])) }
19
+ end
20
+
21
+ context 'on a singleton desc ordering' do
22
+ let(:ordering){ Ordering.new([[:name, :desc]]) }
23
+
24
+ it{ should eq(Ordering.new([[:name, :asc]])) }
25
+ end
26
+
27
+ #
28
+ # name, id => name, id
29
+ # b, 12 a, 13
30
+ # a, 10 a, 10
31
+ # a, 13 b, 12
32
+ #
33
+ context 'on a mixed ordering' do
34
+ let(:ordering){ Ordering.new([[:name, :desc], [:id, :asc]]) }
35
+
36
+ it{ should eq(Ordering.new([[:name, :asc], [:id, :desc]])) }
37
+ end
38
+
39
+ end # Ordering
40
+ end # Alf
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ module Alf
3
+ describe Ordering, "selectors" do
4
+
5
+ subject{ ordering.selectors }
6
+
7
+ context 'with single attribute names' do
8
+ let(:ordering){ Ordering.new([[:a, :asc], [:b, :asc]]) }
9
+
10
+ it "works as expected" do
11
+ subject.should eq([Alf::Selector[:a], Alf::Selector[:b]])
12
+ end
13
+ end
14
+
15
+ context 'with complex attribute names' do
16
+ let(:ordering){ Ordering.new([[:a, :asc], [[:b, :name], :asc]]) }
17
+
18
+ it "works as expected" do
19
+ subject.should eq([Alf::Selector[:a], Alf::Selector[[:b, :name]]])
20
+ end
21
+ end
22
+
23
+ end # Ordering
24
+ end # Alf
@@ -2,14 +2,68 @@ require 'spec_helper'
2
2
  module Alf
3
3
  describe Ordering, "sorter" do
4
4
 
5
- let(:sorter){ Ordering.coerce([[:a, :desc]]).sorter }
6
-
7
- it 'should sort correctly' do
8
- [{:a => 2},
9
- {:a => 7},
10
- {:a => 1}].sort(&sorter).should eq([
11
- {:a => 7}, {:a => 2}, {:a => 1}
12
- ])
5
+ subject{ ordering.sorter }
6
+
7
+ context 'on an ordering with single names' do
8
+ let(:ordering){ Ordering.coerce([[:a, :desc]]) }
9
+
10
+ let(:tuples){
11
+ [{a: 2},
12
+ {a: 7},
13
+ {a: 1}]
14
+ }
15
+
16
+ let(:expected){
17
+ [{a: 7},
18
+ {a: 2},
19
+ {a: 1}]
20
+ }
21
+
22
+ it 'should sort correctly' do
23
+ tuples.sort(&subject).should eq(expected)
24
+ end
25
+ end
26
+
27
+ context 'on an ordering with hierarchical names' do
28
+ let(:ordering){ Ordering.coerce([[:a, :asc], [[:b, :x], :desc]]) }
29
+
30
+ let(:tuples){
31
+ [{a: 2, b: {x: 1}},
32
+ {a: 7, b: {x: 1}},
33
+ {a: 2, b: {x: 2}}]
34
+ }
35
+
36
+ let(:expected){
37
+ [{a: 2, b: {x: 2}},
38
+ {a: 2, b: {x: 1}},
39
+ {a: 7, b: {x: 1}}]
40
+ }
41
+
42
+ it 'should sort correctly' do
43
+ tuples.sort(&subject).should eq(expected)
44
+ end
45
+ end
46
+
47
+ context 'when RVA attributes are involved' do
48
+ let(:ordering){ Ordering.coerce([[:a, :asc], [[:b, :x], :desc]]) }
49
+
50
+ let(:tuples){
51
+ [{a: 2, b: [{x: 1}]},
52
+ {a: 7, b: [{x: 1}]},
53
+ {a: 2, b: [{x: 2}]}]
54
+ }
55
+
56
+ let(:expected){
57
+ [{a: 2, b: [{x: 1}]},
58
+ {a: 2, b: [{x: 2}]},
59
+ {a: 7, b: [{x: 1}]}]
60
+ }
61
+
62
+ it 'should sort correctly' do
63
+ pending{
64
+ tuples.sort(&subject).should eq(expected)
65
+ }
66
+ end
13
67
  end
14
68
 
15
69
  end # Ordering