From 14d2486c1b03d47c2680ca2d368ea649191cfdc8 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 22 Aug 2023 11:38:14 +0200 Subject: [PATCH] postgres direction encoder tests --- .../postgres-direction-encoder.spec.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/modules/geography/tests/unit/infrastructure/postgres-direction-encoder.spec.ts diff --git a/src/modules/geography/tests/unit/infrastructure/postgres-direction-encoder.spec.ts b/src/modules/geography/tests/unit/infrastructure/postgres-direction-encoder.spec.ts new file mode 100644 index 0000000..6f12ed5 --- /dev/null +++ b/src/modules/geography/tests/unit/infrastructure/postgres-direction-encoder.spec.ts @@ -0,0 +1,41 @@ +import { Coordinates } from '@modules/geography/core/application/types/coordinates.type'; +import { PostgresDirectionEncoder } from '@modules/geography/infrastructure/postgres-direction-encoder'; + +describe('Postgres direction encoder', () => { + it('should be defined', () => { + const postgresDirectionEncoder: PostgresDirectionEncoder = + new PostgresDirectionEncoder(); + expect(postgresDirectionEncoder).toBeDefined(); + }); + it('should encode coordinates to a postgres direction', () => { + const postgresDirectionEncoder: PostgresDirectionEncoder = + new PostgresDirectionEncoder(); + const coordinates: Coordinates[] = [ + { + lon: 6, + lat: 47, + }, + { + lon: 6.1, + lat: 47.1, + }, + { + lon: 6.2, + lat: 47.2, + }, + ]; + const direction = postgresDirectionEncoder.encode(coordinates); + expect(direction).toBe("'LINESTRING(6 47,6.1 47.1,6.2 47.2)'"); + }); + it('should decode a postgres direction to coordinates', () => { + const postgresDirectionEncoder: PostgresDirectionEncoder = + new PostgresDirectionEncoder(); + const direction = "'LINESTRING(6 47,6.1 47.1,6.2 47.2)'"; + const coordinates: Coordinates[] = + postgresDirectionEncoder.decode(direction); + expect(coordinates.length).toBe(3); + expect(coordinates[0].lat).toBe(47); + expect(coordinates[1].lon).toBe(6.1); + expect(coordinates[2].lat).toBe(47.2); + }); +});