Procházet zdrojové kódy

Domain API tests, 100% coverage.

Frederic G. MARAND před 5 roky
rodič
revize
155c84799b
1 změnil soubory, kde provedl 125 přidání a 0 odebrání
  1. 125 0
      domain/domain_api_test.go

+ 125 - 0
domain/domain_api_test.go

@@ -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) {
+	// Empty repos should not find any short.
+	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)
+
+	// Existing shorts should return proper target.
+	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")
+	}
+
+	// Non-existent shorts should fail.
+	_, err = GetTargetURL(shortURL + "_other")
+	if err == nil {
+		t.Error("short with no existing target should not be found")
+	}
+}
+
+func TestGetShortURL(t *testing.T) {
+	// No repo should be able to return a short for an empty target.
+	RegisterRepositories(nil, nil)
+	_, _, err := GetShortURL("")
+	if err == nil {
+		t.Error("empty repository should fail to get a short for an empty URL")
+	}
+
+	// Empty repos unable to create shorts should not find any short.
+	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")
+	}
+
+	// Empty repos able to create shorts should return a short for a valid target.
+	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")
+	}
+	// Specific to mock.
+	if actual != targetURL {
+		t.Error("mock repository should return correct generated short")
+	}
+
+	// Repos with a matching short for a target should return it as non-new.
+	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")
+	}
+	// Specific to mock.
+	if actual != targetURL {
+		t.Error("mock repository should return correct generated short")
+	}
+}