This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifier_twitter_test.go
132 lines (125 loc) · 4.71 KB
/
notifier_twitter_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
package main
import (
"fmt"
"testing"
"unicode/utf8"
)
func TestBuildHashtags(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"MSI GeForce RTX 3060 GAMING X", "#nvidia #rtx3060"},
{"MSI GeForce RTX 3060 Ti GAMING X", "#nvidia #rtx3060ti"}, // with space (3060 Ti)
{"MSI RTX 3060Ti VENTUS 2X OC", "#nvidia #rtx3060ti"}, // without space (3060Ti)
{"MSI GeForce RTX 3070 GAMING TRIO", "#nvidia #rtx3070"},
{"MSI GeForce RTX 3080 SUPRIM X", "#nvidia #rtx3080"},
{"MSI GeForce RTX 3090 GAMING X TRIO 24G", "#nvidia #rtx3090"},
{"MSI Radeon RX 5700 XT GAMING X", "#amd #rx5700xt"}, // with space (5700 XT)
{"ASUS Radeon RX 5700XT ROG-STRIX-RX5700XT-O8G-GAMING", "#amd #rx5700xt"}, // without space (5700XT)
{"MSI Radeon RX 6800", "#amd #rx6800"},
{"MSI Radeon RX 6800 XT", "#amd #rx6800xt"}, // with space (6800 XT)
{"POWERCOLOR RX 6800XT Red Dragon", "#amd #rx6800xt"}, // without space (6800XT)
{"MSI Radeon RX 6900 XT GAMING X TRIO 16G", "#amd #rx6900xt"}, // with space (6900 XT)
{"POWERCOLOR RED DEVIL RX 6900XT 16GB", "#amd #rx6900xt"}, // without space (6900XT)
{"unknown product", ""},
{"", ""},
}
notifier := TwitterNotifier{
client: nil,
user: nil,
db: nil,
hashtagsMap: []map[string]string{
{"rtx 3060( )?ti": "#nvidia #rtx3060ti"},
{"rtx 3060": "#nvidia #rtx3060"},
{"rtx 3070": "#nvidia #rtx3070"},
{"rtx 3080": "#nvidia #rtx3080"},
{"rtx 3090": "#nvidia #rtx3090"},
{"rx 6800( )?xt": "#amd #rx6800xt"},
{"rx 6900( )?xt": "#amd #rx6900xt"},
{"rx 6800": "#amd #rx6800"},
{"rx 5700( )?xt": "#amd #rx5700xt"},
},
}
for i, tc := range tests {
t.Run(fmt.Sprintf("TestBuildHashtags#%d", i), func(t *testing.T) {
got := notifier.buildHashtags(tc.input)
if got != tc.expected {
t.Errorf("for %s, got %s, want %s", tc.input, got, tc.expected)
} else {
t.Logf("for %s, want %s, got %s", tc.input, got, tc.expected)
}
})
}
}
func TestFormatAvailableTweet(t *testing.T) {
tests := []struct {
shopName string
productName string
productPrice float64
productCurrency string
productURL string
hashtags string
counter int64
expected string
}{
{
// short product name
"shop.com",
"my awesome product",
999.99,
"USD",
"https://shop.com/awesome",
"#awesome #product",
0,
"shop.com: my awesome product for $999.99 is available at https://shop.com/awesome #awesome #product",
},
{
// long product name
"shop.com",
"my awesome product with very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long name",
999.99,
"USD",
"https://shop.com/awesome",
"#awesome #product",
0,
"shop.com: my awesome product with very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very… for $999.99 is available at https://shop.com/awesome #awesome #product",
},
{
// short product name with a counter
"shop.com",
"my awesome product",
999.99,
"USD",
"https://shop.com/awesome",
"#awesome #product",
2,
"shop.com: my awesome product for $999.99 is available at https://shop.com/awesome #awesome #product (2)",
},
{
// long product name with a counter
"shop.com",
"my awesome product with very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long name",
999.99,
"USD",
"https://shop.com/awesome",
"#awesome #product",
2,
"shop.com: my awesome product with very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very … for $999.99 is available at https://shop.com/awesome #awesome #product (2)",
},
}
for i, tc := range tests {
t.Run(fmt.Sprintf("TestFormatAvailableTweet#%d", i), func(t *testing.T) {
got := formatAvailableTweet(tc.shopName, tc.productName, tc.productPrice, tc.productCurrency, tc.productURL, tc.hashtags, tc.counter)
if got != tc.expected {
t.Errorf("for %s, got '%s', want '%s'", tc.productName, got, tc.expected)
} else {
t.Logf("for %s, got '%s', want '%s'", tc.productName, got, tc.expected)
}
// ensure generated tweet doesn't exceed maximum length
if utf8.RuneCountInString(got) > tweetMaxSize {
t.Errorf("for %s, got '%s' which exceeed tweet maximum lenght of %d chars", tc.productName, got, tweetMaxSize)
}
})
}
}