aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/data/put_test.go
blob: 7e5ad38d1aabf3ce64ddb8b12b334858fa267224 (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
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
package data_test

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

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

func TestPut_Insert(t *testing.T) {
	tests := []struct {
		name    string
		newDb   func(t *testing.T) *sql.DB
		doc     index.Document
		wantErr error
	}{
		{
			"insert on empty",
			func(t *testing.T) *sql.DB {
				t.Helper()
				return data.NewMemDB()
			},
			index.Document{
				Path:     "/file",
				Title:    "A file",
				Date:     time.Unix(1, 0),
				FileTime: time.Unix(2, 0),
				Authors:  []string{"jp"},
				Tags:     []string{"foo", "bar", "oof", "baz"},
			},
			nil,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ctx := context.Background()
			db := tt.newDb(t)
			defer db.Close()

			p, err := data.NewPut(ctx, db, tt.doc)
			if err != nil {
				t.Fatalf("could not construct receiver type: %v", err)
			}

			gotErr := p.Insert()
			if !errors.Is(gotErr, tt.wantErr) {
				t.Fatalf("Unexpected error on Insert():, want %v got %v", tt.wantErr, gotErr)
			} else if gotErr != nil {
				return
			}

			f := data.Fill{Path: tt.doc.Path, Db: db}
			gotDoc, err := f.Get(ctx)
			if err != nil {
				t.Fatal("Error while retrieving document for comparison:", err)
			}

			if !gotDoc.Equal(tt.doc) {
				t.Errorf("Retrieved doc is not stored doc!\nrecv: %+v\nsent: %+v", gotDoc, tt.doc)
			}
		})
	}
}

func TestPutMany_Insert(t *testing.T) {
	tests := []struct {
		name string // description of this test case
		// Named input parameters for receiver constructor.
		db        *sql.DB
		documents map[string]*index.Document
		wantErr   bool
	}{
		// TODO: Add test cases.
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			p, err := data.NewPutMany(tt.db, tt.documents)
			if err != nil {
				t.Fatalf("could not construct receiver type: %v", err)
			}
			gotErr := p.Insert(context.Background())
			if gotErr != nil {
				if !tt.wantErr {
					t.Errorf("Insert() failed: %v", gotErr)
				}
				return
			}
			if tt.wantErr {
				t.Fatal("Insert() succeeded unexpectedly")
			}
		})
	}
}