| 33 | ) |
| 34 | |
| 35 | func TestSQL(t *testing.T) { |
| 36 | path := testutils.Dir(t) |
| 37 | mod, err := NewSQL(container.New(), "sql_table", "") |
| 38 | if err != nil { |
| 39 | t.Fatal("Module create failed:", err) |
| 40 | } |
| 41 | tbl := mod.(*SQL) |
| 42 | err = tbl.Configure(nil, config.NewMap(nil, config.Node{ |
| 43 | Children: []config.Node{ |
| 44 | { |
| 45 | Name: "driver", |
| 46 | Args: []string{"sqlite3"}, |
| 47 | }, |
| 48 | { |
| 49 | Name: "dsn", |
| 50 | Args: []string{filepath.Join(path, "test.db")}, |
| 51 | }, |
| 52 | { |
| 53 | Name: "init", |
| 54 | Args: []string{ |
| 55 | "CREATE TABLE testTbl (key TEXT, value TEXT)", |
| 56 | "INSERT INTO testTbl VALUES ('user1', 'user1a')", |
| 57 | "INSERT INTO testTbl VALUES ('user1', 'user1b')", |
| 58 | "INSERT INTO testTbl VALUES ('user3', NULL)", |
| 59 | }, |
| 60 | }, |
| 61 | { |
| 62 | Name: "lookup", |
| 63 | Args: []string{"SELECT value FROM testTbl WHERE key = $key"}, |
| 64 | }, |
| 65 | }, |
| 66 | })) |
| 67 | if err != nil { |
| 68 | t.Fatal("Init failed:", err) |
| 69 | } |
| 70 | if err := tbl.Start(); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | |
| 74 | check := func(key, res string, ok, fail bool) { |
| 75 | t.Helper() |
| 76 | |
| 77 | actualRes, actualOk, err := tbl.Lookup(context.Background(), key) |
| 78 | if actualRes != res { |
| 79 | t.Errorf("Result mismatch: want %s, got %s", res, actualRes) |
| 80 | } |
| 81 | if actualOk != ok { |
| 82 | t.Errorf("OK mismatch: want %v, got %v", actualOk, ok) |
| 83 | } |
| 84 | if (err != nil) != fail { |
| 85 | t.Errorf("Error mismatch: want failure = %v, got %v", fail, err) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | check("user1", "user1a", true, false) |
| 90 | check("user2", "", false, false) |
| 91 | check("user3", "", false, true) |
| 92 | |