-
Notifications
You must be signed in to change notification settings - Fork 749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON Serialization: Change Libraries #3225
Changes from 10 commits
859c3a4
0fcd2f8
1090b32
31d7abe
2b28db6
1505461
138909a
230efba
72092f2
5a2f3b1
334a2a5
240a7ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package currency | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/prebid/prebid-server/util/jsonutil" | ||
) | ||
|
||
func TestUnMarshallRates(t *testing.T) { | ||
|
@@ -22,7 +23,7 @@ func TestUnMarshallRates(t *testing.T) { | |
ratesJSON: `malformed`, | ||
expectedRates: Rates{}, | ||
expectsError: true, | ||
expectedError: errors.New("invalid character 'm' looking for beginning of value"), | ||
expectedError: errors.New("expect { or n, but found m"), | ||
}, | ||
{ | ||
desc: "Valid JSON field defining valid conversion object. Expect no error", | ||
|
@@ -50,33 +51,39 @@ func TestUnMarshallRates(t *testing.T) { | |
expectedError: nil, | ||
}, | ||
{ | ||
desc: "Valid JSON field defines a conversions map with repeated entries, expect error", | ||
desc: "Valid JSON field defines a conversions map with repeated entries, last one wins", | ||
ratesJSON: `{ | ||
"conversions":{ | ||
"USD":{ | ||
"GBP":0.7662523901, | ||
"MXN":20.00 | ||
}, | ||
"USD":{ | ||
"GBP":0.7662523901 | ||
}, | ||
"GBP":0.4815162342 | ||
} | ||
} | ||
}`, | ||
expectedRates: Rates{}, | ||
expectsError: true, | ||
expectedError: errors.New("invalid character '}' looking for beginning of object key string"), | ||
expectedRates: Rates{ | ||
Conversions: map[string]map[string]float64{ | ||
"USD": { | ||
"GBP": 0.4815162342, | ||
}, | ||
}, | ||
}, | ||
expectsError: false, | ||
expectedError: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
// Execute: | ||
updatedRates := Rates{} | ||
err := json.Unmarshal([]byte(tc.ratesJSON), &updatedRates) | ||
err := jsonutil.UnmarshalValid([]byte(tc.ratesJSON), &updatedRates) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated all tests to use UnmarshalValid because it seemed like we want to validate in tests. Does that check out? |
||
|
||
// Verify: | ||
assert.Equal(t, err != nil, tc.expectsError, tc.desc) | ||
if tc.expectsError { | ||
assert.Equal(t, err.Error(), tc.expectedError.Error(), tc.desc) | ||
assert.Equal(t, tc.expectedError.Error(), err.Error(), tc.desc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected and actual orders were reversed, leading to confusing error messages. Fixed. |
||
} | ||
assert.Equal(t, tc.expectedRates, updatedRates, tc.desc) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior of json and json-iter match here. The test was incorrectly setup. It failed because of the trailing comma, nothing to do with the repeated elements. Fixed the test and manually verified behavior matches.