sqlpostgres 1.2.6 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
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,10 @@
1
+ require File.expand_path('../lib/sqlpostgres.rb', File.dirname(__FILE__))
2
+
3
+ [
4
+ 'support/**/*.rb',
5
+ ].each do |relative_glob|
6
+ absolute_glob = File.expand_path(relative_glob, File.dirname(__FILE__))
7
+ Dir[absolute_glob].each do |path|
8
+ require path
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+
2
+ shared_context "AllCharacters" do
3
+
4
+ def self.allCharactersExceptNull
5
+ allCharacters(1)
6
+ end
7
+
8
+ def self.allCharacters(floor = 0)
9
+ s = (floor..255).to_a.collect do |i|
10
+ i.chr
11
+ end.join
12
+ if s.respond_to?(:force_encoding)
13
+ s = s.force_encoding('ASCII-8BIT')
14
+ end
15
+ s
16
+ end
17
+
18
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.configure do |config|
2
+ config.after(:each) do
3
+ SqlPostgres::Connection.default = nil
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../lib/temporary_table',
2
+ File.dirname(__FILE__))
3
+
4
+ RSpec.configure do |config|
5
+
6
+ shared_context 'temporary table' do |args|
7
+
8
+ def set_message_level(level)
9
+ prior = connection.exec("show client_min_messages").first['client_min_messages']
10
+ connection.exec("set client_min_messages = '#{level}'")
11
+ ensure
12
+ connection.exec("set client_min_messages = '#{prior}'")
13
+ end
14
+
15
+ around(:each) do |block|
16
+ args = args.merge(:connection => connection)
17
+ set_message_level('warning') do
18
+ TestSupport::TemporaryTable.create(args, &block)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path('../lib/target_database_servers',
2
+ File.dirname(__FILE__))
3
+
4
+ def test_connections
5
+ TestSupport::TargetDatabaseServers.instance.test_connections
6
+ end
7
+
8
+ def test_connection
9
+ TestSupport::TargetDatabaseServers.instance.test_connection
10
+ end
data/sqlpostgres.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "sqlpostgres"
8
- s.version = "1.2.6"
8
+ s.version = "1.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Wayne Conrad"]
12
- s.date = "2013-02-18"
12
+ s.date = "2013-02-26"
13
13
  s.description = "A mini-language for building and executing SQL statements against a postgresql database. This is a very old library, pre-dating active record and lacking many of its refinments. New projects will probably not want to use it."
14
14
  s.email = "wconrad@yagni.com"
