-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mprimi/master
Add store test
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package store | ||
|
||
import ( | ||
"testing" | ||
"path/filepath" | ||
"io" | ||
"os" | ||
|
||
"github.com/emersion/go-vcard" | ||
) | ||
|
||
func loadTestCards(t *testing.T, fileName string) []*vcard.Card { | ||
t.Helper() | ||
|
||
f, err := os.Open(filepath.Join("testdata", fileName)) | ||
if err != nil { | ||
t.Fatalf("Failed to open %s: %v", fileName, err) | ||
} | ||
defer f.Close() | ||
|
||
cards := make([]*vcard.Card, 0) | ||
|
||
dec := vcard.NewDecoder(f) | ||
for { | ||
card, err := dec.Decode() | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
t.Fatalf("Failed to load card: %v", err) | ||
} | ||
cards = append(cards, &card) | ||
} | ||
t.Logf("Loaded %d cards from %s", len(cards), fileName) | ||
return cards | ||
|
||
} | ||
|
||
func TestOpenNewAddReopenFind(t *testing.T) { | ||
|
||
// Create new store, tempdir will auto-delete | ||
storePath := filepath.Join(t.TempDir(), "addrb.db") | ||
|
||
// Load some cards from testdata | ||
cards := loadTestCards(t, "4-cards.vcf") | ||
|
||
// Create new store | ||
store, err := Open(storePath) | ||
if err != nil { | ||
t.Fatalf("Failed to open: %v", err) | ||
} | ||
defer store.Close() | ||
|
||
// Add cards | ||
if err := store.Upsert(cards); err != nil { | ||
t.Fatalf("Failed to add cards: %v", err) | ||
} | ||
|
||
store.Close() | ||
|
||
// Reopen store | ||
store, err = Open(storePath) | ||
if err != nil { | ||
t.Fatalf("Failed to reopen: %v", err) | ||
} | ||
defer store.Close() | ||
|
||
foundCards, err := store.FindBy(vcard.FieldFormattedName, "Doe") | ||
if err != nil { | ||
t.Fatalf("Failed to find: %v", err) | ||
} | ||
|
||
t.Logf("Found %d cards: ", len(foundCards)) | ||
for _, card := range foundCards { | ||
t.Logf(" - %s", card.PreferredValue(vcard.FieldFormattedName)) | ||
} | ||
|
||
if len(foundCards) < 2 { | ||
t.Fatalf("Expected at least 2 cards, got %d", len(foundCards)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
UID:test_entry_1 | ||
N:Doe;Jane;;; | ||
FN:Jane Doe | ||
EMAIL;type=INTERNET;type=pref:[email protected] | ||
TEL;type=pref:+10000001 | ||
END:VCARD | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
UID:test_entry_2 | ||
N:Smith;Doctor;;; | ||
FN:Doctor Smith | ||
TEL;type=WORK;type=VOICE;type=pref:+390000000002 | ||
END:VCARD | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
UID:test_entry_3 | ||
N:Doe;John;;; | ||
FN:John Doe | ||
ORG:E-Corp; | ||
EMAIL;type=INTERNET;type=HOME;type=pref:[email protected] | ||
TEL;type=pref:+10000003 | ||
END:VCARD | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
UID:test_entry_4 | ||
N:Oaktree;Eve;;; | ||
FN:Eve Oaktree | ||
TEL;type=pref:+340000000004 | ||
END:VCARD |