mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
![]() The problem started with an internal error in the database (HTTP code 500, pq: duplicate key value) while trying to POST at /api/user with body {"user":{newUserObject}} to the backend, that already had three users User0 (Admin), UserA and UserB. Every test was succeeding. Eventually, the source of the problem was the initialization of the field `ID` (primary key) of the embedded struct `Model` in model `User`. When the `User` global variables User0, UserA and UserB were created in `common/testdata.go` the `Model.ID` field was set manually to the values 1, 2 and 3 respectively. This ofc was unnecessary but those variables were used for comparison with the relevant responses. When gorm (or postgres, not entirely sure) have a new variable of a model type, it will check if its primary key is zero (which means is uninitialized) and will set it to the last used value +1. That means that the new user that we were trying to insert in the DB got the primary key value 1. This value was already in use by the User0 so the DB refused to add the new user since there was a duplicate primary key value. That's why we were getting an postgre's error and HTTP 500. Hence to fix the bug this commit: - Removes `User.ID` initialization from `common/testdata.go` - Omits the `User.ID` from the json marshaler by setting the struct json tag to `json:"-"`. This is done because the request body is compared to the response *after* is serialized by a call to json.Marshal(). Since the `User.ID` is always uninitialized (value 0) the jsondiff.Compare() will always return a "NoMatch". - Includes check for return of "SupersetMatch" in NewTestEndpoint()'s jsondiff.Compare() call. Since the `User.ID` is not serialized the json representation of the request will not include an "id" key. On the contrary, the response *has* an "id" key so it will always going to be a superset json object of the serialized request. - Modifies the validator's data struct to match the form of the request ({"modelKey":{requestBody}}) by adding an intermediate struct with the proper tag `json:"user"`. |
||
---|---|---|
.. | ||
dashboard | ||
file | ||
scenario | ||
signal | ||
simulationmodel | ||
simulator | ||
user | ||
widget |