1package repositories_test 2 3import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 ent "repodiff/entities" 9 repoSQL "repodiff/persistence/sql" 10 "repodiff/repositories" 11 "repodiff/utils" 12) 13 14func init() { 15 clearTableBeforeAfterTest("project_commit")() 16} 17 18func getCommitRowCount() int { 19 db, _ := repoSQL.GetDBConnectionPool() 20 var count int 21 db.QueryRow("SELECT COUNT(*) FROM project_commit").Scan(&count) 22 return count 23} 24 25func TestInsertCommitRows(t *testing.T) { 26 defer clearTableBeforeAfterTest("project_commit")() 27 28 assert.Equal(t, 0, getCommitRowCount(), "Rows should start empty") 29 30 c, err := repositories.NewCommitRepository(fakeMappedTarget) 31 assert.Equal(t, nil, err, "Error should not be nil") 32 33 fixtures := fakeCommitFixtures() 34 err = c.InsertCommitRows(fixtures) 35 assert.Equal(t, nil, err, "Error should be nil") 36 assert.Equal(t, len(fixtures), getCommitRowCount(), "Rows should be inserted") 37} 38 39func TestCommitGetMostRecentOuterKey(t *testing.T) { 40 defer clearTableBeforeAfterTest("project_commit")() 41 c, _ := repositories.NewCommitRepository(fakeMappedTarget) 42 fixtures := fakeCommitFixtures() 43 err := c.InsertCommitRows(fixtures) 44 assert.Equal(t, nil, err, "Eroror should be nil") 45 46 var oldTimestamp ent.RepoTimestamp = 1519333790 47 timestamp, uid, _ := c.GetMostRecentOuterKey() 48 assert.True(t, ent.RepoTimestamp(timestamp) > oldTimestamp, "Insert timestamp should be greater than old") 49 assert.Equal(t, 36, len(uid.String()), "Valid UUID should be generated") 50} 51 52func TestGetMostRecentCommits(t *testing.T) { 53 defer clearTableBeforeAfterTest("project_commit")() 54 55 c, _ := repositories.NewCommitRepository(fakeMappedTarget) 56 dateNow := utils.TimestampToDate(utils.TimestampSeconds()) 57 58 fixtures := fakeCommitFixtures() 59 60 fixtures[0].Date = dateNow 61 c.InsertCommitRows(fixtures) 62 commitRows, err := c.GetMostRecentCommits() 63 assert.Equal(t, nil, err, "Error should not be nil") 64 assert.Equal(t, 1, len(commitRows), "1 result should exist") 65} 66 67func TestGetMostRecentCommitsEmpty(t *testing.T) { 68 c, _ := repositories.NewCommitRepository(testDiffTarget) 69 rows, err := c.GetMostRecentCommits() 70 assert.Equal(t, nil, err, "Error should be nil") 71 assert.Equal(t, 0, len(rows)) 72} 73 74func TestGetFirstSeenTimestamp(t *testing.T) { 75 defer clearTableBeforeAfterTest("project_commit")() 76 c, _ := repositories.NewCommitRepository(fakeMappedTarget) 77 fixtures := fakeCommitFixtures() 78 oldFakeTimestamp := ent.RepoTimestamp(1) 79 c.WithTimestampGenerator( 80 func() ent.RepoTimestamp { return oldFakeTimestamp }, 81 ).InsertCommitRows(fixtures) 82 83 newFakeTimestamp := ent.RepoTimestamp(2) 84 c.WithTimestampGenerator( 85 func() ent.RepoTimestamp { return newFakeTimestamp }, 86 ).InsertCommitRows(fixtures) 87 88 commitHashes := []string{ 89 "61d5e61b6b6dfbf52d0d433759da964db31cc106", 90 } 91 nullTimestamp := ent.RepoTimestamp(0) 92 commitToTimestamp, err := c.GetFirstSeenTimestamp(commitHashes, nullTimestamp) 93 assert.Equal(t, nil, err, "Error should be nil") 94 assert.Equal(t, len(commitHashes), len(commitToTimestamp), "Length of returned values") 95 assert.Equal(t, oldFakeTimestamp, commitToTimestamp["61d5e61b6b6dfbf52d0d433759da964db31cc106"], "Expected returned timestamp") 96} 97 98func TestGetFirstSeenTimestampEmpty(t *testing.T) { 99 c, _ := repositories.NewCommitRepository(fakeMappedTarget) 100 nullTimestamp := ent.RepoTimestamp(0) 101 commitToTimestamp, err := c.GetFirstSeenTimestamp([]string{}, nullTimestamp) 102 assert.Equal(t, nil, err, "Error should be nil") 103 assert.Equal(t, 0, len(commitToTimestamp), "Length of returned values") 104} 105 106func TestGetFirstSeenTimestampMutateReturned(t *testing.T) { 107 c, _ := repositories.NewCommitRepository(fakeMappedTarget) 108 nullTimestamp := ent.RepoTimestamp(0) 109 commitToTimestamp, _ := c.GetFirstSeenTimestamp([]string{}, nullTimestamp) 110 commitToTimestamp["some_key"] = ent.RepoTimestamp(0) 111} 112 113func TestGetFirstSeenTimestampNonExistent(t *testing.T) { 114 c, _ := repositories.NewCommitRepository(fakeMappedTarget) 115 nonExistentHash := "ae8e745ba09f61ddfa46ed6bba54c4bd07b2e93b" 116 nullTimestamp := ent.RepoTimestamp(123) 117 nonExistentHashes := []string{nonExistentHash} 118 commitToTimestamp, err := c.GetFirstSeenTimestamp(nonExistentHashes, nullTimestamp) 119 assert.Equal(t, nil, err, "Error should not be generated") 120 assert.Equal(t, len(nonExistentHashes), len(commitToTimestamp), "Fetched results should match the length of the input") 121 assert.Equal(t, nullTimestamp, commitToTimestamp[nonExistentHash], "Populated value should equal the input null timestamp") 122} 123