copy_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package pq
  2. import (
  3. "bytes"
  4. "database/sql"
  5. "strings"
  6. "testing"
  7. )
  8. func TestCopyInStmt(t *testing.T) {
  9. var stmt string
  10. stmt = CopyIn("table name")
  11. if stmt != `COPY "table name" () FROM STDIN` {
  12. t.Fatal(stmt)
  13. }
  14. stmt = CopyIn("table name", "column 1", "column 2")
  15. if stmt != `COPY "table name" ("column 1", "column 2") FROM STDIN` {
  16. t.Fatal(stmt)
  17. }
  18. stmt = CopyIn(`table " name """`, `co"lumn""`)
  19. if stmt != `COPY "table "" name """"""" ("co""lumn""""") FROM STDIN` {
  20. t.Fatal(stmt)
  21. }
  22. }
  23. func TestCopyInSchemaStmt(t *testing.T) {
  24. var stmt string
  25. stmt = CopyInSchema("schema name", "table name")
  26. if stmt != `COPY "schema name"."table name" () FROM STDIN` {
  27. t.Fatal(stmt)
  28. }
  29. stmt = CopyInSchema("schema name", "table name", "column 1", "column 2")
  30. if stmt != `COPY "schema name"."table name" ("column 1", "column 2") FROM STDIN` {
  31. t.Fatal(stmt)
  32. }
  33. stmt = CopyInSchema(`schema " name """`, `table " name """`, `co"lumn""`)
  34. if stmt != `COPY "schema "" name """"""".`+
  35. `"table "" name """"""" ("co""lumn""""") FROM STDIN` {
  36. t.Fatal(stmt)
  37. }
  38. }
  39. func TestCopyInMultipleValues(t *testing.T) {
  40. db := openTestConn(t)
  41. defer db.Close()
  42. txn, err := db.Begin()
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. defer txn.Rollback()
  47. _, err = txn.Exec("CREATE TEMP TABLE temp (a int, b varchar)")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. stmt, err := txn.Prepare(CopyIn("temp", "a", "b"))
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. longString := strings.Repeat("#", 500)
  56. for i := 0; i < 500; i++ {
  57. _, err = stmt.Exec(int64(i), longString)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. }
  62. _, err = stmt.Exec()
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. err = stmt.Close()
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. var num int
  71. err = txn.QueryRow("SELECT COUNT(*) FROM temp").Scan(&num)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. if num != 500 {
  76. t.Fatalf("expected 500 items, not %d", num)
  77. }
  78. }
  79. func TestCopyInTypes(t *testing.T) {
  80. db := openTestConn(t)
  81. defer db.Close()
  82. txn, err := db.Begin()
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. defer txn.Rollback()
  87. _, err = txn.Exec("CREATE TEMP TABLE temp (num INTEGER, text VARCHAR, blob BYTEA, nothing VARCHAR)")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. stmt, err := txn.Prepare(CopyIn("temp", "num", "text", "blob", "nothing"))
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. _, err = stmt.Exec(int64(1234567890), "Héllö\n ☃!\r\t\\", []byte{0, 255, 9, 10, 13}, nil)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. _, err = stmt.Exec()
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. err = stmt.Close()
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. var num int
  108. var text string
  109. var blob []byte
  110. var nothing sql.NullString
  111. err = txn.QueryRow("SELECT * FROM temp").Scan(&num, &text, &blob, &nothing)
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. if num != 1234567890 {
  116. t.Fatal("unexpected result", num)
  117. }
  118. if text != "Héllö\n ☃!\r\t\\" {
  119. t.Fatal("unexpected result", text)
  120. }
  121. if bytes.Compare(blob, []byte{0, 255, 9, 10, 13}) != 0 {
  122. t.Fatal("unexpected result", blob)
  123. }
  124. if nothing.Valid {
  125. t.Fatal("unexpected result", nothing.String)
  126. }
  127. }
  128. func TestCopyInWrongType(t *testing.T) {
  129. db := openTestConn(t)
  130. defer db.Close()
  131. txn, err := db.Begin()
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. defer txn.Rollback()
  136. _, err = txn.Exec("CREATE TEMP TABLE temp (num INTEGER)")
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. stmt, err := txn.Prepare(CopyIn("temp", "num"))
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. defer stmt.Close()
  145. _, err = stmt.Exec("Héllö\n ☃!\r\t\\")
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. _, err = stmt.Exec()
  150. if err == nil {
  151. t.Fatal("expected error")
  152. }
  153. if pge := err.(*Error); pge.Code.Name() != "invalid_text_representation" {
  154. t.Fatalf("expected 'invalid input syntax for integer' error, got %s (%+v)", pge.Code.Name(), pge)
  155. }
  156. }
  157. func TestCopyOutsideOfTxnError(t *testing.T) {
  158. db := openTestConn(t)
  159. defer db.Close()
  160. _, err := db.Prepare(CopyIn("temp", "num"))
  161. if err == nil {
  162. t.Fatal("COPY outside of transaction did not return an error")
  163. }
  164. if err != errCopyNotSupportedOutsideTxn {
  165. t.Fatalf("expected %s, got %s", err, err.Error())
  166. }
  167. }
  168. func TestCopyInBinaryError(t *testing.T) {
  169. db := openTestConn(t)
  170. defer db.Close()
  171. txn, err := db.Begin()
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. defer txn.Rollback()
  176. _, err = txn.Exec("CREATE TEMP TABLE temp (num INTEGER)")
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. _, err = txn.Prepare("COPY temp (num) FROM STDIN WITH binary")
  181. if err != errBinaryCopyNotSupported {
  182. t.Fatalf("expected %s, got %+v", errBinaryCopyNotSupported, err)
  183. }
  184. // check that the protocol is in a valid state
  185. err = txn.Rollback()
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. }
  190. func TestCopyFromError(t *testing.T) {
  191. db := openTestConn(t)
  192. defer db.Close()
  193. txn, err := db.Begin()
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. defer txn.Rollback()
  198. _, err = txn.Exec("CREATE TEMP TABLE temp (num INTEGER)")
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. _, err = txn.Prepare("COPY temp (num) TO STDOUT")
  203. if err != errCopyToNotSupported {
  204. t.Fatalf("expected %s, got %+v", errCopyToNotSupported, err)
  205. }
  206. // check that the protocol is in a valid state
  207. err = txn.Rollback()
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. }
  212. func TestCopySyntaxError(t *testing.T) {
  213. db := openTestConn(t)
  214. defer db.Close()
  215. txn, err := db.Begin()
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. defer txn.Rollback()
  220. _, err = txn.Prepare("COPY ")
  221. if err == nil {
  222. t.Fatal("expected error")
  223. }
  224. if pge := err.(*Error); pge.Code.Name() != "syntax_error" {
  225. t.Fatalf("expected syntax error, got %s (%+v)", pge.Code.Name(), pge)
  226. }
  227. // check that the protocol is in a valid state
  228. err = txn.Rollback()
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. }
  233. // Tests for connection errors in copyin.resploop()
  234. func TestCopyRespLoopConnectionError(t *testing.T) {
  235. db := openTestConn(t)
  236. defer db.Close()
  237. txn, err := db.Begin()
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. defer txn.Rollback()
  242. var pid int
  243. err = txn.QueryRow("SELECT pg_backend_pid()").Scan(&pid)
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. _, err = txn.Exec("CREATE TEMP TABLE temp (a int)")
  248. if err != nil {
  249. t.Fatal(err)
  250. }
  251. stmt, err := txn.Prepare(CopyIn("temp", "a"))
  252. if err != nil {
  253. t.Fatal(err)
  254. }
  255. _, err = db.Exec("SELECT pg_terminate_backend($1)", pid)
  256. if err != nil {
  257. t.Fatal(err)
  258. }
  259. // We have to try and send something over, since postgres won't process
  260. // SIGTERMs while it's waiting for CopyData/CopyEnd messages; see
  261. // tcop/postgres.c.
  262. _, err = stmt.Exec(1)
  263. if err != nil {
  264. t.Fatal(err)
  265. }
  266. _, err = stmt.Exec()
  267. if err == nil {
  268. t.Fatalf("expected error")
  269. }
  270. pge, ok := err.(*Error)
  271. if !ok {
  272. t.Fatalf("expected *pq.Error, got %+#v", err)
  273. } else if pge.Code.Name() != "admin_shutdown" {
  274. t.Fatalf("expected admin_shutdown, got %s", pge.Code.Name())
  275. }
  276. err = stmt.Close()
  277. if err != nil {
  278. t.Fatal(err)
  279. }
  280. }
  281. func BenchmarkCopyIn(b *testing.B) {
  282. db := openTestConn(b)
  283. defer db.Close()
  284. txn, err := db.Begin()
  285. if err != nil {
  286. b.Fatal(err)
  287. }
  288. defer txn.Rollback()
  289. _, err = txn.Exec("CREATE TEMP TABLE temp (a int, b varchar)")
  290. if err != nil {
  291. b.Fatal(err)
  292. }
  293. stmt, err := txn.Prepare(CopyIn("temp", "a", "b"))
  294. if err != nil {
  295. b.Fatal(err)
  296. }
  297. for i := 0; i < b.N; i++ {
  298. _, err = stmt.Exec(int64(i), "hello world!")
  299. if err != nil {
  300. b.Fatal(err)
  301. }
  302. }
  303. _, err = stmt.Exec()
  304. if err != nil {
  305. b.Fatal(err)
  306. }
  307. err = stmt.Close()
  308. if err != nil {
  309. b.Fatal(err)
  310. }
  311. var num int
  312. err = txn.QueryRow("SELECT COUNT(*) FROM temp").Scan(&num)
  313. if err != nil {
  314. b.Fatal(err)
  315. }
  316. if num != b.N {
  317. b.Fatalf("expected %d items, not %d", b.N, num)
  318. }
  319. }