Skip to content

Commit db5f754

Browse files
committed
updated tests
1 parent 68b771f commit db5f754

File tree

2 files changed

+92
-10
lines changed

2 files changed

+92
-10
lines changed

api/apipatrols.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ type Patrol struct {
3333
}
3434

3535
type PatrolSegment struct {
36-
ID string `json:"id"`
36+
ID string `json:"id"`
3737
Leader *struct {
3838
Name string `json:"name"`
3939
} `json:"leader"`
4040
PatrolType string `json:"patrol_type"`
4141
StartLocation *Location `json:"start_location"`
42+
EndLocation *Location `json:"end_location"`
4243
TimeRange TimeRange `json:"time_range"`
4344
}
4445

api/patrols_test.go

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ func TestPatrols(t *testing.T) {
3636
"title": "Test Patrol",
3737
"patrol_segments": [
3838
{
39+
"id": "segment123",
3940
"leader": {"name": "John Doe"},
4041
"patrol_type": "boat_patrol",
4142
"start_location": {"latitude": 1.234, "longitude": 5.678},
43+
"end_location": null,
4244
"time_range": {
4345
"start_time": "2025-01-15T10:00:00.000Z",
44-
"end_time": "2025-01-15T11:00:00.000Z"
46+
"end_time": null
4547
}
4648
}
4749
]
@@ -82,16 +84,27 @@ func TestPatrols(t *testing.T) {
8284
if len(patrol.PatrolSegments) == 0 {
8385
t.Fatal("Expected at least one patrol segment")
8486
}
85-
if patrol.PatrolSegments[0].Leader == nil {
87+
segment := patrol.PatrolSegments[0]
88+
if segment.ID != "segment123" {
89+
t.Errorf("Expected segment ID 'segment123', got '%s'", segment.ID)
90+
}
91+
if segment.Leader == nil {
8692
t.Fatal("Expected non-nil leader")
8793
}
88-
if patrol.PatrolSegments[0].Leader.Name != "John Doe" {
89-
t.Errorf("Expected leader name 'John Doe', got '%s'", patrol.PatrolSegments[0].Leader.Name)
94+
if segment.Leader.Name != "John Doe" {
95+
t.Errorf("Expected leader name 'John Doe', got '%s'", segment.Leader.Name)
96+
}
97+
// Open patrol should not have end location or end time
98+
if segment.EndLocation != nil {
99+
t.Error("Expected nil end location for open patrol")
100+
}
101+
if segment.TimeRange.EndTime != nil {
102+
t.Error("Expected nil end time for open patrol")
90103
}
91104
},
92105
},
93106
{
94-
name: "successful response with date filter",
107+
name: "successful response with done patrol having end location and time",
95108
days: 7,
96109
status: "",
97110
mockResponse: `{
@@ -101,7 +114,21 @@ func TestPatrols(t *testing.T) {
101114
{
102115
"id": "test456",
103116
"serial_number": 1002,
104-
"state": "closed"
117+
"state": "done",
118+
"title": "Completed Patrol",
119+
"patrol_segments": [
120+
{
121+
"id": "segment456",
122+
"leader": {"name": "Jane Smith"},
123+
"patrol_type": "foot_patrol",
124+
"start_location": {"latitude": 2.345, "longitude": 6.789},
125+
"end_location": {"latitude": 2.355, "longitude": 6.799},
126+
"time_range": {
127+
"start_time": "2025-01-15T10:00:00.000Z",
128+
"end_time": "2025-01-15T14:00:00.000Z"
129+
}
130+
}
131+
]
105132
}
106133
]
107134
},
@@ -129,6 +156,33 @@ func TestPatrols(t *testing.T) {
129156
if len(response.Data.Results) != 1 {
130157
t.Errorf("Expected 1 result, got %d", len(response.Data.Results))
131158
}
159+
patrol := response.Data.Results[0]
160+
if patrol.State != "done" {
161+
t.Errorf("Expected state 'done', got '%s'", patrol.State)
162+
}
163+
if len(patrol.PatrolSegments) == 0 {
164+
t.Fatal("Expected at least one patrol segment")
165+
}
166+
segment := patrol.PatrolSegments[0]
167+
if segment.ID != "segment456" {
168+
t.Errorf("Expected segment ID 'segment456', got '%s'", segment.ID)
169+
}
170+
// Done patrol should have end location and end time
171+
if segment.EndLocation == nil {
172+
t.Fatal("Expected non-nil end location for done patrol")
173+
}
174+
if segment.EndLocation.Latitude != 2.355 {
175+
t.Errorf("Expected end latitude 2.355, got %f", segment.EndLocation.Latitude)
176+
}
177+
if segment.EndLocation.Longitude != 6.799 {
178+
t.Errorf("Expected end longitude 6.799, got %f", segment.EndLocation.Longitude)
179+
}
180+
if segment.TimeRange.EndTime == nil {
181+
t.Fatal("Expected non-nil end time for done patrol")
182+
}
183+
if *segment.TimeRange.EndTime != "2025-01-15T14:00:00.000Z" {
184+
t.Errorf("Expected end time '2025-01-15T14:00:00.000Z', got '%s'", *segment.TimeRange.EndTime)
185+
}
132186
},
133187
},
134188
{
@@ -142,7 +196,19 @@ func TestPatrols(t *testing.T) {
142196
{
143197
"id": "test789",
144198
"serial_number": 1003,
145-
"state": "active"
199+
"state": "active",
200+
"patrol_segments": [
201+
{
202+
"id": "segment789",
203+
"patrol_type": "vehicle_patrol",
204+
"start_location": {"latitude": 3.456, "longitude": 7.890},
205+
"end_location": null,
206+
"time_range": {
207+
"start_time": "2025-01-15T08:00:00.000Z",
208+
"end_time": null
209+
}
210+
}
211+
]
146212
}
147213
]
148214
},
@@ -170,8 +236,23 @@ func TestPatrols(t *testing.T) {
170236
if len(response.Data.Results) != 1 {
171237
t.Errorf("Expected 1 result, got %d", len(response.Data.Results))
172238
}
173-
if response.Data.Results[0].State != "active" {
174-
t.Errorf("Expected state 'active', got '%s'", response.Data.Results[0].State)
239+
patrol := response.Data.Results[0]
240+
if patrol.State != "active" {
241+
t.Errorf("Expected state 'active', got '%s'", patrol.State)
242+
}
243+
if len(patrol.PatrolSegments) == 0 {
244+
t.Fatal("Expected at least one patrol segment")
245+
}
246+
segment := patrol.PatrolSegments[0]
247+
if segment.ID != "segment789" {
248+
t.Errorf("Expected segment ID 'segment789', got '%s'", segment.ID)
249+
}
250+
// Active patrol should not have end location or end time
251+
if segment.EndLocation != nil {
252+
t.Error("Expected nil end location for active patrol")
253+
}
254+
if segment.TimeRange.EndTime != nil {
255+
t.Error("Expected nil end time for active patrol")
175256
}
176257
},
177258
},

0 commit comments

Comments
 (0)