15
15
  s.extra_rdoc_files = [
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
+ "Changelog.md",
20
21
  "Gemfile",
21
22
  "Gemfile.lock",
22
23
  "LICENSE.md",
@@ -121,6 +122,32 @@ Gem::Specification.new do |s|
121
122
  "lib/sqlpostgres/Transaction.rb",
122
123
  "lib/sqlpostgres/Translate.rb",
123
124
  "lib/sqlpostgres/Update.rb",
125
+ "rake_tasks/db.rake",
126
+ "rake_tasks/default.rake",
127
+ "rake_tasks/jeweler.rake",
128
+ "rake_tasks/test.rake",
129
+ "rake_tasks/test_spec.rake",
130
+ "rake_tasks/test_unit.rake",
131
+ "spec/Translate_spec.rb",
132
+ "spec/config/.gitignore",
133
+ "spec/config/config.yml",
134
+ "spec/config/database.yml.template",
135
+ "spec/connection_spec.rb",
136
+ "spec/cursor_spec.rb",
137
+ "spec/lib/database_config.rb",
138
+ "spec/lib/database_server.rb",
139
+ "spec/lib/postgres_template.rb",
140
+ "spec/lib/target_database_servers.rb",
141
+ "spec/lib/temporary_table.rb",
142
+ "spec/lib/test_config.rb",
143
+ "spec/lib/test_connection.rb",
144
+ "spec/lib/test_database.rb",
145
+ "spec/roundtrip_spec.rb",
146
+ "spec/spec_helper.rb",
147
+ "spec/support/all_characters.rb",
148
+ "spec/support/clear_default_connection.rb",
149
+ "spec/support/temporary_table.rb",
150
+ "spec/support/test_connections.rb",
124
151
  "sqlpostgres.gemspec",
125
152
  "test/Assert.rb",
126
153
  "test/Connection.test.rb",
@@ -151,9 +178,7 @@ Gem::Specification.new do |s|
151
178
  "test/TestSetup.rb",
152
179
  "test/TestUtil.rb",
153
180
  "test/Transaction.test.rb",
154
- "test/Translate.test.rb",
155
181
  "test/Update.test.rb",
156
- "test/roundtrip.test.rb",
157
182
  "test/test",
158
183
  "tools/exampleinserter/ExampleInserter.rb",
159
184
  "tools/rdoc/ChangeLog",
@@ -237,16 +262,22 @@ Gem::Specification.new do |s|
237
262
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
238
263
  s.add_runtime_dependency(%q<pg>, ["~> 0.13.2"])
239
264
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
265
+ s.add_development_dependency(%q<memoizer>, ["~> 1.0.1"])
240
266
  s.add_development_dependency(%q<rake>, ["~> 10.0.3"])
267
+ s.add_development_dependency(%q<rspec>, ["~> 2.12.0"])
241
268
  else
242
269
  s.add_dependency(%q<pg>, ["~> 0.13.2"])
243
270
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
271
+ s.add_dependency(%q<memoizer>, ["~> 1.0.1"])
244
272
  s.add_dependency(%q<rake>, ["~> 10.0.3"])
273
+ s.add_dependency(%q<rspec>, ["~> 2.12.0"])
245
274
  end
246
275
  else
247
276
  s.add_dependency(%q<pg>, ["~> 0.13.2"])
248
277
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
278
+ s.add_dependency(%q<memoizer>, ["~> 1.0.1"])
249
279
  s.add_dependency(%q<rake>, ["~> 10.0.3"])
280
+ s.add_dependency(%q<rspec>, ["~> 2.12.0"])
250
281
  end
251
282
  end
252
283
 
@@ -18,9 +18,8 @@ class ConnectionTest < Test
18
18
  def test_ConnectionNewWrap
19
19
  Connection.mockPgClass do
20
20
  rawConnection = MockPGconn.new
21
- assertEquals(MockPGconn.state[:encoding], nil)
22
21
  connection = Connection.new('connection'=>rawConnection)
23
- assertEquals(rawConnection.state[:encoding], 'unicode')
22
+ assertEquals(connection.pgconn, rawConnection)
24
23
  end
25
24
  end
26
25
 
@@ -44,7 +43,8 @@ class ConnectionTest < Test
44
43
  Connection.open(testDbArgs) do |connection1|
45
44
  connection1.exec("create database #{dbName}")
46
45
  begin
47
- Connection.open('db_name'=>dbName) do |connection2|
46
+ sleep(0.1)
47
+ Connection.open(testDbArgs.merge('db_name'=>dbName)) do |connection2|
48
48
  rows = connection2.query("select current_database();")
49
49
  assertEquals(rows[0][0], dbName)
50
50
  end
@@ -85,17 +85,19 @@ class ConnectionTest < Test
85
85
  password = randomString
86
86
  options = randomString
87
87
  tty = randomString
88
+ encoding = randomString
88
89
  connection = Connection.new('host_name'=>hostName,
89
90
  'port'=>port,
90
91
  'options'=>options,
91
92
  'tty'=>tty,
92
93
  'db_name'=>dbName,
93
94
  'login'=>login,
94
- 'password'=>password)
95
+ 'password'=>password,
96
+ 'encoding'=>encoding)
95
97
  assertEquals(MockPGconn.state[:open], true)
96
98
  assertEquals(MockPGconn.state[:openArgs],
97
99
  [hostName, port, options, tty, dbName, login, password])
98
- assertEquals(MockPGconn.state[:encoding], 'unicode')
100
+ assertEquals(MockPGconn.state[:encoding], encoding)
99
101
  end
100
102
  end
101
103
 
data/test/Select.test.rb CHANGED
@@ -231,7 +231,7 @@ class SelectTest < Test
231
231
  def testUserConversion
232
232
  makeTestConnection do |connection|
233
233
  select = Select.new(connection)
