-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpypi_xml_rpc_test.go
116 lines (94 loc) · 3.21 KB
/
pypi_xml_rpc_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
package ingestors
import (
"testing"
"time"
)
type ingestionTest struct {
action string
expected bool
}
var ingestionTests = []ingestionTest{
{"new release", true},
{"yank release", true},
{"unyank release", true},
{"remove release", true},
{"something else", false},
}
func TestPyPiXmlRpcResponse_IsIngestionAction(t *testing.T) {
for _, test := range ingestionTests {
response := PyPiXmlRpcResponse{
"what", "ever", 1, test.action, 1234,
}
if response.IsIngestionAction() != test.expected {
t.Errorf("for %s, got %t, wanted %t", test.action, response.IsIngestionAction(), test.expected)
}
}
}
func TestPyPiXmlRpcResponse_GetPackageVersion(t *testing.T) {
// get a number a set number of seconds ago
// this is the fastest way to strip off microseconds
now := time.Unix(time.Now().Unix(), 0)
fiveSecondsAgoDuration, err := time.ParseDuration("-5s")
if err != nil {
t.Error("Unable to parse test duration string")
}
fiveSecondsAgo := now.Add(fiveSecondsAgoDuration)
response := PyPiXmlRpcResponse{
"name", "version", fiveSecondsAgo.Unix(), "whatever", 1235,
}
packageVersion := response.GetPackageVersion()
// check if this value is within one second
discoveryLagVsFiveSecondDuration := packageVersion.DiscoveryLag + fiveSecondsAgoDuration
// 1 second = 1000000000 microseconds
if discoveryLagVsFiveSecondDuration >= 1000000000 {
t.Errorf("DiscoveryLag is not correct, %d is not within 1 second", discoveryLagVsFiveSecondDuration)
}
if packageVersion.Name != "name" {
t.Error("name is not correct")
}
if packageVersion.Version != "version" {
t.Error("version is not correct")
}
if packageVersion.CreatedAt != fiveSecondsAgo {
t.Errorf("CreatedAt is not correct, expected %#v, got %#v", fiveSecondsAgo, packageVersion.CreatedAt)
}
}
type logResponseTest struct {
log []any
message string
}
var logResponsesFailures = []logResponseTest{
{[]any{nil, "1.0.0", int64(100), "action", 1000}, "package name is not a string"},
{[]any{"name", nil, int64(100), "action", 1001}, "version is not a string"},
{[]any{"name", "1.0.0", nil, "action", 1002}, "created at date is not an int64 number"},
{[]any{"name", "1.0.0", int64(100), nil, 1003}, "action is not a string"},
}
func TestCreateResponseStruct_Failure(t *testing.T) {
for _, test := range logResponsesFailures {
_, err := createResponseStruct(test.log)
if err.Error() != test.message {
t.Errorf("expect message %s, got %s", test.message, err.Error())
}
}
}
func TestCreateResponseStruct_Success(t *testing.T) {
response, err := createResponseStruct([]any{"name", "1.0.0", int64(100), "action", int64(1000)})
if err != nil {
t.Errorf("expected err to be nil, got %#v", err)
}
if response.Name != "name" {
t.Errorf("expected name to equal %s, got %s", "name", response.Name)
}
if response.Version != "1.0.0" {
t.Errorf("expected version to equal %s, got %s", "1.0.0", response.Version)
}
if response.Timestamp != int64(100) {
t.Errorf("expected timestamp to equal %d, got %d", 100, response.Timestamp)
}
if response.Action != "action" {
t.Errorf("expected action to equal %s, got %s", "action", response.Action)
}
if response.Serial != 1000 {
t.Errorf("expected serial to equal %d, got %d", 1000, response.Serial)
}
}