Skip to content
This repository
Browse code

refactor, reuse assertions in range_test.rb.

  • Loading branch information...
commit 63f9a7507b0621c2b7eadc05a3f087e0d453c14f 1 parent c4044b2
Yves Senn senny authored

Showing 1 changed file with 111 additions and 186 deletions. Show diff stats Hide diff stats

  1. +111 186 activerecord/test/cases/adapters/postgresql/range_test.rb
297 activerecord/test/cases/adapters/postgresql/range_test.rb
... ... @@ -1,5 +1,3 @@
1   -# encoding: utf-8
2   -
3 1 require "cases/helper"
4 2 require 'active_record/base'
5 3 require 'active_record/connection_adapters/postgresql_adapter'
@@ -31,106 +29,47 @@ def setup
31 29 return skip "do not test on PG without range"
32 30 end
33 31
34   - @connection.execute <<-SQL
35   - INSERT INTO postgresql_ranges (
36   - id,
37   - date_range,
38   - num_range,
39   - ts_range,
40   - tstz_range,
41   - int4_range,
42   - int8_range
43   - ) VALUES (
44   - 1,
45   - '[''2012-01-02'', ''2012-01-04'']',
46   - '[0.1, 0.2]',
47   - '[''2010-01-01 14:30'', ''2011-01-01 14:30'']',
48   - '[''2010-01-01 14:30:00+05'', ''2011-01-01 14:30:00-03'']',
49   - '[1, 10]',
50   - '[10, 100]'
51   - )
52   - SQL
53   -
54   - @connection.execute <<-SQL
55   - INSERT INTO postgresql_ranges (
56   - id,
57   - date_range,
58   - num_range,
59   - ts_range,
60   - tstz_range,
61   - int4_range,
62   - int8_range
63   - ) VALUES (
64   - 2,
65   - '(''2012-01-02'', ''2012-01-04'')',
66   - '[0.1, 0.2)',
67   - '[''2010-01-01 14:30'', ''2011-01-01 14:30'')',
68   - '[''2010-01-01 14:30:00+05'', ''2011-01-01 14:30:00-03'')',
69   - '(1, 10)',
70   - '(10, 100)'
71   - )
72   - SQL
73   -
74   - @connection.execute <<-SQL
75   - INSERT INTO postgresql_ranges (
76   - id,
77   - date_range,
78   - num_range,
79   - ts_range,
80   - tstz_range,
81   - int4_range,
82   - int8_range
83   - ) VALUES (
84   - 3,
85   - '(''2012-01-02'',]',
86   - '[0.1,]',
87   - '[''2010-01-01 14:30'',]',
88   - '[''2010-01-01 14:30:00+05'',]',
89   - '(1,]',
90   - '(10,]'
91   - )
92   - SQL
93   -
94   - @connection.execute <<-SQL
95   - INSERT INTO postgresql_ranges (
96   - id,
97   - date_range,
98   - num_range,
99   - ts_range,
100   - tstz_range,
101   - int4_range,
102   - int8_range
103   - ) VALUES (
104   - 4,
105   - '[,]',
106   - '[,]',
107   - '[,]',
108   - '[,]',
109   - '[,]',
110   - '[,]'
111   - )
112   - SQL
113   -
114   - @connection.execute <<-SQL
115   - INSERT INTO postgresql_ranges (
116   - id,
117   - date_range,
118   - num_range,
119   - ts_range,
120   - tstz_range,
121   - int4_range,
122   - int8_range
123   - ) VALUES (
124   - 5,
125   - '(''2012-01-02'', ''2012-01-02'')',
126   - '(0.1, 0.1)',
127   - '(''2010-01-01 14:30'', ''2010-01-01 14:30'')',
128   - '(''2010-01-01 14:30:00+05'', ''2010-01-01 06:30:00-03'')',
129   - '(1, 1)',
130   - '(10, 10)'
131   - )
132   - SQL
133   -
  32 + insert_range(id: 1,
  33 + date_range: "[''2012-01-02'', ''2012-01-04'']",
  34 + num_range: "[0.1, 0.2]",
  35 + ts_range: "[''2010-01-01 14:30'', ''2011-01-01 14:30'']",
  36 + tstz_range: "[''2010-01-01 14:30:00+05'', ''2011-01-01 14:30:00-03'']",
  37 + int4_range: "[1, 10]",
  38 + int8_range: "[10, 100]")
  39 +
  40 + insert_range(id: 2,
  41 + date_range: "(''2012-01-02'', ''2012-01-04'')",
  42 + num_range: "[0.1, 0.2)",
  43 + ts_range: "[''2010-01-01 14:30'', ''2011-01-01 14:30'')",
  44 + tstz_range: "[''2010-01-01 14:30:00+05'', ''2011-01-01 14:30:00-03'')",
  45 + int4_range: "(1, 10)",
  46 + int8_range: "(10, 100)")
  47 +
  48 + insert_range(id: 3,
  49 + date_range: "(''2012-01-02'',]",
  50 + num_range: "[0.1,]",
  51 + ts_range: "[''2010-01-01 14:30'',]",
  52 + tstz_range: "[''2010-01-01 14:30:00+05'',]",
  53 + int4_range: "(1,]",
  54 + int8_range: "(10,]")
  55 +
  56 + insert_range(id: 4,
  57 + date_range: "[,]",
  58 + num_range: "[,]",
  59 + ts_range: "[,]",
  60 + tstz_range: "[,]",
  61 + int4_range: "[,]",
  62 + int8_range: "[,]")
  63 +
  64 + insert_range(id: 5,
  65 + date_range: "(''2012-01-02'', ''2012-01-02'')",
  66 + num_range: "(0.1, 0.1)",
  67 + ts_range: "(''2010-01-01 14:30'', ''2010-01-01 14:30'')",
  68 + tstz_range: "(''2010-01-01 14:30:00+05'', ''2010-01-01 06:30:00-03'')",
  69 + int4_range: "(1, 1)",
  70 + int8_range: "(10, 10)")
  71 +
  72 + @new_range = PostgresqlRange.new
