Skip to content
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

TD title matching instead of JSON comparison #29

Merged
merged 2 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions directory/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ func TestCreateEvent(t *testing.T) {
// remove system-generated attributes
delete(data, "registration")

if !serializedEqual(td, data) {
t.Fatalf("notification data is not same as the one created: Expected:\n%v\nRetrieved:\n%v", marshalPrettyJSON(td), marshalPrettyJSON(data))
}

assertEqualTitle(t, td, data)
})
case err := <-errCh:
t.Run("event subscription diff unsupported", func(t *testing.T) {
Expand Down
25 changes: 5 additions & 20 deletions directory/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ func TestJSONPath(t *testing.T) {
// remove system-generated attributes
delete(filterredTD, "registration")

if !serializedEqual(createdTDsMap[id], filterredTD) {
t.Fatalf("Expected:\n%v\nGot:\n%v\n",
marshalPrettyJSON(createdTDsMap[id]), marshalPrettyJSON(filterredTD))
}
assertEqualTitle(t, createdTDsMap[id], filterredTD)
}
})
})
Expand Down Expand Up @@ -231,10 +228,7 @@ func TestXPath(t *testing.T) {
// remove system-generated attributes
delete(filterredTD, "registration")

if !serializedEqual(createdTDsMap[id], filterredTD) {
t.Fatalf("Expected:\n%v\nGot:\n%v\n",
marshalPrettyJSON(createdTDsMap[id]), marshalPrettyJSON(filterredTD))
}
assertEqualTitle(t, createdTDsMap[id], filterredTD)
}
})
})
Expand Down Expand Up @@ -332,10 +326,7 @@ func TestSPARQL(t *testing.T) {
t.Log(responseMap)
delete(responseMap, "results")

if !serializedEqual(responseMap, expectedResult) {
t.Fatalf("Expected:\n%v\nGot:\n%v\n",
marshalPrettyJSON(expectedResult), marshalPrettyJSON(responseMap))
}
assertEqualTitle(t, expectedResult, responseMap)
})

t.Run("search using POST", func(t *testing.T) {
Expand Down Expand Up @@ -363,10 +354,7 @@ func TestSPARQL(t *testing.T) {
t.Log(responseMap)
delete(responseMap, "results")

if !serializedEqual(responseMap, expectedResult) {
t.Fatalf("Expected:\n%v\nGot:\n%v\n",
marshalPrettyJSON(expectedResult), marshalPrettyJSON(responseMap))
}
assertEqualTitle(t, expectedResult, responseMap)
})

t.Run("federated search using GET", func(t *testing.T) {
Expand All @@ -393,10 +381,7 @@ func TestSPARQL(t *testing.T) {
t.Log(responseMap)
delete(responseMap, "results")

if !serializedEqual(responseMap, expectedResult) {
t.Fatalf("Expected:\n%v\nGot:\n%v\n",
marshalPrettyJSON(expectedResult), marshalPrettyJSON(responseMap))
}
assertEqualTitle(t, expectedResult, responseMap)
})

t.Run("HEAD", func(t *testing.T) {
Expand Down
35 changes: 8 additions & 27 deletions directory/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ func TestCreateAnonymousThing(t *testing.T) {
delete(storedTD, "registration")
delete(storedTD, "id")

if !serializedEqual(td, storedTD) {
t.Fatalf("Expected:\n%v\nRetrieved:\n%v\n", marshalPrettyJSON(td), marshalPrettyJSON(storedTD))
}
assertEqualTitle(t, td, storedTD)
})

t.Run("registration info", func(t *testing.T) {
Expand Down Expand Up @@ -199,9 +197,7 @@ func TestCreateThing(t *testing.T) {
delete(td, "registration")
delete(storedTD, "registration")

if !serializedEqual(td, storedTD) {
t.Fatalf("Expected:\n%v\nRetrieved:\n%v\n", marshalPrettyJSON(td), marshalPrettyJSON(storedTD))
}
assertEqualTitle(t, td, storedTD)
})

// t.Run("registration info", func(t *testing.T) {
Expand Down Expand Up @@ -350,9 +346,7 @@ func TestRetrieveThing(t *testing.T) {
// remove system-generated attributes
delete(retrievedTD, "registration")

if !serializedEqual(td, retrievedTD) {
t.Fatalf("Expected:\n%v\nRetrieved:\n%v", marshalPrettyJSON(td), marshalPrettyJSON(retrievedTD))
}
assertEqualTitle(t, td, retrievedTD)
})

t.Run("registration info", func(t *testing.T) {
Expand Down Expand Up @@ -427,10 +421,7 @@ func TestUpdateThing(t *testing.T) {
delete(td, "registration")
delete(storedTD, "registration")

if !serializedEqual(td, storedTD) {
t.Logf("Expected:\n%v\n Retrieved:\n%v\n", marshalPrettyJSON(td), marshalPrettyJSON(storedTD))
t.Fatalf("Unexpected result body; see logs.")
}
assertEqualTitle(t, td, storedTD)
})

t.Run("reject invalid", func(t *testing.T) {
Expand Down Expand Up @@ -524,9 +515,7 @@ func TestPatch(t *testing.T) {
delete(td, "registration")
delete(storedTD, "registration")

if !serializedEqual(td, storedTD) {
t.Fatalf("Expected:\n%s\n Retrieved:\n%s\n", marshalPrettyJSON(td), marshalPrettyJSON(storedTD))
}
assertEqualTitle(t, td, storedTD)
})
})

Expand Down Expand Up @@ -573,9 +562,7 @@ func TestPatch(t *testing.T) {
delete(td, "registration")
delete(storedTD, "registration")

if !serializedEqual(td, storedTD) {
t.Fatalf("Expected:\n%s\n Retrieved:\n%s\n", marshalPrettyJSON(td), marshalPrettyJSON(storedTD))
}
assertEqualTitle(t, td, storedTD)
})
})

