array_test.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. package pq
  2. import (
  3. "bytes"
  4. "database/sql"
  5. "database/sql/driver"
  6. "math/rand"
  7. "reflect"
  8. "strings"
  9. "testing"
  10. )
  11. func TestParseArray(t *testing.T) {
  12. for _, tt := range []struct {
  13. input string
  14. delim string
  15. dims []int
  16. elems [][]byte
  17. }{
  18. {`{}`, `,`, nil, [][]byte{}},
  19. {`{NULL}`, `,`, []int{1}, [][]byte{nil}},
  20. {`{a}`, `,`, []int{1}, [][]byte{{'a'}}},
  21. {`{a,b}`, `,`, []int{2}, [][]byte{{'a'}, {'b'}}},
  22. {`{{a,b}}`, `,`, []int{1, 2}, [][]byte{{'a'}, {'b'}}},
  23. {`{{a},{b}}`, `,`, []int{2, 1}, [][]byte{{'a'}, {'b'}}},
  24. {`{{{a,b},{c,d},{e,f}}}`, `,`, []int{1, 3, 2}, [][]byte{
  25. {'a'}, {'b'}, {'c'}, {'d'}, {'e'}, {'f'},
  26. }},
  27. {`{""}`, `,`, []int{1}, [][]byte{{}}},
  28. {`{","}`, `,`, []int{1}, [][]byte{{','}}},
  29. {`{",",","}`, `,`, []int{2}, [][]byte{{','}, {','}}},
  30. {`{{",",","}}`, `,`, []int{1, 2}, [][]byte{{','}, {','}}},
  31. {`{{","},{","}}`, `,`, []int{2, 1}, [][]byte{{','}, {','}}},
  32. {`{{{",",","},{",",","},{",",","}}}`, `,`, []int{1, 3, 2}, [][]byte{
  33. {','}, {','}, {','}, {','}, {','}, {','},
  34. }},
  35. {`{"\"}"}`, `,`, []int{1}, [][]byte{{'"', '}'}}},
  36. {`{"\"","\""}`, `,`, []int{2}, [][]byte{{'"'}, {'"'}}},
  37. {`{{"\"","\""}}`, `,`, []int{1, 2}, [][]byte{{'"'}, {'"'}}},
  38. {`{{"\""},{"\""}}`, `,`, []int{2, 1}, [][]byte{{'"'}, {'"'}}},
  39. {`{{{"\"","\""},{"\"","\""},{"\"","\""}}}`, `,`, []int{1, 3, 2}, [][]byte{
  40. {'"'}, {'"'}, {'"'}, {'"'}, {'"'}, {'"'},
  41. }},
  42. {`{axyzb}`, `xyz`, []int{2}, [][]byte{{'a'}, {'b'}}},
  43. } {
  44. dims, elems, err := parseArray([]byte(tt.input), []byte(tt.delim))
  45. if err != nil {
  46. t.Fatalf("Expected no error for %q, got %q", tt.input, err)
  47. }
  48. if !reflect.DeepEqual(dims, tt.dims) {
  49. t.Errorf("Expected %v dimensions for %q, got %v", tt.dims, tt.input, dims)
  50. }
  51. if !reflect.DeepEqual(elems, tt.elems) {
  52. t.Errorf("Expected %v elements for %q, got %v", tt.elems, tt.input, elems)
  53. }
  54. }
  55. }
  56. func TestParseArrayError(t *testing.T) {
  57. for _, tt := range []struct {
  58. input, err string
  59. }{
  60. {``, "expected '{' at offset 0"},
  61. {`x`, "expected '{' at offset 0"},
  62. {`}`, "expected '{' at offset 0"},
  63. {`{`, "expected '}' at offset 1"},
  64. {`{{}`, "expected '}' at offset 3"},
  65. {`{}}`, "unexpected '}' at offset 2"},
  66. {`{,}`, "unexpected ',' at offset 1"},
  67. {`{,x}`, "unexpected ',' at offset 1"},
  68. {`{x,}`, "unexpected '}' at offset 3"},
  69. {`{x,{`, "unexpected '{' at offset 3"},
  70. {`{x},`, "unexpected ',' at offset 3"},
  71. {`{x}}`, "unexpected '}' at offset 3"},
  72. {`{{x}`, "expected '}' at offset 4"},
  73. {`{""x}`, "unexpected 'x' at offset 3"},
  74. {`{{a},{b,c}}`, "multidimensional arrays must have elements with matching dimensions"},
  75. } {
  76. _, _, err := parseArray([]byte(tt.input), []byte{','})
  77. if err == nil {
  78. t.Fatalf("Expected error for %q, got none", tt.input)
  79. }
  80. if !strings.Contains(err.Error(), tt.err) {
  81. t.Errorf("Expected error to contain %q for %q, got %q", tt.err, tt.input, err)
  82. }
  83. }
  84. }
  85. func TestArrayScanner(t *testing.T) {
  86. var s sql.Scanner = Array(&[]bool{})
  87. if _, ok := s.(*BoolArray); !ok {
  88. t.Errorf("Expected *BoolArray, got %T", s)
  89. }
  90. s = Array(&[]float64{})
  91. if _, ok := s.(*Float64Array); !ok {
  92. t.Errorf("Expected *Float64Array, got %T", s)
  93. }
  94. s = Array(&[]int64{})
  95. if _, ok := s.(*Int64Array); !ok {
  96. t.Errorf("Expected *Int64Array, got %T", s)
  97. }
  98. s = Array(&[]string{})
  99. if _, ok := s.(*StringArray); !ok {
  100. t.Errorf("Expected *StringArray, got %T", s)
  101. }
  102. for _, tt := range []interface{}{
  103. &[]sql.Scanner{},
  104. &[][]bool{},
  105. &[][]float64{},
  106. &[][]int64{},
  107. &[][]string{},
  108. } {
  109. s = Array(tt)
  110. if _, ok := s.(GenericArray); !ok {
  111. t.Errorf("Expected GenericArray for %T, got %T", tt, s)
  112. }
  113. }
  114. }
  115. func TestArrayValuer(t *testing.T) {
  116. var v driver.Valuer = Array([]bool{})
  117. if _, ok := v.(*BoolArray); !ok {
  118. t.Errorf("Expected *BoolArray, got %T", v)
  119. }
  120. v = Array([]float64{})
  121. if _, ok := v.(*Float64Array); !ok {
  122. t.Errorf("Expected *Float64Array, got %T", v)
  123. }
  124. v = Array([]int64{})
  125. if _, ok := v.(*Int64Array); !ok {
  126. t.Errorf("Expected *Int64Array, got %T", v)
  127. }
  128. v = Array([]string{})
  129. if _, ok := v.(*StringArray); !ok {
  130. t.Errorf("Expected *StringArray, got %T", v)
  131. }
  132. for _, tt := range []interface{}{
  133. nil,
  134. []driver.Value{},
  135. [][]bool{},
  136. [][]float64{},
  137. [][]int64{},
  138. [][]string{},
  139. } {
  140. v = Array(tt)
  141. if _, ok := v.(GenericArray); !ok {
  142. t.Errorf("Expected GenericArray for %T, got %T", tt, v)
  143. }
  144. }
  145. }
  146. func TestBoolArrayScanUnsupported(t *testing.T) {
  147. var arr BoolArray
  148. err := arr.Scan(1)
  149. if err == nil {
  150. t.Fatal("Expected error when scanning from int")
  151. }
  152. if !strings.Contains(err.Error(), "int to BoolArray") {
  153. t.Errorf("Expected type to be mentioned when scanning, got %q", err)
  154. }
  155. }
  156. func TestBoolArrayScanEmpty(t *testing.T) {
  157. var arr BoolArray
  158. err := arr.Scan(`{}`)
  159. if err != nil {
  160. t.Fatalf("Expected no error, got %v", err)
  161. }
  162. if arr == nil || len(arr) != 0 {
  163. t.Errorf("Expected empty, got %#v", arr)
  164. }
  165. }
  166. func TestBoolArrayScanNil(t *testing.T) {
  167. arr := BoolArray{true, true, true}
  168. err := arr.Scan(nil)
  169. if err != nil {
  170. t.Fatalf("Expected no error, got %v", err)
  171. }
  172. if arr != nil {
  173. t.Errorf("Expected nil, got %+v", arr)
  174. }
  175. }
  176. var BoolArrayStringTests = []struct {
  177. str string
  178. arr BoolArray
  179. }{
  180. {`{}`, BoolArray{}},
  181. {`{t}`, BoolArray{true}},
  182. {`{f,t}`, BoolArray{false, true}},
  183. }
  184. func TestBoolArrayScanBytes(t *testing.T) {
  185. for _, tt := range BoolArrayStringTests {
  186. bytes := []byte(tt.str)
  187. arr := BoolArray{true, true, true}
  188. err := arr.Scan(bytes)
  189. if err != nil {
  190. t.Fatalf("Expected no error for %q, got %v", bytes, err)
  191. }
  192. if !reflect.DeepEqual(arr, tt.arr) {
  193. t.Errorf("Expected %+v for %q, got %+v", tt.arr, bytes, arr)
  194. }
  195. }
  196. }
  197. func BenchmarkBoolArrayScanBytes(b *testing.B) {
  198. var a BoolArray
  199. var x interface{} = []byte(`{t,f,t,f,t,f,t,f,t,f}`)
  200. for i := 0; i < b.N; i++ {
  201. a = BoolArray{}
  202. a.Scan(x)
  203. }
  204. }
  205. func TestBoolArrayScanString(t *testing.T) {
  206. for _, tt := range BoolArrayStringTests {
  207. arr := BoolArray{true, true, true}
  208. err := arr.Scan(tt.str)
  209. if err != nil {
  210. t.Fatalf("Expected no error for %q, got %v", tt.str, err)
  211. }
  212. if !reflect.DeepEqual(arr, tt.arr) {
  213. t.Errorf("Expected %+v for %q, got %+v", tt.arr, tt.str, arr)
  214. }
  215. }
  216. }
  217. func TestBoolArrayScanError(t *testing.T) {
  218. for _, tt := range []struct {
  219. input, err string
  220. }{
  221. {``, "unable to parse array"},
  222. {`{`, "unable to parse array"},
  223. {`{{t},{f}}`, "cannot convert ARRAY[2][1] to BoolArray"},
  224. {`{NULL}`, `could not parse boolean array index 0: invalid boolean ""`},
  225. {`{a}`, `could not parse boolean array index 0: invalid boolean "a"`},
  226. {`{t,b}`, `could not parse boolean array index 1: invalid boolean "b"`},
  227. {`{t,f,cd}`, `could not parse boolean array index 2: invalid boolean "cd"`},
  228. } {
  229. arr := BoolArray{true, true, true}
  230. err := arr.Scan(tt.input)
  231. if err == nil {
  232. t.Fatalf("Expected error for %q, got none", tt.input)
  233. }
  234. if !strings.Contains(err.Error(), tt.err) {
  235. t.Errorf("Expected error to contain %q for %q, got %q", tt.err, tt.input, err)
  236. }
  237. if !reflect.DeepEqual(arr, BoolArray{true, true, true}) {
  238. t.Errorf("Expected destination not to change for %q, got %+v", tt.input, arr)
  239. }
  240. }
  241. }
  242. func TestBoolArrayValue(t *testing.T) {
  243. result, err := BoolArray(nil).Value()
  244. if err != nil {
  245. t.Fatalf("Expected no error for nil, got %v", err)
  246. }
  247. if result != nil {
  248. t.Errorf("Expected nil, got %q", result)
  249. }
  250. result, err = BoolArray([]bool{}).Value()
  251. if err != nil {
  252. t.Fatalf("Expected no error for empty, got %v", err)
  253. }
  254. if expected := `{}`; !reflect.DeepEqual(result, expected) {
  255. t.Errorf("Expected empty, got %q", result)
  256. }
  257. result, err = BoolArray([]bool{false, true, false}).Value()
  258. if err != nil {
  259. t.Fatalf("Expected no error, got %v", err)
  260. }
  261. if expected := `{f,t,f}`; !reflect.DeepEqual(result, expected) {
  262. t.Errorf("Expected %q, got %q", expected, result)
  263. }
  264. }
  265. func BenchmarkBoolArrayValue(b *testing.B) {
  266. rand.Seed(1)
  267. x := make([]bool, 10)
  268. for i := 0; i < len(x); i++ {
  269. x[i] = rand.Intn(2) == 0
  270. }
  271. a := BoolArray(x)
  272. for i := 0; i < b.N; i++ {
  273. a.Value()
  274. }
  275. }
  276. func TestByteaArrayScanUnsupported(t *testing.T) {
  277. var arr ByteaArray
  278. err := arr.Scan(1)
  279. if err == nil {
  280. t.Fatal("Expected error when scanning from int")
  281. }
  282. if !strings.Contains(err.Error(), "int to ByteaArray") {
  283. t.Errorf("Expected type to be mentioned when scanning, got %q", err)
  284. }
  285. }
  286. func TestByteaArrayScanEmpty(t *testing.T) {
  287. var arr ByteaArray
  288. err := arr.Scan(`{}`)
  289. if err != nil {
  290. t.Fatalf("Expected no error, got %v", err)
  291. }
  292. if arr == nil || len(arr) != 0 {
  293. t.Errorf("Expected empty, got %#v", arr)
  294. }
  295. }
  296. func TestByteaArrayScanNil(t *testing.T) {
  297. arr := ByteaArray{{2}, {6}, {0, 0}}
  298. err := arr.Scan(nil)
  299. if err != nil {
  300. t.Fatalf("Expected no error, got %v", err)
  301. }
  302. if arr != nil {
  303. t.Errorf("Expected nil, got %+v", arr)
  304. }
  305. }
  306. var ByteaArrayStringTests = []struct {
  307. str string
  308. arr ByteaArray
  309. }{
  310. {`{}`, ByteaArray{}},
  311. {`{NULL}`, ByteaArray{nil}},
  312. {`{"\\xfeff"}`, ByteaArray{{'\xFE', '\xFF'}}},
  313. {`{"\\xdead","\\xbeef"}`, ByteaArray{{'\xDE', '\xAD'}, {'\xBE', '\xEF'}}},
  314. }
  315. func TestByteaArrayScanBytes(t *testing.T) {
  316. for _, tt := range ByteaArrayStringTests {
  317. bytes := []byte(tt.str)
  318. arr := ByteaArray{{2}, {6}, {0, 0}}
  319. err := arr.Scan(bytes)
  320. if err != nil {
  321. t.Fatalf("Expected no error for %q, got %v", bytes, err)
  322. }
  323. if !reflect.DeepEqual(arr, tt.arr) {
  324. t.Errorf("Expected %+v for %q, got %+v", tt.arr, bytes, arr)
  325. }
  326. }
  327. }
  328. func BenchmarkByteaArrayScanBytes(b *testing.B) {
  329. var a ByteaArray
  330. var x interface{} = []byte(`{"\\xfe","\\xff","\\xdead","\\xbeef","\\xfe","\\xff","\\xdead","\\xbeef","\\xfe","\\xff"}`)
  331. for i := 0; i < b.N; i++ {
  332. a = ByteaArray{}
  333. a.Scan(x)
  334. }
  335. }
  336. func TestByteaArrayScanString(t *testing.T) {
  337. for _, tt := range ByteaArrayStringTests {
  338. arr := ByteaArray{{2}, {6}, {0, 0}}
  339. err := arr.Scan(tt.str)
  340. if err != nil {
  341. t.Fatalf("Expected no error for %q, got %v", tt.str, err)
  342. }
  343. if !reflect.DeepEqual(arr, tt.arr) {
  344. t.Errorf("Expected %+v for %q, got %+v", tt.arr, tt.str, arr)
  345. }
  346. }
  347. }
  348. func TestByteaArrayScanError(t *testing.T) {
  349. for _, tt := range []struct {
  350. input, err string
  351. }{
  352. {``, "unable to parse array"},
  353. {`{`, "unable to parse array"},
  354. {`{{"\\xfeff"},{"\\xbeef"}}`, "cannot convert ARRAY[2][1] to ByteaArray"},
  355. {`{"\\abc"}`, "could not parse bytea array index 0: could not parse bytea value"},
  356. } {
  357. arr := ByteaArray{{2}, {6}, {0, 0}}
  358. err := arr.Scan(tt.input)
  359. if err == nil {
  360. t.Fatalf("Expected error for %q, got none", tt.input)
  361. }
  362. if !strings.Contains(err.Error(), tt.err) {
  363. t.Errorf("Expected error to contain %q for %q, got %q", tt.err, tt.input, err)
  364. }
  365. if !reflect.DeepEqual(arr, ByteaArray{{2}, {6}, {0, 0}}) {
  366. t.Errorf("Expected destination not to change for %q, got %+v", tt.input, arr)
  367. }
  368. }
  369. }
  370. func TestByteaArrayValue(t *testing.T) {
  371. result, err := ByteaArray(nil).Value()
  372. if err != nil {
  373. t.Fatalf("Expected no error for nil, got %v", err)
  374. }
  375. if result != nil {
  376. t.Errorf("Expected nil, got %q", result)
  377. }
  378. result, err = ByteaArray([][]byte{}).Value()
  379. if err != nil {
  380. t.Fatalf("Expected no error for empty, got %v", err)
  381. }
  382. if expected := `{}`; !reflect.DeepEqual(result, expected) {
  383. t.Errorf("Expected empty, got %q", result)
  384. }
  385. result, err = ByteaArray([][]byte{{'\xDE', '\xAD', '\xBE', '\xEF'}, {'\xFE', '\xFF'}, {}}).Value()
  386. if err != nil {
  387. t.Fatalf("Expected no error, got %v", err)
  388. }
  389. if expected := `{"\\xdeadbeef","\\xfeff","\\x"}`; !reflect.DeepEqual(result, expected) {
  390. t.Errorf("Expected %q, got %q", expected, result)
  391. }
  392. }
  393. func BenchmarkByteaArrayValue(b *testing.B) {
  394. rand.Seed(1)
  395. x := make([][]byte, 10)
  396. for i := 0; i < len(x); i++ {
  397. x[i] = make([]byte, len(x))
  398. for j := 0; j < len(x); j++ {
  399. x[i][j] = byte(rand.Int())
  400. }
  401. }
  402. a := ByteaArray(x)
  403. for i := 0; i < b.N; i++ {
  404. a.Value()
  405. }
  406. }
  407. func TestFloat64ArrayScanUnsupported(t *testing.T) {
  408. var arr Float64Array
  409. err := arr.Scan(true)
  410. if err == nil {
  411. t.Fatal("Expected error when scanning from bool")
  412. }
  413. if !strings.Contains(err.Error(), "bool to Float64Array") {
  414. t.Errorf("Expected type to be mentioned when scanning, got %q", err)
  415. }
  416. }
  417. func TestFloat64ArrayScanEmpty(t *testing.T) {
  418. var arr Float64Array
  419. err := arr.Scan(`{}`)
  420. if err != nil {
  421. t.Fatalf("Expected no error, got %v", err)
  422. }
  423. if arr == nil || len(arr) != 0 {
  424. t.Errorf("Expected empty, got %#v", arr)
  425. }
  426. }
  427. func TestFloat64ArrayScanNil(t *testing.T) {
  428. arr := Float64Array{5, 5, 5}
  429. err := arr.Scan(nil)
  430. if err != nil {
  431. t.Fatalf("Expected no error, got %v", err)
  432. }
  433. if arr != nil {
  434. t.Errorf("Expected nil, got %+v", arr)
  435. }
  436. }
  437. var Float64ArrayStringTests = []struct {
  438. str string
  439. arr Float64Array
  440. }{
  441. {`{}`, Float64Array{}},
  442. {`{1.2}`, Float64Array{1.2}},
  443. {`{3.456,7.89}`, Float64Array{3.456, 7.89}},
  444. {`{3,1,2}`, Float64Array{3, 1, 2}},
  445. }
  446. func TestFloat64ArrayScanBytes(t *testing.T) {
  447. for _, tt := range Float64ArrayStringTests {
  448. bytes := []byte(tt.str)
  449. arr := Float64Array{5, 5, 5}
  450. err := arr.Scan(bytes)
  451. if err != nil {
  452. t.Fatalf("Expected no error for %q, got %v", bytes, err)
  453. }
  454. if !reflect.DeepEqual(arr, tt.arr) {
  455. t.Errorf("Expected %+v for %q, got %+v", tt.arr, bytes, arr)
  456. }
  457. }
  458. }
  459. func BenchmarkFloat64ArrayScanBytes(b *testing.B) {
  460. var a Float64Array
  461. var x interface{} = []byte(`{1.2,3.4,5.6,7.8,9.01,2.34,5.67,8.90,1.234,5.678}`)
  462. for i := 0; i < b.N; i++ {
  463. a = Float64Array{}
  464. a.Scan(x)
  465. }
  466. }
  467. func TestFloat64ArrayScanString(t *testing.T) {
  468. for _, tt := range Float64ArrayStringTests {
  469. arr := Float64Array{5, 5, 5}
  470. err := arr.Scan(tt.str)
  471. if err != nil {
  472. t.Fatalf("Expected no error for %q, got %v", tt.str, err)
  473. }
  474. if !reflect.DeepEqual(arr, tt.arr) {
  475. t.Errorf("Expected %+v for %q, got %+v", tt.arr, tt.str, arr)
  476. }
  477. }
  478. }
  479. func TestFloat64ArrayScanError(t *testing.T) {
  480. for _, tt := range []struct {
  481. input, err string
  482. }{
  483. {``, "unable to parse array"},
  484. {`{`, "unable to parse array"},
  485. {`{{5.6},{7.8}}`, "cannot convert ARRAY[2][1] to Float64Array"},
  486. {`{NULL}`, "parsing array element index 0:"},
  487. {`{a}`, "parsing array element index 0:"},
  488. {`{5.6,a}`, "parsing array element index 1:"},
  489. {`{5.6,7.8,a}`, "parsing array element index 2:"},
  490. } {
  491. arr := Float64Array{5, 5, 5}
  492. err := arr.Scan(tt.input)
  493. if err == nil {
  494. t.Fatalf("Expected error for %q, got none", tt.input)
  495. }
  496. if !strings.Contains(err.Error(), tt.err) {
  497. t.Errorf("Expected error to contain %q for %q, got %q", tt.err, tt.input, err)
  498. }
  499. if !reflect.DeepEqual(arr, Float64Array{5, 5, 5}) {
  500. t.Errorf("Expected destination not to change for %q, got %+v", tt.input, arr)
  501. }
  502. }
  503. }
  504. func TestFloat64ArrayValue(t *testing.T) {
  505. result, err := Float64Array(nil).Value()
  506. if err != nil {
  507. t.Fatalf("Expected no error for nil, got %v", err)
  508. }
  509. if result != nil {
  510. t.Errorf("Expected nil, got %q", result)
  511. }
  512. result, err = Float64Array([]float64{}).Value()
  513. if err != nil {
  514. t.Fatalf("Expected no error for empty, got %v", err)
  515. }
  516. if expected := `{}`; !reflect.DeepEqual(result, expected) {
  517. t.Errorf("Expected empty, got %q", result)
  518. }
  519. result, err = Float64Array([]float64{1.2, 3.4, 5.6}).Value()
  520. if err != nil {
  521. t.Fatalf("Expected no error, got %v", err)
  522. }
  523. if expected := `{1.2,3.4,5.6}`; !reflect.DeepEqual(result, expected) {
  524. t.Errorf("Expected %q, got %q", expected, result)
  525. }
  526. }
  527. func BenchmarkFloat64ArrayValue(b *testing.B) {
  528. rand.Seed(1)
  529. x := make([]float64, 10)
  530. for i := 0; i < len(x); i++ {
  531. x[i] = rand.NormFloat64()
  532. }
  533. a := Float64Array(x)
  534. for i := 0; i < b.N; i++ {
  535. a.Value()
  536. }
  537. }
  538. func TestInt64ArrayScanUnsupported(t *testing.T) {
  539. var arr Int64Array
  540. err := arr.Scan(true)
  541. if err == nil {
  542. t.Fatal("Expected error when scanning from bool")
  543. }
  544. if !strings.Contains(err.Error(), "bool to Int64Array") {
  545. t.Errorf("Expected type to be mentioned when scanning, got %q", err)
  546. }
  547. }
  548. func TestInt64ArrayScanEmpty(t *testing.T) {
  549. var arr Int64Array
  550. err := arr.Scan(`{}`)
  551. if err != nil {
  552. t.Fatalf("Expected no error, got %v", err)
  553. }
  554. if arr == nil || len(arr) != 0 {
  555. t.Errorf("Expected empty, got %#v", arr)
  556. }
  557. }
  558. func TestInt64ArrayScanNil(t *testing.T) {
  559. arr := Int64Array{5, 5, 5}
  560. err := arr.Scan(nil)
  561. if err != nil {
  562. t.Fatalf("Expected no error, got %v", err)
  563. }
  564. if arr != nil {
  565. t.Errorf("Expected nil, got %+v", arr)
  566. }
  567. }
  568. var Int64ArrayStringTests = []struct {
  569. str string
  570. arr Int64Array
  571. }{
  572. {`{}`, Int64Array{}},
  573. {`{12}`, Int64Array{12}},
  574. {`{345,678}`, Int64Array{345, 678}},
  575. }
  576. func TestInt64ArrayScanBytes(t *testing.T) {
  577. for _, tt := range Int64ArrayStringTests {
  578. bytes := []byte(tt.str)
  579. arr := Int64Array{5, 5, 5}
  580. err := arr.Scan(bytes)
  581. if err != nil {
  582. t.Fatalf("Expected no error for %q, got %v", bytes, err)
  583. }
  584. if !reflect.DeepEqual(arr, tt.arr) {
  585. t.Errorf("Expected %+v for %q, got %+v", tt.arr, bytes, arr)
  586. }
  587. }
  588. }
  589. func BenchmarkInt64ArrayScanBytes(b *testing.B) {
  590. var a Int64Array
  591. var x interface{} = []byte(`{1,2,3,4,5,6,7,8,9,0}`)
  592. for i := 0; i < b.N; i++ {
  593. a = Int64Array{}
  594. a.Scan(x)
  595. }
  596. }
  597. func TestInt64ArrayScanString(t *testing.T) {
  598. for _, tt := range Int64ArrayStringTests {
  599. arr := Int64Array{5, 5, 5}
  600. err := arr.Scan(tt.str)
  601. if err != nil {
  602. t.Fatalf("Expected no error for %q, got %v", tt.str, err)
  603. }
  604. if !reflect.DeepEqual(arr, tt.arr) {
  605. t.Errorf("Expected %+v for %q, got %+v", tt.arr, tt.str, arr)
  606. }
  607. }
  608. }
  609. func TestInt64ArrayScanError(t *testing.T) {
  610. for _, tt := range []struct {
  611. input, err string
  612. }{
  613. {``, "unable to parse array"},
  614. {`{`, "unable to parse array"},
  615. {`{{5},{6}}`, "cannot convert ARRAY[2][1] to Int64Array"},
  616. {`{NULL}`, "parsing array element index 0:"},
  617. {`{a}`, "parsing array element index 0:"},
  618. {`{5,a}`, "parsing array element index 1:"},
  619. {`{5,6,a}`, "parsing array element index 2:"},
  620. } {
  621. arr := Int64Array{5, 5, 5}
  622. err := arr.Scan(tt.input)
  623. if err == nil {
  624. t.Fatalf("Expected error for %q, got none", tt.input)
  625. }
  626. if !strings.Contains(err.Error(), tt.err) {
  627. t.Errorf("Expected error to contain %q for %q, got %q", tt.err, tt.input, err)
  628. }
  629. if !reflect.DeepEqual(arr, Int64Array{5, 5, 5}) {
  630. t.Errorf("Expected destination not to change for %q, got %+v", tt.input, arr)
  631. }
  632. }
  633. }
  634. func TestInt64ArrayValue(t *testing.T) {
  635. result, err := Int64Array(nil).Value()
  636. if err != nil {
  637. t.Fatalf("Expected no error for nil, got %v", err)
  638. }
  639. if result != nil {
  640. t.Errorf("Expected nil, got %q", result)
  641. }
  642. result, err = Int64Array([]int64{}).Value()
  643. if err != nil {
  644. t.Fatalf("Expected no error for empty, got %v", err)
  645. }
  646. if expected := `{}`; !reflect.DeepEqual(result, expected) {
  647. t.Errorf("Expected empty, got %q", result)
  648. }
  649. result, err = Int64Array([]int64{1, 2, 3}).Value()
  650. if err != nil {
  651. t.Fatalf("Expected no error, got %v", err)
  652. }
  653. if expected := `{1,2,3}`; !reflect.DeepEqual(result, expected) {
  654. t.Errorf("Expected %q, got %q", expected, result)
  655. }
  656. }
  657. func BenchmarkInt64ArrayValue(b *testing.B) {
  658. rand.Seed(1)
  659. x := make([]int64, 10)
  660. for i := 0; i < len(x); i++ {
  661. x[i] = rand.Int63()
  662. }
  663. a := Int64Array(x)
  664. for i := 0; i < b.N; i++ {
  665. a.Value()
  666. }
  667. }
  668. func TestStringArrayScanUnsupported(t *testing.T) {
  669. var arr StringArray
  670. err := arr.Scan(true)
  671. if err == nil {
  672. t.Fatal("Expected error when scanning from bool")
  673. }
  674. if !strings.Contains(err.Error(), "bool to StringArray") {
  675. t.Errorf("Expected type to be mentioned when scanning, got %q", err)
  676. }
  677. }
  678. func TestStringArrayScanEmpty(t *testing.T) {
  679. var arr StringArray
  680. err := arr.Scan(`{}`)
  681. if err != nil {
  682. t.Fatalf("Expected no error, got %v", err)
  683. }
  684. if arr == nil || len(arr) != 0 {
  685. t.Errorf("Expected empty, got %#v", arr)
  686. }
  687. }
  688. func TestStringArrayScanNil(t *testing.T) {
  689. arr := StringArray{"x", "x", "x"}
  690. err := arr.Scan(nil)
  691. if err != nil {
  692. t.Fatalf("Expected no error, got %v", err)
  693. }
  694. if arr != nil {
  695. t.Errorf("Expected nil, got %+v", arr)
  696. }
  697. }
  698. var StringArrayStringTests = []struct {
  699. str string
  700. arr StringArray
  701. }{
  702. {`{}`, StringArray{}},
  703. {`{t}`, StringArray{"t"}},
  704. {`{f,1}`, StringArray{"f", "1"}},
  705. {`{"a\\b","c d",","}`, StringArray{"a\\b", "c d", ","}},
  706. }
  707. func TestStringArrayScanBytes(t *testing.T) {
  708. for _, tt := range StringArrayStringTests {
  709. bytes := []byte(tt.str)
  710. arr := StringArray{"x", "x", "x"}
  711. err := arr.Scan(bytes)
  712. if err != nil {
  713. t.Fatalf("Expected no error for %q, got %v", bytes, err)
  714. }
  715. if !reflect.DeepEqual(arr, tt.arr) {
  716. t.Errorf("Expected %+v for %q, got %+v", tt.arr, bytes, arr)
  717. }
  718. }
  719. }
  720. func BenchmarkStringArrayScanBytes(b *testing.B) {
  721. var a StringArray
  722. var x interface{} = []byte(`{a,b,c,d,e,f,g,h,i,j}`)
  723. var y interface{} = []byte(`{"\a","\b","\c","\d","\e","\f","\g","\h","\i","\j"}`)
  724. for i := 0; i < b.N; i++ {
  725. a = StringArray{}
  726. a.Scan(x)
  727. a = StringArray{}
  728. a.Scan(y)
  729. }
  730. }
  731. func TestStringArrayScanString(t *testing.T) {
  732. for _, tt := range StringArrayStringTests {
  733. arr := StringArray{"x", "x", "x"}
  734. err := arr.Scan(tt.str)
  735. if err != nil {
  736. t.Fatalf("Expected no error for %q, got %v", tt.str, err)
  737. }
  738. if !reflect.DeepEqual(arr, tt.arr) {
  739. t.Errorf("Expected %+v for %q, got %+v", tt.arr, tt.str, arr)
  740. }
  741. }
  742. }
  743. func TestStringArrayScanError(t *testing.T) {
  744. for _, tt := range []struct {
  745. input, err string
  746. }{
  747. {``, "unable to parse array"},
  748. {`{`, "unable to parse array"},
  749. {`{{a},{b}}`, "cannot convert ARRAY[2][1] to StringArray"},
  750. {`{NULL}`, "parsing array element index 0: cannot convert nil to string"},
  751. {`{a,NULL}`, "parsing array element index 1: cannot convert nil to string"},
  752. {`{a,b,NULL}`, "parsing array element index 2: cannot convert nil to string"},
  753. } {
  754. arr := StringArray{"x", "x", "x"}
  755. err := arr.Scan(tt.input)
  756. if err == nil {
  757. t.Fatalf("Expected error for %q, got none", tt.input)
  758. }
  759. if !strings.Contains(err.Error(), tt.err) {
  760. t.Errorf("Expected error to contain %q for %q, got %q", tt.err, tt.input, err)
  761. }
  762. if !reflect.DeepEqual(arr, StringArray{"x", "x", "x"}) {
  763. t.Errorf("Expected destination not to change for %q, got %+v", tt.input, arr)
  764. }
  765. }
  766. }
  767. func TestStringArrayValue(t *testing.T) {
  768. result, err := StringArray(nil).Value()
  769. if err != nil {
  770. t.Fatalf("Expected no error for nil, got %v", err)
  771. }
  772. if result != nil {
  773. t.Errorf("Expected nil, got %q", result)
  774. }
  775. result, err = StringArray([]string{}).Value()
  776. if err != nil {
  777. t.Fatalf("Expected no error for empty, got %v", err)
  778. }
  779. if expected := `{}`; !reflect.DeepEqual(result, expected) {
  780. t.Errorf("Expected empty, got %q", result)
  781. }
  782. result, err = StringArray([]string{`a`, `\b`, `c"`, `d,e`}).Value()
  783. if err != nil {
  784. t.Fatalf("Expected no error, got %v", err)
  785. }
  786. if expected := `{"a","\\b","c\"","d,e"}`; !reflect.DeepEqual(result, expected) {
  787. t.Errorf("Expected %q, got %q", expected, result)
  788. }
  789. }
  790. func BenchmarkStringArrayValue(b *testing.B) {
  791. x := make([]string, 10)
  792. for i := 0; i < len(x); i++ {
  793. x[i] = strings.Repeat(`abc"def\ghi`, 5)
  794. }
  795. a := StringArray(x)
  796. for i := 0; i < b.N; i++ {
  797. a.Value()
  798. }
  799. }
  800. func TestGenericArrayScanUnsupported(t *testing.T) {
  801. var s string
  802. var ss []string
  803. var nsa [1]sql.NullString
  804. for _, tt := range []struct {
  805. src, dest interface{}
  806. err string
  807. }{
  808. {nil, nil, "destination <nil> is not a pointer to array or slice"},
  809. {nil, true, "destination bool is not a pointer to array or slice"},
  810. {nil, &s, "destination *string is not a pointer to array or slice"},
  811. {nil, ss, "destination []string is not a pointer to array or slice"},
  812. {nil, &nsa, "<nil> to [1]sql.NullString"},
  813. {true, &ss, "bool to []string"},
  814. {`{{x}}`, &ss, "multidimensional ARRAY[1][1] is not implemented"},
  815. {`{{x},{x}}`, &ss, "multidimensional ARRAY[2][1] is not implemented"},
  816. {`{x}`, &ss, "scanning to string is not implemented"},
  817. } {
  818. err := GenericArray{tt.dest}.Scan(tt.src)
  819. if err == nil {
  820. t.Fatalf("Expected error for [%#v %#v]", tt.src, tt.dest)
  821. }
  822. if !strings.Contains(err.Error(), tt.err) {
  823. t.Errorf("Expected error to contain %q for [%#v %#v], got %q", tt.err, tt.src, tt.dest, err)
  824. }
  825. }
  826. }
  827. func TestGenericArrayScanScannerArrayBytes(t *testing.T) {
  828. src, expected, nsa := []byte(`{NULL,abc,"\""}`),
  829. [3]sql.NullString{{}, {String: `abc`, Valid: true}, {String: `"`, Valid: true}},
  830. [3]sql.NullString{{String: ``, Valid: true}, {}, {}}
  831. if err := (GenericArray{&nsa}).Scan(src); err != nil {
  832. t.Fatalf("Expected no error, got %v", err)
  833. }
  834. if !reflect.DeepEqual(nsa, expected) {
  835. t.Errorf("Expected %v, got %v", expected, nsa)
  836. }
  837. }
  838. func TestGenericArrayScanScannerArrayString(t *testing.T) {
  839. src, expected, nsa := `{NULL,"\"",xyz}`,
  840. [3]sql.NullString{{}, {String: `"`, Valid: true}, {String: `xyz`, Valid: true}},
  841. [3]sql.NullString{{String: ``, Valid: true}, {}, {}}
  842. if err := (GenericArray{&nsa}).Scan(src); err != nil {
  843. t.Fatalf("Expected no error, got %v", err)
  844. }
  845. if !reflect.DeepEqual(nsa, expected) {
  846. t.Errorf("Expected %v, got %v", expected, nsa)
  847. }
  848. }
  849. func TestGenericArrayScanScannerSliceEmpty(t *testing.T) {
  850. var nss []sql.NullString
  851. if err := (GenericArray{&nss}).Scan(`{}`); err != nil {
  852. t.Fatalf("Expected no error, got %v", err)
  853. }
  854. if nss == nil || len(nss) != 0 {
  855. t.Errorf("Expected empty, got %#v", nss)
  856. }
  857. }
  858. func TestGenericArrayScanScannerSliceNil(t *testing.T) {
  859. nss := []sql.NullString{{String: ``, Valid: true}, {}}
  860. if err := (GenericArray{&nss}).Scan(nil); err != nil {
  861. t.Fatalf("Expected no error, got %v", err)
  862. }
  863. if nss != nil {
  864. t.Errorf("Expected nil, got %+v", nss)
  865. }
  866. }
  867. func TestGenericArrayScanScannerSliceBytes(t *testing.T) {
  868. src, expected, nss := []byte(`{NULL,abc,"\""}`),
  869. []sql.NullString{{}, {String: `abc`, Valid: true}, {String: `"`, Valid: true}},
  870. []sql.NullString{{String: ``, Valid: true}, {}, {}, {}, {}}
  871. if err := (GenericArray{&nss}).Scan(src); err != nil {
  872. t.Fatalf("Expected no error, got %v", err)
  873. }
  874. if !reflect.DeepEqual(nss, expected) {
  875. t.Errorf("Expected %v, got %v", expected, nss)
  876. }
  877. }
  878. func BenchmarkGenericArrayScanScannerSliceBytes(b *testing.B) {
  879. var a GenericArray
  880. var x interface{} = []byte(`{a,b,c,d,e,f,g,h,i,j}`)
  881. var y interface{} = []byte(`{"\a","\b","\c","\d","\e","\f","\g","\h","\i","\j"}`)
  882. for i := 0; i < b.N; i++ {
  883. a = GenericArray{new([]sql.NullString)}
  884. a.Scan(x)
  885. a = GenericArray{new([]sql.NullString)}
  886. a.Scan(y)
  887. }
  888. }
  889. func TestGenericArrayScanScannerSliceString(t *testing.T) {
  890. src, expected, nss := `{NULL,"\"",xyz}`,
  891. []sql.NullString{{}, {String: `"`, Valid: true}, {String: `xyz`, Valid: true}},
  892. []sql.NullString{{String: ``, Valid: true}, {}, {}}
  893. if err := (GenericArray{&nss}).Scan(src); err != nil {
  894. t.Fatalf("Expected no error, got %v", err)
  895. }
  896. if !reflect.DeepEqual(nss, expected) {
  897. t.Errorf("Expected %v, got %v", expected, nss)
  898. }
  899. }
  900. type TildeNullInt64 struct{ sql.NullInt64 }
  901. func (TildeNullInt64) ArrayDelimiter() string { return "~" }
  902. func TestGenericArrayScanDelimiter(t *testing.T) {
  903. src, expected, tnis := `{12~NULL~76}`,
  904. []TildeNullInt64{{sql.NullInt64{Int64: 12, Valid: true}}, {}, {sql.NullInt64{Int64: 76, Valid: true}}},
  905. []TildeNullInt64{{sql.NullInt64{Int64: 0, Valid: true}}, {}}
  906. if err := (GenericArray{&tnis}).Scan(src); err != nil {
  907. t.Fatalf("Expected no error for %#v, got %v", src, err)
  908. }
  909. if !reflect.DeepEqual(tnis, expected) {
  910. t.Errorf("Expected %v for %#v, got %v", expected, src, tnis)
  911. }
  912. }
  913. func TestGenericArrayScanErrors(t *testing.T) {
  914. var sa [1]string
  915. var nis []sql.NullInt64
  916. var pss *[]string
  917. for _, tt := range []struct {
  918. src, dest interface{}
  919. err string
  920. }{
  921. {nil, pss, "destination *[]string is nil"},
  922. {`{`, &sa, "unable to parse"},
  923. {`{}`, &sa, "cannot convert ARRAY[0] to [1]string"},
  924. {`{x,x}`, &sa, "cannot convert ARRAY[2] to [1]string"},
  925. {`{x}`, &nis, `parsing array element index 0: converting`},
  926. } {
  927. err := GenericArray{tt.dest}.Scan(tt.src)
  928. if err == nil {
  929. t.Fatalf("Expected error for [%#v %#v]", tt.src, tt.dest)
  930. }
  931. if !strings.Contains(err.Error(), tt.err) {
  932. t.Errorf("Expected error to contain %q for [%#v %#v], got %q", tt.err, tt.src, tt.dest, err)
  933. }
  934. }
  935. }
  936. func TestGenericArrayValueUnsupported(t *testing.T) {
  937. _, err := GenericArray{true}.Value()
  938. if err == nil {
  939. t.Fatal("Expected error for bool")
  940. }
  941. if !strings.Contains(err.Error(), "bool to array") {
  942. t.Errorf("Expected type to be mentioned, got %q", err)
  943. }
  944. }
  945. type ByteArrayValuer [1]byte
  946. type ByteSliceValuer []byte
  947. type FuncArrayValuer struct {
  948. delimiter func() string
  949. value func() (driver.Value, error)
  950. }
  951. func (a ByteArrayValuer) Value() (driver.Value, error) { return a[:], nil }
  952. func (b ByteSliceValuer) Value() (driver.Value, error) { return []byte(b), nil }
  953. func (f FuncArrayValuer) ArrayDelimiter() string { return f.delimiter() }
  954. func (f FuncArrayValuer) Value() (driver.Value, error) { return f.value() }
  955. func TestGenericArrayValue(t *testing.T) {
  956. result, err := GenericArray{nil}.Value()
  957. if err != nil {
  958. t.Fatalf("Expected no error for nil, got %v", err)
  959. }
  960. if result != nil {
  961. t.Errorf("Expected nil, got %q", result)
  962. }
  963. for _, tt := range []interface{}{
  964. []bool(nil),
  965. [][]int(nil),
  966. []*int(nil),
  967. []sql.NullString(nil),
  968. } {
  969. result, err := GenericArray{tt}.Value()
  970. if err != nil {
  971. t.Fatalf("Expected no error for %#v, got %v", tt, err)
  972. }
  973. if result != nil {
  974. t.Errorf("Expected nil for %#v, got %q", tt, result)
  975. }
  976. }
  977. Tilde := func(v driver.Value) FuncArrayValuer {
  978. return FuncArrayValuer{
  979. func() string { return "~" },
  980. func() (driver.Value, error) { return v, nil }}
  981. }
  982. for _, tt := range []struct {
  983. result string
  984. input interface{}
  985. }{
  986. {`{}`, []bool{}},
  987. {`{true}`, []bool{true}},
  988. {`{true,false}`, []bool{true, false}},
  989. {`{true,false}`, [2]bool{true, false}},
  990. {`{}`, [][]int{{}}},
  991. {`{}`, [][]int{{}, {}}},
  992. {`{{1}}`, [][]int{{1}}},
  993. {`{{1},{2}}`, [][]int{{1}, {2}}},
  994. {`{{1,2},{3,4}}`, [][]int{{1, 2}, {3, 4}}},
  995. {`{{1,2},{3,4}}`, [2][2]int{{1, 2}, {3, 4}}},
  996. {`{"a","\\b","c\"","d,e"}`, []string{`a`, `\b`, `c"`, `d,e`}},
  997. {`{"a","\\b","c\"","d,e"}`, [][]byte{{'a'}, {'\\', 'b'}, {'c', '"'}, {'d', ',', 'e'}}},
  998. {`{NULL}`, []*int{nil}},
  999. {`{0,NULL}`, []*int{new(int), nil}},
  1000. {`{NULL}`, []sql.NullString{{}}},
  1001. {`{"\"",NULL}`, []sql.NullString{{String: `"`, Valid: true}, {}}},
  1002. {`{"a","b"}`, []ByteArrayValuer{{'a'}, {'b'}}},
  1003. {`{{"a","b"},{"c","d"}}`, [][]ByteArrayValuer{{{'a'}, {'b'}}, {{'c'}, {'d'}}}},
  1004. {`{"e","f"}`, []ByteSliceValuer{{'e'}, {'f'}}},
  1005. {`{{"e","f"},{"g","h"}}`, [][]ByteSliceValuer{{{'e'}, {'f'}}, {{'g'}, {'h'}}}},
  1006. {`{1~2}`, []FuncArrayValuer{Tilde(int64(1)), Tilde(int64(2))}},
  1007. {`{{1~2}~{3~4}}`, [][]FuncArrayValuer{{Tilde(int64(1)), Tilde(int64(2))}, {Tilde(int64(3)), Tilde(int64(4))}}},
  1008. } {
  1009. result, err := GenericArray{tt.input}.Value()
  1010. if err != nil {
  1011. t.Fatalf("Expected no error for %q, got %v", tt.input, err)
  1012. }
  1013. if !reflect.DeepEqual(result, tt.result) {
  1014. t.Errorf("Expected %q for %q, got %q", tt.result, tt.input, result)
  1015. }
  1016. }
  1017. }
  1018. func TestGenericArrayValueErrors(t *testing.T) {
  1019. v := []interface{}{func() {}}
  1020. if _, err := (GenericArray{v}).Value(); err == nil {
  1021. t.Errorf("Expected error for %q, got nil", v)
  1022. }
  1023. v = []interface{}{nil, func() {}}
  1024. if _, err := (GenericArray{v}).Value(); err == nil {
  1025. t.Errorf("Expected error for %q, got nil", v)
  1026. }
  1027. }
  1028. func BenchmarkGenericArrayValueBools(b *testing.B) {
  1029. rand.Seed(1)
  1030. x := make([]bool, 10)
  1031. for i := 0; i < len(x); i++ {
  1032. x[i] = rand.Intn(2) == 0
  1033. }
  1034. a := GenericArray{x}
  1035. for i := 0; i < b.N; i++ {
  1036. a.Value()
  1037. }
  1038. }
  1039. func BenchmarkGenericArrayValueFloat64s(b *testing.B) {
  1040. rand.Seed(1)
  1041. x := make([]float64, 10)
  1042. for i := 0; i < len(x); i++ {
  1043. x[i] = rand.NormFloat64()
  1044. }
  1045. a := GenericArray{x}
  1046. for i := 0; i < b.N; i++ {
  1047. a.Value()
  1048. }
  1049. }
  1050. func BenchmarkGenericArrayValueInt64s(b *testing.B) {
  1051. rand.Seed(1)
  1052. x := make([]int64, 10)
  1053. for i := 0; i < len(x); i++ {
  1054. x[i] = rand.Int63()
  1055. }
  1056. a := GenericArray{x}
  1057. for i := 0; i < b.N; i++ {
  1058. a.Value()
  1059. }
  1060. }
  1061. func BenchmarkGenericArrayValueByteSlices(b *testing.B) {
  1062. x := make([][]byte, 10)
  1063. for i := 0; i < len(x); i++ {
  1064. x[i] = bytes.Repeat([]byte(`abc"def\ghi`), 5)
  1065. }
  1066. a := GenericArray{x}
  1067. for i := 0; i < b.N; i++ {
  1068. a.Value()
  1069. }
  1070. }
  1071. func BenchmarkGenericArrayValueStrings(b *testing.B) {
  1072. x := make([]string, 10)
  1073. for i := 0; i < len(x); i++ {
  1074. x[i] = strings.Repeat(`abc"def\ghi`, 5)
  1075. }
  1076. a := GenericArray{x}
  1077. for i := 0; i < b.N; i++ {
  1078. a.Value()
  1079. }
  1080. }
  1081. func TestArrayScanBackend(t *testing.T) {
  1082. db := openTestConn(t)
  1083. defer db.Close()
  1084. for _, tt := range []struct {
  1085. s string
  1086. d sql.Scanner
  1087. e interface{}
  1088. }{
  1089. {`ARRAY[true, false]`, new(BoolArray), &BoolArray{true, false}},
  1090. {`ARRAY[E'\\xdead', E'\\xbeef']`, new(ByteaArray), &ByteaArray{{'\xDE', '\xAD'}, {'\xBE', '\xEF'}}},
  1091. {`ARRAY[1.2, 3.4]`, new(Float64Array), &Float64Array{1.2, 3.4}},
  1092. {`ARRAY[1, 2, 3]`, new(Int64Array), &Int64Array{1, 2, 3}},
  1093. {`ARRAY['a', E'\\b', 'c"', 'd,e']`, new(StringArray), &StringArray{`a`, `\b`, `c"`, `d,e`}},
  1094. } {
  1095. err := db.QueryRow(`SELECT ` + tt.s).Scan(tt.d)
  1096. if err != nil {
  1097. t.Errorf("Expected no error when scanning %s into %T, got %v", tt.s, tt.d, err)
  1098. }
  1099. if !reflect.DeepEqual(tt.d, tt.e) {
  1100. t.Errorf("Expected %v when scanning %s into %T, got %v", tt.e, tt.s, tt.d, tt.d)
  1101. }
  1102. }
  1103. }
  1104. func TestArrayValueBackend(t *testing.T) {
  1105. db := openTestConn(t)
  1106. defer db.Close()
  1107. for _, tt := range []struct {
  1108. s string
  1109. v driver.Valuer
  1110. }{
  1111. {`ARRAY[true, false]`, BoolArray{true, false}},
  1112. {`ARRAY[E'\\xdead', E'\\xbeef']`, ByteaArray{{'\xDE', '\xAD'}, {'\xBE', '\xEF'}}},
  1113. {`ARRAY[1.2, 3.4]`, Float64Array{1.2, 3.4}},
  1114. {`ARRAY[1, 2, 3]`, Int64Array{1, 2, 3}},
  1115. {`ARRAY['a', E'\\b', 'c"', 'd,e']`, StringArray{`a`, `\b`, `c"`, `d,e`}},
  1116. } {
  1117. var x int
  1118. err := db.QueryRow(`SELECT 1 WHERE `+tt.s+` <> $1`, tt.v).Scan(&x)
  1119. if err != sql.ErrNoRows {
  1120. t.Errorf("Expected %v to equal %s, got %v", tt.v, tt.s, err)
  1121. }
  1122. }
  1123. }