Skip to content

Commit

Permalink
Merge pull request #2 from mprimi/master
Browse files Browse the repository at this point in the history
Add store test
  • Loading branch information
mrusme authored Sep 3, 2022
2 parents d728fe1 + 493b2a7 commit 66e79a8
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
80 changes: 80 additions & 0 deletions store/store_test.go
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))
}
}
31 changes: 31 additions & 0 deletions store/testdata/4-cards.vcf
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

0 comments on commit 66e79a8

Please sign in to comment.