134 73 @first_range = PostgresqlRange.find(1)
135 74 @second_range = PostgresqlRange.find(2)
136 75 @third_range = PostgresqlRange.find(3)
@@ -196,125 +135,111 @@ def test_tstzrange_values
196 135
197 136 def test_create_tstzrange
198 137 tstzrange = Time.parse('2010-01-01 14:30:00 +0100')...Time.parse('2011-02-02 14:30:00 CDT')
199   - range = PostgresqlRange.new(:tstz_range => tstzrange)
200   - assert range.save
201   - assert range.reload
202   - assert_equal range.tstz_range, tstzrange
203   - assert_equal range.tstz_range, Time.parse('2010-01-01 13:30:00 UTC')...Time.parse('2011-02-02 19:30:00 UTC')
  138 + round_trip(@new_range, :tstz_range, tstzrange)
  139 + assert_equal @new_range.tstz_range, tstzrange
  140 + assert_equal @new_range.tstz_range, Time.parse('2010-01-01 13:30:00 UTC')...Time.parse('2011-02-02 19:30:00 UTC')
204 141 end
205 142
206 143 def test_update_tstzrange
207   - new_tstzrange = Time.parse('2010-01-01 14:30:00 CDT')...Time.parse('2011-02-02 14:30:00 CET')
208   - @first_range.tstz_range = new_tstzrange
209   - assert @first_range.save
210   - assert @first_range.reload
211   - assert_equal new_tstzrange, @first_range.tstz_range
212   - @first_range.tstz_range = Time.parse('2010-01-01 14:30:00 +0100')...Time.parse('2010-01-01 13:30:00 +0000')
213   - assert @first_range.save
214   - assert @first_range.reload
215   - assert_nil @first_range.tstz_range
  144 + assert_equal_round_trip(@first_range, :tstz_range,
  145 + Time.parse('2010-01-01 14:30:00 CDT')...Time.parse('2011-02-02 14:30:00 CET'))
  146 + assert_nil_round_trip(@first_range, :tstz_range,
  147 + Time.parse('2010-01-01 14:30:00 +0100')...Time.parse('2010-01-01 13:30:00 +0000'))
