aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/data/db_test.go
blob: b90a943a9e880711090d063db9dbf60ec2276dde (plain) (blame)
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
package data_test

import (
	"slices"
	"testing"

	"github.com/jpappel/atlas/pkg/data"
)

func TestBatchQuery(t *testing.T) {
	tests := []struct {
		name      string
		query     string
		start     string
		val       string
		delim     string
		stop      string
		n         int
		args      []int
		wantQuery string
		wantArgs  []any
	}{
		{
			"1 val group",
			"INSERT INTO Foo VALUES",
			"(",
			"?",
			",",
			")",
			5,
			[]int{1, 2, 3, 4, 5},
			"INSERT INTO Foo VALUES (?,?,?,?,?)",
			[]any{1, 2, 3, 4, 5},
		},
		{
			"multiple val groups",
			"INSERT INTO Bar VALUES",
			"",
			"(?,?)",
			",",
			"",
			2,
			[]int{1, 2, 3, 4},
			"INSERT INTO Bar VALUES (?,?),(?,?)",
			[]any{1, 2, 3, 4},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotQuery, gotArgs := data.BatchQuery(tt.query, tt.start, tt.val, tt.delim, tt.stop, tt.n, tt.args)
			if gotQuery != tt.wantQuery {
				t.Error("Got different query than wanted")
				t.Log("Wanted:\n" + tt.wantQuery)
				t.Log("Got:\n" + gotQuery)
			}

			if !slices.Equal(tt.wantArgs, gotArgs) {
				t.Error("Got different args than wanted")
				t.Logf("Wanted:\t%v", tt.wantArgs)
				t.Logf("Got:\t%v", gotArgs)
			}
		})
	}
}