From bbec2e29607565ef8efaddc93068787f5ce6aa3a Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 1 May 2024 14:53:42 +0200 Subject: [PATCH] Fix test and add symmetry This maybe was a mixup with TestActivityValidation. We now test if the UnmarshalJSON actually threw an error. --- models/forgefed/activity_test.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/models/forgefed/activity_test.go b/models/forgefed/activity_test.go index a81ea925ba..69dce35803 100644 --- a/models/forgefed/activity_test.go +++ b/models/forgefed/activity_test.go @@ -6,6 +6,7 @@ package forgefed import ( "fmt" "reflect" + "strings" "testing" "time" @@ -93,23 +94,25 @@ func Test_LikeUnmarshalJSON(t *testing.T) { Object: ap.IRI("https://codeberg.org/api/activitypub/repository-id/1"), }, }, + wantErr: nil, }, - "wrong": { - item: []byte(`{"type":"Wrong","actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1","object":"https://codeberg.org/api/activitypub/repository-id/1"}`), - wantErr: fmt.Errorf("an other error"), + "invalid": { // ToDo: Here we are testing if the json parser detects invalid json, we could keep this test in case we bould our own. + item: []byte(`{"type":"Invalid","actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1","object":"https://codeberg.org/api/activitypub/repository-id/1"`), + want: &ForgeLike{}, + wantErr: fmt.Errorf("cannot parse JSON:"), }, } - for name, tt := range tests { + for name, test := range tests { t.Run(name, func(t *testing.T) { got := new(ForgeLike) - err := got.UnmarshalJSON(tt.item) - if (err != nil || tt.wantErr != nil) && tt.wantErr.Error() != err.Error() { - t.Errorf("UnmarshalJSON() error = \"%v\", wantErr \"%v\"", err, tt.wantErr) + err := got.UnmarshalJSON(test.item) + if (err != nil || test.wantErr != nil) && !strings.Contains(err.Error(), test.wantErr.Error()) { + t.Errorf("UnmarshalJSON() error = \"%v\", wantErr \"%v\"", err, test.wantErr) return } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("UnmarshalJSON() got = %q, want %q, err %q", got, tt.want, err.Error()) + if !reflect.DeepEqual(got, test.want) { + t.Errorf("UnmarshalJSON() got = %q, want %q, err %q", got, test.want, err.Error()) } }) }