Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thierry's Technical Test #172

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ env:
parser: babel-eslint
extends:
- eslint:recommended
- plugin:prettier/recommended
- prettier
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
# Coverage directory
coverage
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16.13.0
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:12.10.0-alpine
FROM node:16.13.0-alpine

WORKDIR /usr/src

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ Feel free to make any changes to the `updateBenefitValue` method implementation

Please commit as frequently as possible to make the review easier.

## Installation

```bash
# switch to the right node version
nvm use

# install dependencies
yarn

yarn start
```

## Test

To make sure that you will not break anything in the existing code, we added a log of the simulation in the _output.txt_ file. Make sure that your code is able to generate the same file. You can generate a new file by running one of the following commands:
Expand Down
120 changes: 120 additions & 0 deletions drug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const MAX_BENEFIT = 50;

export class Drug {
/**
*
* @param {string} name - name
* @param {number} expiresIn - nb day before the expiration date is reached, < 0 if the date is passed
* @param {number} benefit - always > 0
*/
constructor(name, expiresIn, benefit) {
if (benefit < 0) {
throw new Error("benefit must be > 0");
}

if (benefit > MAX_BENEFIT) {
throw new Error(`benefit must be < MAX_BENEFIT`);
}

this.name = name;
this.expiresIn = expiresIn;
this.benefit = benefit;
}

/**
* ensure that benefit never exceed the max and is never negative
*/
checkBenefit() {
if (this.benefit >= MAX_BENEFIT) {
this.benefit = MAX_BENEFIT;
}
if (this.benefit < 0) {
this.benefit = 0;
}

return this;
}

updateBenefit() {
switch (this.name) {
case "Magic Pill": {
// magic pills attributes are not changing
break;
}
case "Herbal Tea": {
this.updateHerbalTeaBenefit();
break;
}
case "Fervex": {
this.updateFervexBenefit();
break;
}
case "Dafalgan": {
this.updateDafalganBenefit();
break;
}
default: {
this.updateCommonDrugBenefit();
break;
}
}

return this.checkBenefit();
}

updateHerbalTeaBenefit() {
this.expiresIn -= 1;

if (this.expiresIn < 0) {
this.benefit += 2;
return;
}

this.benefit += 1;
}

updateFervexBenefit() {
this.expiresIn -= 1;

if (this.expiresIn < 0) {
this.benefit = 0;
return;
}

if (this.expiresIn < 5) {
this.benefit += 3;
return;
}

if (this.expiresIn < 10) {
this.benefit += 2;
return;
}

this.benefit += 1;
}

updateDafalganBenefit() {
this.updateCommonDrugBenefit(2);
}

/**
*
* @param {number} degradation - decrease of benefit will be based on the degradation coefficient
* @returns
*/
updateCommonDrugBenefit(degradation = 1) {
this.expiresIn -= 1;

if (this.benefit === 0) {
return;
}

if (this.expiresIn < 0) {
this.benefit -= degradation * 2;
return;
}

this.benefit -= degradation;
}
}
121 changes: 121 additions & 0 deletions drug.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Drug } from "./drug";

