-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_import_test.go
More file actions
213 lines (186 loc) · 5.82 KB
/
main_import_test.go
File metadata and controls
213 lines (186 loc) · 5.82 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"bytes"
"codeplugs/api"
"codeplugs/database"
"codeplugs/models"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestImportResurrectsSoftDeleted(t *testing.T) {
tmpDB, _ := os.CreateTemp("", "test-resurrect-*.db")
defer os.Remove(tmpDB.Name())
database.Connect(tmpDB.Name())
// 1. Create a Contact
contact := models.DigitalContact{
Name: "Ghost Contact",
DMRID: 666,
Callsign: "GHOST",
}
database.DB.Create(&contact)
// 2. Soft Delete it
database.DB.Delete(&contact)
// Verify it's gone (gorm default scope)
var count int64
database.DB.Model(&models.DigitalContact{}).Where("dmr_id = ?", 666).Count(&count)
if count != 0 {
t.Fatalf("Expected 0 contacts after delete, got %d", count)
}
// 3. Import CSV containing the SAME contact
radioCSV := `radio_id,callsign,first_name,last_name
666,GHOST,Ghost,Contact
`
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
writer.WriteField("format", "radioid")
part, _ := writer.CreateFormFile("file", "radioid.csv")
part.Write([]byte(radioCSV))
writer.Close()
req, _ := http.NewRequest("POST", "/api/import", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
rr := httptest.NewRecorder()
http.HandlerFunc(api.HandleImport).ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("Import failed with status: %d", rr.Code)
}
// 4. Verify Contact is Resurrected
var resurrected models.DigitalContact
if err := database.DB.Where("dmr_id = ?", 666).First(&resurrected).Error; err != nil {
t.Fatalf("Failed to find resurrected contact: %v", err)
}
if resurrected.DeletedAt.Valid {
t.Error("Contact should be active (DeletedAt invalid), but it is valid (deleted)")
}
}
func TestImportGenericFallback(t *testing.T) {
// Setup DB
tmpDB, _ := os.CreateTemp("", "test-fallback-*.db")
defer os.Remove(tmpDB.Name())
database.Connect(tmpDB.Name())
defer database.Close()
// Minimal CSV data WITHOUT Location/CrossMode to force Generic Importer
// But WITH Power and NFM to test the new logic in Generic importer
// Also use imprecise freq to test rounding
csvData := `Name,Frequency,Mode,Power
GenericFallbackCh,146.520000000000001,NFM,50W
`
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", "test_fallback.csv")
if err != nil {
t.Fatal(err)
}
part.Write([]byte(csvData))
writer.WriteField("format", "generic")
writer.Close()
req, err := http.NewRequest("POST", "/api/import", body)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
rr := httptest.NewRecorder()
http.HandlerFunc(api.HandleImport).ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// Verify DB content
var ch models.Channel
database.DB.Where("name = ?", "GenericFallbackCh").First(&ch)
if ch.ID == 0 {
t.Fatal("Channel GenericFallbackCh not found in DB")
}
// Check Power (should be "High" from 50W mapping in Generic importer now)
if ch.Power != "High" {
t.Errorf("Expected Power 'High', got '%s'", ch.Power)
}
// Check Bandwidth/Type default for NFM (should be 12.5)
if ch.Bandwidth != "12.5" {
t.Errorf("Expected Bandwidth '12.5', got '%s'", ch.Bandwidth)
}
if ch.Type != models.ChannelTypeAnalog {
t.Errorf("Expected Type 'Analog', got '%s'", ch.Type)
}
// Check SquelchType default
if ch.SquelchType != "None" {
t.Errorf("Expected SquelchType 'None', got '%s'", ch.SquelchType)
}
// Check Frequency Rounding
if ch.RxFrequency != 146.52 {
t.Errorf("Expected RxFrequency 146.52, got %v", ch.RxFrequency)
}
}
func TestImportSingleFile(t *testing.T) {
// Setup DB
tmpDB, _ := os.CreateTemp("", "test-single-*.db")
defer os.Remove(tmpDB.Name())
database.Connect(tmpDB.Name())
defer database.Close()
// 1. Test Generic Talkgroup Import
tgCSV := `Name,ID,Type
MyTalkgroup,12345,Group
PrivateContact,99999,Private
`
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", "talkgroups.csv")
if err != nil {
t.Fatal(err)
}
part.Write([]byte(tgCSV))
writer.WriteField("format", "single")
writer.WriteField("import_type", "talkgroups")
writer.WriteField("radio_platform", "generic")
writer.Close()
req, _ := http.NewRequest("POST", "/api/import", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
rr := httptest.NewRecorder()
http.HandlerFunc(api.HandleImport).ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("Talkgroup import failed with status: %d", rr.Code)
}
// Verify DB
var contacts []models.Contact
database.DB.Find(&contacts)
if len(contacts) != 2 {
t.Errorf("Expected 2 contacts, got %d", len(contacts))
}
for _, c := range contacts {
if c.DMRID == 12345 {
if c.Type != models.ContactTypeGroup {
t.Errorf("Expected Group type for 12345")
}
}
if c.DMRID == 99999 {
if c.Type != models.ContactTypePrivate {
t.Errorf("Expected Private type for 99999")
}
}
}
// 2. Test Generic Channel Import
chanCSV := `Name,Rx Freq
SingleChan,145.500
`
body2 := &bytes.Buffer{}
writer2 := multipart.NewWriter(body2)
part2, _ := writer2.CreateFormFile("file", "channel.csv")
part2.Write([]byte(chanCSV))
writer2.WriteField("format", "single")
writer2.WriteField("import_type", "channels")
writer2.WriteField("radio_platform", "generic")
writer2.Close()
req2, _ := http.NewRequest("POST", "/api/import", body2)
req2.Header.Set("Content-Type", writer2.FormDataContentType())
rr2 := httptest.NewRecorder()
http.HandlerFunc(api.HandleImport).ServeHTTP(rr2, req2)
if rr2.Code != http.StatusOK {
t.Fatalf("Channel import failed with status: %d", rr2.Code)
}
var ch models.Channel
database.DB.First(&ch, "name = ?", "SingleChan")
if ch.ID == 0 {
t.Error("Expected channel SingleChan to be imported")
}
}