sqlpostgres 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (207) hide show
  1. data/Gemfile +8 -0
  2. data/Gemfile.lock +22 -0
  3. data/LICENSE.md +23 -0
  4. data/README.rdoc +59 -0
  5. data/Rakefile +32 -0
  6. data/VERSION +1 -0
  7. data/doc/BUGS +2 -0
  8. data/doc/examples/README +6 -0
  9. data/doc/examples/connection.rb +16 -0
  10. data/doc/examples/connection_auto.rb +22 -0
  11. data/doc/examples/connection_ctor.rb +18 -0
  12. data/doc/examples/connection_default.rb +15 -0
  13. data/doc/examples/connection_exec.rb +18 -0
  14. data/doc/examples/connection_manual.rb +12 -0
  15. data/doc/examples/connection_wrapped_new.rb +13 -0
  16. data/doc/examples/connection_wrapped_open.rb +13 -0
  17. data/doc/examples/cursor.rb +38 -0
  18. data/doc/examples/include_module.rb +9 -0
  19. data/doc/examples/include_module2.rb +12 -0
  20. data/doc/examples/insert.rb +30 -0
  21. data/doc/examples/insert2.rb +36 -0
  22. data/doc/examples/insert_bytea.rb +16 -0
  23. data/doc/examples/insert_bytea_array.rb +17 -0
  24. data/doc/examples/insert_default_values.rb +16 -0
  25. data/doc/examples/insert_insert.rb +16 -0
  26. data/doc/examples/insert_insert_default.rb +16 -0
  27. data/doc/examples/insert_insert_select.rb +20 -0
  28. data/doc/examples/insert_select.rb +20 -0
  29. data/doc/examples/interval.rb +17 -0
  30. data/doc/examples/savepoint.rb +38 -0
  31. data/doc/examples/select.rb +33 -0
  32. data/doc/examples/select2.rb +36 -0
  33. data/doc/examples/select_cross_join.rb +18 -0
  34. data/doc/examples/select_distinct.rb +18 -0
  35. data/doc/examples/select_distinct_on +19 -0
  36. data/doc/examples/select_for_update.rb +18 -0
  37. data/doc/examples/select_from.rb +17 -0
  38. data/doc/examples/select_from_subselect.rb +20 -0
  39. data/doc/examples/select_group_by.rb +19 -0
  40. data/doc/examples/select_having.rb +20 -0
  41. data/doc/examples/select_join_on.rb +18 -0
  42. data/doc/examples/select_join_using.rb +18 -0
  43. data/doc/examples/select_limit.rb +19 -0
  44. data/doc/examples/select_natural_join.rb +18 -0
  45. data/doc/examples/select_offset.rb +19 -0
  46. data/doc/examples/select_order_by.rb +20 -0
  47. data/doc/examples/select_select.rb +30 -0
  48. data/doc/examples/select_select_alias.rb +30 -0
  49. data/doc/examples/select_select_expression.rb +31 -0
  50. data/doc/examples/select_select_literal.rb +24 -0
  51. data/doc/examples/select_union.rb +21 -0
  52. data/doc/examples/select_where_array.rb +18 -0
  53. data/doc/examples/select_where_in.rb +18 -0
  54. data/doc/examples/select_where_string.rb +18 -0
  55. data/doc/examples/simple.rb +34 -0
  56. data/doc/examples/transaction.rb +30 -0
  57. data/doc/examples/transaction_abort.rb +30 -0
  58. data/doc/examples/transaction_commit.rb +34 -0
  59. data/doc/examples/translate_substitute_values.rb +17 -0
  60. data/doc/examples/update.rb +32 -0
  61. data/doc/examples/update2.rb +44 -0
  62. data/doc/examples/update_only.rb +17 -0
  63. data/doc/examples/update_set.rb +17 -0
  64. data/doc/examples/update_set_array.rb +16 -0
  65. data/doc/examples/update_set_bytea.rb +16 -0
  66. data/doc/examples/update_set_expression.rb +16 -0
  67. data/doc/examples/update_set_subselect.rb +20 -0
  68. data/doc/examples/update_where.rb +17 -0
  69. data/doc/examples/use_prefix.rb +8 -0
  70. data/doc/examples/use_prefix2.rb +11 -0
  71. data/doc/index.html +31 -0
  72. data/doc/insertexamples.rb +9 -0
  73. data/doc/makemanual +4 -0
  74. data/doc/makerdoc +5 -0
  75. data/doc/manual.dbk +622 -0
  76. data/lib/sqlpostgres/Connection.rb +198 -0
  77. data/lib/sqlpostgres/Cursor.rb +157 -0
  78. data/lib/sqlpostgres/Delete.rb +67 -0
  79. data/lib/sqlpostgres/Exceptions.rb +15 -0
  80. data/lib/sqlpostgres/Insert.rb +279 -0
  81. data/lib/sqlpostgres/NullConnection.rb +22 -0
  82. data/lib/sqlpostgres/PgBit.rb +73 -0
  83. data/lib/sqlpostgres/PgBox.rb +37 -0
  84. data/lib/sqlpostgres/PgCidr.rb +21 -0
  85. data/lib/sqlpostgres/PgCircle.rb +75 -0
  86. data/lib/sqlpostgres/PgInet.rb +21 -0
  87. data/lib/sqlpostgres/PgInterval.rb +208 -0
  88. data/lib/sqlpostgres/PgLineSegment.rb +37 -0
  89. data/lib/sqlpostgres/PgMacAddr.rb +21 -0
  90. data/lib/sqlpostgres/PgPath.rb +64 -0
  91. data/lib/sqlpostgres/PgPoint.rb +65 -0
  92. data/lib/sqlpostgres/PgPolygon.rb +56 -0
  93. data/lib/sqlpostgres/PgTime.rb +77 -0
  94. data/lib/sqlpostgres/PgTimeWithTimeZone.rb +98 -0
  95. data/lib/sqlpostgres/PgTimestamp.rb +93 -0
  96. data/lib/sqlpostgres/PgTwoPoints.rb +54 -0
  97. data/lib/sqlpostgres/PgType.rb +34 -0
  98. data/lib/sqlpostgres/PgWrapper.rb +41 -0
  99. data/lib/sqlpostgres/Savepoint.rb +98 -0
  100. data/lib/sqlpostgres/Select.rb +855 -0
  101. data/lib/sqlpostgres/Transaction.rb +120 -0
  102. data/lib/sqlpostgres/Translate.rb +436 -0
  103. data/lib/sqlpostgres/Update.rb +188 -0
  104. data/lib/sqlpostgres.rb +67 -0
  105. data/test/Assert.rb +72 -0
  106. data/test/Connection.test.rb +246 -0
  107. data/test/Cursor.test.rb +190 -0
  108. data/test/Delete.test.rb +68 -0
  109. data/test/Insert.test.rb +123 -0
  110. data/test/MockPGconn.rb +62 -0
  111. data/test/NullConnection.test.rb +32 -0
  112. data/test/PgBit.test.rb +98 -0
  113. data/test/PgBox.test.rb +108 -0
  114. data/test/PgCidr.test.rb +61 -0
  115. data/test/PgCircle.test.rb +107 -0
  116. data/test/PgInet.test.rb +61 -0
  117. data/test/PgInterval.test.rb +180 -0
  118. data/test/PgLineSegment.test.rb +108 -0
  119. data/test/PgMacAddr.test.rb +61 -0
  120. data/test/PgPath.test.rb +106 -0
  121. data/test/PgPoint.test.rb +100 -0
  122. data/test/PgPolygon.test.rb +95 -0
  123. data/test/PgTime.test.rb +120 -0
  124. data/test/PgTimeWithTimeZone.test.rb +117 -0
  125. data/test/PgTimestamp.test.rb +134 -0
  126. data/test/RandomThings.rb +25 -0
  127. data/test/Savepoint.test.rb +286 -0
  128. data/test/Select.test.rb +930 -0
  129. data/test/Test.rb +62 -0
  130. data/test/TestConfig.rb +21 -0
  131. data/test/TestSetup.rb +13 -0
  132. data/test/TestUtil.rb +92 -0
  133. data/test/Transaction.test.rb +275 -0
  134. data/test/Translate.test.rb +354 -0
  135. data/test/Update.test.rb +227 -0
  136. data/test/roundtrip.test.rb +565 -0
  137. data/test/test +34 -0
  138. data/tools/exampleinserter/ExampleInserter.rb +177 -0
  139. data/tools/rdoc/ChangeLog +796 -0
  140. data/tools/rdoc/EXAMPLE.rb +48 -0
  141. data/tools/rdoc/MANIFEST +58 -0
  142. data/tools/rdoc/Makefile +27 -0
  143. data/tools/rdoc/NEW_FEATURES +226 -0
  144. data/tools/rdoc/README +390 -0
  145. data/tools/rdoc/ToDo +6 -0
  146. data/tools/rdoc/contrib/Index +6 -0
  147. data/tools/rdoc/contrib/xslfo/ChangeLog +181 -0
  148. data/tools/rdoc/contrib/xslfo/README +106 -0
  149. data/tools/rdoc/contrib/xslfo/TODO +10 -0
  150. data/tools/rdoc/contrib/xslfo/convert.xsl +151 -0
  151. data/tools/rdoc/contrib/xslfo/demo/README +21 -0
  152. data/tools/rdoc/contrib/xslfo/demo/rdocfo +99 -0
  153. data/tools/rdoc/contrib/xslfo/fcm.xsl +54 -0
  154. data/tools/rdoc/contrib/xslfo/files.xsl +62 -0
  155. data/tools/rdoc/contrib/xslfo/labeled-lists.xsl +66 -0
  156. data/tools/rdoc/contrib/xslfo/lists.xsl +44 -0
  157. data/tools/rdoc/contrib/xslfo/modules.xsl +152 -0
  158. data/tools/rdoc/contrib/xslfo/rdoc.xsl +75 -0
  159. data/tools/rdoc/contrib/xslfo/source.xsl +66 -0
  160. data/tools/rdoc/contrib/xslfo/styles.xsl +69 -0
  161. data/tools/rdoc/contrib/xslfo/tables.xsl +67 -0
  162. data/tools/rdoc/contrib/xslfo/utils.xsl +21 -0
  163. data/tools/rdoc/debian/changelog +33 -0
  164. data/tools/rdoc/debian/compat +1 -0
  165. data/tools/rdoc/debian/control +20 -0
  166. data/tools/rdoc/debian/copyright +10 -0
  167. data/tools/rdoc/debian/dirs +2 -0
  168. data/tools/rdoc/debian/docs +2 -0
  169. data/tools/rdoc/debian/rdoc.1 +252 -0
  170. data/tools/rdoc/debian/rdoc.manpages +1 -0
  171. data/tools/rdoc/debian/rdoc.pod +149 -0
  172. data/tools/rdoc/debian/rules +9 -0
  173. data/tools/rdoc/dot/dot.rb +255 -0
  174. data/tools/rdoc/etc/rdoc.dtd +203 -0
  175. data/tools/rdoc/install.rb +137 -0
  176. data/tools/rdoc/markup/install.rb +43 -0
  177. data/tools/rdoc/markup/sample/sample.rb +42 -0
  178. data/tools/rdoc/markup/simple_markup/fragments.rb +323 -0
  179. data/tools/rdoc/markup/simple_markup/inline.rb +348 -0
  180. data/tools/rdoc/markup/simple_markup/lines.rb +147 -0
  181. data/tools/rdoc/markup/simple_markup/preprocess.rb +68 -0
  182. data/tools/rdoc/markup/simple_markup/to_html.rb +281 -0
  183. data/tools/rdoc/markup/simple_markup.rb +474 -0
  184. data/tools/rdoc/markup/test/AllTests.rb +2 -0
  185. data/tools/rdoc/markup/test/TestInline.rb +151 -0
  186. data/tools/rdoc/markup/test/TestParse.rb +411 -0
  187. data/tools/rdoc/rdoc/code_objects.rb +536 -0
  188. data/tools/rdoc/rdoc/diagram.rb +331 -0
  189. data/tools/rdoc/rdoc/generators/chm_generator.rb +112 -0
  190. data/tools/rdoc/rdoc/generators/html_generator.rb +1268 -0
  191. data/tools/rdoc/rdoc/generators/template/chm/chm.rb +86 -0
  192. data/tools/rdoc/rdoc/generators/template/html/html.rb +705 -0
  193. data/tools/rdoc/rdoc/generators/template/html/kilmer.rb +377 -0
  194. data/tools/rdoc/rdoc/generators/template/xml/rdf.rb +110 -0
  195. data/tools/rdoc/rdoc/generators/template/xml/xml.rb +110 -0
  196. data/tools/rdoc/rdoc/generators/xml_generator.rb +130 -0
  197. data/tools/rdoc/rdoc/options.rb +451 -0
  198. data/tools/rdoc/rdoc/parsers/parse_c.rb +287 -0
  199. data/tools/rdoc/rdoc/parsers/parse_f95.rb +118 -0
  200. data/tools/rdoc/rdoc/parsers/parse_rb.rb +2311 -0
  201. data/tools/rdoc/rdoc/parsers/parse_simple.rb +37 -0
  202. data/tools/rdoc/rdoc/parsers/parserfactory.rb +75 -0
  203. data/tools/rdoc/rdoc/rdoc.rb +219 -0
  204. data/tools/rdoc/rdoc/template.rb +234 -0
  205. data/tools/rdoc/rdoc/tokenstream.rb +25 -0
  206. data/tools/rdoc/rdoc.rb +9 -0
  207. metadata +291 -0
