-
Notifications
You must be signed in to change notification settings - Fork 7
/
anki_test.go
145 lines (132 loc) · 3.4 KB
/
anki_test.go
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
// Copyright: Jonathan Hall
// License: GNU AGPL, Version 3 or later; http://www.gnu.org/licenses/agpl.html
package anki
import (
"io/ioutil"
"os"
"testing"
)
const ApkgFile = "t/Test.apkg"
func TestReadFile(t *testing.T) {
if _, err := ReadFile("does not exist"); err.Error() != "open does not exist: no such file or directory" {
t.Fatalf("Unexpected error trying to open non-existant file: %s", err)
}
apkg, err := ReadFile(ApkgFile)
if err != nil {
t.Fatalf("Error opening test file: %s", err)
}
if err := apkg.Close(); err != nil {
t.Fatalf("Error closing apkg: %s", err)
}
}
func TestReadReader(t *testing.T) {
file, err := os.Open(ApkgFile)
if err != nil {
t.Fatalf("Error opening test file: %s", err)
}
fi, err := file.Stat()
if err != nil {
t.Fatalf("Error statting file: %s", err)
}
apkg, err := ReadReader(file, fi.Size())
if err != nil {
t.Fatalf("Error opening apkg from Reader: %s", err)
}
collection, err := apkg.Collection()
if err != nil {
t.Fatalf("Error getting collection: %s", err)
}
if collection.Config.CollapseTime != 1200 {
t.Fatalf("Spot-check failed")
}
notes, err := apkg.Notes()
if err != nil {
t.Fatalf("Error fetching notes: %s", err)
}
for notes.Next() {
note, err := notes.Note()
if err != nil {
t.Fatalf("Error reading note: %s", err)
}
if note.ID != 1388721680877 {
t.Fatalf("note spot-check failed. Expected ID 1388721680877, got %d", note.ID)
}
if note.Checksum != 1090091728 {
t.Fatalf("note spot-check failed. Expected checksum 1090091728, got %d", note.Checksum)
}
}
if err := notes.Close(); err != nil {
t.Fatalf("Error closing Notes: %s", err)
}
reviews, err := apkg.Reviews()
if err != nil {
t.Fatalf("Error fetching reviews: %s", err)
}
for reviews.Next() {
review, err := reviews.Review()
if err != nil {
t.Fatalf("Error reading review: %s", err)
}
if review.CardID != 1388721683902 {
t.Fatalf("review spot-check failed. Expected cid 1388721683902, got %d", review.CardID)
}
}
if err := reviews.Close(); err != nil {
t.Fatalf("Error closing Reviews: %s", err)
}
cards, err := apkg.Cards()
if err != nil {
t.Fatalf("Error fetching cards: %s", err)
}
for cards.Next() {
card, err := cards.Card()
if err != nil {
t.Fatalf("Error reading card: %s", err)
}
if card.ID != 1388721683902 {
t.Fatalf("card spot-check failed. Expected ID 1388721683902, got %d", card.ID)
}
}
if err := cards.Close(); err != nil {
t.Fatalf("Error closing Cards: %s", err)
}
model, ok := collection.Models[1357356563296]
if !ok {
t.Fatalf("Cannot find model '135735656329'")
}
if model.ID != 1357356563296 {
t.Fatalf("Unexpected Model ID: %d", model.ID)
}
if err := apkg.Close(); err != nil {
t.Fatalf("Error closing apkg from Reader: %s", err)
}
}
func TestReadBytes(t *testing.T) {
file, err := os.Open(ApkgFile)
if err != nil {
t.Fatalf("Error opening test file: %s", err)
}
buf, err := ioutil.ReadAll(file)
if err != nil {
t.Fatalf("Error reading test file: %s", err)
}
apkg, err := ReadBytes(buf)
if err != nil {
t.Fatalf("Error calling ReadBytes: %s\n", err)
}
// Try counting cards
if cards, err := apkg.Cards(); err != nil {
t.Fatalf("Error calling Cards(): %s", err)
} else {
var count int
for cards.Next() {
count++
}
if count != 1 {
t.Fatalf("Expected 1 card, found %d\n", count)
}
}
if err := apkg.Close(); err != nil {
t.Fatalf("Error closing apkg: %s", err)
}
}