-
Notifications
You must be signed in to change notification settings - Fork 0
/
E2Etest.go
126 lines (102 loc) · 3.37 KB
/
E2Etest.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
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"sort"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestEndToEndFlow(t *testing.T) {
// 1. Mock an input CSV file (simulate raw delivery data)
inputData := `id_delivery,lat,lng,timestamp
1,35.0,51.0,1609459200
1,35.1,51.1,1609459260
2,36.0,52.0,1609459320
2,36.1,52.1,1609459380`
// Create a temporary input file
inputFile, err := os.CreateTemp("", "test_input.csv")
assert.NoError(t, err)
defer os.Remove(inputFile.Name())
_, err = inputFile.Write([]byte(inputData))
assert.NoError(t, err)
inputFile.Close()
// 2. Create temporary output files for filtered data and fares
filteredFile, err := os.CreateTemp("", "test_filtered_data.csv")
assert.NoError(t, err)
defer os.Remove(filteredFile.Name())
faresFile, err := os.CreateTemp("", "test_fares.csv")
assert.NoError(t, err)
defer os.Remove(faresFile.Name())
// 3. Run the full processing flow (similar to main function)
chunks, err := readDataChunks(inputFile.Name())
assert.NoError(t, err)
outputFile, err := os.Create(filteredFile.Name())
assert.NoError(t, err)
defer outputFile.Close()
writer := csv.NewWriter(outputFile)
defer writer.Flush()
// Write header for the filtered data
writer.Write([]string{"id_delivery", "lat", "lng", "timestamp"})
// Store fares in a map as in main.go
fares := make(map[int]string)
startTime := time.Now() // Capture start time for the elapsed time calculation
for chunk := range chunks {
if len(chunk) == 0 {
continue
}
// Filter invalid points
filteredChunk := filterInvalidPoints(chunk)
for _, point := range filteredChunk {
err := writer.Write([]string{
point.ID,
fmt.Sprintf("%f", point.Latitude),
fmt.Sprintf("%f", point.Longitude),
fmt.Sprintf("%d", point.Timestamp),
})
assert.NoError(t, err)
}
writer.Flush()
// Calculate fare and write to result file
fare := calculateFare(filteredChunk)
deliveryID := filteredChunk[0].ID
elapsed := time.Since(startTime)
fmt.Printf("Calculating delivery %s, total time elapsed: %v Please wait...\n", deliveryID, elapsed)
id, _ := strconv.Atoi(deliveryID)
fares[id] = fmt.Sprintf("%s,%.2f", deliveryID, fare)
}
// 4. Write the fare results to the fares file
outputFareResultsFromMap(fares, faresFile.Name())
// 5. Verify the results
// Checks that the fare-estimatea were correctly written
fareData, err := os.ReadFile(faresFile.Name())
assert.NoError(t, err)
assert.Contains(t, string(fareData), "1,") // Check fare for id_delivery 1
assert.Contains(t, string(fareData), "2,") // Check fare for id_delivery 2
// Optionally, check the content of filtered data
filteredData, err := os.ReadFile(filteredFile.Name())
assert.NoError(t, err)
assert.Contains(t, string(filteredData), "1,35.0,51.0")
}
// Helper function to write fare results from a map (used in main)
func outputFareResultsFromMap(fares map[int]string, filePath string) {
outputFile, err := os.Create(filePath)
if err != nil {
log.Fatal(err)
}
defer outputFile.Close()
writer := csv.NewWriter(outputFile)
defer writer.Flush()
writer.Write([]string{"id_delivery", "fare_estimate"})
var deliveryIDs []int
for id := range fares {
deliveryIDs = append(deliveryIDs, id)
}
sort.Ints(deliveryIDs)
for _, id := range deliveryIDs {
writer.Write([]string{strconv.Itoa(id), fares[id][len(strconv.Itoa(id))+1:]}) // strip id from fare
}
}