aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/data/update_test.go
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2025-07-18 15:28:13 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2025-07-18 16:56:41 -0400
commit09cd2f2f80920830cc3fd3636a6b9fc93b10f743 (patch)
tree28095952f3a26d4923db6aac96d7b7fdddc0dc2a /pkg/data/update_test.go
parent6469090be4935f17d2dfafcee8e41ebff87f2e89 (diff)
Add methods for updating existing document entries
Additionally optimize various database queries
Diffstat (limited to 'pkg/data/update_test.go')
-rw-r--r--pkg/data/update_test.go266
1 files changed, 266 insertions, 0 deletions
diff --git a/pkg/data/update_test.go b/pkg/data/update_test.go
new file mode 100644
index 0000000..d6ef578
--- /dev/null
+++ b/pkg/data/update_test.go
@@ -0,0 +1,266 @@
+package data_test
+
+import (
+ "context"
+ "database/sql"
+ "errors"
+ "maps"
+ "testing"
+ "time"
+
+ "github.com/jpappel/atlas/pkg/data"
+ "github.com/jpappel/atlas/pkg/index"
+)
+
+func TestUpdate_Update(t *testing.T) {
+ tests := []struct {
+ name string
+ newDb func(t *testing.T) *sql.DB
+ doc index.Document
+ wantErr error
+ }{
+ {
+ "update 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"},
+ Links: []string{"link_1", "link_2", "link_3"},
+ },
+ nil,
+ },
+ {
+ "update on existing",
+ func(t *testing.T) *sql.DB {
+ t.Helper()
+ db := data.NewMemDB()
+ p := data.NewPut(db, 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"},
+ Links: []string{"link_1", "link_2", "link_3"},
+ })
+
+ if err := p.Insert(t.Context()); err != nil {
+ panic(err)
+ }
+
+ return db
+ },
+ index.Document{
+ Path: "/file",
+ Title: "A file with a new title",
+ Date: time.Unix(1, 0),
+ FileTime: time.Unix(3, 0),
+ Authors: []string{"jp", "pj"},
+ Tags: []string{"foo", "bar", "oof"},
+ Links: []string{"link_4"},
+ },
+ nil,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ db := tt.newDb(t)
+ defer db.Close()
+
+ u := data.NewUpdate(context.Background(), db, tt.doc)
+ gotErr := u.Update(t.Context())
+ if !errors.Is(gotErr, tt.wantErr) {
+ t.Fatalf("recieved unexpected error: got %v want %v", gotErr, tt.wantErr)
+ } else if gotErr != nil {
+ return
+ }
+
+ f := data.Fill{Path: tt.doc.Path, Db: db}
+ gotDoc, err := f.Get(t.Context())
+ 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 TestUpdateMany_Update(t *testing.T) {
+ tests := []struct {
+ name string
+ newDb func(t *testing.T) *sql.DB
+ docs map[string]*index.Document
+ wantErr error
+ }{
+ {
+ "additions",
+ func(t *testing.T) *sql.DB {
+ return data.NewMemDB()
+ },
+ map[string]*index.Document{
+ "/afile": {
+ Path: "/afile",
+ Title: "A file",
+ Date: time.Unix(1, 0),
+ FileTime: time.Unix(2, 0),
+ Authors: []string{"jp"},
+ Tags: []string{"foo", "bar", "oof", "baz"},
+ Links: []string{"link_1", "link_2", "link_3"},
+ },
+ "/bfile": {
+ Path: "/bfile",
+ Title: "B file",
+ Date: time.Unix(3, 0),
+ FileTime: time.Unix(4, 0),
+ Authors: []string{"pj"},
+ Tags: []string{"foo", "gar"},
+ Links: []string{"link_4"},
+ },
+ },
+ nil,
+ },
+ {
+ "delete",
+ func(t *testing.T) *sql.DB {
+ db := data.NewMemDB()
+
+ docs := map[string]*index.Document{
+ "/afile": {
+ Path: "/afile",
+ Title: "A file",
+ Date: time.Unix(1, 0),
+ FileTime: time.Unix(2, 0),
+ Authors: []string{"jp"},
+ Tags: []string{"foo", "bar", "oof", "baz"},
+ Links: []string{"link_1", "link_2", "link_3"},
+ },
+ "/bfile": {
+ Path: "/bfile",
+ Title: "B file",
+ Date: time.Unix(3, 0),
+ FileTime: time.Unix(4, 0),
+ Authors: []string{"pj"},
+ Tags: []string{"foo", "gar"},
+ Links: []string{"link_4"},
+ },
+ }
+ p, err := data.NewPutMany(t.Context(), db, docs)
+ if err != nil {
+ panic(err)
+ }
+ if err := p.Insert(); err != nil {
+ panic(err)
+ }
+
+ return db
+ },
+ map[string]*index.Document{
+ "/afile": {
+ Path: "/afile",
+ Title: "A file",
+ Date: time.Unix(1, 0),
+ FileTime: time.Unix(2, 0),
+ Authors: []string{"jp"},
+ Tags: []string{"foo", "bar", "oof", "baz"},
+ Links: []string{"link_1", "link_2", "link_3"},
+ },
+ },
+ nil,
+ },
+ {
+ "update",
+ func(t *testing.T) *sql.DB {
+ db := data.NewMemDB()
+
+ docs := map[string]*index.Document{
+ "/afile": {
+ Path: "/afile",
+ Title: "A file",
+ Date: time.Unix(1, 0),
+ FileTime: time.Unix(2, 0),
+ Authors: []string{"jp"},
+ Tags: []string{"foo", "bar", "oof", "baz"},
+ Links: []string{"link_1", "link_2", "link_3"},
+ },
+ "/bfile": {
+ Path: "/bfile",
+ Title: "B file",
+ Date: time.Unix(3, 0),
+ FileTime: time.Unix(4, 0),
+ Authors: []string{"pj"},
+ Tags: []string{"foo", "gar"},
+ Links: []string{"link_4"},
+ },
+ }
+ p, err := data.NewPutMany(t.Context(), db, docs)
+ if err != nil {
+ panic(err)
+ }
+ if err := p.Insert(); err != nil {
+ panic(err)
+ }
+
+ return db
+ },
+ map[string]*index.Document{
+ "/afile": {
+ Path: "/afile",
+ Title: "A file",
+ Date: time.Unix(1, 0),
+ FileTime: time.Unix(10, 0),
+ Authors: []string{"jp"},
+ Tags: []string{"foo", "bar", "bing", "baz"},
+ Links: []string{"link_1", "link_3"},
+ },
+ "/bfile": {
+ Path: "/bfile",
+ Title: "B file",
+ Date: time.Unix(3, 0),
+ FileTime: time.Unix(5, 0),
+ Authors: []string{},
+ Tags: []string{},
+ Links: []string{},
+ },
+ },
+ nil,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ db := tt.newDb(t)
+ defer db.Close()
+
+ u := data.NewUpdateMany(db, tt.docs)
+ gotErr := u.Update(t.Context())
+ if !errors.Is(gotErr, tt.wantErr) {
+ t.Fatalf("recieved unexpected error: got %v want %v", gotErr, tt.wantErr)
+ } else if gotErr != nil {
+ return
+ }
+
+ f := data.FillMany{Db: db}
+ docs, err := f.Get(t.Context())
+ if err != nil {
+ t.Fatal("Error while retrieving documents for comparison:", err)
+ }
+
+ if !maps.EqualFunc(docs, tt.docs, func(a, b *index.Document) bool {
+ return a.Equal(*b)
+ }) {
+ t.Error("Got different docs than expected")
+ t.Logf("Got:\n%+v\n", docs)
+ t.Logf("Want:\n%+v\n", tt.docs)
+ }
+ })
+ }
+}