import { describe, it, before } from "node:test" import assert from "node:assert" import { PlataGlobalRuntime } from "../libs/runtime" import { PlataModels } from ".." describe('PlataModels.env', () => { const plataPromise = (async () => { const g = global as any g.Plata = new PlataGlobalRuntime() })() before(async () => { await plataPromise }) const model = new PlataModels.ModelTemplateEnv({ ENV_enum: [ PlataModels.Enum([ 0, 1 ]) ], ENV_decimal: [ PlataModels.Decimal(2,2) ], ENV_intMin: [ PlataModels.Int(5) ], ENV_intMax: [ PlataModels.Int(undefined, 5) ], ENV_intRequired: [ PlataModels.Required(), PlataModels.Int() ], ENV_smallDateTime: [ PlataModels.SmallDateTime() ], ENV_varChar: [ PlataModels.VarChar(1) ], }) const getOkayObj = () => ({ ENV_enum: `${0}`, ENV_decimal: `${22.22}`, ENV_intMin: `${5}`, ENV_intMax: `${5}`, ENV_intRequired: `${6}`, ENV_smallDateTime: '2022-12-04', ENV_varChar: '1', }) const getNotOkayObj = () => ({ ENV_enum: `${2}`, ENV_decimal: `${222.222}`, ENV_intMin: `${4}`, ENV_intMax: `${6}`, ENV_smallDateTime: '2022-12--04', ENV_varChar: '12', }) it('Validations (Okay)', async () => { const okayObj = getOkayObj() const value = await model.validate(okayObj) assert.strictEqual(value.errors, undefined) assert.deepEqual(value.value, okayObj) }) it('Validations (Not Okay)', async () => { const notOkayObj = getNotOkayObj() const value = await model.validate(notOkayObj) assert.notStrictEqual(value.errors, undefined) assert.strictEqual( value.errors?.length, Object.keys(model.template).length, value.errors?.map(e => `${e.errorID} - ${e.msg}`).join('\n') ) }) })