1package repositories_test 2 3import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 c "repodiff/constants" 9 e "repodiff/entities" 10 repoSQL "repodiff/persistence/sql" 11 "repodiff/repositories" 12 "repodiff/utils" 13) 14 15func init() { 16 clearProjectTable() 17} 18 19var testDiffTarget = e.MappedDiffTarget{ 20 UpstreamTarget: int16(1), 21 DownstreamTarget: int16(1), 22} 23 24func getProjectRowCount() int { 25 db, _ := repoSQL.GetDBConnectionPool() 26 var count int 27 db.QueryRow("SELECT COUNT(*) FROM project_differential").Scan(&count) 28 return count 29} 30 31func clearProjectTable() { 32 db, _ := repoSQL.GetDBConnectionPool() 33 db.Exec("TRUNCATE TABLE project_differential") 34} 35 36func TestInsertDiffRows(t *testing.T) { 37 defer clearProjectTable() 38 39 assert.Equal(t, 0, getProjectRowCount(), "Rows should start empty") 40 41 p, err := repositories.NewProjectRepository(testDiffTarget) 42 assert.Equal(t, nil, err, "Error should not be nil") 43 44 fixtures := fakeFixtures() 45 p.InsertDiffRows(fixtures) 46 assert.Equal(t, len(fixtures), getProjectRowCount(), "Rows should be inserted") 47} 48 49func TestGetMostRecentOuterKey(t *testing.T) { 50 defer clearProjectTable() 51 p, _ := repositories.NewProjectRepository(testDiffTarget) 52 p.InsertDiffRows(fakeFixtures()) 53 54 var oldTimestamp int64 = 1519333790 55 timestamp, uid, _ := p.GetMostRecentOuterKey() 56 57 assert.True(t, timestamp > oldTimestamp, "Insert timestamp should be greater than old") 58 assert.Equal(t, 36, len(uid.String()), "Valid UUID should be generated") 59} 60 61func TestGetMostRecentOuterKeyEmpty(t *testing.T) { 62 assert.Equal(t, 0, getProjectRowCount(), "Database shoudl start empty") 63 64 p, _ := repositories.NewProjectRepository(testDiffTarget) 65 _, _, err := p.GetMostRecentOuterKey() 66 assert.NotEqual(t, nil, err, "Error should be returned when database is empty") 67} 68 69func TestGetMostRecentDifferentials(t *testing.T) { 70 defer clearProjectTable() 71 p, _ := repositories.NewProjectRepository(testDiffTarget) 72 dateNow := utils.TimestampToDate(utils.TimestampSeconds()) 73 fixtures := fakeFixtures() 74 75 fixtures[0].Date = dateNow 76 p.InsertDiffRows(fixtures) 77 diffRows, err := p.GetMostRecentDifferentials() 78 assert.Equal(t, nil, err, "Error should not be nil") 79 assert.Equal(t, 1, len(diffRows), "1 result should exist") 80 81 expected := e.AnalyzedDiffRow{ 82 DiffRow: e.DiffRow{ 83 Date: dateNow, 84 DownstreamProject: "platform/vendor/unbundled_google/packages/Ears", 85 UpstreamProject: "platform/vendor/unbundled_google/packages/Ears", 86 DiffStatus: 3, 87 FilesChanged: 34, 88 LineInsertions: 8, 89 LineDeletions: 25, 90 LineChanges: 32, 91 CommitsNotUpstreamed: 0, 92 }, 93 Type: c.Empty, 94 } 95 d := diffRows[0] 96 97 // not concerned about direct comparison 98 expected.DBInsertTimestamp = d.DBInsertTimestamp 99 assert.Equal(t, expected, d, "Results should be equal") 100} 101 102func TestGetMostRecentDifferentialsEmpty(t *testing.T) { 103 p, _ := repositories.NewProjectRepository(testDiffTarget) 104 rows, err := p.GetMostRecentDifferentials() 105 assert.Equal(t, nil, err, "Error should be nil") 106 assert.Equal(t, 0, len(rows)) 107} 108