sqlpostgres 1.2.6 → 1.3.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.
Files changed (62) hide show
  1. data/Changelog.md +18 -0
  2. data/Gemfile +2 -0
  3. data/Gemfile.lock +12 -0
  4. data/README.rdoc +5 -2
  5. data/Rakefile +5 -24
  6. data/VERSION +1 -1
  7. data/lib/sqlpostgres/Connection.rb +8 -3
  8. data/lib/sqlpostgres/Cursor.rb +2 -2
  9. data/lib/sqlpostgres/Insert.rb +1 -1
  10. data/lib/sqlpostgres/PgBit.rb +1 -1
  11. data/lib/sqlpostgres/PgBox.rb +1 -1
  12. data/lib/sqlpostgres/PgCidr.rb +1 -1
  13. data/lib/sqlpostgres/PgCircle.rb +1 -1
  14. data/lib/sqlpostgres/PgInet.rb +1 -1
  15. data/lib/sqlpostgres/PgInterval.rb +1 -1
  16. data/lib/sqlpostgres/PgLineSegment.rb +1 -1
  17. data/lib/sqlpostgres/PgMacAddr.rb +1 -1
  18. data/lib/sqlpostgres/PgPath.rb +1 -1
  19. data/lib/sqlpostgres/PgPoint.rb +1 -1
  20. data/lib/sqlpostgres/PgPolygon.rb +1 -1
  21. data/lib/sqlpostgres/PgTime.rb +1 -1
  22. data/lib/sqlpostgres/PgTimeWithTimeZone.rb +1 -1
  23. data/lib/sqlpostgres/PgTimestamp.rb +1 -1
  24. data/lib/sqlpostgres/PgTwoPoints.rb +1 -1
  25. data/lib/sqlpostgres/PgWrapper.rb +1 -1
  26. data/lib/sqlpostgres/Select.rb +25 -25
  27. data/lib/sqlpostgres/Translate.rb +7 -29
  28. data/lib/sqlpostgres/Update.rb +1 -1
  29. data/rake_tasks/db.rake +17 -0
  30. data/rake_tasks/default.rake +1 -0
  31. data/rake_tasks/jeweler.rake +18 -0
  32. data/rake_tasks/test.rake +2 -0
  33. data/rake_tasks/test_spec.rake +3 -0
  34. data/rake_tasks/test_unit.rake +4 -0
  35. data/spec/Translate_spec.rb +533 -0
  36. data/spec/config/.gitignore +1 -0
  37. data/spec/config/config.yml +10 -0
  38. data/spec/config/database.yml.template +6 -0
  39. data/spec/connection_spec.rb +515 -0
  40. data/spec/cursor_spec.rb +288 -0
  41. data/spec/lib/database_config.rb +33 -0
  42. data/spec/lib/database_server.rb +42 -0
  43. data/spec/lib/postgres_template.rb +60 -0
  44. data/spec/lib/target_database_servers.rb +55 -0
  45. data/spec/lib/temporary_table.rb +45 -0
  46. data/spec/lib/test_config.rb +24 -0
  47. data/spec/lib/test_connection.rb +29 -0
  48. data/spec/lib/test_database.rb +57 -0
  49. data/spec/roundtrip_spec.rb +582 -0
  50. data/spec/spec_helper.rb +10 -0
  51. data/spec/support/all_characters.rb +18 -0
  52. data/spec/support/clear_default_connection.rb +5 -0
  53. data/spec/support/temporary_table.rb +24 -0
  54. data/spec/support/test_connections.rb +10 -0
  55. data/sqlpostgres.gemspec +35 -4
  56. data/test/Connection.test.rb +7 -5
  57. data/test/Select.test.rb +1 -1
  58. data/test/TestConfig.rb +9 -0
  59. data/test/TestUtil.rb +17 -3
  60. metadata +66 -9
  61. data/test/Translate.test.rb +0 -354
  62. data/test/roundtrip.test.rb +0 -565