describe("Drug", () => {
describe("#constructor", () => {
it("should throw if a drug is intialized with a benefit < 0", () => {
expect(() => new Drug("Doliprane", 20, -1)).toThrowError(
new Error("benefit must be > 0")
);
});
it("should throw if a drug is intialized with a benefit > 50", () => {
expect(() => new Drug("Doliprane", 20, 51)).toThrowError(
new Error("benefit must be < MAX_BENEFIT")
);
});
});

describe("common drugs", () => {
it("should decrease expiresIn and benefit by 1", () => {
expect(new Drug("Doliprane", 20, 30).updateBenefit()).toEqual(
new Drug("Doliprane", 19, 29)
);
});
it("should not decrease the benefit if it's already equal to 0", () => {
expect(new Drug("Doliprane", 20, 0).updateBenefit()).toEqual(
new Drug("Doliprane", 19, 0)
);
});
it("should decrease expiresIn and benefit by 2 if expiresIn is negative", () => {
expect(new Drug("Doliprane", 0, 30).updateBenefit()).toEqual(
new Drug("Doliprane", -1, 28)
);
});

it("should decrease expiresIn and benefit by 2 if expiresIn is negative without exceeding 0", () => {
expect(new Drug("Doliprane", -5, 1).updateBenefit()).toEqual(
new Drug("Doliprane", -6, 0)
);
});
});

describe("Dafalgan", () => {
it("should decrease expiresIn and benefit by 2", () => {
expect(new Drug("Dafalgan", 20, 30).updateBenefit()).toEqual(
new Drug("Dafalgan", 19, 28)
);
});
it("should not decrease the benefit if it's already equal to 0", () => {
expect(new Drug("Dafalgan", 20, 0).updateBenefit()).toEqual(
new Drug("Dafalgan", 19, 0)
);
});
it("should decrease expiresIn and benefit by 4 if expiresIn is negative", () => {
expect(new Drug("Dafalgan", -1, 30).updateBenefit()).toEqual(
new Drug("Dafalgan", -2, 26)
);
});
});

describe("Herbal Tea", () => {
it("should decrease expiresIn and increase benefit by 1", () => {
expect(new Drug("Herbal Tea", 20, 30).updateBenefit()).toEqual(
new Drug("Herbal Tea", 19, 31)
);
});
it("should decrease expiresIn and let the benefit at its maximum (50)", () => {
expect(new Drug("Herbal Tea", 20, 50).updateBenefit()).toEqual(
new Drug("Herbal Tea", 19, 50)
);
});
it("should decrease expiresIn and increase benefit by 2 if expiresIn is negative", () => {
expect(new Drug("Herbal Tea", 0, 30).updateBenefit()).toEqual(
new Drug("Herbal Tea", -1, 32)
);
});
});

describe("Magic Pill", () => {
it("should not change expiresIn of benefit", () => {
expect(new Drug("Magic Pill", 20, 30).updateBenefit()).toEqual(
new Drug("Magic Pill", 20, 30)
);
});
});

describe("Fervex", () => {
it("should decrease expiresIn and increase benefit by 1", () => {
expect(new Drug("Fervex", 20, 30).updateBenefit()).toEqual(
new Drug("Fervex", 19, 31)
);
});
it("should decrease expiresIn and increase benefit by 2 if 5 <= expiresIn <= 10 ", () => {
expect(new Drug("Fervex", 10, 30).updateBenefit()).toEqual(
new Drug("Fervex", 9, 32)
);
});

it("should decrease expiresIn and increase benefit if 5 <= expiresIn <= 10 without exceeding 50", () => {
expect(new Drug("Fervex", 10, 49).updateBenefit()).toEqual(
new Drug("Fervex", 9, 50)
);
});

it("should decrease expiresIn and increase benefit by 3 if 0 <= expiresIn < 5 ", () => {
expect(new Drug("Fervex", 5, 30).updateBenefit()).toEqual(
new Drug("Fervex", 4, 33)
);
});

it("should decrease expiresIn and increase benefit by 3 if 0 <= expiresIn < 5 without exceeding 50", () => {
expect(new Drug("Fervex", 5, 48).updateBenefit()).toEqual(
new Drug("Fervex", 4, 50)
);
});

it("should decrease expiresIn and set benefit a 0 if expiresIn is <= 0", () => {
expect(new Drug("Fervex", 0, 30).updateBenefit()).toEqual(
new Drug("Fervex", -1, 0)
);
});
});
});
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Drug, Pharmacy } from "./pharmacy";
import { Pharmacy } from "./pharmacy";
import { Drug } from "./drug";

import fs from "fs";

const drugs = [
new Drug("Doliprane", 20, 30),
new Drug("Herbal Tea", 10, 5),
new Drug("Fervex", 5, 40),
new Drug("Magic Pill", 15, 40)
new Drug("Magic Pill", 15, 40),
];
const trial = new Pharmacy(drugs);

Expand All @@ -17,7 +18,7 @@ for (let elapsedDays = 0; elapsedDays < 30; elapsedDays++) {
}

/* eslint-disable no-console */
fs.writeFile("output.txt", log, err => {
fs.writeFile("output.txt", log, (err) => {
if (err) {
console.log("error");
} else {
Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "take-home-test",
"version": "1.0.0",
"version": "1.1.0",
"repository": "git@github.com:inato/take-home-test.git",
"author": "inato",
"license": "MIT",
"private": true,
"engines": {
"yarn": ">=1.7.0",
"node": "=12.x"
"node": "16.13.0"
},
"scripts": {
"lint": "eslint .",
"test": "jest",
"test": "eslint . && jest --coverage",
"start": "babel-node index.js"
},
"devDependencies": {
Expand All @@ -28,5 +28,15 @@
"jest": "24.8.0",
"prettier": "^1.14.3",
"regenerator-runtime": "^0.12.1"
},
"jest": {
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
}
}
Loading