package domain_test import ( "fmt" "testing" "gitea.dwysokinski.me/twhelp/dcbot/internal/domain" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestNewTranslateVillageCoordsParams(t *testing.T) { t.Parallel() tooLongStr := "" for len(tooLongStr) <= 1000 { tooLongStr += "too long" } tests := []struct { versionCode string serverKey string coordsStr string coords []string perPage int32 maxPage int32 err error }{ { versionCode: "pl", serverKey: "pl181", coordsStr: "test string 123|123 898|123", coords: []string{"123|123", "898|123"}, perPage: 5, maxPage: 1, }, { versionCode: "pl", serverKey: "pl181", coordsStr: "123|123 898|123 988|123 123|158 785|567", coords: []string{"123|123", "898|123", "988|123", "123|158", "785|567"}, perPage: 4, maxPage: 2, }, { versionCode: "pl", serverKey: "pl181", coordsStr: "123|123 123|123 898|898 898|898", coords: []string{"123|123", "898|898"}, perPage: 4, maxPage: 1, }, { versionCode: "", serverKey: "pl181", coordsStr: "123|123", err: domain.RequiredError{ Field: "VersionCode", }, perPage: 1, }, { versionCode: "pl", serverKey: "", coordsStr: "123|123", err: domain.RequiredError{ Field: "ServerKey", }, perPage: 1, }, { versionCode: "pl", serverKey: "pl181", coordsStr: "123|123", err: domain.GreaterEqualThanError{ Field: "PerPage", Threshold: 1, }, }, { versionCode: "pl", serverKey: "pl181", coordsStr: "no coords", err: domain.VillageCoordsNotFoundInStringError{ Str: "no coords", }, perPage: 1, }, { versionCode: "pl", serverKey: "pl181", coordsStr: tooLongStr, err: domain.VillageCoordsStringTooLongError{ Str: tooLongStr, Max: 1000, }, perPage: 1, }, } for _, tt := range tests { tt := tt testName := fmt.Sprintf("versionCode=%s | serverKey=%s | perPage=%d | coordsStr=%s", tt.versionCode, tt.serverKey, tt.perPage, tt.coordsStr) if len(testName) > 100 { testName = testName[:97] + "..." } t.Run(testName, func(t *testing.T) { t.Parallel() res, err := domain.NewTranslateVillageCoordsParams(tt.versionCode, tt.serverKey, tt.coordsStr, tt.perPage) if tt.err != nil { assert.ErrorIs(t, err, tt.err) assert.Zero(t, res) return } assert.NoError(t, err) assert.Equal(t, tt.versionCode, res.VersionCode()) assert.Equal(t, tt.serverKey, res.ServerKey()) assert.Equal(t, tt.coords, res.Coords()) assert.Equal(t, tt.perPage, res.PerPage()) assert.Equal(t, tt.maxPage, res.MaxPage()) assert.NotEmpty(t, res.SHA256()) }) } } func TestUnmarshalTranslateVillageCoordsParamsFromDatabase(t *testing.T) { t.Parallel() params, err := domain.NewTranslateVillageCoordsParams("pl", "pl181", "123|123 871|981", 1) require.NoError(t, err) paramsUnmarshal, err := domain.UnmarshalTranslateVillageCoordsParamsFromDatabase( params.VersionCode(), params.ServerKey(), params.Coords(), params.PerPage(), params.MaxPage(), params.SHA256(), ) assert.NoError(t, err) assert.Equal(t, params, paramsUnmarshal) }