From 2edb337a09938065878324b345fac3e9f21a215f Mon Sep 17 00:00:00 2001 From: cmoinier Date: Fri, 15 Nov 2024 08:55:55 +0100 Subject: [PATCH 1/2] fix: use hours for durations under 1 day and change condition for days var --- libs/api/metadata-converter/src/lib/iso19139/read-parts.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/api/metadata-converter/src/lib/iso19139/read-parts.ts b/libs/api/metadata-converter/src/lib/iso19139/read-parts.ts index 8581fcf33..e1838d904 100644 --- a/libs/api/metadata-converter/src/lib/iso19139/read-parts.ts +++ b/libs/api/metadata-converter/src/lib/iso19139/read-parts.ts @@ -540,7 +540,7 @@ export function getUpdateFrequencyFromCustomPeriod( per: 'day', updatedTimes: 1, } - } else if (days <= 7) { + } else if (days >= 1 && days <= 7) { return { per: 'week', updatedTimes: Math.round(7 / days - 0.0001), // this is to make sure that 'every 2 days' = '3 times per week' @@ -553,7 +553,7 @@ export function getUpdateFrequencyFromCustomPeriod( } else if (hours) { return { per: 'day', - updatedTimes: Math.round(24 / days), + updatedTimes: Math.round(24 / hours), } } return null From 9db51f0c37d6c15e76fb49569d78d0fc054c7d10 Mon Sep 17 00:00:00 2001 From: cmoinier Date: Fri, 15 Nov 2024 08:56:13 +0100 Subject: [PATCH 2/2] feat: add unit tests for all cases of getUpdateFrequencyFromCustomPerio --- .../src/lib/iso19139/read-parts.spec.ts | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/libs/api/metadata-converter/src/lib/iso19139/read-parts.spec.ts b/libs/api/metadata-converter/src/lib/iso19139/read-parts.spec.ts index 5cb02ae3a..f33351c88 100644 --- a/libs/api/metadata-converter/src/lib/iso19139/read-parts.spec.ts +++ b/libs/api/metadata-converter/src/lib/iso19139/read-parts.spec.ts @@ -113,7 +113,37 @@ describe('read parts', () => { }) }) describe('getUpdateFrequencyFromCustomPeriod', () => { - it('keeps a partial weekly period', () => { + it('returns null for empty input', () => { + expect(getUpdateFrequencyFromCustomPeriod('')).toBeNull() + }) + it('returns null for invalid input', () => { + expect(getUpdateFrequencyFromCustomPeriod('RSYZ45')).toBeNull() + }) + it('handles yearly period', () => { + expect(getUpdateFrequencyFromCustomPeriod('P1Y')).toEqual({ + updatedTimes: 1, + per: 'year', + }) + }) + it('handles monthly period of 1 month', () => { + expect(getUpdateFrequencyFromCustomPeriod('P0Y1M')).toEqual({ + updatedTimes: 1, + per: 'month', + }) + }) + it('handles monthly period of more than 1 month', () => { + expect(getUpdateFrequencyFromCustomPeriod('P0Y3M')).toEqual({ + updatedTimes: 4, + per: 'year', + }) + }) + it('handles daily period of 1 day', () => { + expect(getUpdateFrequencyFromCustomPeriod('P0Y0M1D')).toEqual({ + updatedTimes: 1, + per: 'day', + }) + }) + it('handles weekly period of 1 to 7 days', () => { expect(getUpdateFrequencyFromCustomPeriod('P0Y0M2D')).toEqual({ updatedTimes: 3, per: 'week', @@ -122,6 +152,22 @@ describe('read parts', () => { updatedTimes: 2, per: 'week', }) + expect(getUpdateFrequencyFromCustomPeriod('P0Y0M7D')).toEqual({ + updatedTimes: 1, + per: 'week', + }) + }) + it('handles monthly period of more than 7 days', () => { + expect(getUpdateFrequencyFromCustomPeriod('P0Y0M10D')).toEqual({ + updatedTimes: 3, + per: 'month', + }) + }) + it('handles hourly period', () => { + expect(getUpdateFrequencyFromCustomPeriod('P0Y0M0DT6H')).toEqual({ + updatedTimes: 4, + per: 'day', + }) }) }) })