Improve type safety with proper value objects for HabitType and Frequency
Replace generic string types with strongly-typed value objects throughout the application layer. This change ensures compile-time type checking and automatic validation during JSON deserialization. Changes: - Add JSON marshaling/unmarshaling to HabitType and Frequency value objects - Update all DTOs to use typed fields instead of strings - Update commands and queries to use proper types - Remove unnecessary string conversions - Add comprehensive JSON serialization tests - Fix existing tests to work with typed fields Benefits: - Type safety: compiler catches invalid usage - Automatic validation: invalid values rejected during JSON parsing - Better code documentation and self-explanatory APIs - Reduced runtime errors
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package value_objects
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFrequency_MarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
frequency Frequency
|
||||
expected string
|
||||
}{
|
||||
{"Daily", FrequencyDaily, `"DAILY"`},
|
||||
{"Weekly", FrequencyWeekly, `"WEEKLY"`},
|
||||
{"Monthly", FrequencyMonthly, `"MONTHLY"`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
data, err := json.Marshal(tt.frequency)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal: %v", err)
|
||||
}
|
||||
if string(data) != tt.expected {
|
||||
t.Errorf("Expected %s, got %s", tt.expected, string(data))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrequency_UnmarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected Frequency
|
||||
shouldErr bool
|
||||
}{
|
||||
{"Valid Daily", `"DAILY"`, FrequencyDaily, false},
|
||||
{"Valid Weekly", `"WEEKLY"`, FrequencyWeekly, false},
|
||||
{"Valid Monthly", `"MONTHLY"`, FrequencyMonthly, false},
|
||||
{"Invalid frequency", `"YEARLY"`, "", true},
|
||||
{"Empty string", `""`, "", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var f Frequency
|
||||
err := json.Unmarshal([]byte(tt.input), &f)
|
||||
|
||||
if tt.shouldErr {
|
||||
if err == nil {
|
||||
t.Error("Expected error but got none")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if f != tt.expected {
|
||||
t.Errorf("Expected %s, got %s", tt.expected, f)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrequency_JSONRoundTrip(t *testing.T) {
|
||||
type testStruct struct {
|
||||
Frequency Frequency `json:"frequency"`
|
||||
}
|
||||
|
||||
original := testStruct{Frequency: FrequencyWeekly}
|
||||
|
||||
data, err := json.Marshal(original)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal failed: %v", err)
|
||||
}
|
||||
|
||||
var decoded testStruct
|
||||
err = json.Unmarshal(data, &decoded)
|
||||
if err != nil {
|
||||
t.Fatalf("Unmarshal failed: %v", err)
|
||||
}
|
||||
|
||||
if decoded.Frequency != original.Frequency {
|
||||
t.Errorf("Round trip failed: expected %s, got %s", original.Frequency, decoded.Frequency)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user