Skip to content

Commit

Permalink
chore: ajout des seeds (33)
Browse files Browse the repository at this point in the history
  • Loading branch information
sroccaserra committed Dec 2, 2022
1 parent 6bb03f3 commit a5cf70c
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 35 deletions.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,48 @@ To learn more about Next.js, take a look at the following resources:

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Lancer l'application

### Prérequis

Avoir installé Docker et Docker Compose sur sa machine.

Copier le fichier `.env.example` avec le nom `.env`, et entrez cette
valeur pour la variable d'environnement `DATABASE_URL` :

```env
DATABASE_URL="postgresql://postgresql:secret@localhost:5432/postgresql"
```

Note : ce fichier .env ne sera pas versionné, et sera trouvé automatiquement
par Jest pour lancer les tests.

Démarrer votre service postgres avec Docker Compose :

```bash
docker-compose up -d postgresql
```

Initialiser votre base de tests :

```bash
npm run database:init
```

### Build & Start

Lancer un build puis un start :

```
npm run build
```

et

```
npm start
```

## Lancer les tests automatisés

Les tests automatisés sont séparés en tests du code client, et tests du code
Expand Down Expand Up @@ -86,7 +128,7 @@ docker-compose up -d postgresql
Initialisez votre base de tests :

```bash
npx dotenv -e .env.test -- npm run test:database:init
npx dotenv -e .env.test -- npm run database:init
```

Note : on utilise dotenv pour faire pointer vers la configuration de test.
Expand Down
29 changes: 15 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test:server": "npm run test:server:unit && npm run test:server:integration",
"test:server:unit": "jest src/server/.*.unit.test.* src/utils/.*.unit.test.*",
"test:server:integration": "jest --config=jest.integration.config.js src/server/.*.integration.test.*",
"test:database:init": "prisma migrate reset"
"database:init": "prisma db execute --file scripts/drop.sql && prisma migrate reset"
},
"browserslist": {
"production": [
Expand All @@ -36,6 +36,9 @@
"last 1 safari version"
]
},
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
"dependencies": {
"@gouvfr/dsfr": "^1.8.4",
"@prisma/client": "^4.6.1",
Expand All @@ -51,7 +54,7 @@
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"@types/node": "^18.11.9",
"@types/node": "^18.11.10",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.8",
"@typescript-eslint/eslint-plugin": "^5.40.1",
Expand Down Expand Up @@ -79,6 +82,7 @@
"stylelint-config-standard": "^29.0.0",
"stylelint-config-standard-scss": "^6.1.0",
"stylelint-order": "^5.0.0",
"typescript": "^4.8.4"
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
}
}
}
34 changes: 34 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
const périmètre1 = await prisma.perimetre.upsert({
where: { id: 'PER-001' },
update: {},
create: {
id: 'PER-001',
nom: 'Agriculture',
},
});

const chantier1 = await prisma.chantier.upsert({
where: { id: '' },
update: {},
create: {
id: 'CH-014',
nom: 'Relocaliser la production de fruits et légumes et celle de protéines végétales',
id_perimetre: 'PER-001',
},
});
console.log({ périmètre1, chantier1 });
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (error) => {
console.error(error);
await prisma.$disconnect();
process.exit(1);
});
1 change: 1 addition & 0 deletions scripts/drop_raw_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop schema if exists raw_data cascade;
2 changes: 1 addition & 1 deletion scripts/test_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ status=0
npm run test:client:unit || status=1
npm run test:client:integration || status=1
npm run test:server:unit || status=1
npx dotenv -e .env.ci -- npm run test:database:init -- --force || status=1
npx dotenv -e .env.ci -- npm run database:init -- --force || status=1
npx dotenv -e .env.ci -- npm run test:server:integration || status=1

exit $status
6 changes: 2 additions & 4 deletions src/server/domain/chantier/chantier.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export interface Chantier {
id: string;
nom: string;
axe: string | null;
ppg: string | null;
porteur: string | null;
}
id_perimetre: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('ChantierSQLRepository', () => {
// GIVEN
const prisma = new PrismaClient();
const chantierRepository: ChantierRepository = new ChantierSQLRepository(prisma);
const chantierInitial: Chantier = { id: 'THD', nom: 'Chantier 1', axe: 'X', ppg: 'X', porteur: 'MEFSIN' };
const chantierInitial2: Chantier = { id: 'TUP', nom: 'Chantier 2', axe: 'Y', ppg: 'Y', porteur: 'BOUBOU' };
const chantierInitial: Chantier = { id: 'THD', nom: 'Chantier 1', id_perimetre: 'PER-001' };
const chantierInitial2: Chantier = { id: 'TUP', nom: 'Chantier 2', id_perimetre: 'PER-001' };
await chantierRepository.add(chantierInitial);
await chantierRepository.add(chantierInitial2);

Expand Down
16 changes: 7 additions & 9 deletions src/server/infrastructure/chantierSQLRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chantier_prioritaire, PrismaClient } from '@prisma/client';
import { chantier, PrismaClient } from '@prisma/client';
import { Chantier } from 'server/domain/chantier/chantier.interface';
import { ChantierRepository } from '../domain/chantier/chantierRepository.interface';

Expand All @@ -9,24 +9,22 @@ export class ChantierSQLRepository implements ChantierRepository {
this.prisma = prisma;
}

async add(chantier: Chantier) {
await this.prisma.chantier_prioritaire.create({
data: chantier,
async add(chantierToAdd: Chantier) {
await this.prisma.chantier.create({
data: chantierToAdd,
});
}

async getListeChantiers() {
const chantiersPrisma = await this.prisma.chantier_prioritaire.findMany();
const chantiersPrisma = await this.prisma.chantier.findMany();
return chantiersPrisma.map(chantierPrisma => this.mapToDomain(chantierPrisma));
}

private mapToDomain(chantierPrisma: chantier_prioritaire): Chantier {
private mapToDomain(chantierPrisma: chantier): Chantier {
return {
id: chantierPrisma.id,
nom: chantierPrisma.nom,
axe: chantierPrisma.axe,
ppg: chantierPrisma.ppg,
porteur: chantierPrisma.porteur,
id_perimetre: chantierPrisma.id_perimetre,
};
}
}

0 comments on commit a5cf70c

Please sign in to comment.