File: config_test.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (70 lines) | stat: -rw-r--r-- 1,941 bytes parent folder | download | duplicates (6)
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
package query_test

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

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/lxc/incus/v6/internal/server/db/query"
)

func TestSelectConfig(t *testing.T) {
	tx := newTxForConfig(t)
	values, err := query.SelectConfig(context.Background(), tx, "test", "")
	require.NoError(t, err)
	assert.Equal(t, map[string]string{"foo": "x", "bar": "zz"}, values)
}

func TestSelectConfig_WithFilters(t *testing.T) {
	tx := newTxForConfig(t)
	values, err := query.SelectConfig(context.Background(), tx, "test", "key=?", "bar")
	require.NoError(t, err)
	assert.Equal(t, map[string]string{"bar": "zz"}, values)
}

// New keys are added to the table.
func TestUpdateConfig_NewKeys(t *testing.T) {
	tx := newTxForConfig(t)

	values := map[string]string{"foo": "y"}
	err := query.UpdateConfig(tx, "test", values)
	require.NoError(t, err)

	values, err = query.SelectConfig(context.Background(), tx, "test", "")
	require.NoError(t, err)
	assert.Equal(t, map[string]string{"foo": "y", "bar": "zz"}, values)
}

// Unset keys are deleted from the table.
func TestDeleteConfig_Delete(t *testing.T) {
	tx := newTxForConfig(t)
	values := map[string]string{"foo": ""}

	err := query.UpdateConfig(tx, "test", values)

	require.NoError(t, err)
	values, err = query.SelectConfig(context.Background(), tx, "test", "")
	require.NoError(t, err)
	assert.Equal(t, map[string]string{"bar": "zz"}, values)
}

// Return a new transaction against an in-memory SQLite database with a single
// test table populated with a few rows.
func newTxForConfig(t *testing.T) *sql.Tx {
	db, err := sql.Open("sqlite3", ":memory:")
	assert.NoError(t, err)

	_, err = db.Exec("CREATE TABLE test (key TEXT NOT NULL, value TEXT)")
	assert.NoError(t, err)

	_, err = db.Exec("INSERT INTO test VALUES ('foo', 'x'), ('bar', 'zz')")
	assert.NoError(t, err)

	tx, err := db.Begin()
	assert.NoError(t, err)

	return tx
}