-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingledb_test.go
101 lines (79 loc) · 2.38 KB
/
singledb_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package duckdbreplicator
import (
"context"
"database/sql"
"testing"
"github.com/stretchr/testify/require"
)
func TestSingleDB_test(t *testing.T) {
ctx := context.Background()
db, err := NewSingleDB(ctx, &SingleDBOptions{
DSN: "",
})
require.NoError(t, err)
// create table
rw, release, err := db.AcquireWriteConnection(ctx)
require.NoError(t, err)
err = rw.CreateTableAsSelect(ctx, "test-2", "SELECT 1 AS id, 'India' AS country", nil)
require.NoError(t, err)
// rename table
err = rw.RenameTable(ctx, "test-2", "test")
require.NoError(t, err)
// insert into table
err = rw.InsertTableAsSelect(ctx, "test", "SELECT 2 AS id, 'USA' AS country", nil)
require.NoError(t, err)
// add column
err = rw.AddTableColumn(ctx, "test", "currency_score", "INT")
require.NoError(t, err)
// alter column
err = rw.AlterTableColumn(ctx, "test", "currency_score", "FLOAT")
require.NoError(t, err)
require.NoError(t, release())
// select from table
conn, release, err := db.AcquireReadConnection(ctx)
require.NoError(t, err)
var (
id int
country string
currencyScore sql.NullFloat64
)
err = conn.Connx().QueryRowxContext(ctx, "SELECT id, country, currency_score FROM test WHERE id = 2").Scan(&id, &country, ¤cyScore)
require.NoError(t, err)
require.Equal(t, 2, id)
require.Equal(t, "USA", country)
require.Equal(t, false, currencyScore.Valid)
err = release()
require.NoError(t, err)
// drop table
err = db.DropTable(ctx, "test")
require.NoError(t, err)
}
func TestSingleDB_testRenameExisting(t *testing.T) {
ctx := context.Background()
db, err := NewSingleDB(ctx, &SingleDBOptions{
DSN: "",
})
require.NoError(t, err)
// create table
err = db.CreateTableAsSelect(ctx, "test-2", "SELECT 1 AS id, 'India' AS country", nil)
require.NoError(t, err)
// create another table
err = db.CreateTableAsSelect(ctx, "test-3", "SELECT 2 AS id, 'USA' AS country", nil)
require.NoError(t, err)
// rename table
err = db.RenameTable(ctx, "test-2", "test-3")
require.NoError(t, err)
// select from table
conn, release, err := db.AcquireReadConnection(ctx)
require.NoError(t, err)
var (
id int
country string
)
err = conn.Connx().QueryRowxContext(ctx, "SELECT id, country FROM \"test-3\" WHERE id = 1").Scan(&id, &country)
require.NoError(t, err)
require.Equal(t, 1, id)
require.Equal(t, "India", country)
err = release()
require.NoError(t, err)
}