summaryrefslogtreecommitdiff
path: root/internal/store/shopping_registry_test.go
blob: b8ed79e306f7b128c9ba104c4781729a2f322f74 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package store

import (
	"database/sql"
	"errors"
	"path/filepath"
	"testing"

	_ "github.com/mattn/go-sqlite3"
)

func setupRegistryStore(t *testing.T) *Store {
	t.Helper()
	db, err := sql.Open("sqlite3", filepath.Join(t.TempDir(), "reg.db"))
	if err != nil {
		t.Fatalf("open: %v", err)
	}
	db.SetMaxOpenConns(1)
	if _, err := db.Exec(`
		PRAGMA foreign_keys = ON;
		CREATE TABLE user_shopping_items (
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			name TEXT NOT NULL,
			store TEXT NOT NULL,
			section TEXT NOT NULL DEFAULT '',
			checked INTEGER DEFAULT 0,
			created_at DATETIME DEFAULT CURRENT_TIMESTAMP
		);
		CREATE TABLE shopping_stores (
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			name TEXT NOT NULL UNIQUE,
			sort_order INTEGER NOT NULL DEFAULT 0,
			created_at DATETIME DEFAULT CURRENT_TIMESTAMP
		);
		CREATE TABLE shopping_sections (
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			store_id INTEGER NOT NULL REFERENCES shopping_stores(id) ON DELETE CASCADE,
			name TEXT NOT NULL,
			sort_order INTEGER NOT NULL DEFAULT 0,
			created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
			UNIQUE (store_id, name)
		);`); err != nil {
		t.Fatalf("schema: %v", err)
	}
	return &Store{db: db}
}

// The registry's whole purpose: a store exists whether or not anything is in
// it, so you can move an item *to* an empty store.
func TestShoppingStores_ExistWithoutItems(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	if err := s.AddShoppingStore("Hardware Store"); err != nil {
		t.Fatalf("add: %v", err)
	}
	names, err := s.ShoppingStoreNames()
	if err != nil {
		t.Fatalf("names: %v", err)
	}
	if len(names) != 1 || names[0] != "Hardware Store" {
		t.Errorf("got %v, want [Hardware Store]", names)
	}
}

func TestShoppingStores_AddIsIdempotent(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.AddShoppingStore("Costco")
	if err := s.AddShoppingStore("Costco"); err != nil {
		t.Fatalf("re-add should be a no-op, got: %v", err)
	}
	names, _ := s.ShoppingStoreNames()
	if len(names) != 1 {
		t.Errorf("got %d stores, want 1", len(names))
	}
}

func TestShoppingStores_AddRejectsBlank(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()
	if err := s.AddShoppingStore("   "); err == nil {
		t.Error("expected an error for a blank store name")
	}
}

// Items reference a store by name, so a rename must carry them along or every
// item in that store is orphaned.
func TestShoppingStores_RenameMovesItsItems(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.AddShoppingStore("Hardware")
	_ = s.SaveUserShoppingItem("Nails", "Hardware")
	_ = s.SaveUserShoppingItem("Milk", "Costco")

	stores, _ := s.GetShoppingStores()
	if err := s.RenameShoppingStore(stores[0].ID, "Hardware Store"); err != nil {
		t.Fatalf("rename: %v", err)
	}

	items, _ := s.GetUserShoppingItems()
	for _, it := range items {
		if it.Name == "Nails" && it.Store != "Hardware Store" {
			t.Errorf("item stayed on the old store name: %+v", it)
		}
		if it.Name == "Milk" && it.Store != "Costco" {
			t.Errorf("an unrelated item was moved: %+v", it)
		}
	}
}

func TestShoppingStores_RenameUnknownStore(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()
	if err := s.RenameShoppingStore(999, "Nope"); !errors.Is(err, ErrStoreNotFound) {
		t.Errorf("got %v, want ErrStoreNotFound", err)
	}
}

// Deleting a store must not take its items with it — losing the shopping list
// is a much worse outcome than an item needing to be re-filed.
func TestShoppingStores_DeleteKeepsItems(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.AddShoppingStore("Hardware")
	_ = s.SaveUserShoppingItemInSection("Nails", "Hardware", "Fasteners")

	stores, _ := s.GetShoppingStores()
	if err := s.DeleteShoppingStore(stores[0].ID); err != nil {
		t.Fatalf("delete: %v", err)
	}

	items, _ := s.GetUserShoppingItems()
	if len(items) != 1 {
		t.Fatalf("got %d items, want the item kept", len(items))
	}
	if items[0].Store != "Unassigned" || items[0].Section != "" {
		t.Errorf("got %+v, want it moved to Unassigned with no section", items[0])
	}
}

func TestShoppingSections_AddedAndReturnedWithTheirStore(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.AddShoppingStore("Costco")
	stores, _ := s.GetShoppingStores()
	if err := s.AddShoppingSection(stores[0].ID, "Produce"); err != nil {
		t.Fatalf("add section: %v", err)
	}
	_ = s.AddShoppingSection(stores[0].ID, "Frozen")

	stores, _ = s.GetShoppingStores()
	if len(stores[0].Sections) != 2 {
		t.Fatalf("got %d sections, want 2", len(stores[0].Sections))
	}
}

