JobStatistics.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { faker } from "@faker-js/faker";
  2. import {
  3. JobStatistic,
  4. JobStatistics,
  5. JobStatisticsType
  6. } from "./JobStatistics";
  7. describe("JobStatistics", function () {
  8. describe("getStats", function () {
  9. it("should include jobs statistics", function () {
  10. const statistics = new JobStatistics();
  11. const jobName = faker.lorem.text();
  12. statistics.updateStats(jobName, JobStatisticsType.TOTAL);
  13. statistics.getStats()[jobName].total.should.be.equal(1);
  14. });
  15. [
  16. JobStatisticsType.CONSTRUCTED,
  17. JobStatisticsType.FAILED,
  18. JobStatisticsType.QUEUED,
  19. JobStatisticsType.SUCCESSFUL,
  20. JobStatisticsType.TOTAL
  21. ].forEach(function (type) {
  22. it(`should sum ${type} count for total`, function () {
  23. const statistics = new JobStatistics();
  24. statistics.updateStats(faker.lorem.text(), type);
  25. statistics.updateStats(faker.lorem.text(), type);
  26. statistics
  27. .getStats()
  28. .total[type as keyof JobStatistic].should.be.equal(2);
  29. });
  30. });
  31. it(`should sum total duration for total`, function () {
  32. const statistics = new JobStatistics();
  33. const firstDuration = faker.number.int();
  34. const secondDuration = faker.number.int();
  35. const totalDuration = firstDuration + secondDuration;
  36. statistics.updateStats(
  37. faker.lorem.text(),
  38. JobStatisticsType.DURATION,
  39. firstDuration
  40. );
  41. statistics.updateStats(
  42. faker.lorem.text(),
  43. JobStatisticsType.DURATION,
  44. secondDuration
  45. );
  46. statistics
  47. .getStats()
  48. .total.totalTime.should.be.equal(totalDuration);
  49. });
  50. it("should calculate average time for total", function () {
  51. const statistics = new JobStatistics();
  52. const firstJobName = faker.lorem.text();
  53. const secondJobName = faker.lorem.text();
  54. const firstDuration = faker.number.int();
  55. const secondDuration = faker.number.int();
  56. const averageDuration = (firstDuration + secondDuration) / 2;
  57. statistics.updateStats(firstJobName, JobStatisticsType.TOTAL);
  58. statistics.updateStats(
  59. firstJobName,
  60. JobStatisticsType.DURATION,
  61. firstDuration
  62. );
  63. statistics.updateStats(secondJobName, JobStatisticsType.TOTAL);
  64. statistics.updateStats(
  65. secondJobName,
  66. JobStatisticsType.DURATION,
  67. secondDuration
  68. );
  69. statistics
  70. .getStats()
  71. .total.averageTime.should.be.equal(averageDuration);
  72. });
  73. });
  74. describe("updateStats", function () {
  75. const jobName = faker.lorem.text();
  76. [
  77. JobStatisticsType.CONSTRUCTED,
  78. JobStatisticsType.FAILED,
  79. JobStatisticsType.QUEUED,
  80. JobStatisticsType.SUCCESSFUL,
  81. JobStatisticsType.TOTAL
  82. ].forEach(function (type) {
  83. it(`should increment ${type} count`, function () {
  84. const statistics = new JobStatistics();
  85. statistics.updateStats(jobName, type);
  86. statistics.updateStats(jobName, type);
  87. statistics
  88. .getStats()
  89. [jobName][type as keyof JobStatistic].should.be.equal(2);
  90. });
  91. });
  92. it(`should add to total duration`, function () {
  93. const statistics = new JobStatistics();
  94. const firstDuration = faker.number.int();
  95. const secondDuration = faker.number.int();
  96. const totalDuration = firstDuration + secondDuration;
  97. statistics.updateStats(
  98. jobName,
  99. JobStatisticsType.DURATION,
  100. firstDuration
  101. );
  102. statistics.updateStats(
  103. jobName,
  104. JobStatisticsType.DURATION,
  105. secondDuration
  106. );
  107. statistics
  108. .getStats()
  109. [jobName].totalTime.should.be.equal(totalDuration);
  110. });
  111. it("should calculate average time", function () {
  112. const statistics = new JobStatistics();
  113. const firstDuration = faker.number.int();
  114. const secondDuration = faker.number.int();
  115. const averageDuration = (firstDuration + secondDuration) / 2;
  116. statistics.updateStats(jobName, JobStatisticsType.TOTAL);
  117. statistics.updateStats(
  118. jobName,
  119. JobStatisticsType.DURATION,
  120. firstDuration
  121. );
  122. statistics.updateStats(jobName, JobStatisticsType.TOTAL);
  123. statistics.updateStats(
  124. jobName,
  125. JobStatisticsType.DURATION,
  126. secondDuration
  127. );
  128. statistics
  129. .getStats()
  130. [jobName].averageTime.should.be.equal(averageDuration);
  131. });
  132. });
  133. });