burndown_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. package hercules
  2. import (
  3. "bytes"
  4. "io"
  5. "testing"
  6. "github.com/gogo/protobuf/proto"
  7. "github.com/stretchr/testify/assert"
  8. "gopkg.in/src-d/go-git.v4/plumbing"
  9. "gopkg.in/src-d/go-git.v4/plumbing/object"
  10. "gopkg.in/src-d/hercules.v3/pb"
  11. )
  12. func TestBurndownMeta(t *testing.T) {
  13. burndown := BurndownAnalysis{}
  14. assert.Equal(t, burndown.Name(), "Burndown")
  15. assert.Equal(t, len(burndown.Provides()), 0)
  16. required := [...]string{"file_diff", "changes", "blob_cache", "day", "author"}
  17. for _, name := range required {
  18. assert.Contains(t, burndown.Requires(), name)
  19. }
  20. opts := burndown.ListConfigurationOptions()
  21. matches := 0
  22. for _, opt := range opts {
  23. switch opt.Name {
  24. case ConfigBurndownGranularity, ConfigBurndownSampling, ConfigBurndownTrackFiles,
  25. ConfigBurndownTrackPeople, ConfigBurndownDebug:
  26. matches++
  27. }
  28. }
  29. assert.Len(t, opts, matches)
  30. assert.Equal(t, burndown.Flag(), "burndown")
  31. }
  32. func TestBurndownConfigure(t *testing.T) {
  33. burndown := BurndownAnalysis{}
  34. facts := map[string]interface{}{}
  35. facts[ConfigBurndownGranularity] = 100
  36. facts[ConfigBurndownSampling] = 200
  37. facts[ConfigBurndownTrackFiles] = true
  38. facts[ConfigBurndownTrackPeople] = true
  39. facts[ConfigBurndownDebug] = true
  40. facts[FactIdentityDetectorPeopleCount] = 5
  41. facts[FactIdentityDetectorReversedPeopleDict] = burndown.Requires()
  42. burndown.Configure(facts)
  43. assert.Equal(t, burndown.Granularity, 100)
  44. assert.Equal(t, burndown.Sampling, 200)
  45. assert.Equal(t, burndown.TrackFiles, true)
  46. assert.Equal(t, burndown.PeopleNumber, 5)
  47. assert.Equal(t, burndown.Debug, true)
  48. assert.Equal(t, burndown.reversedPeopleDict, burndown.Requires())
  49. facts[ConfigBurndownTrackPeople] = false
  50. facts[FactIdentityDetectorPeopleCount] = 50
  51. burndown.Configure(facts)
  52. assert.Equal(t, burndown.PeopleNumber, 0)
  53. facts = map[string]interface{}{}
  54. burndown.Configure(facts)
  55. assert.Equal(t, burndown.Granularity, 100)
  56. assert.Equal(t, burndown.Sampling, 200)
  57. assert.Equal(t, burndown.TrackFiles, true)
  58. assert.Equal(t, burndown.PeopleNumber, 0)
  59. assert.Equal(t, burndown.Debug, true)
  60. assert.Equal(t, burndown.reversedPeopleDict, burndown.Requires())
  61. }
  62. func TestBurndownRegistration(t *testing.T) {
  63. tp, exists := Registry.registered[(&BurndownAnalysis{}).Name()]
  64. assert.True(t, exists)
  65. assert.Equal(t, tp.Elem().Name(), "BurndownAnalysis")
  66. tp, exists = Registry.flags[(&BurndownAnalysis{}).Flag()]
  67. assert.True(t, exists)
  68. assert.Equal(t, tp.Elem().Name(), "BurndownAnalysis")
  69. }
  70. func TestBurndownInitialize(t *testing.T) {
  71. burndown := BurndownAnalysis{}
  72. burndown.Sampling = -10
  73. burndown.Granularity = DefaultBurndownGranularity
  74. burndown.Initialize(testRepository)
  75. assert.Equal(t, burndown.Sampling, DefaultBurndownGranularity)
  76. assert.Equal(t, burndown.Granularity, DefaultBurndownGranularity)
  77. burndown.Sampling = 0
  78. burndown.Granularity = DefaultBurndownGranularity - 1
  79. burndown.Initialize(testRepository)
  80. assert.Equal(t, burndown.Sampling, DefaultBurndownGranularity-1)
  81. assert.Equal(t, burndown.Granularity, DefaultBurndownGranularity-1)
  82. burndown.Sampling = DefaultBurndownGranularity - 1
  83. burndown.Granularity = -10
  84. burndown.Initialize(testRepository)
  85. assert.Equal(t, burndown.Sampling, DefaultBurndownGranularity-1)
  86. assert.Equal(t, burndown.Granularity, DefaultBurndownGranularity)
  87. }
  88. func TestBurndownConsumeFinalize(t *testing.T) {
  89. burndown := BurndownAnalysis{
  90. Granularity: 30,
  91. Sampling: 30,
  92. PeopleNumber: 2,
  93. TrackFiles: true,
  94. }
  95. burndown.Initialize(testRepository)
  96. deps := map[string]interface{}{}
  97. // stage 1
  98. deps["author"] = 0
  99. deps["day"] = 0
  100. cache := map[plumbing.Hash]*object.Blob{}
  101. hash := plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe")
  102. cache[hash], _ = testRepository.BlobObject(hash)
  103. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  104. cache[hash], _ = testRepository.BlobObject(hash)
  105. hash = plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  106. cache[hash], _ = testRepository.BlobObject(hash)
  107. hash = plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9")
  108. cache[hash], _ = testRepository.BlobObject(hash)
  109. deps["blob_cache"] = cache
  110. changes := make(object.Changes, 3)
  111. treeFrom, _ := testRepository.TreeObject(plumbing.NewHash(
  112. "a1eb2ea76eb7f9bfbde9b243861474421000eb96"))
  113. treeTo, _ := testRepository.TreeObject(plumbing.NewHash(
  114. "994eac1cd07235bb9815e547a75c84265dea00f5"))
  115. changes[0] = &object.Change{From: object.ChangeEntry{
  116. Name: "analyser.go",
  117. Tree: treeFrom,
  118. TreeEntry: object.TreeEntry{
  119. Name: "analyser.go",
  120. Mode: 0100644,
  121. Hash: plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9"),
  122. },
  123. }, To: object.ChangeEntry{
  124. Name: "analyser.go",
  125. Tree: treeTo,
  126. TreeEntry: object.TreeEntry{
  127. Name: "analyser.go",
  128. Mode: 0100644,
  129. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  130. },
  131. }}
  132. changes[1] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  133. Name: "cmd/hercules/main.go",
  134. Tree: treeTo,
  135. TreeEntry: object.TreeEntry{
  136. Name: "cmd/hercules/main.go",
  137. Mode: 0100644,
  138. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  139. },
  140. },
  141. }
  142. changes[2] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  143. Name: ".travis.yml",
  144. Tree: treeTo,
  145. TreeEntry: object.TreeEntry{
  146. Name: ".travis.yml",
  147. Mode: 0100644,
  148. Hash: plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe"),
  149. },
  150. },
  151. }
  152. deps["changes"] = changes
  153. fd := fixtureFileDiff()
  154. result, err := fd.Consume(deps)
  155. assert.Nil(t, err)
  156. deps["file_diff"] = result["file_diff"]
  157. result, err = burndown.Consume(deps)
  158. assert.Nil(t, result)
  159. assert.Nil(t, err)
  160. assert.Equal(t, burndown.previousDay, 0)
  161. assert.Equal(t, len(burndown.files), 3)
  162. assert.Equal(t, burndown.files["cmd/hercules/main.go"].Len(), 207)
  163. assert.Equal(t, burndown.files["analyser.go"].Len(), 926)
  164. assert.Equal(t, burndown.files[".travis.yml"].Len(), 12)
  165. assert.Equal(t, len(burndown.people), 2)
  166. assert.Equal(t, burndown.people[0][0], int64(12+207+926))
  167. assert.Equal(t, len(burndown.globalStatus), 1)
  168. assert.Equal(t, burndown.globalStatus[0], int64(12+207+926))
  169. assert.Equal(t, len(burndown.globalHistory), 0)
  170. assert.Equal(t, len(burndown.fileHistories), 0)
  171. burndown2 := BurndownAnalysis{
  172. Granularity: 30,
  173. Sampling: 0,
  174. }
  175. burndown2.Initialize(testRepository)
  176. _, err = burndown2.Consume(deps)
  177. assert.Nil(t, err)
  178. assert.Equal(t, len(burndown2.people), 0)
  179. assert.Equal(t, len(burndown2.peopleHistories), 0)
  180. assert.Equal(t, len(burndown2.fileHistories), 0)
  181. // stage 2
  182. // 2b1ed978194a94edeabbca6de7ff3b5771d4d665
  183. deps["author"] = 1
  184. deps["day"] = 30
  185. cache = map[plumbing.Hash]*object.Blob{}
  186. hash = plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe")
  187. cache[hash], _ = testRepository.BlobObject(hash)
  188. hash = plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  189. cache[hash], _ = testRepository.BlobObject(hash)
  190. hash = plumbing.NewHash("29c9fafd6a2fae8cd20298c3f60115bc31a4c0f2")
  191. cache[hash], _ = testRepository.BlobObject(hash)
  192. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  193. cache[hash], _ = testRepository.BlobObject(hash)
  194. hash = plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72")
  195. cache[hash], _ = testRepository.BlobObject(hash)
  196. deps["blob_cache"] = cache
  197. changes = make(object.Changes, 3)
  198. treeFrom, _ = testRepository.TreeObject(plumbing.NewHash(
  199. "96c6ece9b2f3c7c51b83516400d278dea5605100"))
  200. treeTo, _ = testRepository.TreeObject(plumbing.NewHash(
  201. "251f2094d7b523d5bcc60e663b6cf38151bf8844"))
  202. changes[0] = &object.Change{From: object.ChangeEntry{
  203. Name: "analyser.go",
  204. Tree: treeFrom,
  205. TreeEntry: object.TreeEntry{
  206. Name: "analyser.go",
  207. Mode: 0100644,
  208. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  209. },
  210. }, To: object.ChangeEntry{
  211. Name: "burndown.go",
  212. Tree: treeTo,
  213. TreeEntry: object.TreeEntry{
  214. Name: "burndown.go",
  215. Mode: 0100644,
  216. Hash: plumbing.NewHash("29c9fafd6a2fae8cd20298c3f60115bc31a4c0f2"),
  217. },
  218. },
  219. }
  220. changes[1] = &object.Change{From: object.ChangeEntry{
  221. Name: "cmd/hercules/main.go",
  222. Tree: treeFrom,
  223. TreeEntry: object.TreeEntry{
  224. Name: "cmd/hercules/main.go",
  225. Mode: 0100644,
  226. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  227. },
  228. }, To: object.ChangeEntry{
  229. Name: "cmd/hercules/main.go",
  230. Tree: treeTo,
  231. TreeEntry: object.TreeEntry{
  232. Name: "cmd/hercules/main.go",
  233. Mode: 0100644,
  234. Hash: plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72"),
  235. },
  236. },
  237. }
  238. changes[2] = &object.Change{From: object.ChangeEntry{
  239. Name: ".travis.yml",
  240. Tree: treeTo,
  241. TreeEntry: object.TreeEntry{
  242. Name: ".travis.yml",
  243. Mode: 0100644,
  244. Hash: plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe"),
  245. },
  246. }, To: object.ChangeEntry{},
  247. }
  248. deps["changes"] = changes
  249. fd = fixtureFileDiff()
  250. result, err = fd.Consume(deps)
  251. assert.Nil(t, err)
  252. deps["file_diff"] = result["file_diff"]
  253. result, err = burndown.Consume(deps)
  254. assert.Nil(t, result)
  255. assert.Nil(t, err)
  256. assert.Equal(t, burndown.previousDay, 30)
  257. assert.Equal(t, len(burndown.files), 2)
  258. assert.Equal(t, burndown.files["cmd/hercules/main.go"].Len(), 290)
  259. assert.Equal(t, burndown.files["burndown.go"].Len(), 543)
  260. assert.Equal(t, len(burndown.people), 2)
  261. assert.Equal(t, len(burndown.globalStatus), 2)
  262. assert.Equal(t, burndown.globalStatus[0], int64(464))
  263. assert.Equal(t, burndown.globalStatus[1], int64(0))
  264. assert.Equal(t, len(burndown.globalHistory), 1)
  265. assert.Equal(t, len(burndown.globalHistory[0]), 2)
  266. assert.Equal(t, len(burndown.fileHistories), 3)
  267. out := burndown.Finalize().(BurndownResult)
  268. /*
  269. GlobalHistory [][]int64
  270. FileHistories map[string][][]int64
  271. PeopleHistories [][][]int64
  272. PeopleMatrix [][]int64
  273. */
  274. assert.Equal(t, len(out.GlobalHistory), 2)
  275. for i := 0; i < 2; i++ {
  276. assert.Equal(t, len(out.GlobalHistory[i]), 2)
  277. }
  278. assert.Equal(t, len(out.GlobalHistory), 2)
  279. assert.Equal(t, out.GlobalHistory[0][0], int64(1145))
  280. assert.Equal(t, out.GlobalHistory[0][1], int64(0))
  281. assert.Equal(t, out.GlobalHistory[1][0], int64(464))
  282. assert.Equal(t, out.GlobalHistory[1][1], int64(369))
  283. assert.Equal(t, len(out.FileHistories), 2)
  284. assert.Equal(t, len(out.FileHistories["cmd/hercules/main.go"]), 2)
  285. assert.Equal(t, len(out.FileHistories["burndown.go"]), 2)
  286. assert.Equal(t, len(out.FileHistories["cmd/hercules/main.go"][0]), 2)
  287. assert.Equal(t, len(out.FileHistories["burndown.go"][0]), 2)
  288. assert.Equal(t, len(out.PeopleMatrix), 2)
  289. assert.Equal(t, len(out.PeopleMatrix[0]), 4)
  290. assert.Equal(t, len(out.PeopleMatrix[1]), 4)
  291. assert.Equal(t, out.PeopleMatrix[0][0], int64(1145))
  292. assert.Equal(t, out.PeopleMatrix[0][1], int64(0))
  293. assert.Equal(t, out.PeopleMatrix[0][2], int64(0))
  294. assert.Equal(t, out.PeopleMatrix[0][3], int64(-681))
  295. assert.Equal(t, out.PeopleMatrix[1][0], int64(369))
  296. assert.Equal(t, out.PeopleMatrix[1][1], int64(0))
  297. assert.Equal(t, out.PeopleMatrix[1][2], int64(0))
  298. assert.Equal(t, out.PeopleMatrix[1][3], int64(0))
  299. assert.Equal(t, len(out.PeopleHistories), 2)
  300. for i := 0; i < 2; i++ {
  301. assert.Equal(t, len(out.PeopleHistories[i]), 2)
  302. assert.Equal(t, len(out.PeopleHistories[i][0]), 2)
  303. assert.Equal(t, len(out.PeopleHistories[i][1]), 2)
  304. }
  305. }
  306. func TestBurndownAnalysisSerialize(t *testing.T) {
  307. burndown := BurndownAnalysis{
  308. Granularity: 30,
  309. Sampling: 30,
  310. PeopleNumber: 2,
  311. TrackFiles: true,
  312. }
  313. burndown.Initialize(testRepository)
  314. deps := map[string]interface{}{}
  315. // stage 1
  316. deps["author"] = 0
  317. deps["day"] = 0
  318. cache := map[plumbing.Hash]*object.Blob{}
  319. hash := plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe")
  320. cache[hash], _ = testRepository.BlobObject(hash)
  321. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  322. cache[hash], _ = testRepository.BlobObject(hash)
  323. hash = plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  324. cache[hash], _ = testRepository.BlobObject(hash)
  325. hash = plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9")
  326. cache[hash], _ = testRepository.BlobObject(hash)
  327. deps["blob_cache"] = cache
  328. changes := make(object.Changes, 3)
  329. treeFrom, _ := testRepository.TreeObject(plumbing.NewHash(
  330. "a1eb2ea76eb7f9bfbde9b243861474421000eb96"))
  331. treeTo, _ := testRepository.TreeObject(plumbing.NewHash(
  332. "994eac1cd07235bb9815e547a75c84265dea00f5"))
  333. changes[0] = &object.Change{From: object.ChangeEntry{
  334. Name: "analyser.go",
  335. Tree: treeFrom,
  336. TreeEntry: object.TreeEntry{
  337. Name: "analyser.go",
  338. Mode: 0100644,
  339. Hash: plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9"),
  340. },
  341. }, To: object.ChangeEntry{
  342. Name: "analyser.go",
  343. Tree: treeTo,
  344. TreeEntry: object.TreeEntry{
  345. Name: "analyser.go",
  346. Mode: 0100644,
  347. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  348. },
  349. }}
  350. changes[1] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  351. Name: "cmd/hercules/main.go",
  352. Tree: treeTo,
  353. TreeEntry: object.TreeEntry{
  354. Name: "cmd/hercules/main.go",
  355. Mode: 0100644,
  356. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  357. },
  358. },
  359. }
  360. changes[2] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  361. Name: ".travis.yml",
  362. Tree: treeTo,
  363. TreeEntry: object.TreeEntry{
  364. Name: ".travis.yml",
  365. Mode: 0100644,
  366. Hash: plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe"),
  367. },
  368. },
  369. }
  370. deps["changes"] = changes
  371. fd := fixtureFileDiff()
  372. result, _ := fd.Consume(deps)
  373. deps["file_diff"] = result["file_diff"]
  374. burndown.Consume(deps)
  375. // stage 2
  376. // 2b1ed978194a94edeabbca6de7ff3b5771d4d665
  377. deps["author"] = 1
  378. deps["day"] = 30
  379. cache = map[plumbing.Hash]*object.Blob{}
  380. hash = plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe")
  381. cache[hash], _ = testRepository.BlobObject(hash)
  382. hash = plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  383. cache[hash], _ = testRepository.BlobObject(hash)
  384. hash = plumbing.NewHash("29c9fafd6a2fae8cd20298c3f60115bc31a4c0f2")
  385. cache[hash], _ = testRepository.BlobObject(hash)
  386. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  387. cache[hash], _ = testRepository.BlobObject(hash)
  388. hash = plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72")
  389. cache[hash], _ = testRepository.BlobObject(hash)
  390. deps["blob_cache"] = cache
  391. changes = make(object.Changes, 3)
  392. treeFrom, _ = testRepository.TreeObject(plumbing.NewHash(
  393. "96c6ece9b2f3c7c51b83516400d278dea5605100"))
  394. treeTo, _ = testRepository.TreeObject(plumbing.NewHash(
  395. "251f2094d7b523d5bcc60e663b6cf38151bf8844"))
  396. changes[0] = &object.Change{From: object.ChangeEntry{
  397. Name: "analyser.go",
  398. Tree: treeFrom,
  399. TreeEntry: object.TreeEntry{
  400. Name: "analyser.go",
  401. Mode: 0100644,
  402. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  403. },
  404. }, To: object.ChangeEntry{
  405. Name: "burndown.go",
  406. Tree: treeTo,
  407. TreeEntry: object.TreeEntry{
  408. Name: "burndown.go",
  409. Mode: 0100644,
  410. Hash: plumbing.NewHash("29c9fafd6a2fae8cd20298c3f60115bc31a4c0f2"),
  411. },
  412. },
  413. }
  414. changes[1] = &object.Change{From: object.ChangeEntry{
  415. Name: "cmd/hercules/main.go",
  416. Tree: treeFrom,
  417. TreeEntry: object.TreeEntry{
  418. Name: "cmd/hercules/main.go",
  419. Mode: 0100644,
  420. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  421. },
  422. }, To: object.ChangeEntry{
  423. Name: "cmd/hercules/main.go",
  424. Tree: treeTo,
  425. TreeEntry: object.TreeEntry{
  426. Name: "cmd/hercules/main.go",
  427. Mode: 0100644,
  428. Hash: plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72"),
  429. },
  430. },
  431. }
  432. changes[2] = &object.Change{From: object.ChangeEntry{
  433. Name: ".travis.yml",
  434. Tree: treeTo,
  435. TreeEntry: object.TreeEntry{
  436. Name: ".travis.yml",
  437. Mode: 0100644,
  438. Hash: plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe"),
  439. },
  440. }, To: object.ChangeEntry{},
  441. }
  442. deps["changes"] = changes
  443. fd = fixtureFileDiff()
  444. result, _ = fd.Consume(deps)
  445. deps["file_diff"] = result["file_diff"]
  446. people := [...]string{"one@srcd", "two@srcd"}
  447. burndown.reversedPeopleDict = people[:]
  448. burndown.Consume(deps)
  449. out := burndown.Finalize().(BurndownResult)
  450. buffer := &bytes.Buffer{}
  451. burndown.Serialize(out, false, buffer)
  452. assert.Equal(t, buffer.String(), ` granularity: 30
  453. sampling: 30
  454. "project": |-
  455. 1145 0
  456. 464 369
  457. files:
  458. "burndown.go": |-
  459. 0 0
  460. 293 250
  461. "cmd/hercules/main.go": |-
  462. 207 0
  463. 171 119
  464. people_sequence:
  465. - "one@srcd"
  466. - "two@srcd"
  467. people:
  468. "one@srcd": |-
  469. 1145 0
  470. 464 0
  471. "two@srcd": |-
  472. 0 0
  473. 0 369
  474. people_interaction: |-
  475. 1145 0 0 -681
  476. 369 0 0 0
  477. `)
  478. buffer = &bytes.Buffer{}
  479. burndown.Serialize(out, true, buffer)
  480. msg := pb.BurndownAnalysisResults{}
  481. proto.Unmarshal(buffer.Bytes(), &msg)
  482. assert.Equal(t, msg.Granularity, int32(30))
  483. assert.Equal(t, msg.Sampling, int32(30))
  484. assert.Equal(t, msg.Project.Name, "project")
  485. assert.Equal(t, msg.Project.NumberOfRows, int32(2))
  486. assert.Equal(t, msg.Project.NumberOfColumns, int32(2))
  487. assert.Len(t, msg.Project.Rows, 2)
  488. assert.Len(t, msg.Project.Rows[0].Columns, 1)
  489. assert.Equal(t, msg.Project.Rows[0].Columns[0], uint32(1145))
  490. assert.Len(t, msg.Project.Rows[1].Columns, 2)
  491. assert.Equal(t, msg.Project.Rows[1].Columns[0], uint32(464))
  492. assert.Equal(t, msg.Project.Rows[1].Columns[1], uint32(369))
  493. assert.Len(t, msg.Files, 2)
  494. assert.Equal(t, msg.Files[0].Name, "burndown.go")
  495. assert.Equal(t, msg.Files[1].Name, "cmd/hercules/main.go")
  496. assert.Len(t, msg.Files[0].Rows, 2)
  497. assert.Len(t, msg.Files[0].Rows[0].Columns, 0)
  498. assert.Len(t, msg.Files[0].Rows[1].Columns, 2)
  499. assert.Equal(t, msg.Files[0].Rows[1].Columns[0], uint32(293))
  500. assert.Equal(t, msg.Files[0].Rows[1].Columns[1], uint32(250))
  501. assert.Len(t, msg.People, 2)
  502. assert.Equal(t, msg.People[0].Name, "one@srcd")
  503. assert.Equal(t, msg.People[1].Name, "two@srcd")
  504. assert.Len(t, msg.People[0].Rows, 2)
  505. assert.Len(t, msg.People[0].Rows[0].Columns, 1)
  506. assert.Len(t, msg.People[0].Rows[1].Columns, 1)
  507. assert.Equal(t, msg.People[0].Rows[0].Columns[0], uint32(1145))
  508. assert.Equal(t, msg.People[0].Rows[1].Columns[0], uint32(464))
  509. assert.Len(t, msg.People[1].Rows, 2)
  510. assert.Len(t, msg.People[1].Rows[0].Columns, 0)
  511. assert.Len(t, msg.People[1].Rows[1].Columns, 2)
  512. assert.Equal(t, msg.People[1].Rows[1].Columns[0], uint32(0))
  513. assert.Equal(t, msg.People[1].Rows[1].Columns[1], uint32(369))
  514. assert.Equal(t, msg.PeopleInteraction.NumberOfRows, int32(2))
  515. assert.Equal(t, msg.PeopleInteraction.NumberOfColumns, int32(4))
  516. data := [...]int64{1145, -681, 369}
  517. assert.Equal(t, msg.PeopleInteraction.Data, data[:])
  518. indices := [...]int32{0, 3, 0}
  519. assert.Equal(t, msg.PeopleInteraction.Indices, indices[:])
  520. indptr := [...]int64{0, 2, 3}
  521. assert.Equal(t, msg.PeopleInteraction.Indptr, indptr[:])
  522. }
  523. type panickingCloser struct {
  524. }
  525. func (c panickingCloser) Close() error {
  526. return io.EOF
  527. }
  528. func TestCheckClose(t *testing.T) {
  529. closer := panickingCloser{}
  530. assert.Panics(t, func() { checkClose(closer) })
  531. }
  532. func TestBurndownAddMatrix(t *testing.T) {
  533. size := 5*3 + 1
  534. daily := make([][]float32, size)
  535. for i := range daily {
  536. daily[i] = make([]float32, size)
  537. }
  538. added := make([][]int64, 5)
  539. for i := range added {
  540. added[i] = make([]int64, 3)
  541. switch i {
  542. case 0:
  543. added[i][0] = 10
  544. case 1:
  545. added[i][0] = 18
  546. added[i][1] = 2
  547. case 2:
  548. added[i][0] = 12
  549. added[i][1] = 14
  550. case 3:
  551. added[i][0] = 10
  552. added[i][1] = 12
  553. added[i][2] = 6
  554. case 4:
  555. added[i][0] = 8
  556. added[i][1] = 9
  557. added[i][2] = 13
  558. }
  559. }
  560. assert.Panics(t, func() {
  561. daily2 := make([][]float32, 16)
  562. for i := range daily2 {
  563. daily2[i] = make([]float32, 15)
  564. }
  565. addBurndownMatrix(added, 5, 3, daily2, 1)
  566. })
  567. assert.Panics(t, func() {
  568. daily2 := make([][]float32, 15)
  569. for i := range daily2 {
  570. daily2[i] = make([]float32, 16)
  571. }
  572. addBurndownMatrix(added, 5, 3, daily2, 1)
  573. })
  574. // yaml.PrintMatrix(os.Stdout, added, 0, "test", true)
  575. /*
  576. "test": |-
  577. 10 0 0
  578. 18 2 0
  579. 12 14 0
  580. 10 12 6
  581. 8 9 13
  582. */
  583. addBurndownMatrix(added, 5, 3, daily, 1)
  584. for i := range daily[0] {
  585. assert.Equal(t, daily[0][i], float32(0))
  586. }
  587. for i := range daily {
  588. assert.Equal(t, daily[i][0], float32(0))
  589. }
  590. /*for _, row := range daily {
  591. fmt.Println(row)
  592. }*/
  593. // check pinned points
  594. for y := 0; y < 5; y++ {
  595. for x := 0; x < 3; x++ {
  596. var sum float32
  597. for i := x * 5; i < (x+1)*5; i++ {
  598. sum += daily[(y+1)*3][i+1]
  599. }
  600. assert.InDelta(t, sum, added[y][x], 0.00001)
  601. }
  602. }
  603. // check overall trend: 0 -> const -> peak -> decay
  604. for x := 0; x < 15; x++ {
  605. for y := 0; y < x; y++ {
  606. assert.Zero(t, daily[y+1][x+1])
  607. }
  608. var prev float32
  609. for y := x; y < ((x+3)/5)*5; y++ {
  610. if prev == 0 {
  611. prev = daily[y+1][x+1]
  612. }
  613. assert.Equal(t, daily[y+1][x+1], prev)
  614. }
  615. for y := ((x + 3) / 5) * 5; y < 15; y++ {
  616. if prev == 0 {
  617. prev = daily[y+1][x+1]
  618. }
  619. assert.True(t, daily[y+1][x+1] <= prev)
  620. prev = daily[y+1][x+1]
  621. }
  622. }
  623. }
  624. func TestBurndownAddMatrixCrazy(t *testing.T) {
  625. size := 5 * 3
  626. daily := make([][]float32, size)
  627. for i := range daily {
  628. daily[i] = make([]float32, size)
  629. }
  630. added := make([][]int64, 5)
  631. for i := range added {
  632. added[i] = make([]int64, 3)
  633. switch i {
  634. case 0:
  635. added[i][0] = 10
  636. case 1:
  637. added[i][0] = 9
  638. added[i][1] = 2
  639. case 2:
  640. added[i][0] = 8
  641. added[i][1] = 16
  642. case 3:
  643. added[i][0] = 7
  644. added[i][1] = 12
  645. added[i][2] = 6
  646. case 4:
  647. added[i][0] = 6
  648. added[i][1] = 9
  649. added[i][2] = 13
  650. }
  651. }
  652. // yaml.PrintMatrix(os.Stdout, added, 0, "test", true)
  653. /*
  654. "test": |-
  655. 10 0 0
  656. 9 2 0
  657. 8 16 0
  658. 7 12 6
  659. 6 9 13
  660. */
  661. addBurndownMatrix(added, 5, 3, daily, 0)
  662. /*for _, row := range daily {
  663. fmt.Println(row)
  664. }*/
  665. // check pinned points
  666. for y := 0; y < 5; y++ {
  667. for x := 0; x < 3; x++ {
  668. var sum float32
  669. for i := x * 5; i < (x+1)*5; i++ {
  670. sum += daily[(y+1)*3-1][i]
  671. }
  672. assert.InDelta(t, sum, added[y][x], 0.00001)
  673. }
  674. }
  675. // check overall trend: 0 -> const -> peak -> decay
  676. for x := 0; x < 15; x++ {
  677. for y := 0; y < x; y++ {
  678. assert.Zero(t, daily[y][x])
  679. }
  680. var prev float32
  681. for y := x; y < ((x+3)/5)*5; y++ {
  682. if prev == 0 {
  683. prev = daily[y][x]
  684. }
  685. assert.Equal(t, daily[y][x], prev)
  686. }
  687. for y := ((x + 3) / 5) * 5; y < 15; y++ {
  688. if prev == 0 {
  689. prev = daily[y][x]
  690. }
  691. assert.True(t, daily[y][x] <= prev)
  692. prev = daily[y][x]
  693. }
  694. }
  695. }
  696. func TestBurndownMergeGlobalHistory(t *testing.T) {
  697. people1 := [...]string{"one", "two"}
  698. res1 := BurndownResult{
  699. GlobalHistory: [][]int64{},
  700. FileHistories: map[string][][]int64{},
  701. PeopleHistories: [][][]int64{},
  702. PeopleMatrix: [][]int64{},
  703. reversedPeopleDict: people1[:],
  704. sampling: 15,
  705. granularity: 20,
  706. }
  707. c1 := CommonAnalysisResult{
  708. BeginTime: 600566400, // 1989 Jan 12
  709. EndTime: 604713600, // 1989 March 1
  710. CommitsNumber: 10,
  711. RunTime: 100000,
  712. }
  713. // 48 days
  714. res1.GlobalHistory = make([][]int64, 48/15+1 /* 4 samples */)
  715. for i := range res1.GlobalHistory {
  716. res1.GlobalHistory[i] = make([]int64, 48/20+1 /* 3 bands */)
  717. switch i {
  718. case 0:
  719. res1.GlobalHistory[i][0] = 1000
  720. case 1:
  721. res1.GlobalHistory[i][0] = 1100
  722. res1.GlobalHistory[i][1] = 400
  723. case 2:
  724. res1.GlobalHistory[i][0] = 900
  725. res1.GlobalHistory[i][1] = 750
  726. res1.GlobalHistory[i][2] = 100
  727. case 3:
  728. res1.GlobalHistory[i][0] = 850
  729. res1.GlobalHistory[i][1] = 700
  730. res1.GlobalHistory[i][2] = 150
  731. }
  732. }
  733. res1.FileHistories["file1"] = res1.GlobalHistory
  734. res1.FileHistories["file2"] = res1.GlobalHistory
  735. res1.PeopleHistories = append(res1.PeopleHistories, res1.GlobalHistory)
  736. res1.PeopleHistories = append(res1.PeopleHistories, res1.GlobalHistory)
  737. people2 := [...]string{"two", "three"}
  738. res2 := BurndownResult{
  739. GlobalHistory: [][]int64{},
  740. FileHistories: map[string][][]int64{},
  741. PeopleHistories: [][][]int64{},
  742. PeopleMatrix: [][]int64{},
  743. reversedPeopleDict: people2[:],
  744. sampling: 14,
  745. granularity: 19,
  746. }
  747. c2 := CommonAnalysisResult{
  748. BeginTime: 601084800, // 1989 Jan 18
  749. EndTime: 605923200, // 1989 March 15
  750. CommitsNumber: 10,
  751. RunTime: 100000,
  752. }
  753. // 56 days
  754. res2.GlobalHistory = make([][]int64, 56/14 /* 4 samples */)
  755. for i := range res2.GlobalHistory {
  756. res2.GlobalHistory[i] = make([]int64, 56/19+1 /* 3 bands */)
  757. switch i {
  758. case 0:
  759. res2.GlobalHistory[i][0] = 900
  760. case 1:
  761. res2.GlobalHistory[i][0] = 1100
  762. res2.GlobalHistory[i][1] = 400
  763. case 2:
  764. res2.GlobalHistory[i][0] = 900
  765. res2.GlobalHistory[i][1] = 750
  766. res2.GlobalHistory[i][2] = 100
  767. case 3:
  768. res2.GlobalHistory[i][0] = 800
  769. res2.GlobalHistory[i][1] = 600
  770. res2.GlobalHistory[i][2] = 600
  771. }
  772. }
  773. res2.FileHistories["file2"] = res2.GlobalHistory
  774. res2.FileHistories["file3"] = res2.GlobalHistory
  775. res2.PeopleHistories = append(res2.PeopleHistories, res2.GlobalHistory)
  776. res2.PeopleHistories = append(res2.PeopleHistories, res2.GlobalHistory)
  777. burndown := BurndownAnalysis{}
  778. merged := burndown.MergeResults(res1, res2, &c1, &c2).(BurndownResult)
  779. assert.Equal(t, merged.granularity, 19)
  780. assert.Equal(t, merged.sampling, 14)
  781. assert.Len(t, merged.GlobalHistory, 5)
  782. for _, row := range merged.GlobalHistory {
  783. assert.Len(t, row, 4)
  784. }
  785. assert.Equal(t, merged.FileHistories["file1"], res1.GlobalHistory)
  786. assert.Equal(t, merged.FileHistories["file2"], merged.GlobalHistory)
  787. assert.Equal(t, merged.FileHistories["file3"], res2.GlobalHistory)
  788. assert.Len(t, merged.reversedPeopleDict, 3)
  789. assert.Equal(t, merged.PeopleHistories[0], res1.GlobalHistory)
  790. assert.Equal(t, merged.PeopleHistories[1], merged.GlobalHistory)
  791. assert.Equal(t, merged.PeopleHistories[2], res2.GlobalHistory)
  792. }