File: project_test.go

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (107 lines) | stat: -rw-r--r-- 3,068 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
package repositories_test

import (
	"testing"

	"github.com/stretchr/testify/assert"

	c "repodiff/constants"
	e "repodiff/entities"
	repoSQL "repodiff/persistence/sql"
	"repodiff/repositories"
	"repodiff/utils"
)

func init() {
	clearProjectTable()
}

var testDiffTarget = e.MappedDiffTarget{
	UpstreamTarget:   int16(1),
	DownstreamTarget: int16(1),
}

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

func clearProjectTable() {
	db, _ := repoSQL.GetDBConnectionPool()
	db.Exec("TRUNCATE TABLE project_differential")
}

func TestInsertDiffRows(t *testing.T) {
	defer clearProjectTable()

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

	p, err := repositories.NewProjectRepository(testDiffTarget)
	assert.Equal(t, nil, err, "Error should not be nil")

	fixtures := fakeFixtures()
	p.InsertDiffRows(fixtures)
	assert.Equal(t, len(fixtures), getProjectRowCount(), "Rows should be inserted")
}

func TestGetMostRecentOuterKey(t *testing.T) {
	defer clearProjectTable()
	p, _ := repositories.NewProjectRepository(testDiffTarget)
	p.InsertDiffRows(fakeFixtures())

	var oldTimestamp int64 = 1519333790
	timestamp, uid, _ := p.GetMostRecentOuterKey()

	assert.True(t, timestamp > oldTimestamp, "Insert timestamp should be greater than old")
	assert.Equal(t, 36, len(uid.String()), "Valid UUID should be generated")
}

func TestGetMostRecentOuterKeyEmpty(t *testing.T) {
	assert.Equal(t, 0, getProjectRowCount(), "Database shoudl start empty")

	p, _ := repositories.NewProjectRepository(testDiffTarget)
	_, _, err := p.GetMostRecentOuterKey()
	assert.NotEqual(t, nil, err, "Error should be returned when database is empty")
}

func TestGetMostRecentDifferentials(t *testing.T) {
	defer clearProjectTable()
	p, _ := repositories.NewProjectRepository(testDiffTarget)
	dateNow := utils.TimestampToDate(utils.TimestampSeconds())
	fixtures := fakeFixtures()

	fixtures[0].Date = dateNow
	p.InsertDiffRows(fixtures)
	diffRows, err := p.GetMostRecentDifferentials()
	assert.Equal(t, nil, err, "Error should not be nil")
	assert.Equal(t, 1, len(diffRows), "1 result should exist")

	expected := e.AnalyzedDiffRow{
		DiffRow: e.DiffRow{
			Date:                 dateNow,
			DownstreamProject:    "platform/vendor/unbundled_google/packages/Ears",
			UpstreamProject:      "platform/vendor/unbundled_google/packages/Ears",
			DiffStatus:           3,
			FilesChanged:         34,
			LineInsertions:       8,
			LineDeletions:        25,
			LineChanges:          32,
			CommitsNotUpstreamed: 0,
		},
		Type: c.Empty,
	}
	d := diffRows[0]

	// not concerned about direct comparison
	expected.DBInsertTimestamp = d.DBInsertTimestamp
	assert.Equal(t, expected, d, "Results should be equal")
}

func TestGetMostRecentDifferentialsEmpty(t *testing.T) {
	p, _ := repositories.NewProjectRepository(testDiffTarget)
	rows, err := p.GetMostRecentDifferentials()
	assert.Equal(t, nil, err, "Error should be nil")
	assert.Equal(t, 0, len(rows))
}