]> git.lizzy.rs Git - go-anidb.git/blob - cache_test.go
anidb: Delete invalid cache data
[go-anidb.git] / cache_test.go
1 package anidb
2
3 import (
4         "encoding/gob"
5         "os"
6         "path"
7         "reflect"
8         "testing"
9 )
10
11 type stringifyVec struct {
12         result []string
13         data   []cacheKey
14 }
15
16 func TestStringify(T *testing.T) {
17         T.Parallel()
18
19         vec := []stringifyVec{
20                 stringifyVec{[]string{"a"}, []cacheKey{"a"}},
21         }
22         for i, v := range vec {
23                 str := stringify(v.data...)
24                 if !reflect.DeepEqual(v.result, str) {
25                         T.Errorf("Vector #%d: Expected %v, got %v", i+1, v.result, str)
26                 }
27         }
28 }
29
30 type cachePathVec struct {
31         path string
32         data []cacheKey
33 }
34
35 var testDir = path.Join(os.TempDir(), "testing", "anidb")
36
37 func init() { SetCacheDir(testDir) }
38
39 func TestCachePath(T *testing.T) {
40         T.Parallel()
41
42         vec := []cachePathVec{
43                 cachePathVec{path.Join(testDir, "a"), []cacheKey{"a"}},
44                 cachePathVec{path.Join(testDir, "b", "c", "d"), []cacheKey{"b", "c", "d"}},
45         }
46         for i, v := range vec {
47                 str := cachePath(v.data...)
48
49                 if v.path != str {
50                         T.Errorf("Vector #%d: Expected %v, got %v", i+1, v.path, str)
51                 }
52         }
53 }
54
55 type testString string
56
57 func (_ testString) Touch()        {}
58 func (_ testString) IsStale() bool { return false }
59
60 func init() {
61         gob.Register(testString(""))
62 }
63
64 func TestCacheRoundtrip(T *testing.T) {
65         T.Parallel()
66
67         test := testString("some string")
68         _, err := cache.Set(test, "test", "string")
69         if err != nil {
70                 T.Fatalf("Error storing: %v", err)
71         }
72
73         var t2 testString
74         err = cache.Get(&t2, "test", "string")
75         if err != nil {
76                 T.Errorf("Error reading: %v", err)
77         }
78
79         if test != t2 {
80                 T.Errorf("Expected %q, got %q", test, t2)
81         }
82 }