domain_api_testing.go 826 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package domain
  2. import (
  3. "github.com/nicksnyder/go-i18n/v2/i18n"
  4. )
  5. type MockShortRepo map[ShortURL]TargetURL
  6. func (sr MockShortRepo) GetTarget(su ShortURL, localizer *i18n.Localizer) (tu TargetURL, err error) {
  7. tu, ok := sr[su]
  8. if !ok {
  9. err = MakeError(localizer, ShortNotFound.ID, "")
  10. }
  11. return
  12. }
  13. type MockTargetRepo struct {
  14. Data map[TargetURL]ShortURL
  15. create bool
  16. }
  17. func (tr MockTargetRepo) GetShort(tu TargetURL) (su ShortURL, isNew bool, err error) {
  18. su, ok := tr.Data[tu]
  19. if ok {
  20. return
  21. }
  22. if tr.create {
  23. su = ShortURL{URL: tu.URL, target: tu}
  24. tr.Data[tu] = su
  25. isNew = true
  26. } else {
  27. err = MakeError(nil, ShortNotCreated.ID, "")
  28. }
  29. return
  30. }
  31. func MakeMockTargetRepo(create bool) MockTargetRepo {
  32. r := MockTargetRepo{
  33. Data: make(map[TargetURL]ShortURL),
  34. create: create,
  35. }
  36. return r
  37. }