Skip to content

Commit

Permalink
Add support for future years. (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
pazguille authored May 24, 2020
1 parent ca52633 commit 20078c1
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 13 deletions.
7 changes: 3 additions & 4 deletions lib/controller/v2/holidays.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions lib/data/holidays.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,6 +14,7 @@ import h2020 from './holidays/2020.json';

export default {
ref,
future,

h2011,
h2012,
Expand Down
39 changes: 39 additions & 0 deletions lib/data/holidays/future.json
Original file line number Diff line number Diff line change
@@ -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"
}]
6 changes: 6 additions & 0 deletions lib/reducers/holidays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { get } from 'lodash';

export default function(holidays, year) {
return get(holidays, `h${year}`, holidays.future);
}

4 changes: 3 additions & 1 deletion lib/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions test/api/v2/feriados/year.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
}
Expand All @@ -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);
};

Expand All @@ -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', () => {
Expand All @@ -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');
});

});

});
37 changes: 37 additions & 0 deletions test/reducers/holidays.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
1 change: 1 addition & 0 deletions test/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ describe('Reducers', () => {
require('./monthly');
require('./list');
require('./festive');
require('./holidays');
});

0 comments on commit 20078c1

Please sign in to comment.