Skip to content

Commit

Permalink
fix(): use 'repo' instead of 'product'; add InvalidDateUnitError; add…
Browse files Browse the repository at this point in the history
… fixes and tests for dates that were not double-digit
  • Loading branch information
mikaelvesavuori committed Jan 1, 2023
1 parent 4c7ec9e commit 2821d9f
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ With `gitmetrix` you get the possibility to extract a set of core Git metrics ("

```json
{
"product": "ORG/REPO",
"repo": "ORG/REPO",
"period": {
"from": "20221228",
"to": ""
Expand Down Expand Up @@ -235,7 +235,7 @@ GET {BASE_URL}/GetMetrics?repo=SOMEORG/SOMEREPO&from=20221228&to=20221229
```json
{
// Dynamically set by the response
"product": "SOMEORG/SOMEREPO",
"repo": "SOMEORG/SOMEREPO",
"period": {
"from": "20221228",
"to": "20221229"
Expand Down
15 changes: 15 additions & 0 deletions src/application/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,18 @@ export class NoMatchingParserError extends Error {
};
}
}

/**
* @description Used when an incompatible date unit is encountered.
*/
export class InvalidDateUnitError extends Error {
constructor() {
super();
this.name = 'InvalidDateUnitError';
const message = `Incorrect date unit, must be 'day' or 'month'!`;
this.message = message;
this.cause = {
statusCode: 400
};
}
}
2 changes: 1 addition & 1 deletion src/domain/services/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Metrics {
const daily = this.createDailyMetrics(items);

return {
product: repoName,
repo: repoName,
period: {
from: fromDate,
to: toDate
Expand Down
19 changes: 12 additions & 7 deletions src/infrastructure/frameworks/date.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { zuluToUnix } from './time';

import { InvalidDateOrderError } from '../../application/errors';
import { InvalidDateOrderError, InvalidDateUnitError } from '../../application/errors';

/**
* @description Returns the first date in the current month in `YYYY-MM-DD` format.
Expand Down Expand Up @@ -37,8 +37,8 @@ export function getLastDateInCurrentMonth(): string {
*/
export function getCurrentDate(noDashes = false): string {
const date = new Date();
const day = makeTwoDigitDay(date);
const month = date.getMonth() + 1;
const day = makeTwoDigitDate(date, 'day');
const month = makeTwoDigitDate(date.getMonth() + 1, 'day');
const year = date.getFullYear();

const dateString = `${year}-${month}-${day}`;
Expand All @@ -48,11 +48,16 @@ export function getCurrentDate(noDashes = false): string {
}

/**
* @description Add leading zero if day is under 10.
* @description Add leading zero if date (day, month) is under 10.
*/
export function makeTwoDigitDay(date: Date): string {
const day = date.getDate().toString();
return day.length === 1 ? `0${day}` : day;
export function makeTwoDigitDate(date: Date | number, unit: 'day' | 'month'): string {
const value = (() => {
if (unit === 'day') return typeof date === 'number' ? `${date}` : `${date.getDate()}`;
if (unit === 'month') return typeof date === 'number' ? `${date}` : `${date.getMonth()}`;
throw new InvalidDateUnitError();
})();

return value.length === 1 ? `0${value}` : value;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type MakeMetricsInput = {
* @description The result of a metric.
*/
export type MetricsResult = {
product: string;
repo: string;
period: TimePeriod;
total: MetricSet;
daily: DailyMetricSet;
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export interface Repository {
/**
* @description Get metrics for a given repository and a period of time.
*
* @param {RepoName} product Name of the repository
* @param {RepoName} repo Name of the repository
* @param {string} fromDate Date in the form `YYYYMMDD` or `20201030`
* @param {string} [toDate] Date in the form `YYYYMMDD` or `20201030`
*/
getMetrics(repoName: RepoName, fromDate: string, toDate?: string): Promise<CleanedItem[]>;
getMetrics(repo: RepoName, fromDate: string, toDate?: string): Promise<CleanedItem[]>;

addApproval(input: MetricInput): Promise<void>;
addChangesRequested(input: MetricInput): Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion testdata/expectations/metrics-2days.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"from": "20221010",
"to": "20221030"
},
"product": "something",
"repo": "something",
"total": {
"additions": 494,
"approved": 19,
Expand Down
2 changes: 1 addition & 1 deletion testdata/expectations/metrics-range-until-most-recent.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"from": "20221010",
"to": ""
},
"product": "something",
"repo": "something",
"total": {
"additions": 545,
"approved": 21,
Expand Down
45 changes: 41 additions & 4 deletions tests/unit/infrastructure/frameworks/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getLastDateInCurrentMonth,
getCurrentDate,
getDateBefore,
makeTwoDigitDay,
makeTwoDigitDate,
datesWithinMaximumRange
} from '../../../../src/infrastructure/frameworks/date';

Expand All @@ -32,18 +32,42 @@ test.serial('It should get the last date in the current month in `YYYY-MM-DD` fo
t.deepEqual(response, expected);
});

test.serial('It should keep a two digit day date when passing in `15`', (t) => {
const expected = '15';

const response = makeTwoDigitDate(15, 'day');

t.deepEqual(response, expected);
});

test.serial('It should make a two digit day date from `1`', (t) => {
const expected = '01';

const response = makeTwoDigitDate(1, 'day');

t.deepEqual(response, expected);
});

test.serial('It should make a two digit month date from `1`', (t) => {
const expected = '01';

const response = makeTwoDigitDate(1, 'month');

t.deepEqual(response, expected);
});

test.serial('It should get the current date in `YYYY-MM-DD` format', (t) => {
const expected = `${year}-${month}-${day}`;
const expected = `${year}-${makeTwoDigitDate(month, 'month')}-${makeTwoDigitDate(day, 'day')}`;

const response = getCurrentDate();

t.deepEqual(response, expected);
});

test.serial('It should add a leading 0 if day value is below 10', (t) => {
test.serial('It should add a leading zero if day value is below 10', (t) => {
const expected = '05';

const response = makeTwoDigitDay(new Date('2022-12-05'));
const response = makeTwoDigitDate(new Date('2022-12-05'), 'day');

t.deepEqual(response, expected);
});
Expand Down Expand Up @@ -103,6 +127,10 @@ test.serial(
}
);

/**
* NEGATIVE TESTS
*/

test.serial(
'It should throw a InvalidDateOrderError if the start date is after the end date',
(t) => {
Expand All @@ -116,3 +144,12 @@ test.serial(
t.is(error.name, expected);
}
);

test.serial('It should throw a InvalidDateUnitError passed an invalid date unit', (t) => {
const expected = 'InvalidDateUnitError';

// @ts-ignore
const error: any = t.throws(() => makeTwoDigitDate(1, 'asdf'));

t.is(error.name, expected);
});

0 comments on commit 2821d9f

Please sign in to comment.