storage_test.go

  1package routing
  2
  3import (
  4	"encoding/json"
  5	"os"
  6	"path/filepath"
  7	"testing"
  8
  9	"github.com/stretchr/testify/assert"
 10	"github.com/stretchr/testify/require"
 11)
 12
 13func TestRouteStorage_SaveRoute(t *testing.T) {
 14	tempDir := t.TempDir()
 15	storage := NewRouteStorage(tempDir)
 16
 17	points := [][]float64{
 18		{83.45, 27.70},
 19		{83.46, 27.71},
 20		{83.47, 27.72},
 21	}
 22	routeID := HashPoints(points)
 23	distance := 5.5
 24	routeTime := 600.0
 25	bbox := []float64{83.45, 27.70, 83.47, 27.72}
 26
 27	err := storage.SaveRoute(routeID, points, distance, routeTime, bbox)
 28	require.NoError(t, err)
 29
 30	expectedPath := filepath.Join(tempDir, routeID[0:2], routeID[2:]+".geojson")
 31	_, err = os.Stat(expectedPath)
 32	require.NoError(t, err, "GeoJSON file should exist")
 33
 34	data, err := os.ReadFile(expectedPath)
 35	require.NoError(t, err)
 36
 37	// Define proper struct for GeoJSON Feature
 38	type geoJSONFeature struct {
 39		Type     string `json:"type"`
 40		Geometry struct {
 41			Type        string      `json:"type"`
 42			Coordinates [][]float64 `json:"coordinates"`
 43		} `json:"geometry"`
 44		Properties map[string]interface{} `json:"properties"`
 45	}
 46
 47	var feature geoJSONFeature
 48	err = json.Unmarshal(data, &feature)
 49	require.NoError(t, err)
 50
 51	assert.Equal(t, "Feature", feature.Type)
 52	assert.Equal(t, "LineString", feature.Geometry.Type)
 53	assert.Equal(t, points, feature.Geometry.Coordinates)
 54	assert.Equal(t, routeID, feature.Properties["id"])
 55	assert.Equal(t, distance, feature.Properties["distance"])
 56	assert.Equal(t, routeTime, feature.Properties["time"])
 57}
 58
 59func TestRouteStorage_SaveElevation(t *testing.T) {
 60	tempDir := t.TempDir()
 61	storage := NewRouteStorage(tempDir)
 62
 63	points := [][]float64{
 64		{83.45, 27.70},
 65		{83.46, 27.71},
 66	}
 67	routeID := HashPoints(points)
 68	elevation := []float64{1200.5, 1250.0}
 69
 70	err := storage.SaveElevation(routeID, elevation)
 71	require.NoError(t, err)
 72
 73	expectedPath := filepath.Join(tempDir, routeID[0:2], routeID[2:]+".elevation.json")
 74	data, err := os.ReadFile(expectedPath)
 75	require.NoError(t, err)
 76
 77	var savedElevation []float64
 78	err = json.Unmarshal(data, &savedElevation)
 79	require.NoError(t, err)
 80	assert.Equal(t, elevation, savedElevation)
 81}
 82
 83func TestRouteStorage_InvalidRouteID(t *testing.T) {
 84	tempDir := t.TempDir()
 85	storage := NewRouteStorage(tempDir)
 86
 87	points := [][]float64{{83.45, 27.70}}
 88
 89	err := storage.SaveRoute("ab", points, 1.0, 100.0, []float64{0, 0, 1, 1})
 90	assert.Error(t, err)
 91	assert.Contains(t, err.Error(), "invalid route ID")
 92}
 93
 94func TestHashPoints(t *testing.T) {
 95	points1 := [][]float64{
 96		{83.45, 27.70},
 97		{83.46, 27.71},
 98	}
 99	points2 := [][]float64{
100		{83.45, 27.70},
101		{83.46, 27.71},
102	}
103	points3 := [][]float64{
104		{83.46, 27.71},
105		{83.45, 27.70},
106	}
107
108	hash1 := HashPoints(points1)
109	hash2 := HashPoints(points2)
110	hash3 := HashPoints(points3)
111
112	assert.Equal(t, hash1, hash2, "Same points should produce same hash")
113	assert.NotEqual(t, hash1, hash3, "Different points should produce different hash")
114	assert.Len(t, hash1, 64, "SHA256 hex should be 64 characters")
115}