File: store_test.go

package info (click to toggle)
golang-github-canonical-go-dqlite 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 712 kB
  • sloc: sh: 380; makefile: 5
file content (84 lines) | stat: -rw-r--r-- 2,332 bytes parent folder | download | duplicates (2)
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
// +build !nosqlite3

package client_test

import (
	"context"
	"database/sql"
	"testing"

	dqlite "github.com/canonical/go-dqlite/v2"
	"github.com/canonical/go-dqlite/v2/client"
	"github.com/canonical/go-dqlite/v2/driver"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// Exercise setting and getting servers in a DatabaseNodeStore created with
// DefaultNodeStore.
func TestDefaultNodeStore(t *testing.T) {
	// Create a new default store.
	store, err := client.DefaultNodeStore(":memory:")
	require.NoError(t, err)

	// Set and get some targets.
	err = store.Set(context.Background(), []client.NodeInfo{
		{Address: "1.2.3.4:666"}, {Address: "5.6.7.8:666"}},
	)
	require.NoError(t, err)

	servers, err := store.Get(context.Background())
	require.NoError(t, err)
	assert.Equal(t, []client.NodeInfo{
		{ID: uint64(1), Address: "1.2.3.4:666"},
		{ID: uint64(1), Address: "5.6.7.8:666"}},
		servers)

	// Set and get some new targets.
	err = store.Set(context.Background(), []client.NodeInfo{
		{Address: "1.2.3.4:666"}, {Address: "9.9.9.9:666"},
	})
	require.NoError(t, err)

	servers, err = store.Get(context.Background())
	require.NoError(t, err)
	assert.Equal(t, []client.NodeInfo{
		{ID: uint64(1), Address: "1.2.3.4:666"},
		{ID: uint64(1), Address: "9.9.9.9:666"}},
		servers)

	// Setting duplicate targets returns an error and the change is not
	// persisted.
	err = store.Set(context.Background(), []client.NodeInfo{
		{Address: "1.2.3.4:666"}, {Address: "1.2.3.4:666"},
	})
	assert.EqualError(t, err, "failed to insert server 1.2.3.4:666: UNIQUE constraint failed: servers.address")

	servers, err = store.Get(context.Background())
	require.NoError(t, err)
	assert.Equal(t, []client.NodeInfo{
		{ID: uint64(1), Address: "1.2.3.4:666"},
		{ID: uint64(1), Address: "9.9.9.9:666"}},
		servers)
}

func TestConfigMultiThread(t *testing.T) {
	cleanup := dummyDBSetup(t)
	defer cleanup()

	err := dqlite.ConfigMultiThread()
	assert.EqualError(t, err, "SQLite is already initialized")
}

func dummyDBSetup(t *testing.T) func() {
	store := client.NewInmemNodeStore()
	driver, err := driver.New(store)
	require.NoError(t, err)
	sql.Register("dummy", driver)
	db, err := sql.Open("dummy", "test.db")
	require.NoError(t, err)
	cleanup := func() {
		require.NoError(t, db.Close())
	}
	return cleanup
}