package domain import ( "encoding/json" "github.com/stretchr/testify/assert" "testing" "time" ) // assertJSONEqual сравнивает JSON, игнорируя пробелы и порядок ключей func assertJSONEqual(t *testing.T, expected string, actual []byte) { var expectedMap, actualMap map[string]any if err := json.Unmarshal([]byte(expected), &expectedMap); err != nil { t.Fatalf("Ошибка десериализации ожидаемого JSON: %v", err) } if err := json.Unmarshal(actual, &actualMap); err != nil { t.Fatalf("Ошибка десериализации полученного JSON: %v", err) } if len(expectedMap) != len(actualMap) { t.Errorf("Разное количество ключей в JSON: ожидалось %d, получено %d", len(expectedMap), len(actualMap)) } for key, expectedValue := range expectedMap { actualValue, exists := actualMap[key] if !exists { t.Errorf("Ключ %q отсутствует в JSON", key) } else if actualValue != expectedValue { t.Errorf("Для ключа %q ожидалось %v, получено %v", key, expectedValue, actualValue) } } } func TestUser_MarshalBinary(t *testing.T) { validDate := CustomDate{Time: time.Date(1990, time.July, 10, 0, 0, 0, 0, time.UTC)} regDate := CustomDate{Time: time.Date(2023, time.March, 15, 0, 0, 0, 0, time.UTC)} testTable := []struct { name string user User expected string }{ { name: "ok", user: User{ ID: 42, Username: "alice", Email: "alice@example.com", AvatarImage: "https://example.com/avatar.png", BlackPhoenix: true, DateOfBirth: validDate, DateOfRegistration: regDate, }, expected: `{ "id": 42, "username": "alice", "email": "alice@example.com", "avatar_image": "https://example.com/avatar.png", "black_phoenix": true, "date_of_birth": "1990-07-10", "date_of_registration": "2023-03-15" }`, }, { name: "zero_values", user: User{}, expected: `{ "id": 0, "username": "", "email": "", "avatar_image": "", "black_phoenix": false, "date_of_birth": "0001-01-01", "date_of_registration": "0001-01-01" }`, }, { name: "special_characters", user: User{ ID: 1, Username: `test"user`, Email: "test@example.com\nnewline", AvatarImage: `https://example.com/avatar.png`, }, expected: `{ "id": 1, "username": "test\"user", "email": "test@example.com\nnewline", "avatar_image": "https://example.com/avatar.png", "black_phoenix": false, "date_of_birth": "0001-01-01", "date_of_registration": "0001-01-01" }`, }, } for _, tc := range testTable { t.Run(tc.name, func(t *testing.T) { result, err := tc.user.MarshalBinary() if err != nil { t.Fatalf("Ошибка сериализации: %v", err) } assertJSONEqual(t, tc.expected, result) }) } } func TestUser_UnmarshalBinary(t *testing.T) { validDate := CustomDate{Time: time.Date(1990, time.July, 10, 0, 0, 0, 0, time.UTC)} regDate := CustomDate{Time: time.Date(2023, time.March, 15, 0, 0, 0, 0, time.UTC)} testTable := []struct { name string data string user User expectedUser User }{ { name: "ok", data: `{ "id": 42, "username": "alice", "email": "alice@example.com", "avatar_image": "https://example.com/avatar.png", "black_phoenix": true, "date_of_birth": "1990-07-10", "date_of_registration": "2023-03-15" }`, expectedUser: User{ ID: 42, Username: "alice", Email: "alice@example.com", AvatarImage: "https://example.com/avatar.png", BlackPhoenix: true, DateOfBirth: validDate, DateOfRegistration: regDate, }, }, { name: "zero_values", data: `{}`, }, { name: "special_characters", data: `{ "id": 1, "username": "test\"user", "email": "test@example.com\nnewline", "avatar_image": "https://example.com/avatar.png", "black_phoenix": false, "date_of_birth": "0001-01-01", "date_of_registration": "0001-01-01" }`, expectedUser: User{ ID: 1, Username: `test"user`, Email: "test@example.com\nnewline", AvatarImage: `https://example.com/avatar.png`, }, }, } for _, tc := range testTable { t.Run(tc.name, func(t *testing.T) { err := tc.user.UnmarshalBinary([]byte(tc.data)) if err != nil { t.Fatalf("Ошибка сериализации: %v", err) } assert.Equal(t, tc.expectedUser, tc.user) }) } }