Commit 70504619 authored by pom421's avatar pom421
Browse files

feat(test): add users model tests

parent 378e5cba
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`transform should transform in a model entity 1`] = `
Object {
"email": "john.mclane@proton.mail",
"firstName": "John",
"hospital": Object {
"id": 3,
"name": "",
},
"id": 2,
"lastName": "Mc Lane",
"role": "OPERATOR_ACT",
"scope": undefined,
}
`;
exports[`untransform should untransform in a Knex entity even without id and password 1`] = `
Object {
"email": "doc.brown@yahoo.com",
"first_name": "Doc",
"hospital_id": null,
"last_name": "Brown",
"role": "OPERATOR_ACT",
"scope": null,
}
`;
exports[`untransform should untransform in a Knex entity even without id and wih a password 1`] = `
Object {
"email": "doc.brown@yahoo.com",
"first_name": "Doc",
"hospital_id": null,
"id": 2,
"last_name": "Brown",
"password": "12345678",
"role": "OPERATOR_ACT",
"scope": null,
}
`;
exports[`untransform should untransform in a Knex entity with an id and without a password 1`] = `
Object {
"email": "marty.macfly@gmail.com",
"first_name": "Marty",
"hospital_id": null,
"id": 1,
"last_name": "Mac Fly",
"role": "REGIONAL_SUPERVISOR",
"scope": "[1,3,6]",
}
`;
import { transform, untransform } from "../../models/users"
describe("transform", () => {
it("should transform in a model entity", () => {
const knexUser = {
id: 2,
first_name: "John",
last_name: "Mc Lane",
email: "john.mclane@proton.mail",
role: "OPERATOR_ACT",
hospital_id: 3,
}
expect(transform(knexUser)).toMatchSnapshot()
})
})
describe("untransform", () => {
it("should untransform in a Knex entity with an id and without a password", () => {
const modelUser = {
id: 1,
firstName: "Marty",
lastName: "Mac Fly",
email: "marty.macfly@gmail.com",
role: "REGIONAL_SUPERVISOR",
scope: [{ id: 1 }, { id: 3 }, { id: 6 }],
hospital: null,
}
expect(untransform(modelUser)).toMatchSnapshot()
})
it("should untransform in a Knex entity even without id and wih a password", () => {
const modelUser = {
id: 2,
firstName: "Doc",
lastName: "Brown",
email: "doc.brown@yahoo.com",
password: "12345678",
role: "OPERATOR_ACT",
hospital: 2,
}
expect(untransform(modelUser)).toMatchSnapshot()
})
it("should untransform in a Knex entity even without id and password", () => {
const modelUser = {
firstName: "Doc",
lastName: "Brown",
email: "doc.brown@yahoo.com",
role: "OPERATOR_ACT",
hospital: 2,
}
expect(untransform(modelUser)).toMatchSnapshot()
})
})
import moment from "moment"
import { ISO_DATE } from "../utils/date"
// from DB entity to JS entity
export const transform = dbData => {
return !dbData
// from Knex entity to model (JS) entity
export const transform = knexData => {
return !knexData
? null
: {
id: dbData.id,
internalNumber: dbData.internal_number,
pvNumber: dbData.pv_number,
examinationDate: moment(dbData.examination_date).format(ISO_DATE),
profile: dbData.profile,
asker: { id: dbData.asker_id, name: dbData.asker_name },
id: knexData.id,
internalNumber: knexData.internal_number,
pvNumber: knexData.pv_number,
examinationDate: moment(knexData.examination_date).format(ISO_DATE),
profile: knexData.profile,
asker: { id: knexData.asker_id, name: knexData.asker_name },
user: {
id: dbData.added_by,
firstName: dbData.user_first_name,
lastName: dbData.user_last_name,
email: dbData.user_email,
id: knexData.added_by,
firstName: knexData.user_first_name,
lastName: knexData.user_last_name,
email: knexData.user_email,
},
hospital: { id: dbData.hospital_id, name: dbData.hospital_name },
...dbData.extra_data,
hospital: { id: knexData.hospital_id, name: knexData.hospital_name },
...knexData.extra_data,
}
}
export const transformAll = list => list.map(jsData => transform(jsData))
export const transformAll = list => list.map(model => transform(model))
// from JS entity to DB entity
export const untransform = jsData => {
const dbData = { extra_data: {} }
// from model (JS) entity to Knex entity
export const untransform = model => {
const knexData = { extra_data: {} }
const mainKeys = {
id: "id",
......@@ -40,13 +40,13 @@ export const untransform = jsData => {
hospitalId: "hospital_id",
}
Object.keys(jsData).forEach(key => {
Object.keys(model).forEach(key => {
if (mainKeys[key]) {
dbData[mainKeys[key]] = jsData[key]
knexData[mainKeys[key]] = model[key]
} else {
dbData.extra_data[key] = jsData[key]
knexData.extra_data[key] = model[key]
}
})
return dbData
return knexData
}
......@@ -3,28 +3,28 @@ import { logError } from "../utils/logger"
import { APIError } from "../utils/errors"
import { STATUS_400_BAD_REQUEST } from "../utils/http"
// from DB entity to JS entity
export const transform = dbData => {
return !dbData
// from Knex entity to model (JS) entity
export const transform = knexData => {
return !knexData
? null
: {
...dbData.extra_data, // add it first so regular fields can't be overriden by bad luck
id: dbData.id,
finesseNumber: dbData.finesse_number,
name: dbData.name,
addr1: dbData.addr1,
addr2: dbData.addr2,
town: dbData.town,
depCode: dbData.dep_code,
postalCode: dbData.postal_code,
...knexData.extra_data, // add it first so regular fields can't be overriden by bad luck
id: knexData.id,
finesseNumber: knexData.finesse_number,
name: knexData.name,
addr1: knexData.addr1,
addr2: knexData.addr2,
town: knexData.town,
depCode: knexData.dep_code,
postalCode: knexData.postal_code,
}
}
export const transformAll = list => list.map(jsData => transform(jsData))
export const transformAll = list => list.map(model => transform(model))
// from JS entity to DB entity
export const untransform = jsData => {
const dbData = { extra_data: {} }
// from model (JS) entity to Knex entity
export const untransform = model => {
const knexData = { extra_data: {} }
const predefinedKeys = {
id: "id",
......@@ -38,17 +38,17 @@ export const untransform = jsData => {
postalCode: "postal_code",
}
Object.keys(jsData).forEach(key => {
Object.keys(model).forEach(key => {
if (predefinedKeys[key]) {
dbData[predefinedKeys[key]] = jsData[key]
knexData[predefinedKeys[key]] = model[key]
} else {
dbData.extra_data[key] = jsData[key]
knexData.extra_data[key] = model[key]
}
})
// Pas d'id pour une création de user
if (!jsData.id) delete dbData.id
return dbData
if (!model.id) delete knexData.id
return knexData
}
const schema = yup.object().shape({
......@@ -72,9 +72,9 @@ const configValidate = {
abortEarly: false,
}
export const validate = async jsData => {
export const validate = async model => {
try {
const value = await schema.validate(jsData, configValidate)
const value = await schema.validate(model, configValidate)
return value
} catch (error) {
logError(error)
......@@ -86,4 +86,4 @@ export const validate = async jsData => {
}
}
export const cast = async jsData => await schema.cast(jsData)
export const cast = async model => await schema.cast(model)
......@@ -3,45 +3,45 @@ import { logError } from "../utils/logger"
import { APIError } from "../utils/errors"
import { STATUS_400_BAD_REQUEST } from "../utils/http"
// from DB entity to JS entity
export const transform = dbData => {
return !dbData
// from Knex entity to model (JS) entity
export const transform = knexData => {
return !knexData
? null
: {
id: dbData.id,
firstName: dbData.first_name,
lastName: dbData.last_name,
email: dbData.email,
role: dbData.role,
scope: dbData.scope,
hospital: !dbData.hospital_id
id: knexData.id,
firstName: knexData.first_name,
lastName: knexData.last_name,
email: knexData.email,
role: knexData.role,
scope: knexData.scope,
hospital: !knexData.hospital_id
? null
: {
id: dbData.hospital_id,
name: dbData.hospital_name || "",
id: knexData.hospital_id,
name: knexData.hospital_name || "",
},
}
}
export const transformAll = list => list.map(jsData => transform(jsData))
export const transformAll = list => list.map(model => transform(model))
// from JS entity to DB entity
export const untransform = jsData => {
const dbData = {
first_name: jsData.firstName,
last_name: jsData.lastName,
email: jsData.email,
role: jsData.role,
// from model (JS) entity to Knex entity
export const untransform = model => {
const knexData = {
first_name: model.firstName,
last_name: model.lastName,
email: model.email,
role: model.role,
scope:
!jsData.scope || !jsData.scope.length ? null : JSON.stringify(jsData.scope.map(curr => parseInt(curr.id, 10))), // Array needs to be explicitly stringified in PG
hospital_id: (jsData.hospital && jsData.hospital.id) || null,
!model.scope || !model.scope.length ? null : JSON.stringify(model.scope.map(curr => parseInt(curr.id, 10))), // Array needs to be explicitly stringified in PG
hospital_id: (model.hospital && model.hospital.id) || null,
}
// Pas d'id pour une création de user
if (jsData.id) dbData.id = jsData.id
if (jsData.password) dbData.password = jsData.password
if (model.id) knexData.id = model.id
if (model.password) knexData.password = model.password
return dbData
return knexData
}
const schema = yup.object().shape({
......@@ -77,9 +77,9 @@ const configValidate = {
abortEarly: false,
}
export const validate = async jsData => {
export const validate = async model => {
try {
const value = await schema.validate(jsData, configValidate)
const value = await schema.validate(model, configValidate)
return value
} catch (error) {
logError(error)
......@@ -91,4 +91,4 @@ export const validate = async jsData => {
}
}
export const cast = async jsData => await schema.cast(jsData)
export const cast = async model => await schema.cast(model)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment