From 20078c198cb6c5c8be66affad825a0666a45cc7b Mon Sep 17 00:00:00 2001 From: Guille Paz Date: Sun, 24 May 2020 16:56:40 -0300 Subject: [PATCH] Add support for future years. (#28) --- lib/controller/v2/holidays.js | 7 +++---- lib/data/holidays.js | 2 ++ lib/data/holidays/future.json | 39 +++++++++++++++++++++++++++++++++++ lib/reducers/holidays.js | 6 ++++++ lib/reducers/index.js | 4 +++- package-lock.json | 2 +- test/api/v2/feriados/year.js | 24 ++++++++++++++------- test/reducers/holidays.js | 37 +++++++++++++++++++++++++++++++++ test/reducers/index.js | 1 + 9 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 lib/data/holidays/future.json create mode 100644 lib/reducers/holidays.js create mode 100644 test/reducers/holidays.js diff --git a/lib/controller/v2/holidays.js b/lib/controller/v2/holidays.js index 387e4c8..794817d 100644 --- a/lib/controller/v2/holidays.js +++ b/lib/controller/v2/holidays.js @@ -1,11 +1,11 @@ import Joi from 'joi'; import Boom from 'boom'; -import {get} from 'lodash'; import chain from 'lib/controller/chain'; import { monthly as reduceMonthly, - list as reduceList + list as reduceList, + holidays as reduceHolidays } from 'lib/reducers'; import { @@ -21,8 +21,7 @@ import { import holidays, { ref } from 'lib/data/holidays'; const setHolidays = (request, reply) => { - let yHolidays = get(holidays, `h${request.year}`); - if (!yHolidays) return reply(Boom.notFound()); + let yHolidays = reduceHolidays(holidays, request.year); if (request.format === 'mensual'){ let plain = reduceMonthly(yHolidays); diff --git a/lib/data/holidays.js b/lib/data/holidays.js index 9b9d43d..091f183 100644 --- a/lib/data/holidays.js +++ b/lib/data/holidays.js @@ -1,4 +1,5 @@ import ref from './holidays/ref.json'; +import future from './holidays/future.json'; import h2011 from './holidays/2011.json'; import h2012 from './holidays/2012.json'; @@ -13,6 +14,7 @@ import h2020 from './holidays/2020.json'; export default { ref, + future, h2011, h2012, diff --git a/lib/data/holidays/future.json b/lib/data/holidays/future.json new file mode 100644 index 0000000..913fd21 --- /dev/null +++ b/lib/data/holidays/future.json @@ -0,0 +1,39 @@ +[{ + "mes": "enero", + "01": "año-nuevo" +},{ + "mes": "febrero" +},{ + "mes": "marzo", + "24": "memoria-verdad-justicia" +},{ + "mes": "abril", + "02": "veteranos-malvinas", + "24": "armenia" +},{ + "mes": "mayo", + "01": "trabajador", + "25": "revolucion-mayo" +},{ + "mes": "junio", + "17": "martin-guemes", + "20": "belgrano" +},{ + "mes": "julio", + "09": "independencia" +},{ + "mes": "agosto", + "17": "san-martin" +},{ + "mes": "septiembre" +},{ + "mes": "octubre", + "12": "diversidad" +},{ + "mes": "noviembre", + "20": "soberania-nacional" +},{ + "mes": "diciembre", + "08": "inmaculada-maria", + "25": "navidad" +}] diff --git a/lib/reducers/holidays.js b/lib/reducers/holidays.js new file mode 100644 index 0000000..bd9c43f --- /dev/null +++ b/lib/reducers/holidays.js @@ -0,0 +1,6 @@ +import { get } from 'lodash'; + +export default function(holidays, year) { + return get(holidays, `h${year}`, holidays.future); +} + diff --git a/lib/reducers/index.js b/lib/reducers/index.js index a79af4f..2c4ac5b 100644 --- a/lib/reducers/index.js +++ b/lib/reducers/index.js @@ -2,10 +2,12 @@ import holidaysV1 from './holidaysV1'; import monthly from './monthly'; import list from './list'; import festive from './festive'; +import holidays from './holidays'; export default { holidaysV1, monthly, list, - festive + festive, + holidays }; diff --git a/package-lock.json b/package-lock.json index a050780..4dc12c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nolaborables", - "version": "2.3.0", + "version": "2.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/test/api/v2/feriados/year.js b/test/api/v2/feriados/year.js index 8df162d..463afff 100644 --- a/test/api/v2/feriados/year.js +++ b/test/api/v2/feriados/year.js @@ -5,7 +5,8 @@ import {isEqual, isNumber} from 'lodash'; import { monthly as reduceMonthly, - list as reduceList + list as reduceList, + holidays as reduceHolidays } from 'lib/reducers'; import { @@ -41,14 +42,16 @@ const tryYear = (year, expected, query) => { return; } + const holidaysToTest = reduceHolidays(holidays, year); + if (!expected){ // Default holidays are List and without optionals if (query.indexOf('formato=mensual') > -1){ - const plain = reduceMonthly(holidays[`h${year}`]); + const plain = reduceMonthly(holidaysToTest); expected = noOptionalsMonthly(loadMonthly(plain, ref)); } else { - const plain = reduceList(holidays[`h${year}`]); + const plain = reduceList(holidaysToTest); expected = noOptionalsList(loadList(plain, ref)); } } @@ -65,12 +68,13 @@ const tryYear = (year, expected, query) => { }; const getWithOptionals = (year, list) => { + const holidaysToTest = reduceHolidays(holidays, year); if (list){ - const plain = reduceList(holidays[`h${year}`]); + const plain = reduceList(holidaysToTest); return loadList(plain, ref); } - const plain = reduceMonthly(holidays[`h${year}`]); + const plain = reduceMonthly(holidaysToTest); return loadMonthly(plain, ref); }; @@ -88,8 +92,9 @@ describe('GET /{year}', () => { } }); - it('must return 404 on not filled future holidays', async () => { - await tryYear(2080, 404); + it('must return common holidays for future years', async () => { + const futureYear = new Date().getFullYear() + 10; + await tryYear(futureYear); }); describe('?formato=mensual', () => { @@ -106,6 +111,11 @@ describe('GET /{year}', () => { } }); + it('must return common holidays for future years', async () => { + const futureYear = new Date().getFullYear() + 10; + await tryYear(futureYear, getWithOptionals(futureYear), '?formato=mensual&incluir=opcional'); + }); + }); }); diff --git a/test/reducers/holidays.js b/test/reducers/holidays.js new file mode 100644 index 0000000..b6668e4 --- /dev/null +++ b/test/reducers/holidays.js @@ -0,0 +1,37 @@ +import chai from 'chai'; +import _ from 'lodash'; +import { holidays } from 'lib/reducers'; +const expect = chai.expect; + +const holidaysData = { + 'h2020': [{ + "mes": "enero", + "01": "año-nuevo" + },{ + "mes": "febrero", + "24,25": "carnaval" + }], + future: [{ + "mes": "enero", + "01": "año-nuevo" + }], +}; + +describe('#holidays', () => { + + it('must return holidays for the given year', () => { + let result = holidays(holidaysData, 2020); + expect(result).to.be.an('array'); + expect(result.length).to.be.equal(holidaysData.h2020.length); + + expect(_.isEqual(result, holidaysData.h2020)).to.be.true; + }); + + it('must return future common holidays for an undefined year', () => { + let result = holidays(holidaysData, 2024); + expect(result).to.be.an('array'); + expect(result.length).to.be.equal(holidaysData.future.length); + + expect(_.isEqual(result, holidaysData.future)).to.be.true; + }); +}); diff --git a/test/reducers/index.js b/test/reducers/index.js index a8e0f5f..41a0194 100644 --- a/test/reducers/index.js +++ b/test/reducers/index.js @@ -3,4 +3,5 @@ describe('Reducers', () => { require('./monthly'); require('./list'); require('./festive'); + require('./holidays'); });