test(payloads): add encode/decode roundtrip tests for all payload types#95
test(payloads): add encode/decode roundtrip tests for all payload types#95
Conversation
8a5992c to
9809295
Compare
There was a problem hiding this comment.
Pull request overview
Consolidates payload encode/decode roundtrip testing into a single table-driven test that aims to cover all request/response payload structs, with a reflection helper to normalize time.Time values for stable equality comparisons.
Changes:
- Replaces multiple per-payload roundtrip tests with a single
TestPayloads_Encode_Decodetable-driven test covering many payload types. - Adds
normalizeTimesToUTCreflection helper to convert decodedtime.Timefields to UTC before equality checks. - Expands test fixtures to populate many fields across payload structs to exercise encode/decode paths.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
9809295 to
e3e5a70
Compare
Only 3 payload types had roundtrip tests (Register request, Import request, Export response). This adds table-driven tests covering all 27 request/response payload pairs with all fields populated, bringing encode/decode coverage to every payload struct in the package. A normalizeTimesToUTC helper handles timezone differences in TTLV time decoding so that reflect.DeepEqual works correctly regardless of the local timezone. Signed-off-by: Pierre-Henri Symoneaux <pierre-henri.symoneaux@ovhcloud.com>
e3e5a70 to
9bd9900
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for i := range v.NumField() { | ||
| if f := v.Field(i); f.CanSet() { | ||
| normalizeTimesToUTC(f) | ||
| } | ||
| } | ||
| case reflect.Slice: | ||
| for i := range v.Len() { |
There was a problem hiding this comment.
for i := range v.NumField() does not compile because range cannot be used on an int. Use an index loop (e.g., for i := 0; i < v.NumField(); i++) to iterate over struct fields.
| for i := range v.NumField() { | |
| if f := v.Field(i); f.CanSet() { | |
| normalizeTimesToUTC(f) | |
| } | |
| } | |
| case reflect.Slice: | |
| for i := range v.Len() { | |
| for i := 0; i < v.NumField(); i++ { | |
| if f := v.Field(i); f.CanSet() { | |
| normalizeTimesToUTC(f) | |
| } | |
| } | |
| case reflect.Slice: | |
| for i := 0; i < v.Len(); i++ { |
| for i := range v.NumField() { | ||
| if f := v.Field(i); f.CanSet() { | ||
| normalizeTimesToUTC(f) | ||
| } | ||
| } | ||
| case reflect.Slice: | ||
| for i := range v.Len() { |
There was a problem hiding this comment.
for i := range v.Len() does not compile because range cannot be used on an int. Use a standard index loop (e.g., for i := 0; i < v.Len(); i++) to iterate over slice elements.
| for i := range v.NumField() { | |
| if f := v.Field(i); f.CanSet() { | |
| normalizeTimesToUTC(f) | |
| } | |
| } | |
| case reflect.Slice: | |
| for i := range v.Len() { | |
| for i := 0; i < v.NumField(); i++ { | |
| if f := v.Field(i); f.CanSet() { | |
| normalizeTimesToUTC(f) | |
| } | |
| } | |
| case reflect.Slice: | |
| for i := 0; i < v.Len(); i++ { |
| require.NoError(t, err) | ||
| require.EqualValues(t, req, decodedReq) | ||
| // normalizeTimesToUTC recursively converts all time.Time fields to UTC | ||
| // so that reflect.DeepEqual works regardless of timezone differences in TTLV decoding. |
There was a problem hiding this comment.
The comment says TTLV decoding yields time.Time in local timezone, but ttlv's binary DateTime decoding uses time.Unix(...) (which returns UTC). Since this test only exercises TTLV encode/decode, the justification for normalizeTimesToUTC appears inaccurate; either update the comment to match the actual behavior or scope/rename the helper to the encoding(s) that return local times.
| // so that reflect.DeepEqual works regardless of timezone differences in TTLV decoding. | |
| // so that reflect.DeepEqual compares equivalent instants consistently even | |
| // when time values carry different location information. |
Summary
TestPayloads_Encode_Decodecovering all 27 request/response payload pairsnormalizeTimesToUTCreflection helper to handle TTLV decodingtime.Timein local timezone vs UTC originals