-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbAdData_test.go
152 lines (130 loc) · 3.73 KB
/
DbAdData_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
146
147
148
149
150
151
152
package main
import (
"io/ioutil"
"os"
"testing"
)
// TestLoadOrCreate checks that an empty bucket is created for each search.
func TestLoadOrCreate(t *testing.T) {
// Having
searches := createSingletonSearches("toto")
tmpfile := createTemporaryFile(t)
defer deleteTemporaryFile(tmpfile)
// when
dbAdData, _ := LoadOrCreate(tmpfile.Name(), searches)
// Then
datas, _ := dbAdData.GetAllAds(searches[0])
if datas == nil || len(datas) != 0 {
t.Errorf("the bucket of data should be non null and empty : %v", datas)
}
otherSearches := createSingletonSearches("tata")
datas, _ = dbAdData.GetAllAds(otherSearches[0])
if datas == nil || len(datas) != 0 {
t.Errorf("the bucket of data should be non null and empty : %v", datas)
}
}
func TestIsAdKnown(t *testing.T) {
// having
searches := createSingletonSearches("toto")
tmpfile := createTemporaryFile(t)
defer deleteTemporaryFile(tmpfile)
adData := createSampleAd(123)
var dbAdData, _ = LoadOrCreate(tmpfile.Name(), searches)
// when
dbAdData.SaveAd(searches[0], *adData)
// then
if known, _ := dbAdData.IsAdKnown(searches[0], *adData); !known {
t.Fail()
}
adData2 := createSampleAd(456)
if known, _ := dbAdData.IsAdKnown(searches[0], *adData2); known {
t.Fail()
}
}
func TestGetAllAds(t *testing.T) {
// having
searches := createSingletonSearches("toto")
tmpfile := createTemporaryFile(t)
defer deleteTemporaryFile(tmpfile)
var dbAdData, _ = LoadOrCreate(tmpfile.Name(), searches)
dbAdData.SaveAd(searches[0], *createSampleAd(123))
dbAdData.SaveAd(searches[0], *createSampleAd(456))
// when
var ads, _ = dbAdData.GetAllAds(searches[0])
// then
if len(ads) != 2 {
t.Fail()
}
}
func TestGetAd(t *testing.T) {
// having
searches := createSingletonSearches("toto")
tmpfile := createTemporaryFile(t)
defer deleteTemporaryFile(tmpfile)
var dbAdData, _ = LoadOrCreate(tmpfile.Name(), searches)
dbAdData.SaveAd(searches[0], *createSampleAd(123))
dbAdData.SaveAd(searches[0], *createSampleAd(456))
// when
var ad1, _ = dbAdData.GetAd(searches[0], 123)
var ad2, _ = dbAdData.GetAd(searches[0], 456)
var ad3, _ = dbAdData.GetAd(searches[0], 789)
// then
if ad1.Id != 123 {
t.Errorf("expected Id 123, found: %d", ad1.Id)
}
if ad2.Id != 456 {
t.Errorf("expected Id 456, found: %d", ad2.Id)
}
if ad3 != nil {
t.Error("expected nil value")
}
}
func TestSaveAd(t *testing.T) {
// having
searches := createSingletonSearches("toto")
tmpfile := createTemporaryFile(t)
defer deleteTemporaryFile(tmpfile)
var dbAdData, _ = LoadOrCreate(tmpfile.Name(), searches)
// when
dbAdData.SaveAd(searches[0], *createSampleAdWithTitle(123, "1"))
dbAdData.SaveAd(searches[0], *createSampleAdWithTitle(123, "1bis"))
dbAdData.SaveAd(searches[0], *createSampleAdWithTitle(456, "2"))
// then
var ad *AdData
if ad, _ = dbAdData.GetAd(searches[0], 123); ad.Title != "1bis" {
t.Errorf("expected title 1bis, found: %s", ad.Title)
}
if ad, _ = dbAdData.GetAd(searches[0], 456); ad.Title != "2" {
t.Errorf("expected title 1bis, found: %s", ad.Title)
}
if ad, _ = dbAdData.GetAd(searches[0], 789); &ad == nil {
t.Errorf("expected nil value, found: %v", ad)
}
}
func createSingletonSearches(name string) []Search {
search := new(Search)
search.Name = name
search.Terms = "tata"
searches := make([]Search, 1)
searches[0] = *search
return searches
}
func createTemporaryFile(t *testing.T) *os.File {
tmpFile, err := ioutil.TempFile("", "sample.db")
if err != nil {
t.Error(err)
}
return tmpFile
}
func deleteTemporaryFile(tmpFile *os.File) {
os.Remove(tmpFile.Name())
}
func createSampleAd(id int) *AdData {
return createSampleAdWithTitle(id, "")
}
func createSampleAdWithTitle(id int, title string) *AdData {
adData := new(AdData)
adData.Id = id
adData.Title = title
return adData
}