@@ -0,0 +1 @@
1
+ database.yml
@@ -0,0 +1,10 @@
1
+ encodings:
2
+ -
3
+ database_encoding: sql_ascii
4
+ client_encodings:
5
+ - sql_ascii
6
+ -
7
+ database_encoding: unicode
8
+ client_encodings:
9
+ - sql_ascii
10
+ - unicode
@@ -0,0 +1,6 @@
1
+ postgres_18:
2
+ host: localhost
3
+ port: 5433
4
+ postgres_19:
5
+ host: localhost
6
+ port: 5432
@@ -0,0 +1,515 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ module SqlPostgres
4
+
5
+ describe Connection do
6
+
7
+ let(:pg_connection) {mock PG}
8
+
9
+ describe 'host_name' do
10
+
11
+ before(:each) do
12
+ PG.should_receive(:connect)\
13
+ .with(expected_host_name,
14
+ anything,
15
+ anything,
16
+ anything,
17
+ anything,
18
+ anything,
19
+ anything)\
20
+ .and_return(pg_connection)
21
+ end
22
+
23
+ def make_connection
24
+ Connection.new('host_name' => host_name)
25
+ end
26
+
27
+ context '(default)' do
28
+ let(:host_name) {nil}
29
+ let(:expected_host_name) {'localhost'}
30
+ specify {make_connection}
31
+ end
32
+
33
+ context '(supplied)' do
34
+ let(:host_name) {'somehost'}
35
+ let(:expected_host_name) {host_name}
36
+ specify {make_connection}
37
+ end
38
+
39
+ end
40
+
41
+ describe 'port' do
42
+
43
+ before(:each) do
44
+ PG.should_receive(:connect)\
45
+ .with(anything,
46
+ expected_port,
47
+ anything,
48
+ anything,
49
+ anything,
50
+ anything,
51
+ anything)\
52
+ .and_return(pg_connection)
53
+ end
54
+
55
+ def make_connection
56
+ Connection.new('port' => port)
57
+ end
58
+
59
+ context '(default)' do
60
+ let(:port) {nil}
61
+ let(:expected_port) {5432}
62
+ specify {make_connection}
63
+ end
64
+
65
+ context '(supplied)' do
66
+ let(:port) {1234}
67
+ let(:expected_port) {port}
68
+ specify {make_connection}
69
+ end
70
+
71
+ end
72
+
73
+ describe 'options' do
74
+
75
+ before(:each) do
76
+ PG.should_receive(:connect)\
77
+ .with(anything,
78
+ anything,
79
+ expected_options,
80
+ anything,
81
+ anything,
82
+ anything,
83
+ anything)\
84
+ .and_return(pg_connection)
85
+ end
86
+
87
+ def make_connection
88
+ Connection.new('options' => options)
89
+ end
90
+
91
+ context '(default)' do
92
+ let(:options) {nil}
93
+ let(:expected_options) {''}
94
+ specify {make_connection}
95
+ end
96
+
97
+ context '(supplied)' do
98
+ let(:options) {'back end options'}
99
+ let(:expected_options) {options}
100
+ specify {make_connection}
101
+ end
102
+
103
+ end
104
+
105
+ describe 'tty' do
106
+
107
+ before(:each) do
108
+ PG.should_receive(:connect)\
109
+ .with(anything,
110
+ anything,
111
+ anything,
112
+ expected_tty,
113
+ anything,
114
+ anything,
115
+ anything)\
116
+ .and_return(pg_connection)
117
+ end
118
+
119
+ def make_connection
120
+ Connection.new('tty' => tty)
121
+ end
122
+
123
+ context '(default)' do
124
+ let(:tty) {nil}
125
+ let(:expected_tty) {''}
126
+ specify {make_connection}
127
+ end
128
+
129
+ context '(supplied)' do
130
+ let(:tty) {'tty name'}
131
+ let(:expected_tty) {tty}
132
+ specify {make_connection}
133
+ end
134
+
135
+ end
136
+
137
+ describe 'db_name' do
138
+
139
+ before(:each) do
140
+ PG.should_receive(:connect)\
141
+ .with(anything,
142
+ anything,
143
+ anything,
144
+ anything,
145
+ expected_db_name,
146
+ anything,
147
+ anything)\
148
+ .and_return(pg_connection)
149
+ end
150
+
151
+ def make_connection
152
+ Connection.new('db_name' => db_name)
153
+ end
154
+
155
+ context '(default)' do
156
+ let(:db_name) {nil}
157
+ let(:expected_db_name) {''}
158
+ specify {make_connection}
159
+ end
160
+
161
+ context '(supplied)' do
162
+ let(:db_name) {'somedatabase'}
163
+ let(:expected_db_name) {db_name}
164
+ specify {make_connection}
165
+ end
166
+
167
+ end
168
+
169
+ describe 'login' do
170
+
171
+ before(:each) do
172
+ PG.should_receive(:connect)\
173
+ .with(anything,
174
+ anything,
175
+ anything,
176
+ anything,
177
+ anything,
178
+ expected_login,
179
+ anything)\
180
+ .and_return(pg_connection)
181
+ end
182
+
183
+ def make_connection
184
+ Connection.new('login' => login)
185
+ end
186
+
187
+ context '(default)' do
188
+ let(:login) {nil}
189
+ let(:expected_login) {nil}
190
+ specify {make_connection}
191
+ end
192
+
193
+ context '(supplied)' do
194
+ let(:login) {'someuser'}
195
+ let(:expected_login) {login}
196
+ specify {make_connection}
197
+ end
198
+
199
+ end
200
+
201
+ describe 'password' do
202
+
203
+ before(:each) do
204
+ PG.should_receive(:connect)\
205
+ .with(anything,
206
+ anything,
207
+ anything,
208
+ anything,
209
+ anything,
210
+ anything,
211
+ expected_password)\
212
+ .and_return(pg_connection)
213
+ end
214
+
215
+ def make_connection
216
+ Connection.new('password' => password)
217
+ end
218
+
219
+ context '(default)' do
220
+ let(:password) {nil}
221
+ let(:expected_password) {nil}
222
+ specify {make_connection}
223
+ end
224
+
225
+ context '(supplied)' do
226
+ let(:password) {'somepassword'}
227
+ let(:expected_password) {password}
228
+ specify {make_connection}
229
+ end
230
+
231
+ end
232
+
233
+ describe 'encoding' do
234
+
235
+ before(:each) do
236
+ PG.should_receive(:connect).and_return(pg_connection)
237
+ end
238
+
239
+ def make_connection
240
+ Connection.new('encoding' => encoding)
241
+ end
242
+
243
+ context '(default)' do
244
+ let(:encoding) {nil}
245
+ specify {make_connection}
246
+ end
247
+
248
+ context '(supplied)' do
249
+ let(:encoding) {'UNICODE'}
250
+ before(:each) do
251
+ pg_connection.should_receive(:set_client_encoding).with(encoding)
252
+ end
253
+ specify {make_connection}
254
+ end
255
+
256
+ end
257
+
258
+ describe 'statement_in_exception' do
259
+
260
+ before(:each) do
261
+ PG.should_receive(:connect).and_return(pg_connection)
262
+ end
263
+
264
+ subject do
265
+ Connection.new('statement_in_exception' => statement_in_exception)
266
+ end
267
+
268
+ context '(default)' do
269
+ let(:statement_in_exception) {nil}
270
+ its(:statement_in_exception) {should be_true}
271
+ end
272
+
273
+ context '(supplied)' do
274
+ let(:statement_in_exception) {mock 'boolean'}
275
+ its(:statement_in_exception) {should == statement_in_exception}
276
+ end
277
+
278
+ end
279
+
280
+ describe '#pgconn' do
281
+
282
+ context '(when wrapped)' do
283
+ subject {Connection.new('connection' => pg_connection)}
284
+ before(:each) do
285
+ PG.should_not_receive(:connect)
286
+ end
287
+ its(:pgconn) {should == pg_connection}
288
+ end
289
+
290
+ context '(when created)' do
291
+ subject {Connection.new}
292
+ before(:each) do
293
+ PG.should_receive(:connect).and_return(pg_connection)
294
+ end
295
+ its(:pgconn) {should == pg_connection}
296
+ end
297
+
298
+ end
299
+
300
+ describe '#close' do
301
+
302
+ subject(:connection) {Connection.new}
303
+
304
+ before(:each) do
305
+ PG.should_receive(:connect).and_return(pg_connection)
306
+ pg_connection.should_receive(:close).once
307
+ end
308
+
309
+ specify do
310
+ connection.close
311
+ connection.close
312
+ end
313
+
314
+ end
315
+
316
+ describe 'default connection' do
317
+
318
+ context '(when not set)' do
319
+ # Odd: "be_kind_of" doesn't work here.
320
+ # Connection.default.should be_kind_of(NullConnection)
321
+ Connection.default.class.should == NullConnection
322
+ end
323
+
324
+ context '(when set)' do
325
+ let(:default_connection) {mock Connection}
326
+ specify do
327
+ Connection.default = default_connection
328
+ Connection.default.should == default_connection
329
+ end
330
+ end
331
+
332
+ end
333
+
334
+ describe '.open' do
335
+
336
+ let(:args) {mock 'arguments'}
337
+ let(:connection) {mock Connection}
338
+
339
+ before(:each) do
340
+ Connection.should_receive(:new).with(args).and_return(connection)
341
+ end
342
+
343
+ shared_context 'normal close' do
344
+ before(:each) do
345
+ connection.should_receive(:close)
346
+ end
347
+ end
348
+
349
+ shared_context 'failed close' do
350
+ let(:close_exception) {[StandardError, 'failed to close']}
351
+ before(:each) do
352
+ connection.should_receive(:close).and_raise(*close_exception)
353
+ end
354
+ end
355
+
356
+ context '(block runs normally)' do
357
+
358
+ context '(normal close)' do
359
+
360
+ include_context 'normal close'
361
+
362
+ let(:block_result) {mock 'block result'}
363
+
364
+ specify do
365
+ Connection.open(args) do |yielded_connection|
366
+ yielded_connection.should eql connection
367
+ block_result
368
+ end.should == block_result
369
+ end
370
+
371
+ end
372
+
373
+ context '(failed close)' do
374
+
375
+ include_context 'failed close'
376
+
377
+ specify do
378
+ expect {
379
+ Connection.open(args) do
380
+ end
381
+ }.to raise_error *close_exception
382
+ end
383
+
384
+ end
385
+
386
+ end
387
+
388
+ context '(exception in block)' do
389
+
390
+ let(:block_exception) {[StandardError, 'failed in block']}
391
+
392
+ context '(normal close)' do
393
+
394
+ include_context 'normal close'
395
+
396
+ specify do
397
+ expect {
398
+ Connection.open(args) do
399
+ raise *block_exception
400
+ end
401
+ }.to raise_error *block_exception
402
+ end
403
+
404
+ end
405
+
406
+ context '(failed close)' do
407
+
408
+ include_context 'failed close'
409
+
410
+ specify do
411
+ expect {
412
+ Connection.open(args) do
413
+ raise *block_exception
414
+ end
415
+ }.to raise_error *block_exception
416
+ end
417
+
418
+ end
419
+
420
+ end
421
+
422
+ end
423
+
424
+ describe '#exec' do
425
+
426
+ let(:statement_in_exception) {false}
427
+ let(:statement) {'statement'}
428
+
429
+ before(:each) do
430
+ PG.should_receive(:connect).with(any_args).and_return(pg_connection)
431
+ end
432
+
433
+ subject(:connection) do
434
+ Connection.new('statement_in_exception' => statement_in_exception)
435
+ end
436
+
437
+ context '(normal)' do
438
+
439
+ let(:pgresult) {mock PG::Result}
440
+
441
+ before(:each) do
442
+ pg_connection.should_receive(:exec)\
443
+ .with(statement).and_return(pgresult)
444
+ end
445
+
446
+ specify do
447
+ connection.exec(statement).should == pgresult
448
+ end
449
+
450
+ end
451
+
452
+ context '(exception)' do
453
+
454
+ let(:message) {"query failed\n"}
455
+ let(:exception) {[PGError, message]}
456
+
457
+ before(:each) do
458
+ pg_connection.should_receive(:exec)\
459
+ .with(statement).and_raise(*exception)
460
+ end
461
+
462
+ context '(statement_in_exception = false)' do
463
+ let(:statement_in_exception) {false}
464
+ specify do
465
+ expect {
466
+ connection.exec(statement)
467
+ }.to raise_error *exception
468
+ end
469
+ end
470
+
471
+ context '(statement_in_exception = true)' do
472
+ let(:statement_in_exception) {true}
473
+ specify do
474
+ expect {
475
+ connection.exec(statement)
476
+ }.to raise_error(PGError) { |e|
477
+ e.message.should match message
478
+ e.message.should match statement.inspect
479
+ }
480
+ end
481
+ end
482
+
483
+ end
484
+
485
+ end
486
+
487
+ describe '#query' do
488
+
489
+ let(:statement) {'statement'}
490
+
491
+ before(:each) do
492
+ PG.should_receive(:connect).with(any_args).and_return(pg_connection)
493
+ end
494
+
495
+ subject(:connection) do
496
+ Connection.new
497
+ end
498
+
499
+ let(:values) {mock 'values'}
500
+ let(:pgresult) {mock PG::Result, :values => values}
501
+
502
+ before(:each) do
503
+ pg_connection.should_receive(:exec)\
504
+ .with(statement).and_return(pgresult)
505
+ end
506
+
507
+ specify do
508
+ connection.query(statement).should == values
509
+ end
510
+
511
+ end
512
+
513
+ end
514
+
515
+ end