Skip to content

Commit

Permalink
test improvements, longterm quick insulin service, react native paper
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-perpignane committed Oct 16, 2023
1 parent 40e2978 commit 27f66f2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 93 deletions.
4 changes: 0 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
"<node_internals>/**"
],
"env": {"CI": "true"},
//"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
"args": ["--verbose", "-i", "--no-cache", "${workspaceFolder}/src/components/__tests__/DbTextInputComponent.test.tsx"],
"console": "integratedTerminal",
"program": "${workspaceRoot}/node_modules/.bin/jest",
// "outFiles": [
// "${workspaceFolder}/**/*.js"
// ]
}
]
}
7 changes: 6 additions & 1 deletion src/components/__tests__/DbButtonComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ it('renders correctly when pressed', async () => {
let i = 0

const Sample = () => (
<DbButton title="coucou" testID="coucou" testOnly_pressed={true} onPress={() => i++} />
<DbButton
title="coucou"
testID="coucou"
testOnly_pressed={true}
onPress={() => i++}
/>
)

render(<Sample />)
Expand Down
12 changes: 3 additions & 9 deletions src/core/QuickInsulin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import longtermAdaptationCriteria from './quick_insulin_longterm_adaptation_crit
import {Trend, TrendService} from './TrendService'

export interface GlycemiaObjective {
min: number,
min: number
max: number
}

Expand All @@ -19,22 +19,18 @@ export class MealGlycemiaMeasure {
}

private computeTrend(glycemiaObjective: GlycemiaObjective) {

if (this._afterMealGlycemia > glycemiaObjective.max) {
return Trend.UP
}
else if (this._afterMealGlycemia < glycemiaObjective.min) {
} else if (this._afterMealGlycemia < glycemiaObjective.min) {
return Trend.DOWN
}
else {
} else {
return Trend.STABLE
}
}

public get trend() {
return this._trend
}

}

export class PuntualAdaptationResult {
Expand Down Expand Up @@ -72,7 +68,6 @@ export class PuntualAdaptationResult {
}

export class QuickInsulin {

public computePunctualAdaptation(
glycemiaLevel: number,
acetoneLevel?: number,
Expand Down Expand Up @@ -125,7 +120,6 @@ export class QuickInsulin {
findObjectiveCriterion = () => {
return punctualAdaptationCriteria.find(c => c.objective)
}

}

export class AcetoneNeededError extends Error {
Expand Down
117 changes: 58 additions & 59 deletions src/core/__tests__/QuickInsulin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {test, expect} from '@jest/globals'
import {AcetoneNeededError, MealGlycemiaMeasure, QuickInsulin} from '../QuickInsulin'
import {
AcetoneNeededError,
MealGlycemiaMeasure,
QuickInsulin,
} from '../QuickInsulin'

jest.mock(
'../quick_insulin_punctual_adaptation_criteria.json',
Expand Down Expand Up @@ -63,117 +67,112 @@ jest.mock(
describe('punctual adaptation', () => {
test('adaptation for glycemia measure 0.8 is 0', () => {
let quickInsulin = new QuickInsulin()

let glycemiaLevel = 0.8

expect(
quickInsulin.computePunctualAdaptation(glycemiaLevel).totalAdaptation,
).toEqual(0)
})

test('adaptation for glycemia measure 2.3 is +2', () => {
let quickInsulin = new QuickInsulin()

let glycemiaLevel = 2.3

expect(
quickInsulin.computePunctualAdaptation(glycemiaLevel).totalAdaptation,
).toEqual(2)
})

test('adaptation for glycemia measure 1.8 is +1', () => {
let quickInsulin = new QuickInsulin()

let glycemiaLevel = 1.8

expect(
quickInsulin.computePunctualAdaptation(glycemiaLevel).totalAdaptation,
).toEqual(1)
})

test('exception when condition not found', () => {
let quickInsulin = new QuickInsulin()

let glycemiaLevel = 2.1

const t = () => quickInsulin.computePunctualAdaptation(glycemiaLevel)

expect(t).toThrowError()
})

test('adaptation for glycemia measure 2.6 and acetone level = 0 is +3', () => {
let quickInsulin = new QuickInsulin()

let glycemiaLevel = 2.6
let acetoneLevel = 0

expect(
quickInsulin.computePunctualAdaptation(glycemiaLevel, acetoneLevel)
.totalAdaptation,
).toEqual(3)
})

test('acetone level required error when glycemia >= 2.5', () => {
let quickInsulin = new QuickInsulin()

let glycemiaLevel = 2.5

const t = () => {
quickInsulin.computePunctualAdaptation(glycemiaLevel).totalAdaptation
}

expect(t).toThrow(AcetoneNeededError)
})

})

describe('long term adaptaion', () => {

let objective = {min: 0.7, max: 1.4}

it.each(
it.each([
[
'Adaptation is +2 when trend is UP',
[
'Adaptation is +2 when trend is UP',
[
new MealGlycemiaMeasure(1.6, objective),
new MealGlycemiaMeasure(1.7, objective),
new MealGlycemiaMeasure(1.5, objective)
],
+2
new MealGlycemiaMeasure(1.6, objective),
new MealGlycemiaMeasure(1.7, objective),
new MealGlycemiaMeasure(1.5, objective),
],
+2,
],
[
'Adaptation is -2 when trend is DOWN',
[
'Adaptation is -2 when trend is DOWN',
[
new MealGlycemiaMeasure(0.6, objective),
new MealGlycemiaMeasure(0.55, objective),
new MealGlycemiaMeasure(0.65, objective)
],
-2
new MealGlycemiaMeasure(0.6, objective),
new MealGlycemiaMeasure(0.55, objective),
new MealGlycemiaMeasure(0.65, objective),
],
-2,
],
[
'Adaptation is 0 when no trend',
[
'Adaptation is 0 when no trend',
[
new MealGlycemiaMeasure(0.6, objective),
new MealGlycemiaMeasure(0.55, objective),
new MealGlycemiaMeasure(0.65, objective)
],
-2
new MealGlycemiaMeasure(0.6, objective),
new MealGlycemiaMeasure(0.55, objective),
new MealGlycemiaMeasure(0.65, objective),
],
-2,
],
[
'Adaptation is 0 when trend is STABLE',
[
'Adaptation is 0 when trend is STABLE',
[
new MealGlycemiaMeasure(1.3, objective),
new MealGlycemiaMeasure(0.8, objective),
new MealGlycemiaMeasure(0.99, objective)
],
0
new MealGlycemiaMeasure(1.3, objective),
new MealGlycemiaMeasure(0.8, objective),
new MealGlycemiaMeasure(0.99, objective),
],
]
) ('%s', (_label, measures, expectedAdaptation) => {
let quickInsulin = new QuickInsulin()
let adaptation = quickInsulin.computeLongtermAdaptation(measures)
expect(adaptation).toEqual(expectedAdaptation)
})

})
0,
],
])('%s', (_label, measures, expectedAdaptation) => {
let quickInsulin = new QuickInsulin()
let adaptation = quickInsulin.computeLongtermAdaptation(measures)
expect(adaptation).toEqual(expectedAdaptation)
})
})
54 changes: 34 additions & 20 deletions src/core/__tests__/TrendService.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
import {test, expect} from '@jest/globals'
import { Trend, TrendService } from '../TrendService'
import {Trend, TrendService} from '../TrendService'

describe('trend service', () => {

test.each([
[{trend: Trend.DOWN}, {trend:Trend.UP} , {trend: Trend.STABLE} , Trend.STABLE],
[{trend: Trend.DOWN}, {trend:Trend.DOWN} , {trend: Trend.DOWN} , Trend.DOWN],
[{trend: Trend.UP}, {trend:Trend.UP} , {trend: Trend.UP} , Trend.UP],
[{trend: Trend.STABLE}, {trend:Trend.STABLE} , {trend: Trend.STABLE} , Trend.STABLE],
])
[
{trend: Trend.DOWN},
{trend: Trend.UP},
{trend: Trend.STABLE},
Trend.STABLE,
],
[{trend: Trend.DOWN}, {trend: Trend.DOWN}, {trend: Trend.DOWN}, Trend.DOWN],
[{trend: Trend.UP}, {trend: Trend.UP}, {trend: Trend.UP}, Trend.UP],
[
{trend: Trend.STABLE},
{trend: Trend.STABLE},
{trend: Trend.STABLE},
Trend.STABLE,
],
])(
' when trend1 = %j and trend2 = %j and trend3 = %j, computed trend must give %s',
(
' when trend1 = %j and trend2 = %j and trend3 = %j, computed trend must give %s',
(trend1: {trend: Trend}, trend2: {trend: Trend}, trend3: {trend: Trend}, expected: Trend) => {
let trendService = new TrendService()
expect(trendService.findTrend([trend1, trend2, trend3])).toEqual(expected)
}
)
})

it('throws error when not enough measures are provided', () => {
let trendService = new TrendService()
let t = () => {trendService.findTrend([{trend: Trend.DOWN}, {trend: Trend.DOWN}])}
trend1: {trend: Trend},
trend2: {trend: Trend},
trend3: {trend: Trend},
expected: Trend,
) => {
let trendService = new TrendService()
expect(trendService.findTrend([trend1, trend2, trend3])).toEqual(expected)
},
)
})

expect(t).toThrowError()
it('throws error when not enough measures are provided', () => {
let trendService = new TrendService()
let t = () => {
trendService.findTrend([{trend: Trend.DOWN}, {trend: Trend.DOWN}])
}

})
expect(t).toThrowError()
})

0 comments on commit 27f66f2

Please sign in to comment.