Expand Down Expand Up @@ -640,10 +627,7 @@ func TestPatch(t *testing.T) {
delete(td, "registration")
delete(storedTD, "registration")

if !serializedEqual(td, storedTD) {
t.Logf("Expected:\n%s\n Retrieved:\n%s\n", marshalPrettyJSON(td), marshalPrettyJSON(storedTD))
t.Fatalf("Unexpected result body; see logs.")
}
assertEqualTitle(t, td, storedTD)
})
})

Expand Down Expand Up @@ -707,10 +691,7 @@ func TestPatch(t *testing.T) {
delete(td, "registration")
delete(storedTD, "registration")

if !serializedEqual(td, storedTD) {
t.Logf("Expected:\n%s\n Retrieved:\n%s\n", marshalPrettyJSON(td), marshalPrettyJSON(storedTD))
t.Fatalf("Unexpected result body; see logs.")
}
assertEqualTitle(t, td, storedTD)
})
})

Expand Down
17 changes: 5 additions & 12 deletions directory/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"mime"
"net/http"
"reflect"
"testing"

"github.com/r3labs/sse/v2"
Expand Down Expand Up @@ -171,17 +170,11 @@ func retrieveAllThings(serverURL string, t *testing.T) []mapAny {
return retrievedTDs
}

func serializedEqual(td1, td2 mapAny) bool {
// ignore context for now
// See https://github.com/w3c/wot-discovery/issues/291
delete(td1, "@context")
delete(td2, "@context")

// serialize to ease comparison of interfaces and concrete types
tdBytes, _ := json.Marshal(td1)
td2Bytes, _ := json.Marshal(td2)

return reflect.DeepEqual(tdBytes, td2Bytes)
func assertEqualTitle(t *testing.T, expectedTD, retrievedTD mapAny) {
if expectedTD["title"] != retrievedTD["title"] {
t.Fatalf("Expected TD with title: %v, Got: %v",
expectedTD["title"], retrievedTD["title"])
}
}

func httpPut(url, contentType string, b []byte) (*http.Response, error) {
Expand Down