turash/bugulma/backend/internal/domain/geographical_feature_test.go

183 lines
6.2 KiB
Go

package domain_test
import (
"encoding/json"
"testing"
"bugulma/backend/internal/domain"
"github.com/stretchr/testify/assert"
"gorm.io/datatypes"
)
func TestGeographicalFeatureType_Constants(t *testing.T) {
assert.Equal(t, domain.GeographicalFeatureType("road"), domain.GeographicalFeatureTypeRoad)
assert.Equal(t, domain.GeographicalFeatureType("green_space"), domain.GeographicalFeatureTypeGreenSpace)
assert.Equal(t, domain.GeographicalFeatureType("poi"), domain.GeographicalFeatureTypePOI)
assert.Equal(t, domain.GeographicalFeatureType("railway"), domain.GeographicalFeatureTypeRailway)
assert.Equal(t, domain.GeographicalFeatureType("water"), domain.GeographicalFeatureTypeWater)
assert.Equal(t, domain.GeographicalFeatureType("land_use"), domain.GeographicalFeatureTypeLandUse)
}
func TestTransportMode_Constants(t *testing.T) {
assert.Equal(t, domain.TransportMode("truck"), domain.TransportModeTruck)
assert.Equal(t, domain.TransportMode("rail"), domain.TransportModeRail)
assert.Equal(t, domain.TransportMode("pipeline"), domain.TransportModePipe)
}
func TestGeographicalFeature_TableName(t *testing.T) {
feature := &domain.GeographicalFeature{}
assert.Equal(t, "geographical_features", feature.TableName())
}
func TestGeographicalFeature_JSONSerialization(t *testing.T) {
feature := &domain.GeographicalFeature{
ID: "test-id",
Name: "Test Feature",
FeatureType: domain.GeographicalFeatureTypeRoad,
OSMType: "way",
OSMID: "123456",
Properties: datatypes.JSON(`{"highway": "primary", "surface": "asphalt"}`),
Source: "osm",
QualityScore: 0.85,
}
// Test JSON marshaling
data, err := json.Marshal(feature)
assert.NoError(t, err)
assert.Contains(t, string(data), `"id":"test-id"`)
assert.Contains(t, string(data), `"name":"Test Feature"`)
assert.Contains(t, string(data), `"feature_type":"road"`)
assert.Contains(t, string(data), `"osm_type":"way"`)
assert.Contains(t, string(data), `"osm_id":"123456"`)
assert.Contains(t, string(data), `"source":"osm"`)
assert.Contains(t, string(data), `"quality_score":0.85`)
// Test JSON unmarshaling
var unmarshaled domain.GeographicalFeature
err = json.Unmarshal(data, &unmarshaled)
assert.NoError(t, err)
assert.Equal(t, feature.ID, unmarshaled.ID)
assert.Equal(t, feature.Name, unmarshaled.Name)
assert.Equal(t, feature.FeatureType, unmarshaled.FeatureType)
assert.Equal(t, feature.OSMType, unmarshaled.OSMType)
assert.Equal(t, feature.OSMID, unmarshaled.OSMID)
assert.Equal(t, feature.Source, unmarshaled.Source)
assert.Equal(t, feature.QualityScore, unmarshaled.QualityScore)
}
func TestTransportProfile_DefaultValues(t *testing.T) {
profile := domain.TransportProfile{
CostPerKm: 0.12,
SpeedKmH: 60.0,
MaxCapacity: 25.0,
EnvironmentalFactor: 1.0,
}
assert.Equal(t, 0.12, profile.CostPerKm)
assert.Equal(t, 60.0, profile.SpeedKmH)
assert.Equal(t, 25.0, profile.MaxCapacity)
assert.Equal(t, 1.0, profile.EnvironmentalFactor)
}
func TestTransportOption_JSONSerialization(t *testing.T) {
option := &domain.TransportOption{
TransportMode: domain.TransportModeTruck,
DistanceKm: 150.5,
CostEur: 18.06,
TimeHours: 2.508,
EnvironmentalScore: 8.5,
CapacityUtilization: 85.0,
OverallScore: 7.2,
}
// Test JSON marshaling
data, err := json.Marshal(option)
assert.NoError(t, err)
assert.Contains(t, string(data), `"transport_mode":"truck"`)
assert.Contains(t, string(data), `"distance_km":150.5`)
assert.Contains(t, string(data), `"cost_eur":18.06`)
assert.Contains(t, string(data), `"time_hours":2.508`)
assert.Contains(t, string(data), `"environmental_score":8.5`)
assert.Contains(t, string(data), `"capacity_utilization_percent":85`)
assert.Contains(t, string(data), `"overall_score":7.2`)
// Test JSON unmarshaling
var unmarshaled domain.TransportOption
err = json.Unmarshal(data, &unmarshaled)
assert.NoError(t, err)
assert.Equal(t, option.TransportMode, unmarshaled.TransportMode)
assert.Equal(t, option.DistanceKm, unmarshaled.DistanceKm)
assert.Equal(t, option.CostEur, unmarshaled.CostEur)
assert.Equal(t, option.TimeHours, unmarshaled.TimeHours)
assert.Equal(t, option.EnvironmentalScore, unmarshaled.EnvironmentalScore)
assert.Equal(t, option.CapacityUtilization, unmarshaled.CapacityUtilization)
assert.Equal(t, option.OverallScore, unmarshaled.OverallScore)
}
func TestGeographicalFeature_PropertiesJSON(t *testing.T) {
properties := map[string]interface{}{
"name": "Main Street",
"highway": "primary",
"maxspeed": "50",
"surface": "asphalt",
"lanes": 2,
"oneway": true,
}
jsonData, err := json.Marshal(properties)
assert.NoError(t, err)
feature := &domain.GeographicalFeature{
ID: "road-123",
Name: "Main Street",
FeatureType: domain.GeographicalFeatureTypeRoad,
Properties: datatypes.JSON(jsonData),
}
// Test that properties can be unmarshaled back
var unmarshaledProps map[string]interface{}
err = json.Unmarshal(feature.Properties, &unmarshaledProps)
assert.NoError(t, err)
assert.Equal(t, "Main Street", unmarshaledProps["name"])
assert.Equal(t, "primary", unmarshaledProps["highway"])
assert.Equal(t, 2.0, unmarshaledProps["lanes"]) // JSON numbers are float64
assert.Equal(t, true, unmarshaledProps["oneway"])
}
func TestGeographicalFeature_EmptyProperties(t *testing.T) {
feature := &domain.GeographicalFeature{
ID: "empty-feature",
FeatureType: domain.GeographicalFeatureTypePOI,
Properties: datatypes.JSON("{}"),
}
var props map[string]interface{}
err := json.Unmarshal(feature.Properties, &props)
assert.NoError(t, err)
assert.Empty(t, props)
}
func TestTransportProfile_Calculations(t *testing.T) {
profile := domain.TransportProfile{
CostPerKm: 0.15,
SpeedKmH: 80.0,
MaxCapacity: 30.0,
EnvironmentalFactor: 0.9,
}
// Test cost calculation
distance := 100.0
expectedCost := distance * profile.CostPerKm
assert.Equal(t, 15.0, expectedCost)
// Test time calculation
expectedTime := distance / profile.SpeedKmH
assert.Equal(t, 1.25, expectedTime)
// Test capacity utilization
load := 20.0
utilization := (load / profile.MaxCapacity) * 100
assert.InDelta(t, 66.66666666666667, utilization, 1e-13)
}