MCPcopy Create free account
hub / github.com/pace/bricks / TestOmitsEmptyAnnotation

Function TestOmitsEmptyAnnotation

http/jsonapi/response_test.go:382–416  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

380}
381
382func TestOmitsEmptyAnnotation(t *testing.T) {
383 book := &Book{
384 Author: "aren55555",
385 PublishedAt: time.Now().AddDate(0, -1, 0),
386 }
387
388 out := bytes.NewBuffer(nil)
389 if err := MarshalPayload(out, book); err != nil {
390 t.Fatal(err)
391 }
392
393 var jsonData map[string]interface{}
394 if err := json.Unmarshal(out.Bytes(), &jsonData); err != nil {
395 t.Fatal(err)
396 }
397 attributes := jsonData["data"].(map[string]interface{})["attributes"].(map[string]interface{})
398
399 // Verify that the specifically omitted field were omitted
400 if val, exists := attributes["title"]; exists {
401 t.Fatalf("Was expecting the data.attributes.title key/value to have been omitted - it was not and had a value of %v", val)
402 }
403 if val, exists := attributes["pages"]; exists {
404 t.Fatalf("Was expecting the data.attributes.pages key/value to have been omitted - it was not and had a value of %v", val)
405 }
406
407 // Verify the implicitly omitted fields were omitted
408 if val, exists := attributes["PublishedAt"]; exists {
409 t.Fatalf("Was expecting the data.attributes.PublishedAt key/value to have been implicitly omitted - it was not and had a value of %v", val)
410 }
411
412 // Verify the unset fields were not omitted
413 if _, exists := attributes["isbn"]; !exists {
414 t.Fatal("Was expecting the data.attributes.isbn key/value to have NOT been omitted")
415 }
416}
417
418func TestHasPrimaryAnnotation(t *testing.T) {
419 testModel := &Blog{

Callers

nothing calls this directly

Calls 4

MarshalPayloadFunction · 0.85
NowMethod · 0.80
FatalMethod · 0.80
FatalfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…