domain_api_testing.go 724 B

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