// The same section name in two different stores is normal ("Produce" exists in
// both) and must not collide.
func TestShoppingSections_SameNameInDifferentStores(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.AddShoppingStore("Costco")
	_ = s.AddShoppingStore("Safeway")
	stores, _ := s.GetShoppingStores()
	for _, st := range stores {
		if err := s.AddShoppingSection(st.ID, "Produce"); err != nil {
			t.Fatalf("add section to %s: %v", st.Name, err)
		}
	}
	stores, _ = s.GetShoppingStores()
	for _, st := range stores {
		if len(st.Sections) != 1 {
			t.Errorf("%s has %d sections, want 1", st.Name, len(st.Sections))
		}
	}
}

func TestShoppingSections_DeleteClearsItFromItems(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.AddShoppingStore("Costco")
	stores, _ := s.GetShoppingStores()
	_ = s.AddShoppingSection(stores[0].ID, "Produce")
	_ = s.SaveUserShoppingItemInSection("Kale", "Costco", "Produce")

	stores, _ = s.GetShoppingStores()
	if err := s.DeleteShoppingSection(stores[0].Sections[0].ID); err != nil {
		t.Fatalf("delete section: %v", err)
	}

	items, _ := s.GetUserShoppingItems()
	if len(items) != 1 {
		t.Fatalf("item was deleted along with its section")
	}
	if items[0].Section != "" {
		t.Errorf("Section = %q, want cleared", items[0].Section)
	}
	if items[0].Store != "Costco" {
		t.Errorf("Store = %q, want unchanged", items[0].Store)
	}
}

// Deleting a store's section must cascade, not leave orphaned rows pointing at
// a store that no longer exists.
func TestShoppingSections_CascadeWhenStoreDeleted(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.AddShoppingStore("Costco")
	stores, _ := s.GetShoppingStores()
	_ = s.AddShoppingSection(stores[0].ID, "Produce")

	if err := s.DeleteShoppingStore(stores[0].ID); err != nil {
		t.Fatalf("delete store: %v", err)
	}
	var n int
	if err := s.db.QueryRow(`SELECT COUNT(*) FROM shopping_sections`).Scan(&n); err != nil {
		t.Fatalf("count: %v", err)
	}
	if n != 0 {
		t.Errorf("%d orphaned sections remain", n)
	}
}

// ── item editing ─────────────────────────────────────────────────────────────

func TestUpdateUserShoppingItem_RenamesAndMoves(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.SaveUserShoppingItemInSection("Mlik", "Costco", "Dairy")
	items, _ := s.GetUserShoppingItems()

	if err := s.UpdateUserShoppingItem(items[0].ID, "Milk", "Safeway", "Refrigerated"); err != nil {
		t.Fatalf("update: %v", err)
	}

	got, err := s.GetUserShoppingItem(items[0].ID)
	if err != nil {
		t.Fatalf("get: %v", err)
	}
	if got.Name != "Milk" || got.Store != "Safeway" || got.Section != "Refrigerated" {
		t.Errorf("got %+v, want renamed and moved", got)
	}
}

func TestUpdateUserShoppingItem_ClearingTheSection(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.SaveUserShoppingItemInSection("Kale", "Costco", "Produce")
	items, _ := s.GetUserShoppingItems()

	if err := s.UpdateUserShoppingItem(items[0].ID, "Kale", "Costco", ""); err != nil {
		t.Fatalf("update: %v", err)
	}
	got, _ := s.GetUserShoppingItem(items[0].ID)
	if got.Section != "" {
		t.Errorf("Section = %q, want cleared", got.Section)
	}
}

// A SQL UPDATE that matches nothing still succeeds, so a stale id would look
// like a successful edit and the user would think the change stuck.
func TestUpdateUserShoppingItem_UnknownIDIsAnError(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	if err := s.UpdateUserShoppingItem(999, "Ghost", "Costco", ""); !errors.Is(err, ErrItemNotFound) {
		t.Errorf("got %v, want ErrItemNotFound", err)
	}
}

func TestUpdateUserShoppingItem_RejectsBlankNameOrStore(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()

	_ = s.SaveUserShoppingItem("Milk", "Costco")
	items, _ := s.GetUserShoppingItems()

	if err := s.UpdateUserShoppingItem(items[0].ID, "  ", "Costco", ""); err == nil {
		t.Error("expected an error for a blank name")
	}
	if err := s.UpdateUserShoppingItem(items[0].ID, "Milk", "  ", ""); err == nil {
		t.Error("expected an error for a blank store")
	}
}

func TestGetUserShoppingItem_NotFound(t *testing.T) {
	s := setupRegistryStore(t)
	defer func() { _ = s.Close() }()
	if _, err := s.GetUserShoppingItem(999); !errors.Is(err, ErrItemNotFound) {
		t.Errorf("got %v, want ErrItemNotFound", err)
	}
}