package mapping import ( json "encoding/json/v2" "strings" "testing" ) // TestMappedNumbersKeepTheirExactText guards #2118: the payload was decoded into // float64, so an integer id past 2^53 was rounded before it reached the run, and // the run acted on a different record than the event named. func TestMappedNumbersKeepTheirExactText(t *testing.T) { fields := []Field{{Name: "id", Path: "id", Type: TypeNumber, Required: true}} values, failures := Resolve(fields, []byte(`{"id":9007199254740993}`)) if len(failures) > 0 { t.Fatalf("resolve: %+v", failures) } if got := values["id"]; got == nil { t.Fatal("the id resolved to nothing") } input, err := json.Marshal(values) if err != nil { t.Fatal(err) } if !strings.Contains(string(input), "9007199254740993") { t.Fatalf("mapped value is %s, want the exact digits the event carried", input) } } // TestNumbersStillHaveToBeNumbers keeps the type check honest: a number field // now holds a json.Number, and one that accepted any value would let a string // through a mapping declared as a number. func TestNumbersStillHaveToBeNumbers(t *testing.T) { fields := []Field{{Name: "id", Path: "id", Type: TypeNumber, Required: true}} for _, tc := range []struct { name, body string wantFail bool }{ {name: "a number", body: `{"id":42}`}, {name: "a quoted number is a string", body: `{"id":"42"}`, wantFail: true}, {name: "a boolean", body: `{"id":true}`, wantFail: true}, } { t.Run(tc.name, func(t *testing.T) { _, failures := Resolve(fields, []byte(tc.body)) if got := len(failures) > 0; got != tc.wantFail { t.Fatalf("failures = %+v, want a failure: %v", failures, tc.wantFail) } }) } }