memory_test.go 1.2 KB

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