model_property_ext_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package swagger
  2. import (
  3. "net"
  4. "testing"
  5. )
  6. // clear && go test -v -test.run TestThatExtraTagsAreReadIntoModel ...swagger
  7. func TestThatExtraTagsAreReadIntoModel(t *testing.T) {
  8. type fakeint int
  9. type Anything struct {
  10. Name string `description:"name" modelDescription:"a test"`
  11. Size int `minimum:"0" maximum:"10"`
  12. Stati string `enum:"off|on" default:"on" modelDescription:"more description"`
  13. ID string `unique:"true"`
  14. FakeInt fakeint `type:"integer"`
  15. IP net.IP `type:"string"`
  16. Password string
  17. }
  18. m := modelsFromStruct(Anything{})
  19. props, _ := m.At("swagger.Anything")
  20. p1, _ := props.Properties.At("Name")
  21. if got, want := p1.Description, "name"; got != want {
  22. t.Errorf("got %v want %v", got, want)
  23. }
  24. p2, _ := props.Properties.At("Size")
  25. if got, want := p2.Minimum, "0"; got != want {
  26. t.Errorf("got %v want %v", got, want)
  27. }
  28. if got, want := p2.Maximum, "10"; got != want {
  29. t.Errorf("got %v want %v", got, want)
  30. }
  31. p3, _ := props.Properties.At("Stati")
  32. if got, want := p3.Enum[0], "off"; got != want {
  33. t.Errorf("got %v want %v", got, want)
  34. }
  35. if got, want := p3.Enum[1], "on"; got != want {
  36. t.Errorf("got %v want %v", got, want)
  37. }
  38. p4, _ := props.Properties.At("ID")
  39. if got, want := *p4.UniqueItems, true; got != want {
  40. t.Errorf("got %v want %v", got, want)
  41. }
  42. p5, _ := props.Properties.At("Password")
  43. if got, want := *p5.Type, "string"; got != want {
  44. t.Errorf("got %v want %v", got, want)
  45. }
  46. p6, _ := props.Properties.At("FakeInt")
  47. if got, want := *p6.Type, "integer"; got != want {
  48. t.Errorf("got %v want %v", got, want)
  49. }
  50. p7, _ := props.Properties.At("IP")
  51. if got, want := *p7.Type, "string"; got != want {
  52. t.Errorf("got %v want %v", got, want)
  53. }
  54. if got, want := props.Description, "a test\nmore description"; got != want {
  55. t.Errorf("got %v want %v", got, want)
  56. }
  57. }