From 2851964a85d5df902d1b778c295d5f5c1ed56691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bitard=20Micha=C3=ABl?= Date: Tue, 15 Mar 2022 09:52:03 +0100 Subject: [PATCH] =?UTF-8?q?chore(tests):=20supprime=20la=20libraire=20obso?= =?UTF-8?q?l=C3=A8te=20knex-db-manager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 21 ++++ package.json | 1 + .../titre-slug-and-relations-update.test.ts | 8 +- .../permissions/administrations.test.ts | 10 +- .../queries/permissions/documents.test.ts | 9 +- .../queries/permissions/entreprises.test.ts | 8 +- .../queries/permissions/metas.test.ts | 8 +- .../queries/permissions/titres.test.ts | 8 +- .../queries/permissions/utilisateurs.test.ts | 8 +- src/database/queries/titres-activites.test.ts | 8 +- src/knex/config.ts | 3 - src/knex/seeds/00-delete.js | 115 ------------------ src/knex/seeds/10-entreprises.js | 22 ---- src/knex/seeds/11-utilisateurs.js | 15 --- src/knex/seeds/14-titres.js | 55 --------- src/knex/seeds/15-titres-activites.js | 11 -- src/knex/seeds/17-documents.js | 13 -- src/knex/seeds/18-globales.js | 11 -- tests/administration.test.ts | 8 +- ...inistrations-permissions-demarches.test.ts | 8 +- ...administrations-permissions-etapes.test.ts | 8 +- ...administrations-permissions-titres.test.ts | 8 +- tests/db-manager.ts | 115 +++++++++++++++--- tests/documents.test.ts | 8 +- tests/entreprises.test.ts | 8 +- tests/metas.test.ts | 8 +- tests/setup.ts | 11 +- tests/teardown.ts | 4 +- tests/titres-demarches.test.ts | 8 +- tests/titres-etapes-creer.test.ts | 8 +- tests/titres-etapes-modifier.test.ts | 8 +- tests/titres.test.ts | 8 +- tests/token.test.ts | 8 +- tests/upload.test.ts | 8 +- tests/utilisateurs.test.ts | 9 +- 35 files changed, 214 insertions(+), 363 deletions(-) delete mode 100644 src/knex/seeds/00-delete.js delete mode 100644 src/knex/seeds/10-entreprises.js delete mode 100644 src/knex/seeds/11-utilisateurs.js delete mode 100644 src/knex/seeds/14-titres.js delete mode 100644 src/knex/seeds/15-titres-activites.js delete mode 100644 src/knex/seeds/17-documents.js delete mode 100644 src/knex/seeds/18-globales.js diff --git a/package-lock.json b/package-lock.json index 828ec788c..d7db25b0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,6 +35,7 @@ "@types/node": "^17.0.21", "@types/node-fetch": "^2.5.12", "@types/node-mailjet": "^3.3.8", + "@types/pg": "^8.6.5", "@types/proj4": "^2.5.2", "@types/shpjs": "^3.4.1", "@types/sql-formatter": "^2.3.0", @@ -3212,6 +3213,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/pg": { + "version": "8.6.5", + "resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.6.5.tgz", + "integrity": "sha512-tOkGtAqRVkHa/PVZicq67zuujI4Oorfglsr2IbKofDwBSysnaqSx7W1mDqFqdkGE6Fbgh+PZAl0r/BWON/mozw==", + "dependencies": { + "@types/node": "*", + "pg-protocol": "*", + "pg-types": "^2.2.0" + } + }, "node_modules/@types/prettier": { "version": "2.4.4", "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.4.4.tgz", @@ -23545,6 +23556,16 @@ "version": "4.0.0", "dev": true }, + "@types/pg": { + "version": "8.6.5", + "resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.6.5.tgz", + "integrity": "sha512-tOkGtAqRVkHa/PVZicq67zuujI4Oorfglsr2IbKofDwBSysnaqSx7W1mDqFqdkGE6Fbgh+PZAl0r/BWON/mozw==", + "requires": { + "@types/node": "*", + "pg-protocol": "*", + "pg-types": "^2.2.0" + } + }, "@types/prettier": { "version": "2.4.4", "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.4.4.tgz", diff --git a/package.json b/package.json index d8f8223cb..562f1a52c 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "@types/node": "^17.0.21", "@types/node-fetch": "^2.5.12", "@types/node-mailjet": "^3.3.8", + "@types/pg": "^8.6.5", "@types/proj4": "^2.5.2", "@types/shpjs": "^3.4.1", "@types/sql-formatter": "^2.3.0", diff --git a/src/business/utils/titre-slug-and-relations-update.test.ts b/src/business/utils/titre-slug-and-relations-update.test.ts index 020e6175f..7f3af6da7 100644 --- a/src/business/utils/titre-slug-and-relations-update.test.ts +++ b/src/business/utils/titre-slug-and-relations-update.test.ts @@ -5,14 +5,14 @@ import { dbManager } from '../../../tests/db-manager' import { ITitre } from '../../types' import Titres from '../../database/models/titres' import { objectClone } from '../../tools' - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) const titreAdd = async (titre: ITitre) => diff --git a/src/database/queries/permissions/administrations.test.ts b/src/database/queries/permissions/administrations.test.ts index 295dea2c6..9ad640ead 100644 --- a/src/database/queries/permissions/administrations.test.ts +++ b/src/database/queries/permissions/administrations.test.ts @@ -15,14 +15,17 @@ import options from '../_options' console.info = jest.fn() console.error = jest.fn() +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + console.log('BEFORE ALL') + await dbManager.populateDb(knex) + console.log('AFTER BEFORE ALL') }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('administrationsTitresQuery', () => { @@ -35,6 +38,7 @@ describe('administrationsTitresQuery', () => { `( "Vérifie l'écriture de la requête sur les titres dont une administration a des droits sur le type", async ({ gestionnaire, associee, visible }) => { + console.log('PLOOOOOOP') await Titres.query().delete() await AdministrationsTitresTypes.query().delete() diff --git a/src/database/queries/permissions/documents.test.ts b/src/database/queries/permissions/documents.test.ts index 911e8b0dd..db711ac06 100644 --- a/src/database/queries/permissions/documents.test.ts +++ b/src/database/queries/permissions/documents.test.ts @@ -1,4 +1,3 @@ -import { knex } from '../../../knex' import { dbManager } from '../../../../tests/db-manager' import { userSuper } from '../../user-super' @@ -14,14 +13,14 @@ import { etapeTypeDocumentTypeUsedCheck } from './documents' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('documentSupprimer', () => { diff --git a/src/database/queries/permissions/entreprises.test.ts b/src/database/queries/permissions/entreprises.test.ts index b69a3a806..b9d78f673 100644 --- a/src/database/queries/permissions/entreprises.test.ts +++ b/src/database/queries/permissions/entreprises.test.ts @@ -8,14 +8,14 @@ import { entreprisesQueryModify } from './entreprises' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('entreprisesQueryModify', () => { diff --git a/src/database/queries/permissions/metas.test.ts b/src/database/queries/permissions/metas.test.ts index 28cb52173..ad69cc145 100644 --- a/src/database/queries/permissions/metas.test.ts +++ b/src/database/queries/permissions/metas.test.ts @@ -10,14 +10,14 @@ import Administrations from '../../models/administrations' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('metas permissions queries', () => { diff --git a/src/database/queries/permissions/titres.test.ts b/src/database/queries/permissions/titres.test.ts index fa5b6a069..556006f18 100644 --- a/src/database/queries/permissions/titres.test.ts +++ b/src/database/queries/permissions/titres.test.ts @@ -26,14 +26,14 @@ import { userSuper } from '../../user-super' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('titresQueryModify', () => { diff --git a/src/database/queries/permissions/utilisateurs.test.ts b/src/database/queries/permissions/utilisateurs.test.ts index 937be9b1f..f7dbad0b3 100644 --- a/src/database/queries/permissions/utilisateurs.test.ts +++ b/src/database/queries/permissions/utilisateurs.test.ts @@ -7,14 +7,14 @@ import { utilisateursGet } from '../utilisateurs' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) const mockAdministration = { diff --git a/src/database/queries/titres-activites.test.ts b/src/database/queries/titres-activites.test.ts index b663c52e1..70ecfa2f2 100644 --- a/src/database/queries/titres-activites.test.ts +++ b/src/database/queries/titres-activites.test.ts @@ -7,14 +7,14 @@ import Titres from '../models/titres' import AdministrationsTitresTypes from '../models/administrations-titres-types' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('teste les requêtes sur les activités', () => { test('vérifie que le filtrage fonctionne pour les administrations', async () => { diff --git a/src/knex/config.ts b/src/knex/config.ts index d6416c208..f17e5d7a4 100644 --- a/src/knex/config.ts +++ b/src/knex/config.ts @@ -24,9 +24,6 @@ const knexConfig = { // https://github.com/knex/knex/issues/4688 extension: 'js' }, - seeds: { - directory: join(__dirname, './seeds') - }, ...knexSnakeCaseMappers() } diff --git a/src/knex/seeds/00-delete.js b/src/knex/seeds/00-delete.js deleted file mode 100644 index 180bf8e30..000000000 --- a/src/knex/seeds/00-delete.js +++ /dev/null @@ -1,115 +0,0 @@ -const seeding = require('../seeding') - -const seed = seeding(async ({ del }) => { - // 18 - await del('globales') - - // 17 - await del('documents') - - // 15 - await del('titresActivites') - - // 14 - await Promise.all([ - del('titresSubstances'), - del('titresPointsReferences'), - del('titresTitulaires'), - del('titresCommunes'), - del('titresForets'), - del('titresAdministrationsGestionnaires'), - del('titresAdministrationsLocales'), - del('titresAmodiataires'), - del('titresEtapesJustificatifs'), - del('communes'), - del('forets') - ]) - await del('titresPoints') - await Promise.all([ - del('titresEtapes'), - del('titresPhases'), - del('titresDemarchesLiens') - ]) - await del('titresReferences') - await del('titresDemarches') - await del('titres') - - // 11 - await del('utilisateurs__administrations') - await del('utilisateurs__entreprises') - await del('utilisateurs') - - // 10 - await del('entreprisesEtablissements') - await del('entreprises') - - // 08 - await Promise.all([ - del('administrations__titresTypes'), - del('administrations__titresTypes__titresStatuts'), - del('administrations__titresTypes__etapesTypes'), - del('administrations__activitesTypes') - ]) - - // 07 - await del('administrations') - await del('administrationsTypes') - - // 06 - await del('activitesTypes__documentsTypes') - await del('activitesTypes__pays') - await del('activitesTypes__titresTypes') - await del('activitesTypes') - await del('activitesStatuts') - - // 04 - await del('substancesFiscales') - await del('substances__substancesLegales') - await del('substancesLegales') - await del('substancesLegalesCodes') - await del('substances') - - // 03 - await Promise.all([ - del('definitions'), - del('permissions'), - del('titresTypes__demarchesTypes'), - del('titresTypes_demarchesTypes_etapesTypes_documentsTypes'), - del('titresTypes__demarchesTypes__etapesTypes'), - del('entreprises__documents_types'), - del('etapesTypes__justificatifsTypes'), - del('etapesTypes__documentsTypes'), - del('etapesTypes__etapesStatuts'), - del('titresTypes__titresStatuts'), - del('geoSystemes') - ]) - await Promise.all([ - del('titresStatuts'), - del('devises'), - del('unites'), - del('referencesTypes'), - del('phasesStatuts'), - del('demarchesTypes'), - del('etapesTypes'), - del('demarchesStatuts'), - del('etapesStatuts'), - del('documentsTypes'), - del('titresTypes') - ]) - await Promise.all([del('domaines'), del('titresTypesTypes')]) - - // 02 - await del('departements') - await del('regions') - await del('pays') - - // 01 - await del('mois') - await del('trimestres') - await del('annees') - await del('frequences') -}) - -module.exports = seed - -module.exports.seed = seed diff --git a/src/knex/seeds/10-entreprises.js b/src/knex/seeds/10-entreprises.js deleted file mode 100644 index a71f0fafc..000000000 --- a/src/knex/seeds/10-entreprises.js +++ /dev/null @@ -1,22 +0,0 @@ -const seeding = require('../seeding') - -const entreprises = require(`../../sources/entreprises.json`) -const entreprisesEtablissements = require(`../../sources/entreprises-etablissements.json`) - -const findMissing = (elements, relations, field1, field2) => - relations.forEach(r => { - if (!elements.find(e => r[field2] === e[field1])) { - throw new Error(`Missing: ${field1} <-> ${field2} = "${r[field2]}"`) - } - }) - -findMissing(entreprises, entreprisesEtablissements, 'id', 'entreprise_id') - -const seed = seeding(async ({ insert }) => { - await insert('entreprises', entreprises) - await insert('entreprisesEtablissements', entreprisesEtablissements) -}) - -module.exports = seed - -module.exports.seed = seed diff --git a/src/knex/seeds/11-utilisateurs.js b/src/knex/seeds/11-utilisateurs.js deleted file mode 100644 index 308e74ff9..000000000 --- a/src/knex/seeds/11-utilisateurs.js +++ /dev/null @@ -1,15 +0,0 @@ -const seeding = require('../seeding') - -const utilisateurs = require('../../../sources/utilisateurs.json') -const utilisateursEntreprises = require('../../../sources/utilisateurs--entreprises.json') -const utilisateursAdministrations = require('../../../sources/utilisateurs--administrations.json') - -const seed = seeding(async ({ insert }) => { - await insert('utilisateurs', utilisateurs) - await insert('utilisateurs__entreprises', utilisateursEntreprises) - await insert('utilisateurs__administrations', utilisateursAdministrations) -}) - -module.exports = seed - -module.exports.seed = seed diff --git a/src/knex/seeds/14-titres.js b/src/knex/seeds/14-titres.js deleted file mode 100644 index 02297481c..000000000 --- a/src/knex/seeds/14-titres.js +++ /dev/null @@ -1,55 +0,0 @@ -const communes = require('../../../sources/communes.json') -const forets = require('../../../sources/forets.json') -const titres = require('../../../sources/titres.json') -const titresAdministrationsGestionnaires = require('../../../sources/titres-administrations-gestionnaires.json') -const titresAdministrationsLocales = require('../../../sources/titres-administrations-locales.json') -const titresAmodiataires = require('../../../sources/titres-amodiataires.json') -const titresCommunes = require('../../../sources/titres-communes.json') -const titresDemarches = require('../../../sources/titres-demarches.json') -const titresDemarchesLiens = require('../../../sources/titres-demarches-liens.json') -const titresEtapes = require('../../../sources/titres-etapes.json') -const titresForets = require('../../../sources/titres-forets.json') -const titresPhases = require('../../../sources/titres-phases.json') -const titresPoints = require('../../../sources/titres-points.json') -const titresPointsReferences = require('../../../sources/titres-points-references.json') -const titresReferences = require('../../../sources/titres-references.json') -const titresSubstances = require('../../../sources/titres-substances.json') -const titresTitulaires = require('../../../sources/titres-titulaires.json') - -const seeding = require('../seeding') - -const seed = seeding(async ({ insert }) => { - await insert('communes', communes) - await insert('forets', forets) - await insert('titres', titres) - - await Promise.all([ - insert('titresDemarches', titresDemarches), - insert('titresReferences', titresReferences) - ]) - - await Promise.all([ - insert('titresEtapes', titresEtapes), - insert('titresPhases', titresPhases), - insert('titresDemarchesLiens', titresDemarchesLiens) - ]) - - await Promise.all([ - insert( - 'titresAdministrationsGestionnaires', - titresAdministrationsGestionnaires - ), - insert('titresAdministrationsLocales', titresAdministrationsLocales), - insert('titresAmodiataires', titresAmodiataires), - insert('titresSubstances', titresSubstances), - insert('titresTitulaires', titresTitulaires), - insert('titresCommunes', titresCommunes), - insert('titresForets', titresForets), - insert('titresPoints', titresPoints) - ]) - await insert('titresPointsReferences', titresPointsReferences) -}) - -module.exports = seed - -module.exports.seed = seed diff --git a/src/knex/seeds/15-titres-activites.js b/src/knex/seeds/15-titres-activites.js deleted file mode 100644 index e37789eb6..000000000 --- a/src/knex/seeds/15-titres-activites.js +++ /dev/null @@ -1,11 +0,0 @@ -const seeding = require('../seeding') - -const titresActivites = require('../../../sources/titres-activites.json') - -const seed = seeding(async ({ insert }) => { - await insert('titresActivites', titresActivites) -}) - -module.exports = seed - -module.exports.seed = seed diff --git a/src/knex/seeds/17-documents.js b/src/knex/seeds/17-documents.js deleted file mode 100644 index c6a4324ce..000000000 --- a/src/knex/seeds/17-documents.js +++ /dev/null @@ -1,13 +0,0 @@ -const seeding = require('../seeding') - -const documents = require('../../../sources/documents.json') -const titresEtapesJustificatifs = require('../../../sources/titres-etapes-justificatifs.json') - -const seed = seeding(async ({ insert }) => { - await insert('documents', documents) - await insert('titresEtapesJustificatifs', titresEtapesJustificatifs) -}) - -module.exports = seed - -module.exports.seed = seed diff --git a/src/knex/seeds/18-globales.js b/src/knex/seeds/18-globales.js deleted file mode 100644 index 614b3e1d2..000000000 --- a/src/knex/seeds/18-globales.js +++ /dev/null @@ -1,11 +0,0 @@ -const seeding = require('../seeding') - -const globales = require('../../../sources/globales.json') - -const seed = seeding(async ({ insert }) => { - await insert('globales', globales) -}) - -module.exports = seed - -module.exports.seed = seed diff --git a/tests/administration.test.ts b/tests/administration.test.ts index b656817f7..0b111ded2 100644 --- a/tests/administration.test.ts +++ b/tests/administration.test.ts @@ -5,14 +5,14 @@ import { dbManager } from './db-manager' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) const administration = { diff --git a/tests/administrations-permissions-demarches.test.ts b/tests/administrations-permissions-demarches.test.ts index c727097d5..a2bafa1a4 100644 --- a/tests/administrations-permissions-demarches.test.ts +++ b/tests/administrations-permissions-demarches.test.ts @@ -7,14 +7,14 @@ import { console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('Visibilité des démarches', () => { diff --git a/tests/administrations-permissions-etapes.test.ts b/tests/administrations-permissions-etapes.test.ts index aece2d670..814b2a540 100644 --- a/tests/administrations-permissions-etapes.test.ts +++ b/tests/administrations-permissions-etapes.test.ts @@ -21,17 +21,17 @@ jest.mock('../src/tools/file-delete', () => ({ })) console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) await TitresTypesDemarchesTypesEtapesTypesJustificatifsTypes.query().delete() await TitresTypesDemarchesTypesEtapesTypesDocumentsTypes.query().delete() }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('Visibilité des étapes', () => { diff --git a/tests/administrations-permissions-titres.test.ts b/tests/administrations-permissions-titres.test.ts index a9354862a..d69437277 100644 --- a/tests/administrations-permissions-titres.test.ts +++ b/tests/administrations-permissions-titres.test.ts @@ -7,14 +7,14 @@ import { console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('Visibilité des titres par les administrations gestionnaires ou associées', () => { diff --git a/tests/db-manager.ts b/tests/db-manager.ts index f16de9e0e..491bb2005 100644 --- a/tests/db-manager.ts +++ b/tests/db-manager.ts @@ -1,25 +1,108 @@ -import { Knex } from 'knex' -import { databaseManagerFactory } from 'knex-db-manager' import { join } from 'path' -import { knexConfig } from '../src/knex/config' +import { idGenerate } from '../src/database/models/_format/id-create' import { knexInstanceSet } from '../src/knex' +import knex, { Knex } from 'knex' +import { Client } from 'pg' +import { knexSnakeCaseMappers, Model } from 'objection' -// pour les tests on lance que les migrations du schéma -// src/knex/migrations-schema -knexConfig.migrations.directory = [ - join(__dirname, '../src/knex/migrations-schema') -] +class DbManager { + private readonly dbName: string -const dbManager = databaseManagerFactory({ - knex: knexConfig, - dbManager: { - superUser: knexConfig.connection.user, - superPassword: knexConfig.connection.password, - populatePathPattern: join(__dirname, '../src/knex/seeds', '0[1-9]*') + public constructor() { + // jest hack to get a global instance called only once + this.dbName = process.env.DB_NAME + ? process.env.DB_NAME + : `a${idGenerate().toLowerCase()}` + process.env.DB_NAME = this.dbName } -}) -knexInstanceSet(dbManager.knexInstance() as unknown as Knex) + public async init(): Promise { + const globalConnection = 'postgres://postgres:password@localhost/postgres' + const globalClient = new Client(globalConnection) + await globalClient.connect() + const queryResult = await globalClient.query( + `SELECT 1 FROM pg_database WHERE datname='${this.dbName}'` + ) + if (queryResult.rowCount === 0) { + await globalClient.query(`CREATE DATABASE ${this.dbName}`) + } + await globalClient.end() + + const knex = this.getKnex() + await knex.migrate.latest() + await DbManager.injectSeed(knex) + await this.closeKnex(knex) + } + + public getKnex() { + const knexConfig = { + client: 'pg', + connection: { + host: 'localhost', + port: 5432, + database: this.dbName, + user: 'postgres', + password: 'password' + }, + migrations: { + directory: [join(__dirname, '../src/knex/migrations-schema')] + }, + seeds: { + directory: join(__dirname, '../src/knex/seeds') + }, + ...knexSnakeCaseMappers() + } + + return knex(knexConfig) + } + + public async populateDb(knex: Knex): Promise { + this.setGlobally(knex) + await DbManager.truncateSchema(knex) + await DbManager.injectSeed(knex) + } + + public async truncateDb(knex: Knex): Promise { + await DbManager.truncateSchema(knex) + } + + public async closeKnex(knex: Knex): Promise { + knex.destroy() + } + + public setGlobally(knex: Knex): void { + Model.knex(knex) + knexInstanceSet(knex) + } + + public async end(): Promise { + const globalConnection = 'postgres://postgres:password@localhost/postgres' + const globalClient = new Client(globalConnection) + await globalClient.connect() + await globalClient.query(`DROP DATABASE ${this.dbName}`) + await globalClient.end() + } + + private static async injectSeed(knex: Knex) { + await knex.transaction(async trx => trx.seed.run()) + } + + private static async truncateSchema(knex: Knex) { + const tables = + (await knex('pg_tables') + .select('tablename') + .where('schemaname', 'public')) ?? [] + + await knex.raw( + `TRUNCATE TABLE "${tables + .filter(table => table.tablename !== 'knex_migrations') + .map(table => table.tablename) + .join('","')}"` + ) + } +} + +const dbManager = new DbManager() export { dbManager } diff --git a/tests/documents.test.ts b/tests/documents.test.ts index 31eb87c69..d299ade5f 100644 --- a/tests/documents.test.ts +++ b/tests/documents.test.ts @@ -13,17 +13,17 @@ import { titreDemarcheCreate } from '../src/database/queries/titres-demarches' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeEach(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterEach(async () => { - await dbManager.truncateDb() + await dbManager.truncateDb(knex) }) afterAll(async () => { - await dbManager.closeKnex() + await dbManager.closeKnex(knex) }) describe('documentSupprimer', () => { diff --git a/tests/entreprises.test.ts b/tests/entreprises.test.ts index d776becbd..4049f8f2a 100644 --- a/tests/entreprises.test.ts +++ b/tests/entreprises.test.ts @@ -38,17 +38,17 @@ const entreprisesEtablissementsFetchMock = mocked( entreprisesEtablissementsFetch, true ) - +const knex = dbManager.getKnex() beforeEach(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterEach(async () => { - await dbManager.truncateDb() + await dbManager.truncateDb(knex) }) afterAll(async () => { - await dbManager.closeKnex() + await dbManager.closeKnex(knex) }) describe('entrepriseCreer', () => { diff --git a/tests/metas.test.ts b/tests/metas.test.ts index 07726c3dc..603666fcd 100644 --- a/tests/metas.test.ts +++ b/tests/metas.test.ts @@ -3,14 +3,14 @@ import { graphQLCall, queryImport } from './_utils/index' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('statuts', () => { diff --git a/tests/setup.ts b/tests/setup.ts index ef5319f5c..7bf4d93e9 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -1,19 +1,10 @@ import 'dotenv/config' import { dbManager } from './db-manager' -import { connection } from '../src/knex/config' import { mailjet } from '../src/tools/api-mailjet' export default async () => { // https://github.com/mailjet/mailjet-apiv3-nodejs#disable-api-call mailjet.post('send', { version: 'v3.1', perform_api_call: false }) - await dbManager.createDbOwnerIfNotExist() - // la base de donnée est définie dans packageon - // par les variables d'env PGDATABASE=camino_tests - await dbManager.dropDb(connection.database) - await dbManager.createDb(connection.database) - - await dbManager.migrateDb() - - await dbManager.closeKnex() + await dbManager.init() } diff --git a/tests/teardown.ts b/tests/teardown.ts index a47447074..e4ddd656e 100644 --- a/tests/teardown.ts +++ b/tests/teardown.ts @@ -1,7 +1,5 @@ import { dbManager } from './db-manager' -import { knexConfig } from '../src/knex/config' - export default async () => { - await dbManager.dropDb(knexConfig.connection.database) + await dbManager.end() } diff --git a/tests/titres-demarches.test.ts b/tests/titres-demarches.test.ts index 23f1aa3d8..84bd4043f 100644 --- a/tests/titres-demarches.test.ts +++ b/tests/titres-demarches.test.ts @@ -7,17 +7,17 @@ import { userSuper } from '../src/database/user-super' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeEach(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterEach(async () => { - await dbManager.truncateDb() + await dbManager.truncateDb(knex) }) afterAll(async () => { - dbManager.closeKnex() + await dbManager.closeKnex(knex) }) describe('demarcheCreer', () => { diff --git a/tests/titres-etapes-creer.test.ts b/tests/titres-etapes-creer.test.ts index 623ee7957..0225687e7 100644 --- a/tests/titres-etapes-creer.test.ts +++ b/tests/titres-etapes-creer.test.ts @@ -25,9 +25,9 @@ jest.mock('../src/tools/file-delete', () => ({ })) console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) await TitresTypesDemarchesTypesEtapesTypesJustificatifsTypes.query().delete() await TitresTypesDemarchesTypesEtapesTypesDocumentsTypes.query().delete() @@ -48,8 +48,8 @@ beforeEach(async () => { }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) const demarcheCreate = async () => { diff --git a/tests/titres-etapes-modifier.test.ts b/tests/titres-etapes-modifier.test.ts index 2d95b0dc6..dec29f7b0 100644 --- a/tests/titres-etapes-modifier.test.ts +++ b/tests/titres-etapes-modifier.test.ts @@ -26,9 +26,9 @@ jest.mock('../src/tools/file-delete', () => ({ console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) beforeEach(async () => { @@ -36,8 +36,8 @@ beforeEach(async () => { }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) async function etapeCreate() { diff --git a/tests/titres.test.ts b/tests/titres.test.ts index fd3abfeac..2f3579f2f 100644 --- a/tests/titres.test.ts +++ b/tests/titres.test.ts @@ -13,17 +13,17 @@ import { titreCreate } from '../src/database/queries/titres' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeEach(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterEach(async () => { - await dbManager.truncateDb() + await dbManager.truncateDb(knex) }) afterAll(async () => { - dbManager.closeKnex() + await dbManager.closeKnex(knex) }) describe('titre', () => { diff --git a/tests/token.test.ts b/tests/token.test.ts index ac228cb1f..db365a8c2 100644 --- a/tests/token.test.ts +++ b/tests/token.test.ts @@ -4,14 +4,14 @@ import Utilisateurs from '../src/database/models/utilisateurs' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('utilisateurConnecter', () => { diff --git a/tests/upload.test.ts b/tests/upload.test.ts index c4aeb4289..2b3814043 100644 --- a/tests/upload.test.ts +++ b/tests/upload.test.ts @@ -4,15 +4,15 @@ import { dbManager } from './db-manager' jest.mock('tus-node-server') console.info = jest.fn() - +const knex = dbManager.getKnex() describe('téléversement de fichier par rest (tus)', () => { beforeAll(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterAll(async () => { - await dbManager.truncateDb() - await dbManager.closeKnex() + await dbManager.truncateDb(knex) + await dbManager.closeKnex(knex) }) describe('permission de téléverser', () => { diff --git a/tests/utilisateurs.test.ts b/tests/utilisateurs.test.ts index b6807c1b2..94c9d8a2a 100644 --- a/tests/utilisateurs.test.ts +++ b/tests/utilisateurs.test.ts @@ -1,5 +1,4 @@ import { app } from './app' -import { knex } from '../src/knex' import { graphQLCall, queryImport, tokenCreate } from './_utils/index' import { userAdd } from '../src/knex/user-add' import request from 'supertest' @@ -9,17 +8,17 @@ import { IUtilisateur } from '../src/types' console.info = jest.fn() console.error = jest.fn() - +const knex = dbManager.getKnex() beforeEach(async () => { - await dbManager.populateDb() + await dbManager.populateDb(knex) }) afterEach(async () => { - await dbManager.truncateDb() + await dbManager.truncateDb(knex) }) afterAll(async () => { - dbManager.closeKnex() + await dbManager.closeKnex(knex) }) describe('utilisateurModifier', () => {