216 148 end
217 149
218 150 def test_create_tsrange
219 151 tz = ::ActiveRecord::Base.default_timezone
220   - tsrange = Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2011, 2, 2, 14, 30, 0)
221   - range = PostgresqlRange.new(:ts_range => tsrange)
222   - assert range.save
223   - assert range.reload
224   - assert_equal range.ts_range, tsrange
  152 + assert_equal_round_trip(@new_range, :ts_range,
  153 + Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2011, 2, 2, 14, 30, 0))
225 154 end
226 155
227 156 def test_update_tsrange
228 157 tz = ::ActiveRecord::Base.default_timezone
229   - new_tsrange = Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2011, 2, 2, 14, 30, 0)
230   - @first_range.ts_range = new_tsrange
231   - assert @first_range.save
232   - assert @first_range.reload
233   - assert_equal new_tsrange, @first_range.ts_range
234   - @first_range.ts_range = Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2010, 1, 1, 14, 30, 0)
235   - assert @first_range.save
236   - assert @first_range.reload
237   - assert_nil @first_range.ts_range
  158 + assert_equal_round_trip(@first_range, :ts_range,
  159 + Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2011, 2, 2, 14, 30, 0))
  160 + assert_nil_round_trip(@first_range, :ts_range,
  161 + Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2010, 1, 1, 14, 30, 0))
238 162 end
239 163
240 164 def test_create_numrange
241   - numrange = BigDecimal.new('0.5')...BigDecimal.new('1')
242   - range = PostgresqlRange.new(:num_range => numrange)
243   - assert range.save
244   - assert range.reload
245   - assert_equal range.num_range, numrange
  165 + assert_equal_round_trip(@new_range, :num_range,
  166 + BigDecimal.new('0.5')...BigDecimal.new('1'))
246 167 end
247 168
248 169 def test_update_numrange
249   - new_numrange = BigDecimal.new('0.5')...BigDecimal.new('1')
250   - @first_range.num_range = new_numrange
251   - assert @first_range.save
252   - assert @first_range.reload
253   - assert_equal new_numrange, @first_range.num_range
254   - @first_range.num_range = BigDecimal.new('0.5')...BigDecimal.new('0.5')
255   - assert @first_range.save
256   - assert @first_range.reload
257   - assert_nil @first_range.num_range
  170 + assert_equal_round_trip(@first_range, :num_range,
  171 + BigDecimal.new('0.5')...BigDecimal.new('1'))
  172 + assert_nil_round_trip(@first_range, :num_range,
  173 + BigDecimal.new('0.5')...BigDecimal.new('0.5'))
258 174 end
259 175
260 176 def test_create_daterange
261   - daterange = Range.new(Date.new(2012, 1, 1), Date.new(2013, 1, 1), true)
262   - range = PostgresqlRange.new(:date_range => daterange)
263   - assert range.save
264   - assert range.reload
265   - assert_equal range.date_range, daterange
  177 + assert_equal_round_trip(@new_range, :date_range,
  178 + Range.new(Date.new(2012, 1, 1), Date.new(2013, 1, 1), true))