234
- select.select(["%s", "abc"], 'letters') do |s|
234
+ select.select(["%s", "abc"], 'letters') do |s, server_version|
235
235
  s.scan(/./)
236
236
  end
237
237
  assertEquals(select.exec, [{'letters'=>['a', 'b', 'c']}])
data/test/TestConfig.rb CHANGED
@@ -8,10 +8,19 @@ module TestConfig
8
8
 
9
9
  TEST_DB_NAME = "sqlpostgres_test"
10
10
 
11
+ # The database port to use for testing.
12
+
13
+ TEST_DB_PORT = ENV['PGPORT'] || 5432
14
+
11
15
  # The prefix for temporary database objects.
12
16
 
13
17
  TEST_DB_PREFIX = "SQLPOSTGRES_TEST_"
14
18
 
19
+ # Encodings
20
+
21
+ TEST_CLIENT_ENCODING = 'SQL_ASCII'
22
+ TEST_DB_ENCODING = 'SQL_ASCII'
23
+
15
24
  end
16
25
 
17
26
  # Local Variables:
data/test/TestUtil.rb CHANGED
@@ -6,7 +6,11 @@ module TestUtil
6
6
  include SqlPostgres
7
7
 
8
8
  def testDbArgs
9
- {'db_name'=>TEST_DB_NAME}
9
+ {
10
+ 'db_name' => TEST_DB_NAME,
11
+ 'port'=> TEST_DB_PORT,
12
+ 'encoding' => TEST_CLIENT_ENCODING,
13
+ }
10
14
  end
11
15
 
12
16
  def testForTestDb
@@ -14,13 +18,23 @@ module TestUtil
14
18
  Connection.new(testDbArgs)
15
19
  rescue PGError => message
16
20
  puts "Creating test database"
17
- system("psql -c 'create database #{TEST_DB_NAME}' template1 > /dev/null 2>&1")
21
+ run_psql "create database #{TEST_DB_NAME} with encoding '#{TEST_DB_ENCODING}' template template0"
18
22
  end
19
23
  end
20
24
 
21
25
  def removeTestDb
22
26
  puts "Removing test database"
23
- system("psql -c 'drop database #{TEST_DB_NAME}' template1 > /dev/null 2>&1")
27
+ run_psql "drop database #{TEST_DB_NAME}"
28
+ end
29
+
30
+ def run_psql(command)
31
+ command = "psql -p #{TEST_DB_PORT} -c #{command.inspect} 2>&1"
32
+ output = `#{command}`
33
+ if $? != 0
34
+ $stderr.puts "Failed: #{command}"
35
+ $stderr.print output
36
+ exit(1)
37
+ end
24
38
  end
25
39
 
26
40
  def makeTestConnection
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlpostgres
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 2
9
- - 6
10
- version: 1.2.6
8
+ - 3
9
+ - 0
10
+ version: 1.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Wayne Conrad
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-02-18 00:00:00 Z
18
+ date: 2013-02-26 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: pg
@@ -50,10 +50,26 @@ dependencies:
50
50
  version: 1.8.4
51
51
  version_requirements: *id002
52
52
  - !ruby/object:Gem::Dependency
53
- name: rake
53
+ name: memoizer
54
54
  prerelease: false
55
55
  type: :development
56
56
  requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 21
62
+ segments:
63
+ - 1
64
+ - 0
65
+ - 1
66
+ version: 1.0.1
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: rake
70
+ prerelease: false
71
+ type: :development
72
+ requirement: &id004 !ruby/object:Gem::Requirement
57
73
  none: false
58
74
  requirements:
59
75
  - - ~>
@@ -64,7 +80,23 @@ dependencies:
64
80
  - 0
65
81
  - 3
66
82
  version: 10.0.3
67
- version_requirements: *id003
83
+ version_requirements: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ prerelease: false
87
+ type: :development
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ hash: 63
94
+ segments:
95
+ - 2
96
+ - 12
97
+ - 0
98
+ version: 2.12.0
99
+ version_requirements: *id005
68
100
  description: A mini-language for building and executing SQL statements against a postgresql database. This is a very old library, pre-dating active record and lacking many of its refinments. New projects will probably not want to use it.
