-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
178 lines (158 loc) · 4.96 KB
/
integration_test.go
File metadata and controls
178 lines (158 loc) · 4.96 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/csv"
"encoding/json"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDomainJSONUnmarshaling(t *testing.T) {
tests := []struct {
name string
jsonData string
expected []Domain
}{
{
name: "single domain",
jsonData: `[{"name":"example.com","order_status":"active","order_datetime":"2023-01-01T00:00:00Z","registration_datetime":"2023-01-01T00:00:00Z","delete_datetime":""}]`,
expected: []Domain{
{
Name: "example.com",
OrderStatus: "active",
OrderDateTime: "2023-01-01T00:00:00Z",
RegistrationDateTime: "2023-01-01T00:00:00Z",
DeleteDateTime: "",
},
},
},
{
name: "multiple domains",
jsonData: `[{"name":"example.com","order_status":"active","order_datetime":"2023-01-01T00:00:00Z","registration_datetime":"2023-01-01T00:00:00Z","delete_datetime":""},{"name":"test.com","order_status":"deleted","order_datetime":"2022-01-01T00:00:00Z","registration_datetime":"2022-01-01T00:00:00Z","delete_datetime":"2023-06-01T00:00:00Z"}]`,
expected: []Domain{
{
Name: "example.com",
OrderStatus: "active",
OrderDateTime: "2023-01-01T00:00:00Z",
RegistrationDateTime: "2023-01-01T00:00:00Z",
DeleteDateTime: "",
},
{
Name: "test.com",
OrderStatus: "deleted",
OrderDateTime: "2022-01-01T00:00:00Z",
RegistrationDateTime: "2022-01-01T00:00:00Z",
DeleteDateTime: "2023-06-01T00:00:00Z",
},
},
},
{
name: "empty array",
jsonData: `[]`,
expected: []Domain{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var domains []Domain
err := json.Unmarshal([]byte(tt.jsonData), &domains)
require.NoError(t, err, "Failed to unmarshal JSON")
assert.Equal(t, tt.expected, domains, "Unmarshaled domains don't match expected")
})
}
}
func TestCSVWriting(t *testing.T) {
// Create a temporary file for testing
tmpFile, err := os.CreateTemp("", "test_export_*.csv")
require.NoError(t, err, "Failed to create temp file")
defer os.Remove(tmpFile.Name()) // Clean up
// Test data
domains := []Domain{
{
Name: "example.com",
OrderDateTime: "2023-01-01T00:00:00Z",
RegistrationDateTime: "2023-01-01T00:00:00Z",
DeleteDateTime: "",
},
{
Name: "test.com",
OrderDateTime: "2022-01-01T00:00:00Z",
RegistrationDateTime: "2022-01-01T00:00:00Z",
DeleteDateTime: "2023-06-01T00:00:00Z",
},
}
cutoffDate := time.Date(2023, 6, 1, 0, 0, 0, 0, time.UTC)
// Write CSV data
csvWriter := csv.NewWriter(tmpFile)
headerWritten := false
for _, domain := range domains {
if !headerWritten {
csvWriter.Write([]string{
"Domain",
"Order Date",
"Reg Date",
"Close Date",
})
headerWritten = true
}
if domain.IsBelowCutoff(cutoffDate) {
// Parse dates
dateOrd, _ := parseAPIdate(domain.OrderDateTime)
dateReg, _ := parseAPIdate(domain.RegistrationDateTime)
// Format Delete date for output
dateDelFmt := ""
if domain.DeleteDateTime != "" {
parsedDate, _ := parseAPIdate(domain.DeleteDateTime)
dateDelFmt = parsedDate.Format("2006-01-02")
}
csvWriter.Write([]string{
domain.Name,
dateOrd.Format("2006-01-02"),
dateReg.Format("2006-01-02"),
dateDelFmt,
})
}
}
csvWriter.Flush()
tmpFile.Close()
// Read and verify the CSV content
content, err := os.ReadFile(tmpFile.Name())
require.NoError(t, err, "Failed to read temp file")
lines := strings.Split(strings.TrimSpace(string(content)), "\n")
assert.Len(t, lines, 2, "Expected header + 1 data row") // Only example.com should be included
// Verify header
assert.Equal(t, "Domain,Order Date,Reg Date,Close Date", lines[0])
// Verify data row (only example.com should be included as test.com is deleted before cutoff)
assert.Equal(t, "example.com,2023-01-01,2023-01-01,", lines[1])
}
func TestDomainFilteringWithCutoff(t *testing.T) {
cutoffDate := time.Date(2023, 6, 1, 0, 0, 0, 0, time.UTC)
domains := []Domain{
{
Name: "active.com",
DeleteDateTime: "", // No delete date - should be included
},
{
Name: "deleted-after.com",
DeleteDateTime: "2023-07-01T00:00:00Z", // Deleted after cutoff - should be included
},
{
Name: "deleted-before.com",
DeleteDateTime: "2023-05-01T00:00:00Z", // Deleted before cutoff - should be excluded
},
{
Name: "deleted-on-cutoff.com",
DeleteDateTime: "2023-06-01T00:00:00Z", // Deleted exactly on cutoff - should be excluded
},
}
var includedDomains []string
for _, domain := range domains {
if domain.IsBelowCutoff(cutoffDate) {
includedDomains = append(includedDomains, domain.Name)
}
}
expected := []string{"active.com", "deleted-after.com"}
assert.Equal(t, expected, includedDomains, "Filtered domains don't match expected")
}