|
| 1 | +// Copyright 2017 Sourcerer Inc. All Rights Reserved. |
| 2 | +// Author: Alexander Surkov ([email protected]) |
| 3 | + |
| 4 | +package test.tests.hashers |
| 5 | + |
| 6 | +import app.api.MockApi |
| 7 | +import app.FactCodes |
| 8 | +import app.hashers.CodeLine |
| 9 | +import app.hashers.CodeLineAges |
| 10 | +import app.hashers.CodeLongevity |
| 11 | +import app.hashers.RevCommitLine |
| 12 | +import app.model.* |
| 13 | + |
| 14 | +import test.utils.TestRepo |
| 15 | + |
| 16 | +import kotlin.test.assertEquals |
| 17 | +import kotlin.test.assertTrue |
| 18 | +import kotlin.test.fail |
| 19 | + |
| 20 | +import org.eclipse.jgit.revwalk.RevCommit |
| 21 | + |
| 22 | +import org.jetbrains.spek.api.Spek |
| 23 | +import org.jetbrains.spek.api.dsl.given |
| 24 | +import org.jetbrains.spek.api.dsl.it |
| 25 | + |
| 26 | +import java.util.Calendar |
| 27 | +import kotlin.test.assertNotNull |
| 28 | + |
| 29 | +/** |
| 30 | + * Testing class. |
| 31 | + */ |
| 32 | +class ColleaguesTest : Spek({ |
| 33 | + given("'colleagues #1'") { |
| 34 | + val testRepoPath = "../CodeLongevity_colleagues1" |
| 35 | + val testRepo = TestRepo(testRepoPath) |
| 36 | + val testRehash = "rehash_colleagues1" |
| 37 | + val fileName = "test1.txt" |
| 38 | + val author1 = Author(testRepo.userName, testRepo.userEmail) |
| 39 | + val author2 = Author(" Vasya Pupkin", " [email protected]") |
| 40 | + val emails = hashSetOf(author1.email, author2.email) |
| 41 | + |
| 42 | + val serverRepo = Repo(rehash = testRehash) |
| 43 | + val mockApi = MockApi(mockRepo = serverRepo) |
| 44 | + |
| 45 | + testRepo.createFile(fileName, listOf("line1", "line2")) |
| 46 | + testRepo.commit(message = "initial commit", |
| 47 | + author = author1, |
| 48 | + date = Calendar.Builder().setTimeOfDay(0, 0, 0) |
| 49 | + .build().time) |
| 50 | + |
| 51 | + testRepo.deleteLines(fileName, 1, 1) |
| 52 | + testRepo.commit(message = "delete line", |
| 53 | + author = author1, |
| 54 | + date = Calendar.Builder().setTimeOfDay(0, 1, 0) |
| 55 | + .build().time) |
| 56 | + |
| 57 | + testRepo.deleteLines(fileName, 0, 0) |
| 58 | + testRepo.commit(message = "delete line #2", |
| 59 | + author = author2, |
| 60 | + date = Calendar.Builder().setTimeOfDay(0, 1, 0) |
| 61 | + .build().time) |
| 62 | + |
| 63 | + val cl = CodeLongevity(serverRepo, emails, testRepo.git) |
| 64 | + cl.updateFromObservable(onError = { _ -> fail("exception") }, |
| 65 | + api = mockApi) |
| 66 | + |
| 67 | + it("'t1'") { |
| 68 | + val triple1 = cl.colleagues.get(author1.email)[0] |
| 69 | + assertEquals(triple1.first, author2.email, |
| 70 | + "Wrong colleague email #1") |
| 71 | + assertEquals(triple1.second, "1970-01", "Wrong colleague month #1") |
| 72 | + assertEquals(triple1.third, 60, "Wrong colleague vicinity #1") |
| 73 | + |
| 74 | + val triple2 = cl.colleagues.get(author2.email)[0] |
| 75 | + assertEquals(triple2.first, author1.email, |
| 76 | + "Wrong colleague email #1") |
| 77 | + assertEquals(triple2.second, "1970-01", "Wrong colleague month #1") |
| 78 | + assertEquals(triple2.third, 60, "Wrong colleague vicinity #1") |
| 79 | + } |
| 80 | + |
| 81 | + afterGroup { |
| 82 | + CodeLongevity(Repo(rehash = testRehash), emails, testRepo.git) |
| 83 | + .dropSavedData() |
| 84 | + testRepo.destroy() |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + given("'colleagues stats'") { |
| 89 | + val testRepoPath = "../CodeLongevity_colleagues_stats" |
| 90 | + val testRepo = TestRepo(testRepoPath) |
| 91 | + val testRehash = "rehash_lngstats2" |
| 92 | + val fileName = "test1.txt" |
| 93 | + val author1 = Author(testRepo.userName, testRepo.userEmail) |
| 94 | + val author2 = Author(" Vasya Pupkin", " [email protected]") |
| 95 | + val emails = hashSetOf(author1.email, author2.email) |
| 96 | + |
| 97 | + val serverRepo = Repo(rehash = testRehash) |
| 98 | + val mockApi = MockApi(mockRepo = serverRepo) |
| 99 | + |
| 100 | + testRepo.createFile(fileName, listOf("line1", "line2")) |
| 101 | + testRepo.commit(message = "initial commit", |
| 102 | + author = author1, |
| 103 | + date = Calendar.Builder().setTimeOfDay(0, 0, 0) |
| 104 | + .build().time) |
| 105 | + |
| 106 | + testRepo.deleteLines(fileName, 1, 1) |
| 107 | + testRepo.commit(message = "delete line", |
| 108 | + author = author2, |
| 109 | + date = Calendar.Builder().setTimeOfDay(0, 1, 0) |
| 110 | + .build().time) |
| 111 | + |
| 112 | + testRepo.insertLines(fileName, 1, listOf("line in the end")) |
| 113 | + testRepo.commit(message = "insert line", |
| 114 | + author = author2, |
| 115 | + date = Calendar.Builder().setTimeOfDay(0, 10, 0) |
| 116 | + .build().time) |
| 117 | + |
| 118 | + testRepo.deleteLines(fileName, 1, 1) |
| 119 | + testRepo.commit(message = "delete line #2", |
| 120 | + author = author1, |
| 121 | + date = Calendar.Builder().setTimeOfDay(0, 20, 0) |
| 122 | + .build().time) |
| 123 | + |
| 124 | + CodeLongevity(serverRepo, emails, testRepo.git) |
| 125 | + .updateFromObservable(onError = { _ -> fail("exception") }, |
| 126 | + api = mockApi) |
| 127 | + |
| 128 | + it("'t1'") { |
| 129 | + for (f in mockApi.receivedFacts) { |
| 130 | + println(f) |
| 131 | + } |
| 132 | + assertTrue(mockApi.receivedFacts.contains( |
| 133 | + Fact(repo = serverRepo, |
| 134 | + code = FactCodes.COLLEAGUES, |
| 135 | + value = author2.email, |
| 136 | + value2 = author1.email, |
| 137 | + value3 = (60).toString()) |
| 138 | + )) |
| 139 | + } |
| 140 | + |
| 141 | + afterGroup { |
| 142 | + CodeLongevity(Repo(rehash = testRehash), emails, testRepo.git) |
| 143 | + .dropSavedData() |
| 144 | + testRepo.destroy() |
| 145 | + } |
| 146 | + } |
| 147 | +}) |
0 commit comments