69
101
  email: wconrad@yagni.com
70
102
  executables: []
@@ -75,6 +107,7 @@ extra_rdoc_files:
75
107
  - LICENSE.md
76
108
  - README.rdoc
77
109
  files:
110
+ - Changelog.md
78
111
  - Gemfile
79
112
  - Gemfile.lock
80
113
  - LICENSE.md
@@ -179,6 +212,32 @@ files:
179
212
  - lib/sqlpostgres/Transaction.rb
180
213
  - lib/sqlpostgres/Translate.rb
181
214
  - lib/sqlpostgres/Update.rb
215
+ - rake_tasks/db.rake
216
+ - rake_tasks/default.rake
217
+ - rake_tasks/jeweler.rake
218
+ - rake_tasks/test.rake
219
+ - rake_tasks/test_spec.rake
220
+ - rake_tasks/test_unit.rake
221
+ - spec/Translate_spec.rb
222
+ - spec/config/.gitignore
223
+ - spec/config/config.yml
224
+ - spec/config/database.yml.template
225
+ - spec/connection_spec.rb
226
+ - spec/cursor_spec.rb
227
+ - spec/lib/database_config.rb
228
+ - spec/lib/database_server.rb
229
+ - spec/lib/postgres_template.rb
230
+ - spec/lib/target_database_servers.rb
231
+ - spec/lib/temporary_table.rb
232
+ - spec/lib/test_config.rb
233
+ - spec/lib/test_connection.rb
234
+ - spec/lib/test_database.rb
235
+ - spec/roundtrip_spec.rb
236
+ - spec/spec_helper.rb
237
+ - spec/support/all_characters.rb
238
+ - spec/support/clear_default_connection.rb
239
+ - spec/support/temporary_table.rb
240
+ - spec/support/test_connections.rb
182
241
  - sqlpostgres.gemspec
183
242
  - test/Assert.rb
184
243
  - test/Connection.test.rb
@@ -209,9 +268,7 @@ files:
209
268
  - test/TestSetup.rb
210
269
  - test/TestUtil.rb
211
270
  - test/Transaction.test.rb
212
- - test/Translate.test.rb
213
271
  - test/Update.test.rb
214
- - test/roundtrip.test.rb
215
272
  - test/test
216
273
  - tools/exampleinserter/ExampleInserter.rb
217
274
  - tools/rdoc/ChangeLog