@@ -0,0 +1,190 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'TestSetup'
7
+
8
+ require 'MockPGconn'
9
+
10
+ class CursorTest < Test
11
+
12
+ include SqlPostgres
13
+ include TestUtil
14
+
15
+ def testBasic
16
+ cursor_setup do |connection|
17
+ Transaction.new(connection) do
18
+ sql = Select.new
19
+ sql.select('i')
20
+ sql.from(table1)
21
+ cursor = Cursor.new('cursor1', sql, {}, connection)
22
+ assertEquals(cursor.fetch, [{'i'=>0}])
23
+ assertEquals(cursor.fetch, [{'i'=>1}])
24
+ assertEquals(cursor.fetch, [{'i'=>2}])
25
+ assertEquals(cursor.fetch, [{'i'=>3}])
26
+ assertEquals(cursor.fetch, [{'i'=>4}])
27
+ assertEquals(cursor.fetch, [])
28
+ cursor.close
29
+ end
30
+ end
31
+ end
32
+
33
+ def testInitWithClosure
34
+ cursor_setup do |connection|
35
+ Transaction.new(connection) do
36
+ sql = Select.new
37
+ sql.select('i')
38
+ sql.from(table1)
39
+ Cursor.new('cursor1', sql, {}, connection) do |cursor|
40
+ assertEquals(cursor.fetch, [{'i'=>0}])
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def test_fetch_count
47
+ cursor_setup do |connection|
48
+ Transaction.new(connection) do
49
+ sql = Select.new
50
+ sql.select('i')
51
+ sql.from(table1)
52
+ cursor = Cursor.new('cursor1', sql, {}, connection)
53
+ assertEquals(cursor.fetch(2), [{'i'=>0}, {'i'=>1}])
54
+ assertEquals(cursor.fetch(2), [{'i'=>2}, {'i'=>3}])
55
+ assertEquals(cursor.fetch(2), [{'i'=>4}])
56
+ assertEquals(cursor.fetch(2), [])
57
+ cursor.close
58
+ end
59
+ end
60
+ end
61
+
62
+ def testHold
63
+ cursor_setup do |connection|
64
+ cursor = nil
65
+ Transaction.new(connection) do
66
+ sql = Select.new
67
+ sql.select('i')
68
+ sql.from(table1)
69
+ cursor = Cursor.new('cursor1', sql, {:hold=>true}, connection)
70
+ end
71
+ assertEquals(cursor.fetch, [{'i'=>0}])
72
+ cursor.close
73
+ end
74
+ end
75
+
76
+ def testDefaultHold
77
+ cursor_setup do |connection|
78
+ cursor = nil
79
+ Transaction.new(connection) do
80
+ sql = Select.new
81
+ sql.select('i')
82
+ sql.from(table1)
83
+ cursor = Cursor.new('cursor1', sql, {}, connection)
84
+ end
85
+ assertException(PGError, /cursor "cursor1" does not exist/)do
86
+ assertEquals(cursor.fetch, [{'i'=>0}])
87
+ end
88
+ end
89
+ end
90
+
91
+ def testNoHold
92
+ cursor_setup do |connection|
93
+ cursor = nil
94
+ Transaction.new(connection) do
95
+ sql = Select.new
96
+ sql.select('i')
97
+ sql.from(table1)
98
+ cursor = Cursor.new('cursor1', sql, {:hold=>false}, connection)
99
+ end
100
+ assertException(PGError, /cursor "cursor1" does not exist/)do
101
+ assertEquals(cursor.fetch, [{'i'=>0}])
102
+ end
103
+ end
104
+ end
105
+
106
+ def testNoScroll
107
+ cursor_setup do |connection|
108
+ cursor = nil
109
+ Transaction.new(connection) do
110
+ sql = select_too_complex_for_backwards_fetch_without_scroll_option
111
+ cursor = Cursor.new('cursor1', sql, {:scroll=>false}, connection)
112
+ assertException(PGError, /cursor can only scan forward/) do
113
+ cursor.fetch('PRIOR')
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ def testScroll
120
+ cursor_setup do |connection|
121
+ Transaction.new(connection) do
122
+ sql = select_too_complex_for_backwards_fetch_without_scroll_option
123
+ Cursor.new('cursor1', sql, {:scroll=>true}, connection) do |cursor|
124
+ assertEquals(cursor.fetch, [{'i'=>0}])
125
+ assertEquals(cursor.fetch, [{'i'=>2}])
126
+ assertEquals(cursor.fetch('PRIOR'), [{'i'=>0}])
127
+ assertEquals(cursor.fetch('PRIOR'), [])
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+ def testDefaultScroll
134
+ cursor_setup do |connection|
135
+ Transaction.new(connection) do
136
+ sql = select_too_complex_for_backwards_fetch_without_scroll_option
137
+ cursor = Cursor.new('cursor1', sql, {}, connection)
138
+ assertException(PGError, /cursor can only scan forward/) do
139
+ cursor.fetch('PRIOR')
140
+ end
141
+ end
142
+ end
143
+ end
144
+
145
+ def testMove
146
+ cursor_setup do |connection|
147
+ Transaction.new(connection) do
148
+ sql = Select.new
149
+ sql.select('i')
150
+ sql.from(table1)
151
+ Cursor.new('cursor1', sql, {}, connection) do |cursor|
152
+ cursor.move("absolute 2")
153
+ assertEquals(cursor.fetch, [{'i'=>2}])
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ private
160
+
161
+ def select_too_complex_for_backwards_fetch_without_scroll_option
162
+ sql = Select.new
163
+ sql.select('i')
164
+ sql.from(table1)
165
+ sql.join_using('inner', table2, 'i')
166
+ sql.where('i % 2 = 0')
167
+ sql
168
+ end
169
+
170
+ def cursor_setup
171
+ makeTestConnection do |connection|
172
+ connection.exec("create temporary table #{table1} (i int)")
173
+ connection.exec("create temporary table #{table2} (i int)")
174
+ 5.times do |i|
175
+ connection.exec("insert into #{table1} values (#{i})")
176
+ connection.exec("insert into #{table2} values (#{i})")
177
+ end
178
+ yield(connection)
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ CursorTest.new.run if $0 == __FILE__
185
+
186
+ # Local Variables:
187
+ # tab-width: 2
188
+ # ruby-indent-level: 2
189
+ # indent-tabs-mode: nil
190
+ # End:
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'TestSetup'
7
+
8
+ # Much of Insert is tested in the "roundtrip" test.
9
+
10
+ class DeleteTest < Test
11
+
12
+ include SqlPostgres
13
+ include TestUtil
14
+
15
+ def testDelete_NoWhereClause
16
+ testit([1, 2, 3], []) do |table, connection|
17
+ sql = Delete.new(table, connection)
18
+ sql.exec
19
+ end
20
+ end
21
+
22
+ def testDelete_OneWhereClause
23
+ testit([1, 2, 3], [2]) do |table, connection|
24
+ sql = Delete.new(table, connection)
25
+ sql.where('i % 2 = 1')
26
+ sql.exec
27
+ end
28
+ end
29
+
30
+ def testDelete_TwoWhereClauses
31
+ testit([1, 2, 3], [1, 2]) do |table, connection|
32
+ sql = Delete.new(table, connection)
33
+ sql.where('i % 2 = 1')
34
+ sql.where('i > 2')
35
+ sql.exec
36
+ end
37
+ end
38
+
39
+ def testDelete_PassConnectionToExec
40
+ testit([1, 2, 3], []) do |table, connection|
41
+ sql = Delete.new(table)
42
+ sql.exec(connection)
43
+ end
44
+ end
45
+
46
+ def testit(initial, final)
47
+ makeTestConnection do |connection|
48
+ connection.exec("create temporary table #{table1} (i int)")
49
+ for i in initial
50
+ connection.exec("insert into #{table1} (i) values (#{i})")
51
+ end
52
+ yield(table1, connection)
53
+ values = connection.query("select i from #{table1}").collect do |row|
54
+ row[0].to_i
55
+ end
56
+ assertEquals(values, final)
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ DeleteTest.new.run if $0 == __FILE__
63
+
64
+ # Local Variables:
65
+ # tab-width: 2
66
+ # ruby-indent-level: 2
67
+ # indent-tabs-mode: nil
68
+ # End:
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'TestSetup'
7
+
8
+ # Much of Insert is tested in the "roundtrip" test.
9
+
10
+ class InsertTest < Test
11
+
12
+ include SqlPostgres
13
+ include TestUtil
14
+
15
+ def testInsert_Subselect
16
+ makeTestConnection do |connection|
17
+ connection.exec("create temporary table #{table1} (i int)")
18
+ select = Select.new
19
+ select.select_literal(1)
20
+ insert = Insert.new(table1, connection)
21
+ insert.insert('i', select)
22
+ ret = insert.exec
23
+ assertEquals(connection.query("select i from #{table1}"),
24
+ [["1"]])
25
+ end
26
+ end
27
+
28
+ def testInsert_Expression
29
+ makeTestConnection do |connection|
30
+ connection.exec("create temporary table #{table1} (i int)")
31
+ insert = Insert.new(table1, connection)
32
+ insert.insert('i', ['1 + 1'])
33
+ assertEquals(insert.statement,
34
+ "insert into #{table1} (i) values (1 + 1)")
35
+ insert.exec
36
+ assertEquals(connection.query("select i from #{table1}"),
37
+ [["2"]])
38
+ end
39
+ end
40
+
41
+ def testSelect
42
+ makeTestConnection do |connection|
43
+ connection.exec("create temporary table #{table1} (i int)")
44
+ select = Select.new
45
+ select.select_literal(1)
46
+ insert = Insert.new(table1, connection)
47
+ insert.insert('i')
48
+ insert.select(select)
49
+ insert.exec
50
+ assertEquals(connection.query("select i from #{table1}"), [["1"]])
51
+ end
52
+ end
53
+
54
+ def testDefaultConnection
55
+ makeTestConnection do |connection|
56
+ connection.exec("create temporary table #{table1} (i int)")
57
+ setDefaultConnection(connection) do
58
+ insert = Insert.new(table1)
59
+ insert.insert('i', 1)
60
+ insert.exec
61
+ assertEquals(connection.query("select i from #{table1}"),
62
+ [["1"]])
63
+ end
64
+ end
65
+ end
66
+
67
+ def testGiveConnectionToExec
68
+ makeTestConnection do |connection|
69
+ connection.exec("create temporary table #{table1} (i int)")
70
+ insert = Insert.new(table1)
71
+ insert.insert('i', 1)
72
+ insert.exec(connection)
73
+ assertEquals(connection.query("select i from #{table1}"),
74
+ [["1"]])
75
+ end
76
+ end
77
+
78
+ def testInsertDefaultValues
79
+ makeTestConnection do |connection|
80
+ connection.exec("create temporary table #{table1} (i int default 0)")
81
+ insert = Insert.new(table1, connection)
82
+ insert.default_values
83
+ insert.exec
84
+ assertEquals(connection.query("select i from #{table1}"),
85
+ [["0"]])
86
+ end
87
+ end
88
+
89
+ def testInsertDefault
90
+ makeTestConnection do |connection|
91
+ connection.exec("create temporary table #{table1} (i int default 0)")
92
+ insert = Insert.new(table1, connection)
93
+ insert.insert('i', :default)
94
+ insert.exec
95
+ assertEquals(connection.query("select i from #{table1}"),
96
+ [["0"]])
97
+ end
98
+ end
99
+
100
+ def testInsertReturning
101
+ makeTestConnection do |connection|
102
+ connection.exec("create temporary table #{table1} (i int)")
103
+ insert = Insert.new(table1, connection)
104
+ insert.insert('i', 2)
105
+ insert.returning('i * 3', 'calc')
106
+ assertEquals(insert.statement,
107
+ "insert into #{table1} (i) values (2) returning i * 3 as calc")
108
+ ret = insert.exec
109
+ assertEquals(connection.query("select i from #{table1}"),
110
+ [["2"]])
111
+ assertEquals(ret.values, [["6"]])
112
+ end
113
+ end
114
+
115
+ end
116
+
117
+ InsertTest.new.run if $0 == __FILE__
118
+
119
+ # Local Variables:
120
+ # tab-width: 2
121
+ # ruby-indent-level: 2
122
+ # indent-tabs-mode: nil
123
+ # End:
@@ -0,0 +1,62 @@
1
+ class MockPGconn
2
+
3
+ @@state = {}
4
+
5
+ def MockPGconn.state
6
+ @@state
7
+ end
8
+
9
+ def state
10
+ @@state
11
+ end
12
+
13
+ def MockPGconn.connect(*args)
14
+ @@state[:open] = true
15
+ @@state[:openArgs] = args
16
+ @@state[:connection] = MockPGconn.new
17
+ end
18
+
19
+ def set_client_encoding(encoding)
20
+ @@state[:encoding] = encoding
21
+ end
22
+
23
+ def exec(statement)
24
+ @@state[:statements] ||= []
25
+ @@state[:statements] << statement
26
+ result = (state[:results] || []).shift
27
+ raise result if result.kind_of?(Exception)
28
+ result
29
+ end
30
+
31
+ def close
32
+ raise "Already closed" unless @@state[:open]
33
+ @@state[:open] = false
34
+ raise @@state[:close_exception] if @@state[:close_exception]
35
+ end
36
+
37
+ end
38
+
39
+ module SqlPostgres
40
+
41
+ class Connection
42
+
43
+ def Connection.mockPgClass
44
+ oldPgClass = @@pgClass
45
+ begin
46
+ MockPGconn.state.clear
47
+ @@pgClass = MockPGconn
48
+ yield
49
+ ensure
50
+ @@pgClass = oldPgClass
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ # Local Variables:
59
+ # tab-width: 2
60
+ # ruby-indent-level: 2
61
+ # indent-tabs-mode: nil
62
+ # End:
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'TestSetup'
7
+
8
+ class NullConnectionTest < Test
9
+
10
+ include SqlPostgres
11
+
12
+ def testExec
13
+ assertException(NoConnection) do
14
+ NullConnection.new.exec("foo")
15
+ end
16
+ end
17
+
18
+ def testClose
19
+ assertException(NoConnection) do
20
+ NullConnection.new.close
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ NullConnectionTest.new.run if $0 == __FILE__
27
+
28
+ # Local Variables:
29
+ # tab-width: 2
30
+ # ruby-indent-level: 2
31
+ # indent-tabs-mode: nil
32
+ # End:
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'TestSetup'
7
+ require 'RandomThings'
8
+
9
+ class PgBitTest < Test
10
+
11
+ include SqlPostgres
12
+ include RandomThings
13
+
14
+ def test_ctor_defaults
15
+ assertEquals(PgBit.new.bits, [])
16
+ end
17
+
18
+ def test_ctor_and_accessors
19
+ bits = (0..4).collect do rand(2) end
20
+ assertEquals(PgBit.new(bits).bits, bits)
21
+ assertEquals(PgBit.new(bits.join.to_s).bits, bits)
22
+ end
23
+
24
+ def test_from_sql
25
+ testCases = [
26
+ ["", []],
27
+ ["0", [0]],
28
+ ["101", [1, 0, 1]],
29
+ ]
30
+ for testCase in testCases
31
+ sql, bits = *testCase
32
+ assertInfo("For sql #{sql.inspect}") do
33
+ assertEquals(PgBit.from_sql(sql), PgBit.new(bits))
34
+ end
35
+ end
36
+ assertException(ArgumentError, 'Invalid bit: "foo"') do
37
+ PgBit.from_sql("foo")
38
+ end
39
+ end
40
+
41
+ def test_equality
42
+ fields = ["bits"]
43
+ for field in fields
44
+ assertInfo("For field #{field}") do
45
+ bit1 = PgBit.new
46
+ bit2 = PgBit.new
47
+ assertEquals(bit1.eql?(bit2), true)
48
+ assertEquals(bit1 == bit2, true)
49
+ assertEquals(bit1 != bit2, false)
50
+ args = fields.collect do |f|
51
+ if f == field then 1 else 0 end
52
+ end
53
+ bit2 = PgBit.new(*args)
54
+ assertEquals(bit1 == bit2, false)
55
+ assertEquals(bit1 != bit2, true)
56
+ assertEquals(bit1.eql?(Object.new), false)
57
+ assertEquals(bit1 == Object.new, false)
58
+ end
59
+ end
60
+ end
61
+
62
+ def test_hash
63
+ count = 0
64
+ hashes = Hash.new(0)
65
+ testHash = proc { |*args|
66
+ assertEquals(PgBit.new(*args).hash, PgBit.new(*args).hash)
67
+ hashes[PgBit.new(*args).hash] += 1
68
+ count += 1
69
+ }
70
+ for i in (0...10)
71
+ testHash.call(i)
72
+ end
73
+ assertGreaterOrEqual(hashes.size.to_f / count, 0.5)
74
+ end
75
+
76
+ def test_to_s
77
+ testCases = [
78
+ [[], ""],
79
+ [[1], "1"],
80
+ [[1, 0, 1], "101"],
81
+ ]
82
+ for testCase in testCases
83
+ bits, expected = *testCase
84
+ bit = PgBit.new(bits)
85
+ assertEquals(bit.to_s, expected)
86
+ assertEquals(bit.to_sql, "bit '#{expected}'")
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ PgBitTest.new.run if $0 == __FILE__
93
+
94
+ # Local Variables:
95
+ # tab-width: 2
96
+ # ruby-indent-level: 2
97
+ # indent-tabs-mode: nil
98
+ # End:
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'TestSetup'
7
+ require 'RandomThings'
8
+
9
+ class PgBoxTest < Test
10
+
11
+ include SqlPostgres
12
+ include RandomThings
13
+
14
+ def test_ctor_defaults
15
+ assertEquals(PgBox.new, PgBox.new(PgPoint.new, PgPoint.new))
16
+ end
17
+
18
+ def test_ctor_and_accessors
19
+ p1 = randomWhatever
20
+ p2 = randomWhatever
21
+ line = PgBox.new(p1, p2)
22
+ assertEquals(line.p1, p1)
23
+ assertEquals(line.p2, p2)
24
+
25
+ x1 = randomFloat
26
+ x2 = randomFloat
27
+ y1 = randomFloat
28
+ y2 = randomFloat
29
+ line = PgBox.new(x1, y1, x2, y2)
30
+ assertEquals(line.p1, PgPoint.new(x1, y1))
31
+ assertEquals(line.p2, PgPoint.new(x2, y2))
32
+ end
33
+
34
+ def test_from_sql
35
+ testCases = [
36
+ ["(0,0),(0,0)", [0, 0, 0, 0]],
37
+ ["(1.2,-3),(1e+20,-1e20)", [1.2, -3, 1e+20, -1e20]],
38
+ ]
39
+ for testCase in testCases
40
+ sql, args = *testCase
41
+ assertInfo("For sql #{sql.inspect}") do
42
+ assertEquals(PgBox.from_sql(sql), PgBox.new(*args))
43
+ end
44
+ end
45
+ assertException(ArgumentError, 'Invalid box: "foo"') do
46
+ PgBox.from_sql("foo")
47
+ end
48
+ end
49
+
50
+ def test_equality
51
+ fields = ["p1", "p2"]
52
+ for field in fields
53
+ assertInfo("For field #{field}") do
54
+ line1 = PgBox.new
55
+ line2 = PgBox.new
56
+ assertEquals(line1.eql?(line2), true)
57
+ assertEquals(line1 == line2, true)
58
+ assertEquals(line1 != line2, false)
59
+ args = fields.collect do |f|
60
+ if f == field then 1 else 0 end
61
+ end
62
+ line2 = PgBox.new(*args)
63
+ assertEquals(line1 == line2, false)
64
+ assertEquals(line1 != line2, true)
65
+ assertEquals(line1.eql?(Object.new), false)
66
+ assertEquals(line1 == Object.new, false)
67
+ end
68
+ end
69
+ end
70
+
71
+ def test_hash
72
+ count = 0
73
+ hashes = Hash.new(0)
74
+ testHash = proc { |*args|
75
+ assertEquals(PgBox.new(*args).hash, PgBox.new(*args).hash)
76
+ hashes[PgBox.new(*args).hash] += 1
77
+ count += 1
78
+ }
79
+ for i in (0...10)
80
+ testHash.call(i, 0, 0, 0)
81
+ testHash.call(0, i, 0, 0)
82
+ testHash.call(0, 0, i, 0)
83
+ testHash.call(0, 0, 0, i)
84
+ end
85
+ assertGreaterOrEqual(hashes.size.to_f / count, 0.5)
86
+ end
87
+
88
+ def test_to_s
89
+ testCases = [
90
+ [[0, 0, 0, 0], "((0, 0), (0, 0))"],
91
+ [[1.2, -2, 1e10, -1e10], "((1.2, -2), (1e+10, -1e+10))"],
92
+ ]
93
+ for testCase in testCases
94
+ args, expected = *testCase
95
+ assertEquals(PgBox.new(*args).to_s, expected)
96
+ assertEquals(PgBox.new(*args).to_sql, "box '#{expected}'")
97
+ end
98
+ end
99
+
100
+ end
101
+
102
+ PgBoxTest.new.run if $0 == __FILE__
103
+
104
+ # Local Variables:
105
+ # tab-width: 2
106
+ # ruby-indent-level: 2
107
+ # indent-tabs-mode: nil
108
+ # End: