File: commit_test.go

package info (click to toggle)
android-platform-tools 29.0.6-28
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 365,224 kB
  • sloc: cpp: 1,049,638; java: 460,532; ansic: 375,452; asm: 301,257; xml: 134,509; python: 92,731; perl: 62,008; sh: 26,753; makefile: 3,210; javascript: 3,172; yacc: 1,403; lex: 455; awk: 368; ruby: 183; sql: 140
file content (122 lines) | stat: -rw-r--r-- 4,475 bytes parent folder | download | duplicates (5)
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_test

import (
	"testing"

	"github.com/stretchr/testify/assert"

	ent "repodiff/entities"
	repoSQL "repodiff/persistence/sql"
	"repodiff/repositories"
	"repodiff/utils"
)

func init() {
	clearTableBeforeAfterTest("project_commit")()
}

func getCommitRowCount() int {
	db, _ := repoSQL.GetDBConnectionPool()
	var count int
	db.QueryRow("SELECT COUNT(*) FROM project_commit").Scan(&count)
	return count
}

func TestInsertCommitRows(t *testing.T) {
	defer clearTableBeforeAfterTest("project_commit")()

	assert.Equal(t, 0, getCommitRowCount(), "Rows should start empty")

	c, err := repositories.NewCommitRepository(fakeMappedTarget)
	assert.Equal(t, nil, err, "Error should not be nil")

	fixtures := fakeCommitFixtures()
	err = c.InsertCommitRows(fixtures)
	assert.Equal(t, nil, err, "Error should be nil")
	assert.Equal(t, len(fixtures), getCommitRowCount(), "Rows should be inserted")
}

func TestCommitGetMostRecentOuterKey(t *testing.T) {
	defer clearTableBeforeAfterTest("project_commit")()
	c, _ := repositories.NewCommitRepository(fakeMappedTarget)
	fixtures := fakeCommitFixtures()
	err := c.InsertCommitRows(fixtures)
	assert.Equal(t, nil, err, "Eroror should be nil")

	var oldTimestamp ent.RepoTimestamp = 1519333790
	timestamp, uid, _ := c.GetMostRecentOuterKey()
	assert.True(t, ent.RepoTimestamp(timestamp) > oldTimestamp, "Insert timestamp should be greater than old")
	assert.Equal(t, 36, len(uid.String()), "Valid UUID should be generated")
}

func TestGetMostRecentCommits(t *testing.T) {
	defer clearTableBeforeAfterTest("project_commit")()

	c, _ := repositories.NewCommitRepository(fakeMappedTarget)
	dateNow := utils.TimestampToDate(utils.TimestampSeconds())

	fixtures := fakeCommitFixtures()

	fixtures[0].Date = dateNow
	c.InsertCommitRows(fixtures)
	commitRows, err := c.GetMostRecentCommits()
	assert.Equal(t, nil, err, "Error should not be nil")
	assert.Equal(t, 1, len(commitRows), "1 result should exist")
}

func TestGetMostRecentCommitsEmpty(t *testing.T) {
	c, _ := repositories.NewCommitRepository(testDiffTarget)
	rows, err := c.GetMostRecentCommits()
	assert.Equal(t, nil, err, "Error should be nil")
	assert.Equal(t, 0, len(rows))
}

func TestGetFirstSeenTimestamp(t *testing.T) {
	defer clearTableBeforeAfterTest("project_commit")()
	c, _ := repositories.NewCommitRepository(fakeMappedTarget)
	fixtures := fakeCommitFixtures()
	oldFakeTimestamp := ent.RepoTimestamp(1)
	c.WithTimestampGenerator(
		func() ent.RepoTimestamp { return oldFakeTimestamp },
	).InsertCommitRows(fixtures)

	newFakeTimestamp := ent.RepoTimestamp(2)
	c.WithTimestampGenerator(
		func() ent.RepoTimestamp { return newFakeTimestamp },
	).InsertCommitRows(fixtures)

	commitHashes := []string{
		"61d5e61b6b6dfbf52d0d433759da964db31cc106",
	}
	nullTimestamp := ent.RepoTimestamp(0)
	commitToTimestamp, err := c.GetFirstSeenTimestamp(commitHashes, nullTimestamp)
	assert.Equal(t, nil, err, "Error should be nil")
	assert.Equal(t, len(commitHashes), len(commitToTimestamp), "Length of returned values")
	assert.Equal(t, oldFakeTimestamp, commitToTimestamp["61d5e61b6b6dfbf52d0d433759da964db31cc106"], "Expected returned timestamp")
}

func TestGetFirstSeenTimestampEmpty(t *testing.T) {
	c, _ := repositories.NewCommitRepository(fakeMappedTarget)
	nullTimestamp := ent.RepoTimestamp(0)
	commitToTimestamp, err := c.GetFirstSeenTimestamp([]string{}, nullTimestamp)
	assert.Equal(t, nil, err, "Error should be nil")
	assert.Equal(t, 0, len(commitToTimestamp), "Length of returned values")
}

func TestGetFirstSeenTimestampMutateReturned(t *testing.T) {
	c, _ := repositories.NewCommitRepository(fakeMappedTarget)
	nullTimestamp := ent.RepoTimestamp(0)
	commitToTimestamp, _ := c.GetFirstSeenTimestamp([]string{}, nullTimestamp)
	commitToTimestamp["some_key"] = ent.RepoTimestamp(0)
}

func TestGetFirstSeenTimestampNonExistent(t *testing.T) {
	c, _ := repositories.NewCommitRepository(fakeMappedTarget)
	nonExistentHash := "ae8e745ba09f61ddfa46ed6bba54c4bd07b2e93b"
	nullTimestamp := ent.RepoTimestamp(123)
	nonExistentHashes := []string{nonExistentHash}
	commitToTimestamp, err := c.GetFirstSeenTimestamp(nonExistentHashes, nullTimestamp)
	assert.Equal(t, nil, err, "Error should not be generated")
	assert.Equal(t, len(nonExistentHashes), len(commitToTimestamp), "Fetched results should match the length of the input")
	assert.Equal(t, nullTimestamp, commitToTimestamp[nonExistentHash], "Populated value should equal the input null timestamp")
}