266 179 end
267 180
268 181 def test_update_daterange
269   - new_daterange = Date.new(2012, 2, 3)...Date.new(2012, 2, 10)
270   - @first_range.date_range = new_daterange
271   - assert @first_range.save
272   - assert @first_range.reload
273   - assert_equal new_daterange, @first_range.date_range
274   - @first_range.date_range = Date.new(2012, 2, 3)...Date.new(2012, 2, 3)
275   - assert @first_range.save
276   - assert @first_range.reload
277   - assert_nil @first_range.date_range
  182 + assert_equal_round_trip(@first_range, :date_range,
  183 + Date.new(2012, 2, 3)...Date.new(2012, 2, 10))
  184 + assert_nil_round_trip(@first_range, :date_range,
  185 + Date.new(2012, 2, 3)...Date.new(2012, 2, 3))
278 186 end
279 187
280 188 def test_create_int4range
281   - int4range = Range.new(3, 50, true)
282   - range = PostgresqlRange.new(:int4_range => int4range)
283   - assert range.save
284   - assert range.reload
285   - assert_equal range.int4_range, int4range
  189 + assert_equal_round_trip(@new_range, :int4_range, Range.new(3, 50, true))
286 190 end
287 191
288 192 def test_update_int4range
289   - new_int4range = 6...10
290   - @first_range.int4_range = new_int4range
291   - assert @first_range.save
292   - assert @first_range.reload
293   - assert_equal new_int4range, @first_range.int4_range
294   - @first_range.int4_range = 3...3
295   - assert @first_range.save
296   - assert @first_range.reload
297   - assert_nil @first_range.int4_range
  193 + assert_equal_round_trip(@first_range, :int4_range, 6...10)
  194 + assert_nil_round_trip(@first_range, :int4_range, 3...3)
298 195 end
299 196
300 197 def test_create_int8range
301   - int8range = Range.new(30, 50, true)
302   - range = PostgresqlRange.new(:int8_range => int8range)
303   - assert range.save
304   - assert range.reload
305   - assert_equal range.int8_range, int8range
  198 + assert_equal_round_trip(@new_range, :int8_range, Range.new(30, 50, true))
306 199 end
307 200
308 201 def test_update_int8range
309   - new_int8range = 60000...10000000
310   - @first_range.int8_range = new_int8range
311   - assert @first_range.save
312   - assert @first_range.reload
313   - assert_equal new_int8range, @first_range.int8_range
314   - @first_range.int8_range = 39999...39999
315   - assert @first_range.save
316   - assert @first_range.reload
317   - assert_nil @first_range.int8_range
  202 + assert_equal_round_trip(@first_range, :int8_range, 60000...10000000)
  203 + assert_nil_round_trip(@first_range, :int8_range, 39999...39999)
318 204 end
  205 +
  206 + private
  207 + def assert_equal_round_trip(range, attribute, value)
  208 + round_trip(range, attribute, value)
  209 + assert_equal value, range.public_send(attribute)
  210 + end
  211 +
  212 + def assert_nil_round_trip(range, attribute, value)
  213 + round_trip(range, attribute, value)
  214 + assert_nil range.public_send(attribute)
  215 + end
  216 +
  217 + def round_trip(range, attribute, value)
  218 + range.public_send "#{attribute}=", value
  219 + assert range.save
  220 + assert range.reload
  221 + end
  222 +
  223 + def insert_range(values)
  224 + @connection.execute <<-SQL
  225 + INSERT INTO postgresql_ranges (
  226 + id,
  227 + date_range,
  228 + num_range,
  229 + ts_range,
  230 + tstz_range,
  231 + int4_range,
  232 + int8_range
  233 + ) VALUES (
  234 + #{values[:id]},
  235 + '#{values[:date_range]}',
  236 + '#{values[:num_range]}',
  237 + '#{values[:ts_range]}',
  238 + '#{values[:tstz_range]}',
  239 + '#{values[:int4_range]}',
  240 + '#{values[:int8_range]}'
  241 + )
  242 + SQL
  243 + end
319 244 end
320 245 end

0 comments on commit 63f9a75

Please sign in to comment.
Something went wrong with that request. Please try again.