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