memory_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package infrastructure
  2. import (
  3. "code.osinet.fr/fgm/kurz/domain"
  4. "testing"
  5. )
  6. func TestMemoryEmptyRepo(test *testing.T) {
  7. domain.RegisterRepositories(
  8. shortRepo,
  9. targetRepo,
  10. )
  11. _, err := domain.GetTargetURL("whatever", nil)
  12. if err == nil {
  13. test.Error("Empty repository should not find a target for any URL")
  14. }
  15. s1, isNew, err := domain.GetShortURL(exampleValidHTTPURL, nil)
  16. if err != nil {
  17. test.Error("Creating a short URL for a valid URL should not fail")
  18. }
  19. if !isNew {
  20. test.Error("The first short URL in an empty repository should be new")
  21. }
  22. s2, isNew, err := domain.GetShortURL(exampleValidHTTPURL, nil)
  23. if err != nil {
  24. test.Error("Creating a short URL for a valid URL should not fail")
  25. }
  26. if isNew {
  27. test.Error("The second short URL for an already shortened URL should not be new")
  28. }
  29. if s1 != s2 {
  30. test.Error("The second short URL for an already shortened URL should be the same as the first one")
  31. }
  32. t, err := domain.GetTargetURL(s1, nil)
  33. if err != nil {
  34. test.Error("Repository should find a target for an existing short URL")
  35. }
  36. if t != exampleValidHTTPURL {
  37. test.Error("Target URL for a short URL should match the target it was created for")
  38. }
  39. }
  40. func TestMemorySad(test *testing.T) {
  41. domain.RegisterRepositories(
  42. MemoryShortURLRepository{},
  43. MemoryTargetURLRepository{},
  44. )
  45. _, _, err := domain.GetShortURL("", nil)
  46. if err == nil {
  47. test.Error("Empty target URL has no valid short URL")
  48. }
  49. }