|
16 | 16 |
|
17 | 17 | package com.github.mauricio.async.db.postgresql
|
18 | 18 |
|
19 |
| -import com.github.mauricio.async.db.column.TimestampWithTimezoneEncoderDecoder |
| 19 | +import com.github.mauricio.async.db.column.{TimestampWithTimezoneEncoderDecoder, InetAddressEncoderDecoder} |
20 | 20 | import org.specs2.mutable.Specification
|
| 21 | +import java.net.InetAddress |
21 | 22 |
|
22 | 23 | class ArrayTypesSpec extends Specification with DatabaseTestHelper {
|
23 |
| - |
24 |
| - val simpleCreate = """create temp table type_test_table ( |
25 |
| - bigserial_column bigserial not null, |
26 |
| - smallint_column integer[] not null, |
27 |
| - text_column text[] not null, |
28 |
| - timestamp_column timestamp with time zone[] not null, |
29 |
| - constraint bigserial_column_pkey primary key (bigserial_column) |
30 |
| - )""" |
| 24 | + // `uniq` allows sbt to run the tests concurrently as there is no CREATE TEMP TYPE |
| 25 | + def simpleCreate(uniq: String) = s"""DROP TYPE IF EXISTS dir_$uniq; |
| 26 | + CREATE TYPE direction_$uniq AS ENUM ('in','out'); |
| 27 | + DROP TYPE IF EXISTS endpoint_$uniq; |
| 28 | + CREATE TYPE endpoint_$uniq AS (ip inet, port integer); |
| 29 | + create temp table type_test_table_$uniq ( |
| 30 | + bigserial_column bigserial not null, |
| 31 | + smallint_column integer[] not null, |
| 32 | + text_column text[] not null, |
| 33 | + inet_column inet[] not null, |
| 34 | + direction_column direction_$uniq[] not null, |
| 35 | + endpoint_column endpoint_$uniq[] not null, |
| 36 | + timestamp_column timestamp with time zone[] not null, |
| 37 | + constraint bigserial_column_pkey primary key (bigserial_column) |
| 38 | + )""" |
| 39 | + def simpleDrop(uniq: String) = s"""drop table if exists type_test_table_$uniq; |
| 40 | + drop type if exists endpoint_$uniq; |
| 41 | + drop type if exists direction_$uniq""" |
31 | 42 |
|
32 | 43 | val insert =
|
33 |
| - """insert into type_test_table |
34 |
| - (smallint_column, text_column, timestamp_column) |
| 44 | + """insert into type_test_table_cptat |
| 45 | + (smallint_column, text_column, inet_column, direction_column, endpoint_column, timestamp_column) |
35 | 46 | values (
|
36 | 47 | '{1,2,3,4}',
|
37 | 48 | '{"some,\"comma,separated,text","another line of text","fake\,backslash","real\\,backslash\\",NULL}',
|
| 49 | + '{"127.0.0.1","2002:15::1"}', |
| 50 | + '{"in","out"}', |
| 51 | + '{"(\"127.0.0.1\",80)","(\"2002:15::1\",443)"}', |
38 | 52 | '{"2013-04-06 01:15:10.528-03","2013-04-06 01:15:08.528-03"}'
|
39 | 53 | )"""
|
40 | 54 |
|
41 |
| - val insertPreparedStatement = """insert into type_test_table |
42 |
| - (smallint_column, text_column, timestamp_column) |
43 |
| - values (?,?,?)""" |
| 55 | + val insertPreparedStatement = """insert into type_test_table_csaups |
| 56 | + (smallint_column, text_column, inet_column, direction_column, endpoint_column, timestamp_column) |
| 57 | + values (?,?,?,?,?,?)""" |
44 | 58 |
|
45 | 59 | "connection" should {
|
46 | 60 |
|
47 | 61 | "correctly parse the array type" in {
|
48 | 62 |
|
49 | 63 | withHandler {
|
50 | 64 | handler =>
|
51 |
| - executeDdl(handler, simpleCreate) |
52 |
| - executeDdl(handler, insert, 1) |
53 |
| - val result = executeQuery(handler, "select * from type_test_table").rows.get |
54 |
| - result(0)("smallint_column") === List(1,2,3,4) |
55 |
| - result(0)("text_column") === List("some,\"comma,separated,text", "another line of text", "fake,backslash", "real\\,backslash\\", null ) |
56 |
| - result(0)("timestamp_column") === List( |
57 |
| - TimestampWithTimezoneEncoderDecoder.decode("2013-04-06 01:15:10.528-03"), |
58 |
| - TimestampWithTimezoneEncoderDecoder.decode("2013-04-06 01:15:08.528-03") |
59 |
| - ) |
| 65 | + try { |
| 66 | + executeDdl(handler, simpleCreate("cptat")) |
| 67 | + executeDdl(handler, insert, 1) |
| 68 | + val result = executeQuery(handler, "select * from type_test_table_cptat").rows.get |
| 69 | + result(0)("smallint_column") === List(1,2,3,4) |
| 70 | + result(0)("text_column") === List("some,\"comma,separated,text", "another line of text", "fake,backslash", "real\\,backslash\\", null ) |
| 71 | + result(0)("timestamp_column") === List( |
| 72 | + TimestampWithTimezoneEncoderDecoder.decode("2013-04-06 01:15:10.528-03"), |
| 73 | + TimestampWithTimezoneEncoderDecoder.decode("2013-04-06 01:15:08.528-03") |
| 74 | + ) |
| 75 | + } finally { |
| 76 | + executeDdl(handler, simpleDrop("cptat")) |
| 77 | + } |
60 | 78 | }
|
61 | 79 |
|
62 | 80 | }
|
63 | 81 |
|
64 | 82 | "correctly send arrays using prepared statements" in {
|
| 83 | + case class Endpoint(ip: InetAddress, port: Int) |
65 | 84 |
|
66 | 85 | val timestamps = List(
|
67 | 86 | TimestampWithTimezoneEncoderDecoder.decode("2013-04-06 01:15:10.528-03"),
|
68 | 87 | TimestampWithTimezoneEncoderDecoder.decode("2013-04-06 01:15:08.528-03")
|
69 | 88 | )
|
| 89 | + val inets = List( |
| 90 | + InetAddressEncoderDecoder.decode("127.0.0.1"), |
| 91 | + InetAddressEncoderDecoder.decode("2002:15::1") |
| 92 | + ) |
| 93 | + val directions = List("in", "out") |
| 94 | + val endpoints = List( |
| 95 | + Endpoint(InetAddress.getByName("127.0.0.1"), 80), // case class |
| 96 | + (InetAddress.getByName("2002:15::1"), 443) // tuple |
| 97 | + ) |
70 | 98 | val numbers = List(1,2,3,4)
|
71 | 99 | val texts = List("some,\"comma,separated,text", "another line of text", "fake,backslash", "real\\,backslash\\", null )
|
72 | 100 |
|
73 | 101 | withHandler {
|
74 | 102 | handler =>
|
75 |
| - executeDdl(handler, simpleCreate) |
76 |
| - executePreparedStatement( |
77 |
| - handler, |
78 |
| - this.insertPreparedStatement, |
79 |
| - Array( numbers, texts, timestamps ) ) |
80 |
| - |
81 |
| - val result = executeQuery(handler, "select * from type_test_table").rows.get |
82 |
| - |
83 |
| - result(0)("smallint_column") === numbers |
84 |
| - result(0)("text_column") === texts |
85 |
| - result(0)("timestamp_column") === timestamps |
| 103 | + try { |
| 104 | + executeDdl(handler, simpleCreate("csaups")) |
| 105 | + executePreparedStatement( |
| 106 | + handler, |
| 107 | + this.insertPreparedStatement, |
| 108 | + Array( numbers, texts, inets, directions, endpoints, timestamps ) ) |
| 109 | + |
| 110 | + val result = executeQuery(handler, "select * from type_test_table_csaups").rows.get |
| 111 | + |
| 112 | + result(0)("smallint_column") === numbers |
| 113 | + result(0)("text_column") === texts |
| 114 | + result(0)("inet_column") === inets |
| 115 | + result(0)("direction_column") === "{in,out}" // user type decoding not supported |
| 116 | + result(0)("endpoint_column") === """{"(127.0.0.1,80)","(2002:15::1,443)"}""" // user type decoding not supported |
| 117 | + result(0)("timestamp_column") === timestamps |
| 118 | + } finally { |
| 119 | + executeDdl(handler, simpleDrop("csaups")) |
| 120 | + } |
86 | 121 | }
|
87 | 122 |
|
88 | 123 | }
|
|
0 commit comments