You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.5 KiB

3 years ago
  1. package api
  2. import (
  3. "testing"
  4. )
  5. func TestDeserializePetJson(t *testing.T) {
  6. jsonData := `
  7. {
  8. "id": 5,
  9. "category": {
  10. "id": 6,
  11. "name": "test_category"
  12. },
  13. "name": "doggie",
  14. "photoUrls": [
  15. "photo_url_test"
  16. ],
  17. "tags": [
  18. {
  19. "id": 0,
  20. "name": "test_tags"
  21. }
  22. ],
  23. "status": "available"
  24. }
  25. `
  26. petData, err := deserialsePetJson(jsonData)
  27. if err != nil {
  28. t.Error(err.Error())
  29. return
  30. }
  31. if petData.Id != 5 {
  32. t.Errorf("Id was incorrect, got: %d, want: %d", petData.Id, 5)
  33. }
  34. if petData.Name != "doggie" {
  35. t.Errorf("Name was incorrect, got: %s, want: %s", petData.Name, "doggie")
  36. }
  37. if petData.Status != "available" {
  38. t.Errorf("Status was incorrect, got: %s, want: %s", petData.Status, "doggie")
  39. }
  40. if petData.Category.Id != 6 {
  41. t.Errorf("Category Id was incorrect, got: %d, want: %d", petData.Category.Id, 6)
  42. }
  43. if petData.Category.Name != "test_category" {
  44. t.Errorf("Category Name was incorrect, got: %s, want: %s", petData.Category.Name, "test_category")
  45. }
  46. if len(petData.PhotoUrls) != 1 && petData.PhotoUrls[0] != "photo_url_test" {
  47. t.Errorf("PhotoUrls was incorrect, length: %d", len(petData.PhotoUrls))
  48. }
  49. if len(petData.Tags) != 1 && petData.Tags[0].Id != 0 && petData.Tags[0].Name != "test_tags" {
  50. t.Errorf("Tags was incorrect, length: %d", len(petData.Tags))
  51. }
  52. }
  53. func TestDeserializePetJsonFail(t *testing.T) {
  54. jsonData := `
  55. {
  56. "invalid": "data"
  57. }
  58. `
  59. _, err := deserialsePetJson(jsonData)
  60. if err == nil {
  61. t.Error("Invalid data was provided, expected error was not thrown.")
  62. t.Error(err.Error())
  63. }
  64. }