Enhance MySubaru API integration with improved error handling and new utility functions
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 25s

- Refactor Response struct's parse method to return detailed error messages based on API error codes.
- Introduce UnixTime type for handling Unix timestamps in JSON marshaling and unmarshaling.
- Add email masking utility function to obfuscate email addresses for privacy.
- Implement containsValueInStruct function to check for substring presence in struct fields.
- Create comprehensive unit tests for UnixTime, email masking, and struct value checking.
- Update vehicle service request method documentation for clarity.
This commit is contained in:
2025-07-08 11:26:45 -04:00
parent 1d8d175be0
commit aec4b8435b
7 changed files with 747 additions and 220 deletions

View File

@ -128,3 +128,89 @@ func TestUrlToGen_NoApiGen(t *testing.T) {
t.Errorf("urlToGen(%q, %q) = %q, want %q", url, gen, got, url)
}
}
func TestEmailHidder(t *testing.T) {
tests := []struct {
email string
expected string
wantErr bool
}{
{"alex@example.com", "a**x@example.com", false},
{"a@example.com", "a@example.com", false},
{"ab@example.com", "ab@example.com", false},
{"", "", true},
{"notanemail", "", true},
}
for _, tt := range tests {
got, err := emailMasking(tt.email)
if (err != nil) != tt.wantErr {
t.Errorf("emailHidder(%q) error = %v, wantErr %v", tt.email, err, tt.wantErr)
continue
}
if got != tt.expected {
t.Errorf("emailHidder(%q) = %q, want %q", tt.email, got, tt.expected)
}
}
}
func TestContainsValueInStruct(t *testing.T) {
type TestStruct struct {
Name string
Address string
Age int
Note string
}
tests := []struct {
s any
search string
want bool
}{
{
s: TestStruct{Name: "Alice", Address: "123 Main St", Age: 30, Note: "VIP customer"},
search: "alice",
want: true,
},
{
s: TestStruct{Name: "Bob", Address: "456 Elm St", Age: 25, Note: "Regular"},
search: "elm",
want: true,
},
{
s: TestStruct{Name: "Charlie", Address: "789 Oak St", Age: 40, Note: "VIP"},
search: "vip",
want: true,
},
{
s: TestStruct{Name: "Diana", Address: "101 Pine St", Age: 22, Note: ""},
search: "xyz",
want: false,
},
{
s: TestStruct{Name: "", Address: "", Age: 0, Note: ""},
search: "",
want: true, // empty string is contained in all strings
},
{
s: struct{ Foo int }{Foo: 42},
search: "42",
want: false,
},
{
s: "not a struct",
search: "struct",
want: false,
},
{
s: struct{ S string }{S: "CaseInsensitive"},
search: "caseinsensitive",
want: true,
},
}
for i, tt := range tests {
got := containsValueInStruct(tt.s, tt.search)
if got != tt.want {
t.Errorf("Test %d: containsStringInStruct(%#v, %q) = %v, want %v", i, tt.s, tt.search, got, tt.want)
}
}
}