ardb 0.14.0 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ardb/relation_spy.rb +64 -0
- data/lib/ardb/version.rb +1 -1
- data/test/unit/relation_spy_tests.rb +160 -0
- metadata +7 -4
@@ -0,0 +1,64 @@
|
|
1
|
+
module Ardb
|
2
|
+
|
3
|
+
class RelationSpy
|
4
|
+
|
5
|
+
attr_reader :applied
|
6
|
+
attr_accessor :results
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@applied, @results = [], []
|
10
|
+
@offset, @limit = 0, nil
|
11
|
+
end
|
12
|
+
|
13
|
+
[ :select,
|
14
|
+
:joins,
|
15
|
+
:where,
|
16
|
+
:order,
|
17
|
+
:group, :having,
|
18
|
+
:merge
|
19
|
+
].each do |type|
|
20
|
+
|
21
|
+
define_method(type) do |*args|
|
22
|
+
@applied << AppliedExpression.new(type, args)
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def limit(value)
|
29
|
+
@limit = value ? value.to_i : nil
|
30
|
+
@applied << AppliedExpression.new(:limit, [ value ])
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def offset(value)
|
35
|
+
@offset = value ? value.to_i : 0
|
36
|
+
@applied << AppliedExpression.new(:offset, [ value ])
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
def all
|
41
|
+
@results[@offset, (@limit || @results.size)] || []
|
42
|
+
end
|
43
|
+
|
44
|
+
def count
|
45
|
+
all.size
|
46
|
+
end
|
47
|
+
|
48
|
+
def limit_value
|
49
|
+
@limit
|
50
|
+
end
|
51
|
+
|
52
|
+
def offset_value
|
53
|
+
@offset
|
54
|
+
end
|
55
|
+
|
56
|
+
def ==(other)
|
57
|
+
@applied == other.applied
|
58
|
+
end
|
59
|
+
|
60
|
+
AppliedExpression = Struct.new(:type, :args)
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
data/lib/ardb/version.rb
CHANGED
@@ -0,0 +1,160 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ardb/relation_spy'
|
3
|
+
|
4
|
+
class Ardb::RelationSpy
|
5
|
+
|
6
|
+
class UnitTests < Assert::Context
|
7
|
+
desc "Ardb::RelationSpy"
|
8
|
+
setup do
|
9
|
+
@relation_spy = Ardb::RelationSpy.new
|
10
|
+
end
|
11
|
+
subject{ @relation_spy }
|
12
|
+
|
13
|
+
should have_readers :applied
|
14
|
+
should have_accessors :results
|
15
|
+
should have_imeths :select, :joins, :where, :order, :group, :having, :merge
|
16
|
+
should have_imeths :limit, :offset
|
17
|
+
should have_imeths :all, :count
|
18
|
+
should have_imeths :limit_value, :offset_value
|
19
|
+
|
20
|
+
should "default it's attributes" do
|
21
|
+
assert_equal [], subject.applied
|
22
|
+
assert_equal [], subject.results
|
23
|
+
assert_equal nil, subject.limit_value
|
24
|
+
assert_equal 0, subject.offset_value
|
25
|
+
end
|
26
|
+
|
27
|
+
should "add an applied expression using `select`" do
|
28
|
+
subject.select :column_a, :column_b
|
29
|
+
assert_equal 1, subject.applied.size
|
30
|
+
applied_expression = subject.applied.first
|
31
|
+
assert_instance_of AppliedExpression, applied_expression
|
32
|
+
assert_equal :select, applied_expression.type
|
33
|
+
assert_equal [ :column_a, :column_b ], applied_expression.args
|
34
|
+
end
|
35
|
+
|
36
|
+
should "add an applied expression using `joins`" do
|
37
|
+
subject.joins :table_a, :table_b
|
38
|
+
assert_equal 1, subject.applied.size
|
39
|
+
applied_expression = subject.applied.first
|
40
|
+
assert_instance_of AppliedExpression, applied_expression
|
41
|
+
assert_equal :joins, applied_expression.type
|
42
|
+
assert_equal [ :table_a, :table_b ], applied_expression.args
|
43
|
+
end
|
44
|
+
|
45
|
+
should "add an applied expression using `where`" do
|
46
|
+
subject.where :column_a => 'some value'
|
47
|
+
assert_equal 1, subject.applied.size
|
48
|
+
applied_expression = subject.applied.first
|
49
|
+
assert_instance_of AppliedExpression, applied_expression
|
50
|
+
assert_equal :where, applied_expression.type
|
51
|
+
assert_equal [ { :column_a => 'some value' } ], applied_expression.args
|
52
|
+
end
|
53
|
+
|
54
|
+
should "add an applied expression using `order`" do
|
55
|
+
subject.order :column_a, :column_b
|
56
|
+
assert_equal 1, subject.applied.size
|
57
|
+
applied_expression = subject.applied.first
|
58
|
+
assert_instance_of AppliedExpression, applied_expression
|
59
|
+
assert_equal :order, applied_expression.type
|
60
|
+
assert_equal [ :column_a, :column_b ], applied_expression.args
|
61
|
+
end
|
62
|
+
|
63
|
+
should "add an applied expression using `group`" do
|
64
|
+
subject.group :column_a, :column_b
|
65
|
+
assert_equal 1, subject.applied.size
|
66
|
+
applied_expression = subject.applied.first
|
67
|
+
assert_instance_of AppliedExpression, applied_expression
|
68
|
+
assert_equal :group, applied_expression.type
|
69
|
+
assert_equal [ :column_a, :column_b ], applied_expression.args
|
70
|
+
end
|
71
|
+
|
72
|
+
should "add an applied expression using `having`" do
|
73
|
+
subject.having 'COUNT(column_a) > 0'
|
74
|
+
assert_equal 1, subject.applied.size
|
75
|
+
applied_expression = subject.applied.first
|
76
|
+
assert_instance_of AppliedExpression, applied_expression
|
77
|
+
assert_equal :having, applied_expression.type
|
78
|
+
assert_equal [ 'COUNT(column_a) > 0' ], applied_expression.args
|
79
|
+
end
|
80
|
+
|
81
|
+
should "add an applied expression using `merge`" do
|
82
|
+
other_relation = Ardb::RelationSpy.new
|
83
|
+
subject.merge other_relation
|
84
|
+
assert_equal 1, subject.applied.size
|
85
|
+
applied_expression = subject.applied.first
|
86
|
+
assert_instance_of AppliedExpression, applied_expression
|
87
|
+
assert_equal :merge, applied_expression.type
|
88
|
+
assert_equal [ other_relation ], applied_expression.args
|
89
|
+
end
|
90
|
+
|
91
|
+
should "add an applied expression using `limit`" do
|
92
|
+
subject.limit 100
|
93
|
+
assert_equal 1, subject.applied.size
|
94
|
+
applied_expression = subject.applied.first
|
95
|
+
assert_instance_of AppliedExpression, applied_expression
|
96
|
+
assert_equal :limit, applied_expression.type
|
97
|
+
assert_equal [ 100 ], applied_expression.args
|
98
|
+
end
|
99
|
+
|
100
|
+
should "set it's limit value using `limit`" do
|
101
|
+
subject.limit 100
|
102
|
+
assert_equal 100, subject.limit_value
|
103
|
+
end
|
104
|
+
|
105
|
+
should "add an applied expression using `offset`" do
|
106
|
+
subject.offset 100
|
107
|
+
assert_equal 1, subject.applied.size
|
108
|
+
applied_expression = subject.applied.first
|
109
|
+
assert_instance_of AppliedExpression, applied_expression
|
110
|
+
assert_equal :offset, applied_expression.type
|
111
|
+
assert_equal [ 100 ], applied_expression.args
|
112
|
+
end
|
113
|
+
|
114
|
+
should "set it's offset value using `offset`" do
|
115
|
+
subject.offset 100
|
116
|
+
assert_equal 100, subject.offset_value
|
117
|
+
end
|
118
|
+
|
119
|
+
should "return it's results using `all`" do
|
120
|
+
subject.results = [ 1, 2, 3 ]
|
121
|
+
assert_equal [ 1, 2, 3 ], subject.all
|
122
|
+
end
|
123
|
+
|
124
|
+
should "honor limit and offset values using `all`" do
|
125
|
+
subject.results = [ 1, 2, 3, 4, 5 ]
|
126
|
+
|
127
|
+
subject.limit 2
|
128
|
+
subject.offset nil
|
129
|
+
assert_equal [ 1, 2 ], subject.all
|
130
|
+
|
131
|
+
subject.limit nil
|
132
|
+
subject.offset 3
|
133
|
+
assert_equal [ 4, 5 ], subject.all
|
134
|
+
|
135
|
+
subject.limit 2
|
136
|
+
subject.offset 2
|
137
|
+
assert_equal [ 3, 4 ], subject.all
|
138
|
+
end
|
139
|
+
|
140
|
+
should "return the size of `all` using `count`" do
|
141
|
+
subject.results = [ 1, 2, 3, 4, 5 ]
|
142
|
+
assert_equal 5, subject.count
|
143
|
+
|
144
|
+
subject.limit 2
|
145
|
+
subject.offset 2
|
146
|
+
assert_equal 2, subject.count
|
147
|
+
end
|
148
|
+
|
149
|
+
should "be comparable using there applied collections" do
|
150
|
+
other_relation = Ardb::RelationSpy.new
|
151
|
+
other_relation.select :column_a
|
152
|
+
assert_not_equal other_relation, subject
|
153
|
+
|
154
|
+
subject.select :column_a
|
155
|
+
assert_equal other_relation, subject
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ardb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 35
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 15
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.15.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2013-11-
|
19
|
+
date: 2013-11-18 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- lib/ardb/cli.rb
|
106
106
|
- lib/ardb/migration_helpers.rb
|
107
107
|
- lib/ardb/record_spy.rb
|
108
|
+
- lib/ardb/relation_spy.rb
|
108
109
|
- lib/ardb/root_path.rb
|
109
110
|
- lib/ardb/runner.rb
|
110
111
|
- lib/ardb/runner/connect_command.rb
|
@@ -125,6 +126,7 @@ files:
|
|
125
126
|
- test/unit/config_tests.rb
|
126
127
|
- test/unit/migration_helpers_tests.rb
|
127
128
|
- test/unit/record_spy_tests.rb
|
129
|
+
- test/unit/relation_spy_tests.rb
|
128
130
|
- test/unit/runner/connect_command_tests.rb
|
129
131
|
- test/unit/runner/create_command_tests.rb
|
130
132
|
- test/unit/runner/drop_command_tests.rb
|
@@ -189,6 +191,7 @@ test_files:
|
|
189
191
|
- test/unit/config_tests.rb
|
190
192
|
- test/unit/migration_helpers_tests.rb
|
191
193
|
- test/unit/record_spy_tests.rb
|
194
|
+
- test/unit/relation_spy_tests.rb
|
192
195
|
- test/unit/runner/connect_command_tests.rb
|
193
196
|
- test/unit/runner/create_command_tests.rb
|
194
197
|
- test/unit/runner/drop_command_tests.rb
|