Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/implement distance #13

Merged
merged 4 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package main

func main() {}
func main() {
}
40 changes: 40 additions & 0 deletions repository/distance/calculate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package distance

import (
"math"
"mock-shipping-provider/primitive"
"mock-shipping-provider/repository"
)

type CalculateDistance struct {
Radius float64
ServicableDistance float64
}

func NewCalculateDistance() repository.DistanceCalculation {
return &CalculateDistance{
Radius: 6371.0,
ServicableDistance: 5245.0,
}
}

func (c *CalculateDistance) Calculate(from primitive.Coordinate, to primitive.Coordinate) (distance float64, serviceable bool) {
lat1Rad := from.Latitude * math.Pi / 180
lon1Rad := from.Longitude * math.Pi / 180
lat2Rad := to.Latitude * math.Pi / 180
lon2Rad := to.Longitude * math.Pi / 180

dlat := lat2Rad - lat1Rad
dlon := lon2Rad - lon1Rad

a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Cos(lat1Rad)*math.Cos(lat2Rad)*math.Sin(dlon/2)*math.Sin(dlon/2)
cal := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))

distanceCalculation := c.Radius * cal

if distanceCalculation > c.ServicableDistance {
return distanceCalculation, false
}

return distanceCalculation, true
}
44 changes: 44 additions & 0 deletions repository/distance/calculate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package distance_test

import (
"mock-shipping-provider/primitive"
"mock-shipping-provider/repository/distance"
"testing"
)

func TestDistanceCalculation(t *testing.T) {
distanceCalculation := distance.NewCalculateDistance()

t.Run("test distance not service", func(t *testing.T) {
from := primitive.Coordinate{
Latitude: -4.5387718,
Longitude: 120.3146973,
}

to := primitive.Coordinate{
Latitude: 40.7128,
Longitude: -74.0060,
}
distance, serviceable := distanceCalculation.Calculate(from, to)
if serviceable {
t.Errorf("must not servicable: %v", distance)
}
})

t.Run("test distance serviceable", func(t *testing.T) {
from := primitive.Coordinate{
Latitude: -6.1601629,
Longitude: 106.6793193,
}

to := primitive.Coordinate{
Latitude: -4.5387718,
Longitude: 120.3146973,
}
distance, serviceable := distanceCalculation.Calculate(from, to)
if !serviceable {
t.Errorf("must be servicable: %v", distance)
}
})

}
Loading