1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
package repositories
import (
"testing"
lru "github.com/hashicorp/golang-lru"
"github.com/stretchr/testify/assert"
e "repodiff/entities"
repoSQL "repodiff/persistence/sql"
)
func init() {
clearIDTable()
}
func tearDown() {
clearIDTable()
cacheSingleton, _ = lru.New(cacheSize)
}
func clearIDTable() {
db, _ := repoSQL.GetDBConnectionPool()
db.Exec("TRUNCATE TABLE id_to_url_branch")
}
func TestProtocolStrippedURL(t *testing.T) {
urlWithProtocol := "https://keystone-qcom.googlesource.com/platform/manifest"
expected := "//keystone-qcom.googlesource.com/platform/manifest"
assert.Equal(t, expected, protocolStrippedURL(urlWithProtocol), "Protocol should be removed")
}
func TestGetOrCreateURLBranchID(t *testing.T) {
defer tearDown()
url := "https://keystone-qcom.googlesource.com/platform/manifest"
branch := "p-fs-release"
sourceRepo, _ := NewSourceRepository()
id, err := sourceRepo.getOrCreateURLBranchID(url, branch)
assert.Equal(t, nil, err, "Error should be nil")
assert.True(t, id > 0, "ID should be non-zero")
idSecondFetch, err := sourceRepo.getOrCreateURLBranchID(url, branch)
assert.Equal(t, id, idSecondFetch, "Retrieved ID values should be identical")
}
func TestGetOrCreateURLBranchIDCache(t *testing.T) {
// this test relies on the assumption that database rows will be immutable in production
defer tearDown()
url := "https://keystone-qcom.googlesource.com/platform/manifest"
branch := "p-fs-release"
sourceRepo, _ := NewSourceRepository()
id, _ := sourceRepo.getOrCreateURLBranchID(url, branch)
assert.Equal(t, int16(1), id, "ID should be 1 since it's first row")
clearIDTable()
nextURL := "https://totally-new-url-to-replace-first-row/platform/manifest"
nextBranch := "master"
id, _ = sourceRepo.getOrCreateURLBranchID(nextURL, nextBranch)
assert.Equal(t, int16(1), id, "ID should be 1 since it's first row")
id, _ = sourceRepo.getOrCreateURLBranchID(url, branch)
assert.Equal(t, int16(1), id, "ID should be 1 since it hit the cache")
}
func TestGetURLBranchByID(t *testing.T) {
defer tearDown()
url := "https://keystone-qcom.googlesource.com/platform/manifest"
branch := "p-fs-release"
sourceRepo, _ := NewSourceRepository()
id, _ := sourceRepo.getOrCreateURLBranchID(url, branch)
assert.Equal(t, int16(1), id, "ID should be 1 since it's first row")
fetchedURL, fetchedBranch, err := sourceRepo.GetURLBranchByID(id)
assert.Equal(t, nil, err, "Error should be nil")
assert.Equal(t, "//keystone-qcom.googlesource.com/platform/manifest", fetchedURL, "Fetched URL should be equal")
assert.Equal(t, branch, fetchedBranch, "Fetched branch should be equal")
}
func TestGetURLBranchByIDCache(t *testing.T) {
// this test relies on the assumption that database rows will be immutable in production
defer tearDown()
url := "https://keystone-qcom.googlesource.com/platform/manifest"
branch := "p-fs-release"
sourceRepo, _ := NewSourceRepository()
id, _ := sourceRepo.getOrCreateURLBranchID(url, branch)
assert.Equal(t, int16(1), id, "ID should be 1 since it's first row")
fetchedURL, _, _ := sourceRepo.GetURLBranchByID(id)
assert.Equal(t, "//keystone-qcom.googlesource.com/platform/manifest", fetchedURL, "Fetched URL should be equal")
clearIDTable()
nextURL := "https://totally-new-url-to-replace-first-row/platform/manifest"
nextBranch := "master"
id, _ = sourceRepo.getOrCreateURLBranchID(nextURL, nextBranch)
assert.Equal(t, int16(1), id, "ID should be 1 since it's first row")
fetchedURL, _, _ = sourceRepo.GetURLBranchByID(int16(1))
assert.Equal(t, "//keystone-qcom.googlesource.com/platform/manifest", fetchedURL, "Fetched URL should equal first value because of hitting cache")
}
func TestGetDiffTargetToMapped(t *testing.T) {
defer tearDown()
target := e.DiffTarget{
Upstream: e.Project{
URL: "https://keystone-qcom.googlesource.com/platform/manifest",
Branch: "p-fs-release",
},
Downstream: e.Project{
URL: "https://keystone-qcom.googlesource.com/platform/manifest",
Branch: "p-keystone-qcom",
},
}
sourceRepo, _ := NewSourceRepository()
mappedTarget, _ := sourceRepo.DiffTargetToMapped(target)
assert.Equal(t, int16(1), mappedTarget.UpstreamTarget, "Expected value for upstream")
assert.Equal(t, int16(2), mappedTarget.DownstreamTarget, "Expected value for downstream")
}
|