arel 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/README.markdown +184 -0
- data/Rakefile +60 -0
- data/VERSION +1 -0
- data/arel.gemspec +233 -0
- data/doc/CONVENTIONS +17 -0
- data/doc/TODO +118 -0
- data/lib/arel.rb +10 -0
- data/lib/arel/algebra.rb +4 -0
- data/lib/arel/algebra/extensions.rb +4 -0
- data/lib/arel/algebra/extensions/class.rb +32 -0
- data/lib/arel/algebra/extensions/hash.rb +11 -0
- data/lib/arel/algebra/extensions/object.rb +17 -0
- data/lib/arel/algebra/extensions/symbol.rb +9 -0
- data/lib/arel/algebra/predicates.rb +41 -0
- data/lib/arel/algebra/primitives.rb +5 -0
- data/lib/arel/algebra/primitives/attribute.rb +150 -0
- data/lib/arel/algebra/primitives/expression.rb +43 -0
- data/lib/arel/algebra/primitives/ordering.rb +23 -0
- data/lib/arel/algebra/primitives/value.rb +14 -0
- data/lib/arel/algebra/relations.rb +14 -0
- data/lib/arel/algebra/relations/operations/alias.rb +7 -0
- data/lib/arel/algebra/relations/operations/group.rb +12 -0
- data/lib/arel/algebra/relations/operations/join.rb +64 -0
- data/lib/arel/algebra/relations/operations/order.rb +18 -0
- data/lib/arel/algebra/relations/operations/project.rb +20 -0
- data/lib/arel/algebra/relations/operations/skip.rb +6 -0
- data/lib/arel/algebra/relations/operations/take.rb +10 -0
- data/lib/arel/algebra/relations/operations/where.rb +16 -0
- data/lib/arel/algebra/relations/relation.rb +136 -0
- data/lib/arel/algebra/relations/row.rb +26 -0
- data/lib/arel/algebra/relations/utilities/compound.rb +30 -0
- data/lib/arel/algebra/relations/utilities/externalization.rb +24 -0
- data/lib/arel/algebra/relations/utilities/nil.rb +7 -0
- data/lib/arel/algebra/relations/writes.rb +36 -0
- data/lib/arel/engines.rb +2 -0
- data/lib/arel/engines/memory.rb +4 -0
- data/lib/arel/engines/memory/engine.rb +16 -0
- data/lib/arel/engines/memory/predicates.rb +35 -0
- data/lib/arel/engines/memory/primitives.rb +27 -0
- data/lib/arel/engines/memory/relations.rb +5 -0
- data/lib/arel/engines/memory/relations/array.rb +25 -0
- data/lib/arel/engines/memory/relations/compound.rb +9 -0
- data/lib/arel/engines/memory/relations/operations.rb +61 -0
- data/lib/arel/engines/memory/relations/writes.rb +7 -0
- data/lib/arel/engines/sql.rb +7 -0
- data/lib/arel/engines/sql/christener.rb +13 -0
- data/lib/arel/engines/sql/engine.rb +37 -0
- data/lib/arel/engines/sql/extensions.rb +4 -0
- data/lib/arel/engines/sql/extensions/array.rb +16 -0
- data/lib/arel/engines/sql/extensions/nil_class.rb +11 -0
- data/lib/arel/engines/sql/extensions/object.rb +15 -0
- data/lib/arel/engines/sql/extensions/range.rb +15 -0
- data/lib/arel/engines/sql/formatters.rb +113 -0
- data/lib/arel/engines/sql/predicates.rb +51 -0
- data/lib/arel/engines/sql/primitives.rb +85 -0
- data/lib/arel/engines/sql/relations.rb +9 -0
- data/lib/arel/engines/sql/relations/operations/alias.rb +5 -0
- data/lib/arel/engines/sql/relations/operations/join.rb +33 -0
- data/lib/arel/engines/sql/relations/relation.rb +50 -0
- data/lib/arel/engines/sql/relations/table.rb +52 -0
- data/lib/arel/engines/sql/relations/utilities/compound.rb +10 -0
- data/lib/arel/engines/sql/relations/utilities/externalization.rb +14 -0
- data/lib/arel/engines/sql/relations/utilities/nil.rb +6 -0
- data/lib/arel/engines/sql/relations/utilities/recursion.rb +13 -0
- data/lib/arel/engines/sql/relations/writes.rb +39 -0
- data/lib/arel/session.rb +48 -0
- data/spec/arel/algebra/unit/predicates/binary_spec.rb +33 -0
- data/spec/arel/algebra/unit/predicates/equality_spec.rb +27 -0
- data/spec/arel/algebra/unit/predicates/in_spec.rb +10 -0
- data/spec/arel/algebra/unit/primitives/attribute_spec.rb +183 -0
- data/spec/arel/algebra/unit/primitives/expression_spec.rb +45 -0
- data/spec/arel/algebra/unit/primitives/value_spec.rb +15 -0
- data/spec/arel/algebra/unit/relations/alias_spec.rb +16 -0
- data/spec/arel/algebra/unit/relations/delete_spec.rb +9 -0
- data/spec/arel/algebra/unit/relations/group_spec.rb +10 -0
- data/spec/arel/algebra/unit/relations/insert_spec.rb +9 -0
- data/spec/arel/algebra/unit/relations/join_spec.rb +26 -0
- data/spec/arel/algebra/unit/relations/order_spec.rb +21 -0
- data/spec/arel/algebra/unit/relations/project_spec.rb +34 -0
- data/spec/arel/algebra/unit/relations/relation_spec.rb +188 -0
- data/spec/arel/algebra/unit/relations/skip_spec.rb +10 -0
- data/spec/arel/algebra/unit/relations/table_spec.rb +39 -0
- data/spec/arel/algebra/unit/relations/take_spec.rb +10 -0
- data/spec/arel/algebra/unit/relations/update_spec.rb +9 -0
- data/spec/arel/algebra/unit/relations/where_spec.rb +18 -0
- data/spec/arel/algebra/unit/session/session_spec.rb +84 -0
- data/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb +48 -0
- data/spec/arel/engines/memory/unit/relations/array_spec.rb +32 -0
- data/spec/arel/engines/memory/unit/relations/insert_spec.rb +28 -0
- data/spec/arel/engines/memory/unit/relations/join_spec.rb +31 -0
- data/spec/arel/engines/memory/unit/relations/order_spec.rb +27 -0
- data/spec/arel/engines/memory/unit/relations/project_spec.rb +27 -0
- data/spec/arel/engines/memory/unit/relations/skip_spec.rb +26 -0
- data/spec/arel/engines/memory/unit/relations/take_spec.rb +26 -0
- data/spec/arel/engines/memory/unit/relations/where_spec.rb +39 -0
- data/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb +209 -0
- data/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb +167 -0
- data/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb +107 -0
- data/spec/arel/engines/sql/unit/engine_spec.rb +45 -0
- data/spec/arel/engines/sql/unit/predicates/binary_spec.rb +117 -0
- data/spec/arel/engines/sql/unit/predicates/equality_spec.rb +46 -0
- data/spec/arel/engines/sql/unit/predicates/in_spec.rb +86 -0
- data/spec/arel/engines/sql/unit/predicates/predicates_spec.rb +65 -0
- data/spec/arel/engines/sql/unit/primitives/attribute_spec.rb +32 -0
- data/spec/arel/engines/sql/unit/primitives/expression_spec.rb +24 -0
- data/spec/arel/engines/sql/unit/primitives/literal_spec.rb +23 -0
- data/spec/arel/engines/sql/unit/primitives/value_spec.rb +29 -0
- data/spec/arel/engines/sql/unit/relations/alias_spec.rb +43 -0
- data/spec/arel/engines/sql/unit/relations/delete_spec.rb +63 -0
- data/spec/arel/engines/sql/unit/relations/group_spec.rb +56 -0
- data/spec/arel/engines/sql/unit/relations/insert_spec.rb +107 -0
- data/spec/arel/engines/sql/unit/relations/join_spec.rb +57 -0
- data/spec/arel/engines/sql/unit/relations/order_spec.rb +113 -0
- data/spec/arel/engines/sql/unit/relations/project_spec.rb +110 -0
- data/spec/arel/engines/sql/unit/relations/skip_spec.rb +32 -0
- data/spec/arel/engines/sql/unit/relations/table_spec.rb +69 -0
- data/spec/arel/engines/sql/unit/relations/take_spec.rb +32 -0
- data/spec/arel/engines/sql/unit/relations/update_spec.rb +151 -0
- data/spec/arel/engines/sql/unit/relations/where_spec.rb +56 -0
- data/spec/connections/mysql_connection.rb +16 -0
- data/spec/connections/postgresql_connection.rb +15 -0
- data/spec/connections/sqlite3_connection.rb +25 -0
- data/spec/doubles/hash.rb +23 -0
- data/spec/matchers/be_like.rb +24 -0
- data/spec/matchers/disambiguate_attributes.rb +28 -0
- data/spec/matchers/hash_the_same_as.rb +26 -0
- data/spec/schemas/mysql_schema.rb +18 -0
- data/spec/schemas/postgresql_schema.rb +18 -0
- data/spec/schemas/sqlite3_schema.rb +18 -0
- data/spec/spec_helper.rb +47 -0
- metadata +250 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Table do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '[]' do
|
10
|
+
describe 'when given a', Symbol do
|
11
|
+
it "manufactures an attribute if the symbol names an attribute within the relation" do
|
12
|
+
@relation[:id].should == Attribute.new(@relation, :id)
|
13
|
+
@relation[:does_not_exist].should be_nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'when given an', Attribute do
|
18
|
+
it "returns the attribute if the attribute is within the relation" do
|
19
|
+
@relation[@relation[:id]].should == @relation[:id]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns nil if the attribtue is not within the relation" do
|
23
|
+
another_relation = Table.new(:photos)
|
24
|
+
@relation[another_relation[:id]].should be_nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'when given an', Expression do
|
29
|
+
before do
|
30
|
+
@expression = @relation[:id].count
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns the Expression if the Expression is within the relation" do
|
34
|
+
@relation[@expression].should be_nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Where do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@predicate = @relation[:id].eq(1)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#initialize' do
|
11
|
+
it "manufactures nested where relations if multiple predicates are provided" do
|
12
|
+
another_predicate = @relation[:name].lt(2)
|
13
|
+
Where.new(@relation, @predicate, another_predicate). \
|
14
|
+
should == Where.new(Where.new(@relation, another_predicate), @predicate)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Session do
|
5
|
+
before do
|
6
|
+
@relation = Table.new(:users)
|
7
|
+
@session = Session.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '::start' do
|
11
|
+
describe '::instance' do
|
12
|
+
it "it is a singleton within the started session" do
|
13
|
+
Session.start do
|
14
|
+
Session.new.should == Session.new
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "is a singleton across nested sessions" do
|
19
|
+
Session.start do
|
20
|
+
outside = Session.new
|
21
|
+
Session.start do
|
22
|
+
Session.new.should == outside
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "manufactures new sessions outside of the started session" do
|
28
|
+
Session.new.should_not == Session.new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe Session::CRUD do
|
34
|
+
before do
|
35
|
+
@insert = Insert.new(@relation, @relation[:name] => 'nick')
|
36
|
+
@update = Update.new(@relation, @relation[:name] => 'nick')
|
37
|
+
@delete = Deletion.new(@relation)
|
38
|
+
@read = @relation
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#create' do
|
42
|
+
it "executes an insertion on the connection" do
|
43
|
+
mock(@insert).call
|
44
|
+
@session.create(@insert)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#read' do
|
49
|
+
it "executes an selection on the connection" do
|
50
|
+
mock(@read).call
|
51
|
+
@session.read(@read)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "is memoized" do
|
55
|
+
mock(@read).call.once
|
56
|
+
@session.read(@read)
|
57
|
+
@session.read(@read)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#update' do
|
62
|
+
it "executes an update on the connection" do
|
63
|
+
mock(@update).call
|
64
|
+
@session.update(@update)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#delete' do
|
69
|
+
it "executes a delete on the connection" do
|
70
|
+
mock(@delete).call
|
71
|
+
@session.delete(@delete)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'Transactions' do
|
77
|
+
describe '#begin' do
|
78
|
+
end
|
79
|
+
|
80
|
+
describe '#end' do
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Join do
|
5
|
+
before do
|
6
|
+
@users = Array.new([
|
7
|
+
[1, 'bryan' ],
|
8
|
+
[2, 'emilio' ],
|
9
|
+
[3, 'nick']
|
10
|
+
], [:id, :name])
|
11
|
+
@photos = Table.new(:photos)
|
12
|
+
@photos.delete
|
13
|
+
@photos \
|
14
|
+
.insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6) \
|
15
|
+
.insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'when the in memory relation is on the left' do
|
19
|
+
it 'joins across engines' do
|
20
|
+
@users \
|
21
|
+
.join(@photos) \
|
22
|
+
.on(@users[:id].eq(@photos[:user_id])) \
|
23
|
+
.project(@users[:name], @photos[:camera_id]) \
|
24
|
+
.let do |relation|
|
25
|
+
relation.call.should == [
|
26
|
+
Row.new(relation, ['bryan', '6']),
|
27
|
+
Row.new(relation, ['emilio', '42'])
|
28
|
+
]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'when the in memory relation is on the right' do
|
34
|
+
it 'joins across engines' do
|
35
|
+
@photos \
|
36
|
+
.join(@users) \
|
37
|
+
.on(@users[:id].eq(@photos[:user_id])) \
|
38
|
+
.project(@users[:name], @photos[:camera_id]) \
|
39
|
+
.let do |relation|
|
40
|
+
relation.call.should == [
|
41
|
+
Row.new(relation, ['bryan', '6']),
|
42
|
+
Row.new(relation, ['emilio', '42'])
|
43
|
+
]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Array do
|
5
|
+
before do
|
6
|
+
@relation = Array.new([
|
7
|
+
[1, 'duck' ],
|
8
|
+
[2, 'duck' ],
|
9
|
+
[3, 'goose']
|
10
|
+
], [:id, :name])
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#attributes' do
|
14
|
+
it 'manufactures attributes corresponding to the names given on construction' do
|
15
|
+
@relation.attributes.should == [
|
16
|
+
Attribute.new(@relation, :id),
|
17
|
+
Attribute.new(@relation, :name)
|
18
|
+
]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#call' do
|
23
|
+
it "manufactures an array of hashes of attributes to values" do
|
24
|
+
@relation.call.should == [
|
25
|
+
Row.new(@relation, [1, 'duck']),
|
26
|
+
Row.new(@relation, [2, 'duck']),
|
27
|
+
Row.new(@relation, [3, 'goose'])
|
28
|
+
]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Insert do
|
5
|
+
before do
|
6
|
+
@relation = Array.new([
|
7
|
+
[1, 'duck' ],
|
8
|
+
[2, 'duck' ],
|
9
|
+
[3, 'goose']
|
10
|
+
], [:id, :name])
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#call' do
|
14
|
+
it "manufactures an array of hashes of attributes to values" do
|
15
|
+
@relation \
|
16
|
+
.insert(@relation[:id] => 4, @relation[:name] => 'guinea fowl') \
|
17
|
+
.let do |relation|
|
18
|
+
relation.call.should == [
|
19
|
+
Row.new(relation, [1, 'duck']),
|
20
|
+
Row.new(relation, [2, 'duck']),
|
21
|
+
Row.new(relation, [3, 'goose']),
|
22
|
+
Row.new(relation, [4, 'guinea fowl'])
|
23
|
+
]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Join do
|
5
|
+
before do
|
6
|
+
@relation1 = Array.new([
|
7
|
+
[1, 'duck' ],
|
8
|
+
[2, 'duck' ],
|
9
|
+
[3, 'goose']
|
10
|
+
], [:id, :name])
|
11
|
+
@relation2 = @relation1.alias
|
12
|
+
end
|
13
|
+
|
14
|
+
describe InnerJoin do
|
15
|
+
describe '#call' do
|
16
|
+
it 'combines the two tables where the predicate obtains' do
|
17
|
+
@relation1 \
|
18
|
+
.join(@relation2) \
|
19
|
+
.on(@relation1[:id].eq(@relation2[:id])) \
|
20
|
+
.let do |relation|
|
21
|
+
relation.call.should == [
|
22
|
+
Row.new(relation, [1, 'duck', 1, 'duck' ]),
|
23
|
+
Row.new(relation, [2, 'duck', 2, 'duck' ]),
|
24
|
+
Row.new(relation, [3, 'goose', 3, 'goose'])
|
25
|
+
]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Order do
|
5
|
+
before do
|
6
|
+
@relation = Array.new([
|
7
|
+
[1, 'duck' ],
|
8
|
+
[2, 'duck' ],
|
9
|
+
[3, 'goose']
|
10
|
+
], [:id, :name])
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#call' do
|
14
|
+
it 'sorts the relation with the provided ordering' do
|
15
|
+
@relation \
|
16
|
+
.order(@relation[:id].desc) \
|
17
|
+
.let do |relation|
|
18
|
+
relation.call.should == [
|
19
|
+
Row.new(relation, [3, 'goose']),
|
20
|
+
Row.new(relation, [2, 'duck' ]),
|
21
|
+
Row.new(relation, [1, 'duck' ])
|
22
|
+
]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Project do
|
5
|
+
before do
|
6
|
+
@relation = Array.new([
|
7
|
+
[1, 'duck' ],
|
8
|
+
[2, 'duck' ],
|
9
|
+
[3, 'goose']
|
10
|
+
], [:id, :name])
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#call' do
|
14
|
+
it 'retains only the attributes that are provided' do
|
15
|
+
@relation \
|
16
|
+
.project(@relation[:id]) \
|
17
|
+
.let do |relation|
|
18
|
+
relation.call.should == [
|
19
|
+
Row.new(relation, [1]),
|
20
|
+
Row.new(relation, [2]),
|
21
|
+
Row.new(relation, [3])
|
22
|
+
]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Skip do
|
5
|
+
before do
|
6
|
+
@relation = Array.new([
|
7
|
+
[1, 'duck' ],
|
8
|
+
[2, 'duck' ],
|
9
|
+
[3, 'goose']
|
10
|
+
], [:id, :name])
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#call' do
|
14
|
+
it 'removes the first n rows' do
|
15
|
+
@relation \
|
16
|
+
.skip(1) \
|
17
|
+
.let do |relation|
|
18
|
+
relation.call.should == [
|
19
|
+
Row.new(relation, [2, 'duck']),
|
20
|
+
Row.new(relation, [3, 'goose']),
|
21
|
+
]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Take do
|
5
|
+
before do
|
6
|
+
@relation = Array.new([
|
7
|
+
[1, 'duck' ],
|
8
|
+
[2, 'duck' ],
|
9
|
+
[3, 'goose']
|
10
|
+
], [:id, :name])
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#call' do
|
14
|
+
it 'removes the rows after the first n' do
|
15
|
+
@relation \
|
16
|
+
.take(2) \
|
17
|
+
.let do |relation|
|
18
|
+
relation.call.should == [
|
19
|
+
Row.new(relation, [1, 'duck']),
|
20
|
+
Row.new(relation, [2, 'duck']),
|
21
|
+
]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
describe Where do
|
5
|
+
before do
|
6
|
+
@relation = Array.new([
|
7
|
+
[1, 'duck' ],
|
8
|
+
[2, 'duck' ],
|
9
|
+
[3, 'goose']
|
10
|
+
], [:id, :name])
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#call' do
|
14
|
+
it 'filters the relation with the provided predicate' do
|
15
|
+
@relation \
|
16
|
+
.where(@relation[:id].lt(3)) \
|
17
|
+
.let do |relation|
|
18
|
+
relation.call.should == [
|
19
|
+
Row.new(relation, [1, 'duck']),
|
20
|
+
Row.new(relation, [2, 'duck']),
|
21
|
+
]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'when filtering a where relation' do
|
26
|
+
it 'further filters the already-filtered relation with the provided predicate' do
|
27
|
+
@relation \
|
28
|
+
.where(@relation[:id].gt(1)) \
|
29
|
+
.where(@relation[:id].lt(3)) \
|
30
|
+
.let do |relation|
|
31
|
+
relation.call.should == [
|
32
|
+
Row.new(relation, [2, 'duck'])
|
33
|
+
]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|