-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb_test.go
More file actions
571 lines (498 loc) · 14.2 KB
/
db_test.go
File metadata and controls
571 lines (498 loc) · 14.2 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package tests
import (
"log"
"path/filepath"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"testing"
"varuh"
)
func createMockDb(fileName string) error {
// Just open it with GORM/SQLite driver
db, err := gorm.Open(sqlite.Open(fileName), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return err
}
// This will create the DB file with a proper SQLite header if it doesnt exist
sqlDB, _ := db.DB()
defer sqlDB.Close()
log.Printf("SQLite database %s created and ready.\n", fileName)
return nil
}
func TestEntry_Copy(t *testing.T) {
tests := []struct {
name string
e1 *varuh.Entry
e2 *varuh.Entry
want *varuh.Entry
}{
{
name: "copy password entry",
e1: &varuh.Entry{},
e2: &varuh.Entry{
Title: "Test Title",
User: "[email protected]",
Url: "https://example.com",
Password: "secret123",
Notes: "Test notes",
Tags: "test,example",
Type: "password",
},
want: &varuh.Entry{
Title: "Test Title",
User: "[email protected]",
Url: "https://example.com",
Password: "secret123",
Notes: "Test notes",
Tags: "test,example",
Type: "password",
},
},
{
name: "copy card entry",
e1: &varuh.Entry{},
e2: &varuh.Entry{
Title: "Test Card",
User: "John Doe",
Issuer: "Chase Bank",
Url: "4111111111111111",
Password: "123",
ExpiryDate: "12/25",
Tags: "credit,card",
Notes: "Main card",
Type: "card",
},
want: &varuh.Entry{
Title: "Test Card",
User: "John Doe",
Issuer: "Chase Bank",
Url: "4111111111111111",
Password: "123",
ExpiryDate: "12/25",
Tags: "credit,card",
Notes: "Main card",
Type: "card",
},
},
{
name: "copy nil entry",
e1: &varuh.Entry{Title: "Original", User: "[email protected]"},
e2: nil,
want: &varuh.Entry{Title: "Original", User: "[email protected]"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.e1.Copy(tt.e2)
if tt.e2 == nil {
// Should remain unchanged
if tt.e1.Title != tt.want.Title || tt.e1.User != tt.want.User {
t.Errorf("Entry should remain unchanged when copying nil")
}
return
}
// Compare relevant fields based on type
switch tt.e2.Type {
case "password":
if tt.e1.Title != tt.want.Title ||
tt.e1.User != tt.want.User ||
tt.e1.Url != tt.want.Url ||
tt.e1.Password != tt.want.Password ||
tt.e1.Notes != tt.want.Notes ||
tt.e1.Tags != tt.want.Tags ||
tt.e1.Type != tt.want.Type {
t.Errorf("Password entry copy failed")
}
case "card":
if tt.e1.Title != tt.want.Title ||
tt.e1.User != tt.want.User ||
tt.e1.Issuer != tt.want.Issuer ||
tt.e1.Url != tt.want.Url ||
tt.e1.Password != tt.want.Password ||
tt.e1.ExpiryDate != tt.want.ExpiryDate ||
tt.e1.Tags != tt.want.Tags ||
tt.e1.Notes != tt.want.Notes ||
tt.e1.Type != tt.want.Type {
t.Errorf("Card entry copy failed")
}
}
})
}
}
func TestExtendedEntry_Copy(t *testing.T) {
tests := []struct {
name string
e1 *varuh.ExtendedEntry
e2 *varuh.ExtendedEntry
want *varuh.ExtendedEntry
}{
{
name: "copy extended entry",
e1: &varuh.ExtendedEntry{},
e2: &varuh.ExtendedEntry{
FieldName: "CustomField1",
FieldValue: "CustomValue1",
EntryID: 123,
},
want: &varuh.ExtendedEntry{
FieldName: "CustomField1",
FieldValue: "CustomValue1",
EntryID: 123,
},
},
{
name: "copy nil extended entry",
e1: &varuh.ExtendedEntry{
FieldName: "Original",
FieldValue: "OriginalValue",
EntryID: 1,
},
e2: nil,
want: &varuh.ExtendedEntry{
FieldName: "Original",
FieldValue: "OriginalValue",
EntryID: 1,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.e1.Copy(tt.e2)
if tt.e2 == nil {
// Should remain unchanged
if tt.e1.FieldName != tt.want.FieldName ||
tt.e1.FieldValue != tt.want.FieldValue ||
tt.e1.EntryID != tt.want.EntryID {
t.Errorf("ExtendedEntry should remain unchanged when copying nil")
}
return
}
if tt.e1.FieldName != tt.want.FieldName ||
tt.e1.FieldValue != tt.want.FieldValue ||
tt.e1.EntryID != tt.want.EntryID {
t.Errorf("ExtendedEntry copy failed, got FieldName=%s FieldValue=%s EntryID=%d, want FieldName=%s FieldValue=%s EntryID=%d",
tt.e1.FieldName, tt.e1.FieldValue, tt.e1.EntryID,
tt.want.FieldName, tt.want.FieldValue, tt.want.EntryID)
}
})
}
}
func TestOpenDatabase(t *testing.T) {
tempDir := t.TempDir()
// Create a test SQLite database file
testDB := filepath.Join(tempDir, "test.db")
err := createMockDb(testDB)
if err != nil {
t.Fatalf("Failed to create test database file: %v", err)
}
tests := []struct {
name string
filePath string
wantErr bool
}{
{"valid database", testDB, false},
{"empty path", "", true},
{"non-existent file", filepath.Join(tempDir, "nonexistent.db"), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err, db := varuh.OpenDatabase(tt.filePath)
if (err != nil) != tt.wantErr {
t.Errorf("OpenDatabase() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && db == nil {
t.Error("OpenDatabase() returned nil database for valid input")
}
})
}
}
func TestCreateNewEntry(t *testing.T) {
tempDir := t.TempDir()
testDB := filepath.Join(tempDir, "test.db")
// Create a basic SQLite file
err := createMockDb(testDB)
if err != nil {
t.Fatalf("Failed to create test database file: %v", err)
}
err, db := varuh.OpenDatabase(testDB)
if err != nil {
t.Fatalf("Failed to open test database: %v", err)
}
err = varuh.CreateNewEntry(db)
if err != nil {
t.Errorf("CreateNewEntry() error = %v", err)
}
}
func TestCreateNewExEntry(t *testing.T) {
tempDir := t.TempDir()
testDB := filepath.Join(tempDir, "test.db")
// Create a basic SQLite file
err := createMockDb(testDB)
if err != nil {
t.Fatalf("Failed to create test database file: %v", err)
}
err, db := varuh.OpenDatabase(testDB)
if err != nil {
t.Fatalf("Failed to open test database: %v", err)
}
err = varuh.CreateNewExEntry(db)
if err != nil {
t.Errorf("CreateNewExEntry() error = %v", err)
}
}
func TestInitNewDatabase(t *testing.T) {
tempDir := t.TempDir()
tests := []struct {
name string
dbPath string
wantErr bool
}{
{"valid path", filepath.Join(tempDir, "new.db"), false},
{"empty path", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Skip tests that depend on global state/config
if tt.name == "valid path" {
t.Skip("Skipping InitNewDatabase test as it depends on global config state")
}
err := varuh.InitNewDatabase(tt.dbPath)
if (err != nil) != tt.wantErr {
t.Errorf("InitNewDatabase() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetEntryById(t *testing.T) {
// This function depends on active database state
// We'll test that it doesn't panic and returns appropriate error
t.Run("no active database", func(t *testing.T) {
err, entry := varuh.GetEntryById(1)
// Should return an error or nil entry when no active database
if err == nil && entry != nil {
t.Error("GetEntryById() should return error or nil entry when no active database")
}
})
t.Run("invalid id", func(t *testing.T) {
err, entry := varuh.GetEntryById(-1)
// Should handle invalid IDs gracefully
if entry != nil && entry.ID == -1 {
t.Error("GetEntryById() should not return entry with invalid ID")
}
_ = err // err can be nil or non-nil depending on database state
})
}
func TestSearchDatabaseEntry(t *testing.T) {
// This function depends on active database state
t.Run("empty search term", func(t *testing.T) {
err, entries := varuh.SearchDatabaseEntry("")
// Should handle empty search term gracefully
_ = err // err can be nil or non-nil depending on database state
_ = entries // entries can be empty or nil
})
t.Run("normal search term", func(t *testing.T) {
err, entries := varuh.SearchDatabaseEntry("test")
// Should handle normal search without panic
_ = err // err can be nil or non-nil depending on database state
_ = entries // entries can be empty or nil
})
}
func TestUnion(t *testing.T) {
entry1 := varuh.Entry{ID: 1, Title: "Entry 1"}
entry2 := varuh.Entry{ID: 2, Title: "Entry 2"}
entry3 := varuh.Entry{ID: 3, Title: "Entry 3"}
entry1Dup := varuh.Entry{ID: 1, Title: "Entry 1 Duplicate"}
tests := []struct {
name string
slice1 []varuh.Entry
slice2 []varuh.Entry
want int // expected length of result
}{
{
name: "empty slices",
slice1: []varuh.Entry{},
slice2: []varuh.Entry{},
want: 0,
},
{
name: "no overlap",
slice1: []varuh.Entry{entry1, entry2},
slice2: []varuh.Entry{entry3},
want: 3,
},
{
name: "with overlap",
slice1: []varuh.Entry{entry1, entry2},
slice2: []varuh.Entry{entry1Dup, entry3},
want: 3, // should not duplicate entry1
},
}
// Use reflection to call the unexported union function
// Since it's unexported, we'll test the public functions that use it
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// We can't directly test the unexported union function
// So we test SearchDatabaseEntries which uses it internally
terms := []string{"term1", "term2"}
_, entries := varuh.SearchDatabaseEntries(terms, "OR")
// Just ensure no panic occurs and entries is a valid slice
if entries == nil {
entries = []varuh.Entry{}
}
_ = len(entries) // Use the result to avoid unused variable error
})
}
}
func TestSearchDatabaseEntries(t *testing.T) {
tests := []struct {
name string
terms []string
operator string
}{
{"empty terms", []string{}, "AND"},
{"single term", []string{"test"}, "AND"},
{"multiple terms AND", []string{"test", "example"}, "AND"},
{"multiple terms OR", []string{"test", "example"}, "OR"},
{"invalid operator", []string{"test"}, "INVALID"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err, entries := varuh.SearchDatabaseEntries(tt.terms, tt.operator)
// Should handle all cases without panic
_ = err // err can be nil or non-nil depending on database state
_ = entries // entries can be empty or nil
})
}
}
func TestRemoveDatabaseEntry(t *testing.T) {
entry := &varuh.Entry{ID: 1, Title: "Test Entry"}
t.Run("remove entry", func(t *testing.T) {
err := varuh.RemoveDatabaseEntry(entry)
// Should handle gracefully whether or not there's an active database
_ = err // err can be nil or non-nil depending on database state
})
}
func TestCloneEntry(t *testing.T) {
entry := &varuh.Entry{
ID: 1,
Title: "Original Entry",
User: "[email protected]",
Password: "secret123",
Type: "password",
}
t.Run("clone entry", func(t *testing.T) {
err, clonedEntry := varuh.CloneEntry(entry)
// Should handle gracefully whether or not there's an active database
_ = err // err can be nil or non-nil depending on database state
_ = clonedEntry // clonedEntry can be nil if no active database
})
}
func TestIterateEntries(t *testing.T) {
tests := []struct {
name string
orderKey string
order string
}{
{"order by id asc", "id", "asc"},
{"order by title desc", "title", "desc"},
{"order by timestamp asc", "timestamp", "asc"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err, entries := varuh.IterateEntries(tt.orderKey, tt.order)
// Should handle all cases without panic
_ = err // err can be nil or non-nil depending on database state
_ = entries // entries can be empty or nil
})
}
}
func TestEntriesToStringArray(t *testing.T) {
tests := []struct {
name string
skipLongFields bool
}{
{"include long fields", false},
{"skip long fields", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err, dataArray := varuh.EntriesToStringArray(tt.skipLongFields)
// Should handle gracefully whether or not there's an active database
_ = err // err can be nil or non-nil depending on database state
_ = dataArray // dataArray can be empty or nil
})
}
}
func TestGetExtendedEntries(t *testing.T) {
entry := &varuh.Entry{ID: 1, Title: "Test Entry"}
t.Run("get extended entries", func(t *testing.T) {
extEntries := varuh.GetExtendedEntries(entry)
// Should return a valid slice (can be empty)
if extEntries == nil {
extEntries = []varuh.ExtendedEntry{}
}
_ = len(extEntries) // Use the result
})
}
// Integration tests that would work with actual database setup
func TestEntryTableName(t *testing.T) {
entry := &varuh.Entry{}
if entry.TableName() != "entries" {
t.Errorf("Entry.TableName() = %s, want entries", entry.TableName())
}
}
func TestExtendedEntryTableName(t *testing.T) {
extEntry := &varuh.ExtendedEntry{}
if extEntry.TableName() != "exentries" {
t.Errorf("ExtendedEntry.TableName() = %s, want exentries", extEntry.TableName())
}
}
func TestAddressTableName(t *testing.T) {
address := &varuh.Address{}
if address.TableName() != "address" {
t.Errorf("Address.TableName() = %s, want address", address.TableName())
}
}
// Test that database operations handle nil inputs gracefully
func TestDatabaseOperationsWithNilInputs(t *testing.T) {
t.Run("operations with nil entry", func(t *testing.T) {
// Test that functions handle nil entries gracefully
err := varuh.RemoveDatabaseEntry(nil)
_ = err // Should not panic, may return error
_, cloned := varuh.CloneEntry(nil)
_ = cloned // Should not panic, may return nil
extEntries := varuh.GetExtendedEntries(nil)
if extEntries == nil {
extEntries = []varuh.ExtendedEntry{}
}
_ = len(extEntries)
})
}
// Benchmark tests
func BenchmarkEntry_Copy(b *testing.B) {
e1 := &varuh.Entry{}
e2 := &varuh.Entry{
Title: "Benchmark Title",
User: "[email protected]",
Password: "secret123",
Type: "password",
}
for i := 0; i < b.N; i++ {
e1.Copy(e2)
}
}
func BenchmarkExtendedEntry_Copy(b *testing.B) {
e1 := &varuh.ExtendedEntry{}
e2 := &varuh.ExtendedEntry{
FieldName: "BenchField",
FieldValue: "BenchValue",
EntryID: 1,
}
for i := 0; i < b.N; i++ {
e1.Copy(e2)
}
}