domain_api_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package domain
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. type mockShortRepo map[ShortURL]TargetURL
  7. func (sr mockShortRepo) GetTarget(su ShortURL) (tu TargetURL, err error) {
  8. tu, ok := sr[su]
  9. if !ok {
  10. err = errors.New("no such short")
  11. }
  12. return
  13. }
  14. type mockTargetRepo struct {
  15. data map[TargetURL]ShortURL
  16. create bool
  17. }
  18. func (tr mockTargetRepo) GetShort(tu TargetURL) (su ShortURL, isNew bool, err error) {
  19. su, ok := tr.data[tu]
  20. if ok {
  21. return
  22. }
  23. if tr.create {
  24. su = ShortURL{URL: tu.URL}
  25. tr.data[tu] = su
  26. isNew = true
  27. } else {
  28. err = errors.New("short not found and not created")
  29. }
  30. return
  31. }
  32. func makeMockTargetRepo(create bool) mockTargetRepo {
  33. r := mockTargetRepo{
  34. data: make(map[TargetURL]ShortURL),
  35. create: create,
  36. }
  37. return r
  38. }
  39. func TestGetTargetURL(t *testing.T) {
  40. // Empty repos should not find any short.
  41. RegisterRepositories(make(mockShortRepo), nil)
  42. _, err := GetTargetURL("")
  43. if err == nil {
  44. t.Error("empty repository should fail to get a target")
  45. }
  46. var mockSR mockShortRepo = make(map[ShortURL]TargetURL)
  47. shortURL := "some_short"
  48. expected := TargetURL{URL: URL("some target")}
  49. mockSR[ShortURL{URL: URL(shortURL)}] = expected
  50. RegisterRepositories(mockSR, nil)
  51. // Existing shorts should return proper target.
  52. actual, err := GetTargetURL(shortURL)
  53. if err != nil {
  54. t.Error("short with an existing target should be found")
  55. }
  56. if actual != string(expected.URL) {
  57. t.Error("short with an existing target should return it")
  58. }
  59. // Non-existent shorts should fail.
  60. _, err = GetTargetURL(shortURL + "_other")
  61. if err == nil {
  62. t.Error("short with no existing target should not be found")
  63. }
  64. }
  65. func TestGetShortURL(t *testing.T) {
  66. // No repo should be able to return a short for an empty target.
  67. RegisterRepositories(nil, nil)
  68. _, _, err := GetShortURL("")
  69. if err == nil {
  70. t.Error("empty repository should fail to get a short for an empty URL")
  71. }
  72. // Empty repos unable to create shorts should not find any short.
  73. RegisterRepositories(nil, makeMockTargetRepo(false))
  74. _, isNew, err := GetShortURL("some_target")
  75. if err == nil {
  76. t.Error("empty repository should fail to get a short")
  77. }
  78. if isNew {
  79. t.Error("empty repository without create ability should fail to create a short")
  80. }
  81. // Empty repos able to create shorts should return a short for a valid target.
  82. RegisterRepositories(nil, makeMockTargetRepo(true))
  83. const targetURL = "some_target"
  84. actual, isNew, err := GetShortURL(targetURL)
  85. if err != nil {
  86. t.Error("repository with create ability should not fail to create a short")
  87. }
  88. if !isNew {
  89. t.Error("repository creating a new short should report is as new")
  90. }
  91. if len(actual) == 0 {
  92. t.Error("repository should not return empty short URLs")
  93. }
  94. // Specific to mock.
  95. if actual != targetURL {
  96. t.Error("mock repository should return correct generated short")
  97. }
  98. // Repos with a matching short for a target should return it as non-new.
  99. actual, isNew, err = GetShortURL(targetURL)
  100. if err != nil {
  101. t.Error("repository with matching short should return it")
  102. }
  103. if isNew {
  104. t.Error("repository with matching short should now claim it is new")
  105. }
  106. // Specific to mock.
  107. if actual != targetURL {
  108. t.Error("mock repository should return correct generated short")
  109. }
  110. }