Skip to content

Commit

Permalink
chore: Add support for empty locations array in IsLocationInRange fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
waveyboym committed Aug 22, 2024
1 parent a6ce937 commit b831e09
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
31 changes: 27 additions & 4 deletions occupi-backend/pkg/database/database_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,36 @@ func IsLocationInRange(locations []models.Location, unrecognizedLogger *ipinfo.C
}

for _, loc := range locations {
// Skip if loc.Location is empty
if loc.Location == "" {
continue
}

coords1 := strings.Split(loc.Location, ",")
lat1, _ := strconv.ParseFloat(coords1[0], 64)
lon1, _ := strconv.ParseFloat(coords1[1], 64)
// Skip if coords1 does not contain exactly 2 elements (latitude and longitude)
if len(coords1) != 2 {
continue
}

lat1, err1 := strconv.ParseFloat(coords1[0], 64)
lon1, err2 := strconv.ParseFloat(coords1[1], 64)
// Skip if parsing latitude or longitude fails
if err1 != nil || err2 != nil {
continue
}

coords2 := strings.Split(unrecognizedLogger.Location, ",")
lat2, _ := strconv.ParseFloat(coords2[0], 64)
lon2, _ := strconv.ParseFloat(coords2[1], 64)
// Skip if coords2 does not contain exactly 2 elements (latitude and longitude)
if len(coords2) != 2 {
continue
}

lat2, err3 := strconv.ParseFloat(coords2[0], 64)
lon2, err4 := strconv.ParseFloat(coords2[1], 64)
// Skip if parsing latitude or longitude fails
if err3 != nil || err4 != nil {
continue
}

loc1 := haversine.Coord{Lat: lat1, Lon: lon1}
loc2 := haversine.Coord{Lat: lat2, Lon: lon2}
Expand Down
40 changes: 40 additions & 0 deletions occupi-backend/tests/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4789,6 +4789,46 @@ func TestIsLocationInRange(t *testing.T) {
unrecognizedLogger: &ipinfo.Core{Location: "34.0522,-118.2437"}, // Los Angeles
expected: true,
},
{
name: "Empty Location String",
locations: []models.Location{
{City: "CityF", Region: "RegionF", Country: "CountryF", Location: ""}, // Empty location
},
unrecognizedLogger: &ipinfo.Core{Location: "34.0522,-118.2437"}, // Los Angeles
expected: false, // Should skip and return false since there's no valid location within range
},
{
name: "Invalid Location Format (empty string) for Unrecognized Logger",
locations: []models.Location{
{City: "CityG", Region: "RegionG", Country: "CountryG", Location: "40.7128,-74.0060"}, // New York
},
unrecognizedLogger: &ipinfo.Core{Location: ""}, // Empty location
expected: false, // Should skip and return false since the location format is invalid
},
{
name: "Invalid Location Format (single coordinate)",
locations: []models.Location{
{City: "CityG", Region: "RegionG", Country: "CountryG", Location: "40.7128"}, // Incomplete location
},
unrecognizedLogger: &ipinfo.Core{Location: "34.0522,-118.2437"}, // Los Angeles
expected: false, // Should skip and return false since the location format is invalid
},
{
name: "Invalid Location Format (non-numeric coordinates)",
locations: []models.Location{
{City: "CityH", Region: "RegionH", Country: "CountryH", Location: "abc,xyz"}, // Non-numeric location
},
unrecognizedLogger: &ipinfo.Core{Location: "34.0522,-118.2437"}, // Los Angeles
expected: false, // Should skip and return false since the location format is invalid
},
{
name: "Invalid Location Format (non-numeric latitude) for Unrecognized Logger",
locations: []models.Location{
{City: "CityI", Region: "RegionI", Country: "CountryI", Location: "40.7128,-74.0060"}, // New York
},
unrecognizedLogger: &ipinfo.Core{Location: "abc,-118"}, // Non-numeric latitude
expected: false, // Should skip and return false since the location format is invalid
},
}

for _, tt := range tests {
Expand Down

0 comments on commit b831e09

Please sign in to comment.