|
@@ -0,0 +1,125 @@
|
|
|
+package domain
|
|
|
+
|
|
|
+import (
|
|
|
+ "errors"
|
|
|
+ "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) {
|
|
|
+
|
|
|
+ RegisterRepositories(make(mockShortRepo), nil)
|
|
|
+ _, err := GetTargetURL("")
|
|
|
+ if err == nil {
|
|
|
+ t.Error("empty repository should fail to get a target")
|
|
|
+ }
|
|
|
+
|
|
|
+ var mockSR mockShortRepo = make(map[ShortURL]TargetURL)
|
|
|
+ shortURL := "some_short"
|
|
|
+ expected := TargetURL{URL: URL("some target")}
|
|
|
+ mockSR[ShortURL{URL: URL(shortURL)}] = expected
|
|
|
+ RegisterRepositories(mockSR, nil)
|
|
|
+
|
|
|
+
|
|
|
+ actual, err := GetTargetURL(shortURL)
|
|
|
+ if err != nil {
|
|
|
+ t.Error("short with an existing target should be found")
|
|
|
+ }
|
|
|
+ if actual != string(expected.URL) {
|
|
|
+ t.Error("short with an existing target should return it")
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ _, err = GetTargetURL(shortURL + "_other")
|
|
|
+ if err == nil {
|
|
|
+ t.Error("short with no existing target should not be found")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestGetShortURL(t *testing.T) {
|
|
|
+
|
|
|
+ RegisterRepositories(nil, nil)
|
|
|
+ _, _, err := GetShortURL("")
|
|
|
+ if err == nil {
|
|
|
+ t.Error("empty repository should fail to get a short for an empty URL")
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ RegisterRepositories(nil, makeMockTargetRepo(false))
|
|
|
+ _, isNew, err := GetShortURL("some_target")
|
|
|
+ if err == nil {
|
|
|
+ t.Error("empty repository should fail to get a short")
|
|
|
+ }
|
|
|
+ if isNew {
|
|
|
+ t.Error("empty repository without create ability should fail to create a short")
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ RegisterRepositories(nil, makeMockTargetRepo(true))
|
|
|
+ const targetURL = "some_target"
|
|
|
+ actual, isNew, err := GetShortURL(targetURL)
|
|
|
+ if err != nil {
|
|
|
+ t.Error("repository with create ability should not fail to create a short")
|
|
|
+ }
|
|
|
+ if !isNew {
|
|
|
+ t.Error("repository creating a new short should report is as new")
|
|
|
+ }
|
|
|
+ if len(actual) == 0 {
|
|
|
+ t.Error("repository should not return empty short URLs")
|
|
|
+ }
|
|
|
+
|
|
|
+ if actual != targetURL {
|
|
|
+ t.Error("mock repository should return correct generated short")
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ actual, isNew, err = GetShortURL(targetURL)
|
|
|
+ if err != nil {
|
|
|
+ t.Error("repository with matching short should return it")
|
|
|
+ }
|
|
|
+ if isNew {
|
|
|
+ t.Error("repository with matching short should now claim it is new")
|
|
|
+ }
|
|
|
+
|
|
|
+ if actual != targetURL {
|
|
|
+ t.Error("mock repository should return correct generated short")
|
|
|
+ }
|
|
|
+}
|