forked from afair/postgresql_cursor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_postgresql_cursor.rb
194 lines (169 loc) · 5.49 KB
/
test_postgresql_cursor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
################################################################################
# Before running test, set up the test db & table with:
# rake setup
# or create the database manually if your environment doesn't permit
################################################################################
require_relative 'helper'
require 'minitest/autorun'
require 'minitest/pride'
class TestPostgresqlCursor < Minitest::Test
def test_each
c = PostgreSQLCursor::Cursor.new("select * from products order by 1")
nn = 0
n = c.each { nn += 1}
assert_equal nn, n
end
def test_each_batch
c = PostgreSQLCursor::Cursor.new("select * from products order by 1")
nn = 0
n = c.each_batch { |b| nn += 1 }
assert_equal nn, n
end
def test_enumerables
assert_equal true, PostgreSQLCursor::Cursor.new("select * from products order by 1").any?
assert_equal false, PostgreSQLCursor::Cursor.new("select * from products where id<0").any?
end
def test_each_while_until
c = PostgreSQLCursor::Cursor.new("select * from products order by 1", until:true)
n = c.each { |r| r['id'].to_i > 100 }
assert_equal 101, n
c = PostgreSQLCursor::Cursor.new("select * from products order by 1", while:true)
n = c.each { |r| r['id'].to_i < 100 }
assert_equal 100, n
end
def test_each_batch_while_until
c = PostgreSQLCursor::Cursor.new("select * from products order by id asc", until: true, block_size: 50)
n = c.each_batch { |b| b.last['id'].to_i > 100 }
assert_equal 3, n
c = PostgreSQLCursor::Cursor.new("select * from products order by id asc", while: true, block_size: 50)
n = c.each_batch { |b| b.last['id'].to_i < 100 }
assert_equal 2, n
end
def test_each_array
c = PostgreSQLCursor::Cursor.new("select * from products where id = 1")
c.each_array do |ary|
assert_equal Array, ary.class
assert_equal 1, ary[0].to_i
end
end
def test_each_array_batch
c = PostgreSQLCursor::Cursor.new("select * from products where id = 1")
c.each_array_batch do |b|
assert_equal 1, b.size
ary = b.first
assert_equal Array, ary.class
assert_equal 1, ary[0].to_i
end
end
def test_relation
nn = 0
Product.where("id>0").each_row {|r| nn += 1 }
assert_equal 1000, nn
end
def test_relation_batch
nn = 0
row = nil
Product.where("id>0").each_row_batch(block_size: 100) { |b| row = b.last; nn += 1 }
assert_equal 10, nn
assert_equal Hash, row.class
nn = 0
row = nil
Product.where("id>0").each_instance_batch(block_size: 100) { |b| row = b.last; nn += 1 }
assert_equal 10, nn
assert_equal Product, row.class
end
def test_activerecord
nn = 0
row = nil
Product.each_row_by_sql("select * from products") {|r| row = r; nn += 1 }
assert_equal 1000, nn
assert_equal Hash, row.class
nn = 0
Product.each_instance_by_sql("select * from products") {|r| row = r; nn += 1 }
assert_equal 1000, nn
assert_equal Product, row.class
end
def test_activerecord_batch
nn = 0
row = nil
Product.each_row_batch_by_sql("select * from products", block_size: 100) { |b| row = b.last; nn += 1 }
assert_equal 10, nn
assert_equal Hash, row.class
nn = 0
Product.each_instance_batch_by_sql("select * from products", block_size: 100) { |b| row = b.last; nn += 1 }
assert_equal 10, nn
assert_equal Product, row.class
end
def test_exception
begin
Product.each_row_by_sql("select * from products") do |r|
raise "Oops"
end
rescue Exception => e
assert_equal e.message, 'Oops'
end
end
def test_batch_exception
Product.each_row_batch_by_sql("select * from products") do |r|
raise 'Oops'
end
rescue => e
assert_equal e.message, 'Oops'
end
def test_cursor
cursor = Product.all.each_row
assert cursor.respond_to?(:each)
r = cursor.map { |row| row["id"] }
assert_equal 1000, r.size
cursor = Product.each_row_by_sql("select * from products")
assert cursor.respond_to?(:each)
r = cursor.map { |row| row["id"] }
assert_equal 1000, r.size
end
def test_batched_cursor
cursor = Product.all.each_row_batch(block_size: 100)
assert cursor.respond_to?(:each)
b = cursor.map { |batch| batch.map { |r| r['id'] } }
assert_equal 10, b.size
cursor = Product.each_row_batch_by_sql("select * from products", block_size: 100)
assert cursor.respond_to?(:each)
b = cursor.map { |batch| batch.map { |r| r['id'] } }
assert_equal 10, b.size
end
def test_pluck
r = Product.pluck_rows(:id)
assert_equal 1000, r.size
r = Product.all.pluck_instances(:id)
assert_equal 1000, r.size
assert_equal Integer, r.first.class
end
def test_with_hold
items = 0
Product.where("id < 4") .each_instance(with_hold: true, block_size:1) do |row|
Product.transaction do
row.update(data:Time.now.to_f.to_s)
items += 1
end
end
assert_equal 3, items
end
def test_fetch_symbolize_keys
Product.transaction do
#cursor = PostgreSQLCursor::Cursor.new("select * from products order by 1")
cursor = Product.all.each_row
r = cursor.fetch
assert r.has_key?("id")
r = cursor.fetch(symbolize_keys:true)
assert r.has_key?(:id)
cursor.close
end
end
def test_bad_sql
begin
ActiveRecord::Base.each_row_by_sql('select * from bad_table') { }
raise "Did Not Raise Expected Exception"
rescue Exception => e
assert_match(/bad_table/, e.message)
end
end
end