|
@@ -1,58 +1,18 @@
|
|
package domain
|
|
package domain
|
|
|
|
|
|
import (
|
|
import (
|
|
- "errors"
|
|
|
|
"testing"
|
|
"testing"
|
|
)
|
|
)
|
|
|
|
|
|
-type mockShortRepo map[ShortURL]TargetURL
|
|
|
|
-
|
|
|
|
-func (sr mockShortRepo) GetTarget(su ShortURL) (tu TargetURL, err error) {
|
|
|
|
- tu, ok := sr[su]
|
|
|
|
- if !ok {
|
|
|
|
- err = errors.New("no such short")
|
|
|
|
- }
|
|
|
|
- return
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-type mockTargetRepo struct {
|
|
|
|
- data map[TargetURL]ShortURL
|
|
|
|
- create bool
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (tr mockTargetRepo) GetShort(tu TargetURL) (su ShortURL, isNew bool, err error) {
|
|
|
|
- su, ok := tr.data[tu]
|
|
|
|
- if ok {
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- if tr.create {
|
|
|
|
- su = ShortURL{URL: tu.URL}
|
|
|
|
- tr.data[tu] = su
|
|
|
|
- isNew = true
|
|
|
|
- } else {
|
|
|
|
- err = errors.New("short not found and not created")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func makeMockTargetRepo(create bool) mockTargetRepo {
|
|
|
|
- r := mockTargetRepo{
|
|
|
|
- data: make(map[TargetURL]ShortURL),
|
|
|
|
- create: create,
|
|
|
|
- }
|
|
|
|
- return r
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func TestGetTargetURL(t *testing.T) {
|
|
func TestGetTargetURL(t *testing.T) {
|
|
// Empty repos should not find any short.
|
|
// Empty repos should not find any short.
|
|
- RegisterRepositories(make(mockShortRepo), nil)
|
|
|
|
|
|
+ RegisterRepositories(make(MockShortRepo), nil)
|
|
_, err := GetTargetURL("")
|
|
_, err := GetTargetURL("")
|
|
if err == nil {
|
|
if err == nil {
|
|
t.Error("empty repository should fail to get a target")
|
|
t.Error("empty repository should fail to get a target")
|
|
}
|
|
}
|
|
|
|
|
|
- var mockSR mockShortRepo = make(map[ShortURL]TargetURL)
|
|
|
|
|
|
+ var mockSR MockShortRepo = make(map[ShortURL]TargetURL)
|
|
shortURL := "some_short"
|
|
shortURL := "some_short"
|
|
expected := TargetURL{URL: URL("some target")}
|
|
expected := TargetURL{URL: URL("some target")}
|
|
mockSR[ShortURL{URL: URL(shortURL)}] = expected
|
|
mockSR[ShortURL{URL: URL(shortURL)}] = expected
|
|
@@ -83,7 +43,7 @@ func TestGetShortURL(t *testing.T) {
|
|
}
|
|
}
|
|
|
|
|
|
// Empty repos unable to create shorts should not find any short.
|
|
// Empty repos unable to create shorts should not find any short.
|
|
- RegisterRepositories(nil, makeMockTargetRepo(false))
|
|
|
|
|
|
+ RegisterRepositories(nil, MakeMockTargetRepo(false))
|
|
_, isNew, err := GetShortURL("some_target")
|
|
_, isNew, err := GetShortURL("some_target")
|
|
if err == nil {
|
|
if err == nil {
|
|
t.Error("empty repository should fail to get a short")
|
|
t.Error("empty repository should fail to get a short")
|
|
@@ -93,7 +53,7 @@ func TestGetShortURL(t *testing.T) {
|
|
}
|
|
}
|
|
|
|
|
|
// Empty repos able to create shorts should return a short for a valid target.
|
|
// Empty repos able to create shorts should return a short for a valid target.
|
|
- RegisterRepositories(nil, makeMockTargetRepo(true))
|
|
|
|
|
|
+ RegisterRepositories(nil, MakeMockTargetRepo(true))
|
|
const targetURL = "some_target"
|
|
const targetURL = "some_target"
|
|
actual, isNew, err := GetShortURL(targetURL)
|
|
actual, isNew, err := GetShortURL(targetURL)
|
|
if err != nil {
|
|
if err != nil {
|