-
Notifications
You must be signed in to change notification settings - Fork 18
/
campaigns_test.go
107 lines (96 loc) · 2.58 KB
/
campaigns_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
package main
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var camp = CampaignAd{
Placeholder: "placholder",
Ratio: 0.5,
Id: "id",
Probability: 1,
Fallback: true,
Ad: Ad{
Source: "source",
Image: "image",
Link: "http://link.com",
Description: "desc",
Company: "company",
},
}
func TestAddAndFetchCampaigns(t *testing.T) {
migrateDatabase()
initializeDatabase()
defer tearDatabase()
defer dropDatabase()
err := addCampaign(context.Background(), ScheduledCampaignAd{
CampaignAd: camp,
Start: time.Now().Add(time.Hour * -1),
End: time.Now().Add(time.Hour),
})
assert.Nil(t, err)
err = addOrUpdateUserTags(context.Background(), "1", []string{"javascript"})
assert.Nil(t, err)
var res []CampaignAd
res, err = fetchCampaigns(context.Background(), time.Now(), "1")
assert.Nil(t, err)
assert.Equal(t, []CampaignAd{camp}, res)
}
func TestFetchExpiredCampaigns(t *testing.T) {
migrateDatabase()
initializeDatabase()
defer tearDatabase()
defer dropDatabase()
err := addCampaign(context.Background(), ScheduledCampaignAd{
CampaignAd: camp,
Start: time.Now().Add(time.Hour * -2),
End: time.Now().Add(time.Hour * -1),
})
assert.Nil(t, err)
var res []CampaignAd
res, err = fetchCampaigns(context.Background(), time.Now(), "1")
assert.Nil(t, err)
assert.Equal(t, []CampaignAd(nil), res)
}
func TestFetchCampaignsWithTags(t *testing.T) {
migrateDatabase()
initializeDatabase()
defer tearDatabase()
defer dropDatabase()
err := addCampaign(context.Background(), ScheduledCampaignAd{
CampaignAd: camp,
Start: time.Now().Add(time.Hour * -1),
End: time.Now().Add(time.Hour),
})
assert.Nil(t, err)
err = addCampaign(context.Background(), ScheduledCampaignAd{
CampaignAd: CampaignAd{
Placeholder: "placholder",
Ratio: 0.5,
Id: "id2",
Probability: 1,
Fallback: true,
Ad: Ad{
Source: "source",
Image: "image",
Link: "http://link.com",
Description: "desc",
Company: "company",
},
},
Start: time.Now().Add(time.Hour * -1),
End: time.Now().Add(time.Hour),
})
assert.Nil(t, err)
err = addOrUpdateUserTags(context.Background(), "1", []string{"javascript"})
assert.Nil(t, err)
_, err = db.Exec("insert into ad_tags (ad_id, tag) values ('id', ?), ('id2', ?)", "javascript", "php")
assert.Nil(t, err)
var res []CampaignAd
res, err = fetchCampaigns(context.Background(), time.Now(), "1")
dup := camp
dup.IsTagTargeted = true
assert.Nil(t, err)
assert.Equal(t, []CampaignAd{dup}, res)
}