agi_sqlite_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. //go:build !(linux && mipsle) && !(windows && arm) && !(windows && 386)
  2. package agi
  3. import (
  4. "database/sql"
  5. "fmt"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. _ "github.com/glebarez/go-sqlite"
  11. "github.com/robertkrimen/otto"
  12. "imuslab.com/arozos/mod/agi/static"
  13. user "imuslab.com/arozos/mod/user"
  14. )
  15. // ─── pure-Go helper tests ─────────────────────────────────────────────────────
  16. func TestSQLiteParseParams_EmptyInputs(t *testing.T) {
  17. for _, input := range []string{"", "null", "[]"} {
  18. if got := sqliteParseParams(input); len(got) != 0 {
  19. t.Errorf("sqliteParseParams(%q): expected empty slice, got %v", input, got)
  20. }
  21. }
  22. }
  23. func TestSQLiteParseParams_Values(t *testing.T) {
  24. got := sqliteParseParams(`["Alice", 30, null]`)
  25. if len(got) != 3 {
  26. t.Fatalf("expected 3 params, got %d", len(got))
  27. }
  28. if got[0] != "Alice" {
  29. t.Errorf("param[0]: expected Alice, got %v", got[0])
  30. }
  31. if got[2] != nil {
  32. t.Errorf("param[2]: expected nil, got %v", got[2])
  33. }
  34. }
  35. func TestSQLiteParseParams_InvalidJSON(t *testing.T) {
  36. if got := sqliteParseParams("{not json}"); len(got) != 0 {
  37. t.Errorf("expected nil on invalid JSON, got %v", got)
  38. }
  39. }
  40. func TestSQLiteConvertValue_ByteSlice(t *testing.T) {
  41. if got := sqliteConvertValue([]byte("hello")); got != "hello" {
  42. t.Errorf("expected string 'hello', got %v", got)
  43. }
  44. }
  45. func TestSQLiteConvertValue_Passthrough(t *testing.T) {
  46. if sqliteConvertValue(42) != 42 {
  47. t.Error("integer should pass through unchanged")
  48. }
  49. if sqliteConvertValue(nil) != nil {
  50. t.Error("nil should pass through as nil")
  51. }
  52. if sqliteConvertValue("text") != "text" {
  53. t.Error("string should pass through unchanged")
  54. }
  55. }
  56. func TestSQLiteQuoteIdent(t *testing.T) {
  57. tests := []struct{ in, want string }{
  58. {"users", `"users"`},
  59. {"my table", `"my table"`},
  60. {`has"quote`, `"has""quote"`},
  61. {"", `""`},
  62. }
  63. for _, tt := range tests {
  64. if got := sqliteQuoteIdent(tt.in); got != tt.want {
  65. t.Errorf("sqliteQuoteIdent(%q) = %q, want %q", tt.in, got, tt.want)
  66. }
  67. }
  68. }
  69. // ─── library registration ─────────────────────────────────────────────────────
  70. func TestSQLiteLibRegister_AddsToLoadedLibs(t *testing.T) {
  71. g := minimalGateway()
  72. g.SQLiteLibRegister()
  73. if _, ok := g.LoadedAGILibrary["sqlite"]; !ok {
  74. t.Error("expected 'sqlite' in LoadedAGILibrary after SQLiteLibRegister")
  75. }
  76. }
  77. func TestSQLiteLibRegister_IdempotentDoesNotPanic(t *testing.T) {
  78. g := minimalGateway()
  79. g.SQLiteLibRegister()
  80. // Second call should log but not os.Exit in tests — verify no panic at least
  81. defer func() {
  82. if r := recover(); r != nil {
  83. t.Errorf("second SQLiteLibRegister panicked: %v", r)
  84. }
  85. }()
  86. }
  87. // ─── DSN construction ─────────────────────────────────────────────────────────
  88. func TestSQLiteBuildDSN_CarriesConcurrencyPragmas(t *testing.T) {
  89. dsn := sqliteBuildDSN(filepath.Join("some", "dir", "data.db"))
  90. // Without these two the library fails instantly on any lock contention,
  91. // which is the SQLITE_BUSY class of bug this DSN exists to prevent.
  92. for _, want := range []string{
  93. "_pragma=busy_timeout(5000)",
  94. "_pragma=journal_mode(WAL)",
  95. "_pragma=synchronous(NORMAL)",
  96. "_txlock=immediate",
  97. } {
  98. if !strings.Contains(dsn, want) {
  99. t.Errorf("DSN %q is missing %q", dsn, want)
  100. }
  101. }
  102. }
  103. func TestSQLiteBuildDSN_PreservesPathAndSeparator(t *testing.T) {
  104. dsn := sqliteBuildDSN("plain.db")
  105. if !strings.HasPrefix(dsn, "plain.db?") {
  106. t.Errorf("expected DSN to start with the path then '?', got %q", dsn)
  107. }
  108. }
  109. func TestSQLiteBuildDSN_PathWithQuestionMarkReturnedBare(t *testing.T) {
  110. // A '?' in the path would be parsed as the query separator, corrupting both
  111. // the filename and the pragmas — such a path must be passed through as-is.
  112. weird := filepath.Join("dir", "wh?at.db")
  113. if got := sqliteBuildDSN(weird); got != weird {
  114. t.Errorf("expected bare path %q, got %q", weird, got)
  115. }
  116. }
  117. func TestSQLiteBuildDSN_OpensRealDatabaseInWALMode(t *testing.T) {
  118. dbPath := filepath.Join(t.TempDir(), "wal.db")
  119. db, err := sql.Open("sqlite", sqliteBuildDSN(dbPath))
  120. if err != nil {
  121. t.Fatalf("sql.Open with pragma DSN: %v", err)
  122. }
  123. defer db.Close()
  124. if err := db.Ping(); err != nil {
  125. t.Fatalf("Ping: %v", err)
  126. }
  127. var mode string
  128. if err := db.QueryRow(`PRAGMA journal_mode`).Scan(&mode); err != nil {
  129. t.Fatalf("reading journal_mode: %v", err)
  130. }
  131. if !strings.EqualFold(mode, "wal") {
  132. t.Errorf("expected journal_mode=wal, got %q", mode)
  133. }
  134. var timeout int
  135. if err := db.QueryRow(`PRAGMA busy_timeout`).Scan(&timeout); err != nil {
  136. t.Fatalf("reading busy_timeout: %v", err)
  137. }
  138. if timeout != sqliteBusyTimeoutMs {
  139. t.Errorf("expected busy_timeout=%d, got %d", sqliteBusyTimeoutMs, timeout)
  140. }
  141. }
  142. // ─── Concurrency behaviour ────────────────────────────────────────────────────
  143. // Two connections to the same file, mimicking two simultaneous ArozOS requests:
  144. // one writing (the indexer) while the other reads (the UI). Before the WAL +
  145. // busy_timeout DSN this combination produced "database is locked (5)".
  146. func TestSQLiteDSN_ConcurrentReaderDuringWriteTransaction(t *testing.T) {
  147. dbPath := filepath.Join(t.TempDir(), "concurrent.db")
  148. dsn := sqliteBuildDSN(dbPath)
  149. writer, err := sql.Open("sqlite", dsn)
  150. if err != nil {
  151. t.Fatalf("open writer: %v", err)
  152. }
  153. defer writer.Close()
  154. writer.SetMaxOpenConns(1)
  155. if _, err := writer.Exec(`CREATE TABLE t (id INTEGER PRIMARY KEY, v TEXT)`); err != nil {
  156. t.Fatalf("CREATE TABLE: %v", err)
  157. }
  158. if _, err := writer.Exec(`INSERT INTO t (v) VALUES ('seed')`); err != nil {
  159. t.Fatalf("seed INSERT: %v", err)
  160. }
  161. reader, err := sql.Open("sqlite", dsn)
  162. if err != nil {
  163. t.Fatalf("open reader: %v", err)
  164. }
  165. defer reader.Close()
  166. reader.SetMaxOpenConns(1)
  167. // Hold an open write transaction while the reader works.
  168. tx, err := writer.Begin()
  169. if err != nil {
  170. t.Fatalf("Begin: %v", err)
  171. }
  172. if _, err := tx.Exec(`INSERT INTO t (v) VALUES ('pending')`); err != nil {
  173. tx.Rollback()
  174. t.Fatalf("INSERT inside tx: %v", err)
  175. }
  176. // In WAL mode this reads the last committed snapshot instead of erroring.
  177. var count int
  178. if err := reader.QueryRow(`SELECT COUNT(*) FROM t`).Scan(&count); err != nil {
  179. tx.Rollback()
  180. t.Fatalf("concurrent read during open write transaction: %v", err)
  181. }
  182. if count != 1 {
  183. t.Errorf("reader should see the pre-transaction snapshot (1 row), got %d", count)
  184. }
  185. if err := tx.Commit(); err != nil {
  186. t.Fatalf("Commit: %v", err)
  187. }
  188. if err := reader.QueryRow(`SELECT COUNT(*) FROM t`).Scan(&count); err != nil {
  189. t.Fatalf("read after commit: %v", err)
  190. }
  191. if count != 2 {
  192. t.Errorf("expected 2 rows after commit, got %d", count)
  193. }
  194. }
  195. // ─── Cleanup on VM teardown ───────────────────────────────────────────────────
  196. func TestInjectSQLiteLib_RegistersCleanup(t *testing.T) {
  197. g := minimalGateway()
  198. registered := 0
  199. payload := &static.AgiLibInjectionPayload{
  200. VM: otto.New(),
  201. User: &user.User{Username: "test"},
  202. RegisterCleanup: func(func()) { registered++ },
  203. }
  204. g.injectSQLiteLibFunctions(payload)
  205. if registered != 1 {
  206. t.Errorf("expected the sqlite lib to register exactly 1 cleanup, got %d", registered)
  207. }
  208. }
  209. func TestInjectSQLiteLib_NilRegisterCleanupDoesNotPanic(t *testing.T) {
  210. g := minimalGateway()
  211. // init.agi-style payloads carry no cleanup registry; injection must still work.
  212. defer func() {
  213. if caught := recover(); caught != nil {
  214. t.Errorf("injection panicked with nil RegisterCleanup: %v", caught)
  215. }
  216. }()
  217. g.injectSQLiteLibFunctions(&static.AgiLibInjectionPayload{
  218. VM: otto.New(),
  219. User: &user.User{Username: "test"},
  220. })
  221. }
  222. // ─── JS object structure ──────────────────────────────────────────────────────
  223. func TestInjectSQLiteLib_TransactionExposed(t *testing.T) {
  224. g := minimalGateway()
  225. vm := otto.New()
  226. g.injectSQLiteLibFunctions(&static.AgiLibInjectionPayload{
  227. VM: vm,
  228. User: &user.User{Username: "test"},
  229. })
  230. // sqlite.open() needs a real user filesystem, so assert on the wrapper source
  231. // instead: transaction must exist and must open with BEGIN IMMEDIATE so the
  232. // busy timeout applies rather than deadlocking on a deferred upgrade.
  233. for _, want := range []string{"transaction:", "BEGIN IMMEDIATE", "ROLLBACK", "COMMIT"} {
  234. val, err := vm.Run(`sqlite.open.toString().indexOf(` + strconv.Quote(want) + `) >= 0`)
  235. if err != nil {
  236. t.Fatalf("inspecting wrapper for %q: %v", want, err)
  237. }
  238. found, _ := val.ToBoolean()
  239. if !found {
  240. t.Errorf("sqlite.open wrapper should contain %q", want)
  241. }
  242. }
  243. }
  244. // injectSQLiteWithStubbedNatives injects the library, then replaces the native
  245. // bridge functions with JS stubs that append every executed statement to a global
  246. // `log` array. This exercises the real JS wrapper (BEGIN/COMMIT/ROLLBACK ordering,
  247. // the nesting guard, error propagation) without needing a user filesystem.
  248. func injectSQLiteWithStubbedNatives(t *testing.T) *otto.Otto {
  249. t.Helper()
  250. g := minimalGateway()
  251. vm := otto.New()
  252. g.injectSQLiteLibFunctions(&static.AgiLibInjectionPayload{
  253. VM: vm,
  254. User: &user.User{Username: "test"},
  255. })
  256. if _, err := vm.Run(`
  257. var log = [];
  258. _sqlite_open = function(p) { return 1; };
  259. _sqlite_exec = function(h, sql, pj) { log.push(sql); return '{"lastInsertId":0,"rowsAffected":1}'; };
  260. _sqlite_query = function(h, sql, pj) { log.push(sql); return '[]'; };
  261. `); err != nil {
  262. t.Fatalf("installing native stubs: %v", err)
  263. }
  264. return vm
  265. }
  266. func jsStringSlice(t *testing.T, vm *otto.Otto, expr string) []string {
  267. t.Helper()
  268. val, err := vm.Run(expr)
  269. if err != nil {
  270. t.Fatalf("evaluating %s: %v", expr, err)
  271. }
  272. raw, err := val.Export()
  273. if err != nil {
  274. t.Fatalf("exporting %s: %v", expr, err)
  275. }
  276. // Otto exports a homogeneous array as []string but an empty one as []interface{}.
  277. switch v := raw.(type) {
  278. case []string:
  279. return v
  280. case []interface{}:
  281. out := make([]string, 0, len(v))
  282. for _, item := range v {
  283. out = append(out, fmt.Sprint(item))
  284. }
  285. return out
  286. default:
  287. t.Fatalf("expected a string array from %s, got %T", expr, raw)
  288. return nil
  289. }
  290. }
  291. func TestSQLiteTransaction_CommitsOnSuccess(t *testing.T) {
  292. vm := injectSQLiteWithStubbedNatives(t)
  293. if _, err := vm.Run(`
  294. var db = sqlite.open("x");
  295. db.transaction(function(tx) {
  296. tx.exec("INSERT 1");
  297. tx.exec("INSERT 2");
  298. });
  299. `); err != nil {
  300. t.Fatalf("transaction: %v", err)
  301. }
  302. got := jsStringSlice(t, vm, `log`)
  303. want := []string{"BEGIN IMMEDIATE", "INSERT 1", "INSERT 2", "COMMIT"}
  304. if len(got) != len(want) {
  305. t.Fatalf("expected %v, got %v", want, got)
  306. }
  307. for i := range want {
  308. if got[i] != want[i] {
  309. t.Errorf("statement %d: expected %q, got %q", i, want[i], got[i])
  310. }
  311. }
  312. }
  313. func TestSQLiteTransaction_RollsBackAndRethrowsOnError(t *testing.T) {
  314. vm := injectSQLiteWithStubbedNatives(t)
  315. val, err := vm.Run(`
  316. var db = sqlite.open("x");
  317. var caught = "";
  318. try {
  319. db.transaction(function(tx) {
  320. tx.exec("INSERT 1");
  321. throw new Error("boom");
  322. });
  323. } catch (e) {
  324. caught = e.message;
  325. }
  326. caught;
  327. `)
  328. if err != nil {
  329. t.Fatalf("running transaction: %v", err)
  330. }
  331. // The caller's error must survive the rollback, not be masked by it.
  332. msg, _ := val.ToString()
  333. if !strings.Contains(msg, "boom") {
  334. t.Errorf("expected the original error to propagate, got %q", msg)
  335. }
  336. got := jsStringSlice(t, vm, `log`)
  337. want := []string{"BEGIN IMMEDIATE", "INSERT 1", "ROLLBACK"}
  338. if len(got) != len(want) {
  339. t.Fatalf("expected %v, got %v", want, got)
  340. }
  341. for i := range want {
  342. if got[i] != want[i] {
  343. t.Errorf("statement %d: expected %q, got %q", i, want[i], got[i])
  344. }
  345. }
  346. }
  347. func TestSQLiteTransaction_ReturnsCallbackValueAndAllowsReuse(t *testing.T) {
  348. vm := injectSQLiteWithStubbedNatives(t)
  349. val, err := vm.Run(`
  350. var db = sqlite.open("x");
  351. var first = db.transaction(function(tx) { return 42; });
  352. // A committed transaction must leave the connection usable for the next one.
  353. var second = db.transaction(function(tx) { return first + 1; });
  354. second;
  355. `)
  356. if err != nil {
  357. t.Fatalf("running transactions: %v", err)
  358. }
  359. n, _ := val.ToInteger()
  360. if n != 43 {
  361. t.Errorf("expected the callback's return value to propagate (43), got %d", n)
  362. }
  363. }
  364. func TestSQLiteTransaction_RejectsNesting(t *testing.T) {
  365. vm := injectSQLiteWithStubbedNatives(t)
  366. val, err := vm.Run(`
  367. var db = sqlite.open("x");
  368. var caught = "";
  369. try {
  370. db.transaction(function(tx) {
  371. tx.transaction(function() {});
  372. });
  373. } catch (e) {
  374. caught = e.message;
  375. }
  376. caught;
  377. `)
  378. if err != nil {
  379. t.Fatalf("running nested transaction: %v", err)
  380. }
  381. msg, _ := val.ToString()
  382. if !strings.Contains(msg, "nested") {
  383. t.Errorf("expected a nesting error, got %q", msg)
  384. }
  385. }
  386. func TestSQLiteTransaction_RequiresFunction(t *testing.T) {
  387. vm := injectSQLiteWithStubbedNatives(t)
  388. val, err := vm.Run(`
  389. var db = sqlite.open("x");
  390. var caught = "";
  391. try { db.transaction("not a function"); } catch (e) { caught = e.message; }
  392. caught;
  393. `)
  394. if err != nil {
  395. t.Fatalf("running transaction: %v", err)
  396. }
  397. msg, _ := val.ToString()
  398. if !strings.Contains(msg, "requires a function") {
  399. t.Errorf("expected an argument-type error, got %q", msg)
  400. }
  401. // Nothing should have been sent to SQLite.
  402. if got := jsStringSlice(t, vm, `log`); len(got) != 0 {
  403. t.Errorf("expected no statements executed, got %v", got)
  404. }
  405. }
  406. func TestInjectSQLiteLib_JSObjectExposed(t *testing.T) {
  407. g := minimalGateway()
  408. vm := otto.New()
  409. payload := &static.AgiLibInjectionPayload{
  410. VM: vm,
  411. User: &user.User{Username: "test"},
  412. }
  413. g.injectSQLiteLibFunctions(payload)
  414. val, err := vm.Run(`typeof sqlite.open`)
  415. if err != nil {
  416. t.Fatalf("evaluating typeof sqlite.open: %v", err)
  417. }
  418. s, _ := val.ToString()
  419. if s != "function" {
  420. t.Errorf("sqlite.open should be a function, got %q", s)
  421. }
  422. }
  423. func TestInjectSQLiteLib_NativeFunctionsRegistered(t *testing.T) {
  424. g := minimalGateway()
  425. vm := otto.New()
  426. payload := &static.AgiLibInjectionPayload{
  427. VM: vm,
  428. User: &user.User{Username: "test"},
  429. }
  430. g.injectSQLiteLibFunctions(payload)
  431. for _, fn := range []string{
  432. "_sqlite_open",
  433. "_sqlite_exec",
  434. "_sqlite_query",
  435. "_sqlite_tables",
  436. "_sqlite_schema",
  437. "_sqlite_close",
  438. } {
  439. val, err := vm.Run(`typeof ` + fn)
  440. if err != nil {
  441. t.Fatalf("evaluating typeof %s: %v", fn, err)
  442. }
  443. s, _ := val.ToString()
  444. if s != "function" {
  445. t.Errorf("%s should be a function, got %q", fn, s)
  446. }
  447. }
  448. }
  449. // ─── driver integration (no AGI VM, direct sql.Open) ─────────────────────────
  450. func TestSQLiteDriver_CreateInsertQuery(t *testing.T) {
  451. tmpDir := t.TempDir()
  452. dbPath := filepath.Join(tmpDir, "test.db")
  453. db, err := sql.Open("sqlite", dbPath)
  454. if err != nil {
  455. t.Fatalf("sql.Open: %v", err)
  456. }
  457. defer db.Close()
  458. if _, err = db.Exec(`CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)`); err != nil {
  459. t.Fatalf("CREATE TABLE: %v", err)
  460. }
  461. res, err := db.Exec(`INSERT INTO users (name) VALUES (?)`, "Alice")
  462. if err != nil {
  463. t.Fatalf("INSERT: %v", err)
  464. }
  465. id, _ := res.LastInsertId()
  466. if id != 1 {
  467. t.Errorf("expected lastInsertId=1, got %d", id)
  468. }
  469. var name string
  470. if err := db.QueryRow(`SELECT name FROM users WHERE id = ?`, 1).Scan(&name); err != nil {
  471. t.Fatalf("SELECT: %v", err)
  472. }
  473. if name != "Alice" {
  474. t.Errorf("expected Alice, got %s", name)
  475. }
  476. }
  477. func TestSQLiteDriver_ParameterisedQuery(t *testing.T) {
  478. tmpDir := t.TempDir()
  479. db, err := sql.Open("sqlite", filepath.Join(tmpDir, "p.db"))
  480. if err != nil {
  481. t.Fatalf("sql.Open: %v", err)
  482. }
  483. defer db.Close()
  484. if _, err = db.Exec(`CREATE TABLE kv (k TEXT PRIMARY KEY, v TEXT)`); err != nil {
  485. t.Fatalf("CREATE: %v", err)
  486. }
  487. for _, pair := range [][2]string{{"a", "1"}, {"b", "2"}, {"c", "3"}} {
  488. if _, err = db.Exec(`INSERT INTO kv VALUES (?, ?)`, pair[0], pair[1]); err != nil {
  489. t.Fatalf("INSERT: %v", err)
  490. }
  491. }
  492. rows, err := db.Query(`SELECT k, v FROM kv WHERE k != ? ORDER BY k`, "b")
  493. if err != nil {
  494. t.Fatalf("SELECT: %v", err)
  495. }
  496. defer rows.Close()
  497. var keys []string
  498. for rows.Next() {
  499. var k, v string
  500. if err := rows.Scan(&k, &v); err != nil {
  501. t.Fatalf("Scan: %v", err)
  502. }
  503. keys = append(keys, k)
  504. }
  505. if len(keys) != 2 || keys[0] != "a" || keys[1] != "c" {
  506. t.Errorf("expected [a c], got %v", keys)
  507. }
  508. }
  509. func TestSQLiteDriver_TableListViaSchemaQuery(t *testing.T) {
  510. tmpDir := t.TempDir()
  511. db, err := sql.Open("sqlite", filepath.Join(tmpDir, "schema.db"))
  512. if err != nil {
  513. t.Fatalf("sql.Open: %v", err)
  514. }
  515. defer db.Close()
  516. // Use AUTOINCREMENT so SQLite creates sqlite_sequence internally —
  517. // the filtered query must still return only the 3 user tables.
  518. for _, tbl := range []string{"alpha", "beta", "gamma"} {
  519. if _, err = db.Exec(`CREATE TABLE ` + tbl + ` (id INTEGER PRIMARY KEY AUTOINCREMENT)`); err != nil {
  520. t.Fatalf("CREATE %s: %v", tbl, err)
  521. }
  522. }
  523. // Matches the filter used by _sqlite_tables
  524. rows, err := db.Query(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`)
  525. if err != nil {
  526. t.Fatalf("sqlite_master query: %v", err)
  527. }
  528. defer rows.Close()
  529. var tables []string
  530. for rows.Next() {
  531. var name string
  532. if err := rows.Scan(&name); err != nil {
  533. t.Fatalf("Scan: %v", err)
  534. }
  535. tables = append(tables, name)
  536. }
  537. if len(tables) != 3 {
  538. t.Errorf("expected 3 user tables, got %v", tables)
  539. }
  540. }