47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package findtender
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// TestRoundTrip decodes a release package that uses several extensions and
|
|
// checks that re-encoding it loses nothing. Numeric IDs are the one expected
|
|
// change: ID always encodes as a string.
|
|
func TestRoundTrip(t *testing.T) {
|
|
in, err := os.ReadFile("testdata/release-package.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var pkg ReleasePackage
|
|
if err := json.Unmarshal(in, &pkg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
r := pkg.Releases[0]
|
|
if got := r.Tender.Items[0].ID; got != "1" {
|
|
t.Errorf("numeric item id: got %q, want \"1\"", got)
|
|
}
|
|
if !*r.Tender.Lots[0].Suitability.SME {
|
|
t.Error("lot suitability lost")
|
|
}
|
|
if got := r.Contracts[0].Implementation.PerformanceFailures[0].ID; got != "PF1" {
|
|
t.Errorf("performance failure: got %q", got)
|
|
}
|
|
|
|
out, err := json.Marshal(pkg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var want, got any
|
|
json.Unmarshal(in, &want)
|
|
json.Unmarshal(out, &got)
|
|
// Normalise the one numeric ID in the fixture before comparing.
|
|
want.(map[string]any)["releases"].([]any)[0].(map[string]any)["tender"].(map[string]any)["items"].([]any)[0].(map[string]any)["id"] = "1"
|
|
if !reflect.DeepEqual(want, got) {
|
|
t.Fatalf("round trip changed the data:\n%s", out)
|
|
}
|
|
}
|