parallel_structure.spec.ts 4.86 KB
Newer Older
1
2
3
4
5
6
/**
 * IMPORTANT !
 * Décommenter temporairement la ligne suivante (import { } from "./mock_jasmine")
 * Pour exécuter ce code dans le débugger.
 * Faire de même avec le fichier test_func.ts
 */
Grand Francois's avatar
Grand Francois committed
7
// import { describe, expect, it, xdescribe, xit } from "../mock_jasmine";
8

9
import { MessageCode } from "../../src/index";
10
import { CreateStructure } from "../../src/structure/factory_structure";
11
12
import { ParallelStructure } from "../../src/structure/parallel_structure";
import { ParallelStructureParams } from "../../src/structure/parallel_structure_params";
13
import { loiAdmissiblesOuvrages, StructureType } from "../../src/structure/structure_props";
14
import { EnumEx } from "../../src/util/enum";
15
import { Result } from "../../src/util/result";
Dorchies David's avatar
Dorchies David committed
16
import { precDigits } from "../test_config";
17
import { checkResult } from "../test_func";
Dorchies David's avatar
Dorchies David committed
18
import { testParallelStructures } from "./functions";
19
20
import { structTest } from "./structure_test";

21
22
23
24
25
26
27
28
29
30
31
32
function createEnv() {
    const pstruct = new ParallelStructure(
        new ParallelStructureParams(30, 30, 15), // Q = 30, Z1 = 30, Z2 = 15
        false // debug
    );
    /*
     * Tests avec deux structures test identiques
     */
    pstruct.addChild(structTest);
    pstruct.addChild(structTest);
    return pstruct;
}
33

34
let pstruct: ParallelStructure;
35
36
37

describe("Class ParallelStructure: ", () => {
    describe("Calc()", () => {
38
39
40
        beforeEach(() => {
            pstruct = createEnv();
        });
Dorchies David's avatar
Dorchies David committed
41
        it("should return 1 result", () => {
42
43
44
            const p1 = createEnv();
            const res: Result = p1.Calc("Q");
            expect(p1.Calc("Q").nbResultElements).toEqual(1);
Dorchies David's avatar
Dorchies David committed
45
        });
46
47
48
49
50
        itParallelStructure(null, "Q", 30, 15);
        itParallelStructure(null, "Z1", 30, 15);
        itParallelStructure(null, "Z2", 15, 15);
        itParallelStructure(0, "ZDV", 0, 15);
        itParallelStructure(1, "ZDV", 0, 15);
Dorchies David's avatar
Dorchies David committed
51
        it("shoud return an error Q too high", () => {
52
            pstruct.prms.Q.v = 14;
53
54
55
56
            const res: Result = pstruct.Calc({
                uid: pstruct.structures[1].uid,
                symbol: "ZDV"
            });
57
            expect(res.code).toBe(MessageCode.ERROR_STRUCTURE_Q_TROP_ELEVE);
Dorchies David's avatar
Dorchies David committed
58
        });
59
60
    });
});
61
62
63
64
65
66
67
68
69
70
function getVarCalc(pstructLoc: ParallelStructure, structIndex: number, sVarCalc: string) : any { 
    if (structIndex !== null) {
        return {
            uid: pstructLoc.structures[structIndex].uid,
            symbol: sVarCalc
        };
    } else {
        return sVarCalc;
    }
}
71

72
73
74
75
76
77
/**
 * Test sur ParallelStructure
 * @param sVarCalc Variable à calculer
 * @param rVcalc Valeur de référence à retrouver
 * @param Q Débit de chacune des structures (pour structures identiques uniquement)
 */
78
function itParallelStructure(structIndex: number, sVarCalc: string, rVcalc: number, Q?: number) {  
79
    it(`${sVarCalc} should be ${rVcalc}`, () => {
80
81
        const VC1: any = getVarCalc(pstruct, structIndex, sVarCalc);
        checkResult(pstruct.Calc(VC1), rVcalc);
82
    });
Dorchies David's avatar
Dorchies David committed
83
    if (Q !== undefined) {
84
85
86
87
        const pstructLocal = createEnv();
        for (let i = 0; i < pstructLocal.structures.length; i++) {
            it(`Calc(${JSON.stringify(sVarCalc)}) ExtraResult[ouvrage[${i}].Q] should be ${Q}`, () => {
                const VC2: any = getVarCalc(pstruct, structIndex, sVarCalc);
Dorchies David's avatar
Dorchies David committed
88
                expect(
89
                    pstruct.Calc(VC2).resultElement.extraResults[`ouvrage[${i}].Q`]
Dorchies David's avatar
Dorchies David committed
90
91
                ).toBeCloseTo(Q, Math.max(0, precDigits - 1));
            });
92
93
            it(`Calc(${JSON.stringify(sVarCalc)}) ExtraResult[ouvrage[${i}].Q_Mode] should be 0`, () => {
                const VC3 = getVarCalc(pstruct, structIndex, sVarCalc);
Dorchies David's avatar
Dorchies David committed
94
                expect(
95
                    pstruct.Calc(VC3).resultElement.extraResults[`ouvrage[${i}].Q_ENUM_StructureFlowMode`]
Dorchies David's avatar
Dorchies David committed
96
97
                ).toEqual(0);
            });
98
99
            it(`Calc(${JSON.stringify(sVarCalc)}) ExtraResult[ouvrage[${i}].Q_Regime] should be 0`, () => {
                const VC4 = getVarCalc(pstruct, structIndex, sVarCalc);
Dorchies David's avatar
Dorchies David committed
100
                expect(
101
                    pstruct.Calc(VC4).resultElement.extraResults[`ouvrage[${i}].Q_ENUM_StructureFlowRegime`]
Dorchies David's avatar
Dorchies David committed
102
103
104
105
                ).toEqual(0);
            });
        }
    }
106
}
107
108
109
110
111
112
113
114
115
116

/*
 * Tests avec toutes les équations et toutes les variables (cf. jalhyd#38)
 */

const ps2: ParallelStructure = new ParallelStructure(
    new ParallelStructureParams(0, 102, 101.5), // Q = 0, Z1 = 102, Z2 = 101.5
    false // debug
);

Dorchies David's avatar
Dorchies David committed
117
118
119
120
121
describe("Class ParallelStructure: ", () => {
    // Ajout d'une structure de chaque type dans ParallelStructure
    const iLoiDebits: number[] = [];
    const iStTypes: number[] = [];
    for (const s of EnumEx.getValues(StructureType)) {
122
        for (const la of loiAdmissiblesOuvrages[StructureType[s]]) {
Mathias Chouet's avatar
Mathias Chouet committed
123
            ps2.addChild(CreateStructure(la, ps2, false));
Dorchies David's avatar
Dorchies David committed
124
125
            iLoiDebits.push(la);
            iStTypes.push(s);
Grand Francois's avatar
Grand Francois committed
126
127
        }
    }
Mathias Chouet's avatar
Mathias Chouet committed
128
    testParallelStructures(ps2, iLoiDebits);
129
});