@@ -1,354 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require 'bundler/setup'
5
- $:.unshift(File.dirname(__FILE__))
6
- require 'TestSetup'
7
-
8
- class TranslateTest < Test
9
-
10
- include SqlPostgres
11
- include TestUtil
12
-
13
- def testEscapeSql
14
- pi = 3.1415926535897932384626433832795028841971693993751058209749445923
15
- testCases = [
16
- [nil, 'null'],
17
- ["", "E''"],
18
- ["foo", %q"E'foo'"],
19
- ["fred's", %q"E'fred\\047s'"],
20
- ['\\', %q"E'\\134'"],
21
- [Time.local(2000, 1, 2, 3, 4, 5, 6),
22
- "timestamp '2000-01-02 03:04:05.000006'"],
23
- [Time.local(1999, 12, 31, 23, 59, 59, 999999),
24
- "timestamp '1999-12-31 23:59:59.999999'"],
25
- [1, '1'],
26
- [-1, '-1'],
27
- [1.0, "1"],
28
- [-1.0, "-1"],
29
- [pi, "3.1415926535898"],
30
- [-pi, "-3.1415926535898"],
31
- [1e100, "1e+100"],
32
- [-1e-100, "-1e-100"],
33
- [true, "true"],
34
- [false, "false"],
35
- [:default, "default"],
36
- [['1 + %s', 1], '1 + 1'],
37
- [[:in, 1, 2], '(1, 2)'],
38
- [[:in, 'foo', 'bar'], "(E'foo', E'bar')"],
39
- [BigDecimal('0'), '0.0'],
40
- [BigDecimal('0.'), '0.0'],
41
- [BigDecimal('1234567890.0987654321'), '1234567890.0987654321'],
42
- [BigDecimal('0.000000000000000000000000000001'), '0.000000000000000000000000000001'],
43
- [PgTime.new(0, 0, 0), "time '00:00:00'"],
44
- [PgTime.new(23, 59, 59), "time '23:59:59'"],
45
- [
46
- PgTimeWithTimeZone.new,
47
- "time with time zone '00:00:00+00:00'"
48
- ],
49
- [
50
- PgTimeWithTimeZone.new(12, 0, 0, -8),
51
- "time with time zone '12:00:00-08:00'"
52
- ],
53
- [
54
- PgTimeWithTimeZone.new(23, 59, 59, 8),
55
- "time with time zone '23:59:59+08:00'"
56
- ],
57
- [
58
- DateTime.civil(2001, 1, 1, 0, 0, 0, Rational(7, 24)),
59
- "timestamp with time zone '2001-01-01 00:00:00.000000000+0700'",
60
- ],
61
- [Date.civil(2001, 1, 1), "date '2001-01-01'"],
62
- [
63
- PgTimestamp.new(2001, 1, 2, 12, 0, 1),
64
- "timestamp '2001-01-02 12:00:01.00000'"
65
- ],
66
- [PgInterval.new('hours'=>1), "interval '01:00'"],
67
- [PgInterval.new('days'=>1), "interval '1 day'"],
68
- [PgPoint.new(1, 2), "point '(1, 2)'"],
69
- [PgPoint.new(3, 4), "point '(3, 4)'"],
70
- [
71
- PgLineSegment.new(PgPoint.new(1, 2), PgPoint.new(3, 4)),
72
- "lseg '((1, 2), (3, 4))'"
73
- ],
74
- [
75
- PgBox.new(PgPoint.new(1, 2), PgPoint.new(3, 4)),
76
- "box '((1, 2), (3, 4))'"
77
- ],
78
- [
79
- PgPath.new(true, PgPoint.new(1, 2), PgPoint.new(3, 4)),
80
- "path '((1, 2), (3, 4))'"
81
- ],
82
- [
83
- PgPolygon.new(PgPoint.new(1, 2), PgPoint.new(3, 4)),
84
- "polygon '((1, 2), (3, 4))'"
85
- ],
86
- [["%s %s", 1, 'Fred'], "1 E'Fred'"],
87
- ]
88
- for testCase in testCases
89
- assertInfo("For test case #{testCase.inspect}") do
90
- raw, escaped = *testCase
91
- assertEquals(Translate.escape_sql(raw), escaped)
92
- end
93
- end
94
- end
95
-
96
- def test_escape_array
97
- testCases = [
98
- [nil, "null"],
99
- [[], %q"'{}'"],
100
- [[1], %q"ARRAY[1]"],
101
- [[1, 2], %q"ARRAY[1, 2]"],
102
- [['foo'], %q"ARRAY[E'foo']"],
103
- [['\\'], %q"ARRAY[E'\\134']"],
104
- [["a,b,c"], %q"ARRAY[E'a,b,c']"],
105
- [["a", "b", "c"], %q"ARRAY[E'a', E'b', E'c']"],
106
- [["\"Hello\""], %q"ARRAY[E'\"Hello\"']"],
107
- [
108
- [[0, 0], [0, 1], [1, 0], [1, 1]],
109
- "ARRAY[ARRAY[0, 0], ARRAY[0, 1], ARRAY[1, 0], ARRAY[1, 1]]"
110
- ],
111
- ]
112
- for array, escaped in testCases
113
- assertInfo("For array #{array.inspect}") do
114
- assertEquals(Translate.escape_array(array), escaped)
115
- end
116
- end
117
- end
118
-
119
- def test_escape_bytea_array
120
- testCases = [
121
- [[], "'{}'"],
122
- [["", "foo"], "'{\"\",\"foo\"}'"],
123
- ["\000\037 ", "'{\"\\\\\\\\000\037 \"}'"],
124
- [["'\\"], "'{\"\\'\\\\\\\\\\\\\\\\\"}'"],
125
- [["~\177\377"], "'{\"~\177\377\"}'"],
126
- [["\""], "'{\"\\\\\"\"}'"],
127
- [nil, "null"],
128
- ]
129
- for array, escaped in testCases
130
- assertInfo("For array #{array.inspect}") do
131
- assertEquals(Translate.escape_bytea_array(array), escaped)
132
- end
133
- end
134
- end
135
-
136
- def test_sql_to_array
137
- goodTestCases = [
138
- ["{}", []],
139
- ["{foo}", ["foo"]],
140
- ["{foo,bar,\"fool's gold\"}", ["foo", "bar", "fool's gold"]],
141
- ["{\"\\\\\"}", ["\\"]],
142
- ["{\"\\\\\",fool's}", ["\\", "fool's"]],
143
- ["{\"a,b,c\"}", ["a,b,c"]],
144
- ["{\"\\\"Hello!\\\"\"}", ["\"Hello!\""]],
145
- ["{\"\001\n\037\"}", ["\001\n\037"]],
146
- [
147
- "{{f,f},{f,t},{t,f},{t,t}}",
148
- [["f", "f"], ["f", "t"], ["t", "f"], ["t", "t"]],
149
- ],
150
- ]
151
- for sql, array in goodTestCases
152
- assertInfo("For sql #{sql.inspect}") do
153
- assertEquals(Translate.sql_to_array(sql), array)
154
- end
155
- end
156
- badTestCases = [
157
- "",
158
- "{",
159
- "{foo",
160
- "{foo}x",
161
- ]
162
- for sql in badTestCases
163
- assertInfo("For sql #{sql.inspect}") do
164
- assertException(ArgumentError, sql.inspect) do
165
- Translate.sql_to_array(sql)
166
- end
167
- end
168
- end
169
- end
170
-
171
- def test_escape_qchar
172
- test_cases = [
173
- [nil, 'null'],
174
- ["\000", %q"E'\\000'"],
175
- ["\001", %q"E'\\001'"],
176
- ["\377", %q"E'\\377'"],
177
- ["a", "E'\\141'"],
178
- ]
179
- for raw, escaped in test_cases
180
- assertInfo("For raw=#{raw.inspect}") do
181
- assertEquals(Translate.escape_qchar(raw), escaped)
182
- end
183
- end
184
- end
185
-
186
- def test_escape_bytea
187
- testCases = [
188
- [nil, 'null'],
189
- [:default, "default"],
190
- ["", "E''"],
191
- ["foo", %q"E'foo'"],
192
- ["\000\037 ", %q"E'\\\\000\\\\037 '"],
193
- ["'\\", %q"E'''\\\\\\\\'"],
194
- ["~\177\377", "E'~\\\\177\\\\377'"],
195
- ]
196
- for testCase in testCases
197
- assertInfo("For test case #{testCase.inspect}") do
198
- raw, escaped = *testCase
199
- assertEquals(Translate.escape_bytea(raw), escaped)
200
- end
201
- end
202
- end
203
-
204
- def test_unescape_bytea
205
- testCases = [
206
- ["", ""],
207
- ["abc", "abc"],
208
- ["\\\\", "\\"],
209
- ["\\001", "\001"],
210
- ["\\037", "\037"],
211
- ["\\177", "\177"],
212
- ["\\200", "\200"],
213
- ["\\377", "\377"],
214
- ["\\477", "\\477"],
215
- ["\\387", "\\387"],
216
- ["\\378", "\\378"],
217
- ["\\n", "\\n"],
218
- ["abc\\", "abc\\"],
219
- ]
220
- for testCase in testCases
221
- assertInfo("For test case #{testCase.inspect}") do
222
- escaped, raw = *testCase
223
- assertEquals(Translate.unescape_bytea(escaped), raw)
224
- end
225
- end
226
- end
227
-
228
- def test_unescape_qchar
229
- testCases = [
230
- ["", "\000"],
231
- ["\001", "\001"],
232
- ["\037", "\037"],
233
- [" ", " "],
234
- ["~", '~'],
235
- ["\277", "\277"],
236
- ["\300", "\300"],
237
- ["\377", "\377"],
238
- ]
239
- for testCase in testCases
240
- assertInfo("For test case #{testCase.inspect}") do
241
- escaped, raw = *testCase
242
- assertEquals(Translate.unescape_qchar(escaped), raw)
243
- end
244
- end
245
- end
246
-
247
- def testEscapeSql_Select
248
- select = Object.new
249
- def select.statement
250
- "foo"
251
- end
252
- assertEquals(Translate.escape_sql(select), "(foo)")
253
- end
254
-
255
- def testEscapeSql_AllCharValues
256
- tableName = testTableName("foo")
257
- Connection.open(testDbArgs) do |connection|
258
- connection.exec("begin transaction;")
259
- connection.exec("create temporary table #{tableName} "\
260
- "(i int, t text);")
261
- range = (1..255)
262
- for i in range
263
- assertInfo("For i=#{i.inspect}") do
264
- statement = ("insert into #{tableName} (i, t) "\
265
- "values (#{i}, #{Translate.escape_sql(i.chr)});")
266
- connection.query(statement)
267
- end
268
- end
269
- connection.exec("end transaction;")
270
- statement = "select i, t, length(t) from #{tableName} order by i;"
271
- rows = connection.query(statement)
272
- for i in range
273
- assertInfo("for i = #{i}") do
274
- row = rows.assoc(i.to_s)
275
- char_number, text, length = *row
276
- assertEquals(length.to_i, 1)
277
- c = Translate.unescape_text(text)
278
- assertEquals(c, i.chr)
279
- end
280
- end
281
- end
282
- end
283
-
284
- def testSubstituteValues
285
- assertEquals(Translate.substitute_values("foo"), "foo")
286
- assertEquals(Translate.substitute_values(["bar %s", 1]), "bar 1")
287
- assertEquals(Translate.substitute_values(["bar %s", "O'Malley"]),
288
- "bar E'O\\047Malley'")
289
- assertEquals(Translate.substitute_values(["%s %s", nil, 1.23]), "null 1.23")
290
- end
291
-
292
- def test_sql_to_datetime
293
- testCases = [
294
- [2003, 10, 18, 11, 30, 24, -7],
295
- [2001, 1, 1, 0, 0, 0, 0],
296
- [1970, 12, 31, 23, 59, 59, -11],
297
- ]
298
- for testCase in testCases
299
- sql = "%04d-%02d-%02d %02d:%02d:%02d%+03d" % testCase
300
- dateTime = DateTime.civil(*testCase[0..5] +
301
- [Rational(testCase[6], 24)])
302
- assertEquals(Translate.sql_to_datetime(sql), dateTime)
303
- end
304
- end
305
-
306
- def test_datetime_to_sql
307
- testCases = [
308
- [2003, 10, 18, 11, 30, 24, -7],
309
- [2001, 1, 1, 0, 0, 0, 0],
310
- [1970, 12, 31, 23, 59, 59, -11],
311
- ]
312
- for testCase in testCases
313
- sql = "%04d-%02d-%02d %02d:%02d:%02d.000000000%+03d00" % testCase
314
- dateTime = DateTime.civil(*testCase[0..5] +
315
- [Rational(testCase[6], 24)])
316
- assertEquals(Translate.datetime_to_sql(dateTime), sql)
317
- end
318
- end
319
-
320
- def test_sql_to_date
321
- testCases = [
322
- [2000, 1, 1],
323
- [1899, 12, 31],
324
- ]
325
- for testCase in testCases
326
- sql = "%04d-%02d-%02d" % testCase
327
- date = Date.civil(*testCase)
328
- assertEquals(Translate.sql_to_date(sql), date)
329
- end
330
- end
331
-
332
- def test_deep_collect
333
- testCases = [
334
- ["1", 1],
335
- [[], []],
336
- [["1"], [1]],
337
- [["1", "2"], [1, 2]],
338
- [["1", ["2", "3"], []], [1, [2, 3], []]],
339
- ]
340
- for testCase in testCases
341
- a, result = *testCase
342
- assertEquals(Translate.deep_collect(a) do |e| e.to_i end, result)
343
- end
344
- end
345
-
346
- end
347
-
348
- TranslateTest.new.run if $0 == __FILE__
349
-
350
- # Local Variables:
351
- # tab-width: 2
352
- # ruby-indent-level: 2
353
- # indent-tabs-mode: nil
354
- # End: