Skip to content
Snippets Groups Projects
Commit b215d742 authored by Leander Tolksdorf's avatar Leander Tolksdorf
Browse files

add database tests

parent 7c9f2ebb
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,8 @@ variables:
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
POSTGRES_HOST_AUTH_METHOD: trust
SKIP_PREFLIGHT_CHECK: "true"
INITIAL_COORDINATOR_EMAIL: "initial@fahrtenbuch.example"
INITIAL_COORDINATOR_PASSWORD: "password"
# These folders and files are cached between builds
......
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
\ No newline at end of file
preset: "ts-jest",
testEnvironment: "node",
modulePathIgnorePatterns: ["<rootDir>/dist/"],
};
require('dotenv').config();
import request from 'supertest';
require("dotenv").config();
import request from "supertest";
describe('GET Endpoints', () => {
/*
* NOTICE:
* Those are structural test examples.
* I've made it, that they all expect 404 errors,
* since the database models have changes while the API hasn't
*/
it('if response is 404, server is alive (since GET / isn\'t defined)', (done) => {
request("http://localhost:4000")
.get('/')
.set('Accept', 'text/html')
.expect('Content-Type', /html/)
.expect(404, done);
});
it('Get Vehicle Type with ID (returns 404 since no data)', (done) => {
request("http://localhost:4000")
.get('/vehicleType/1')
.set('Accept', 'text/html')
.expect('Content-Type', /html/)
.expect(404, done);
});
it('Get all Vehicle Types', (done) => {
request("http://localhost:4000")
.get('/vehicleType')
.set('Accept', 'text/html')
.expect('Content-Type', /html/)
.expect(404, done);
});
});
\ No newline at end of file
describe("GET Endpoints", () => {
/*
* NOTICE:
* Those are structural test examples.
* I've made it, that they all expect 404 errors,
* since the database models have changes while the API hasn't
*/
it("if response is 200, server is alive ", (done) => {
request("http://localhost:4000")
.get("/")
.set("Accept", "text/html")
.expect("Content-Type", /html/)
.expect(200, done);
});
it("Get Vehicle Type with ID (returns 404 since no data)", (done) => {
request("http://localhost:4000")
.get("/vehicleType/1")
.set("Accept", "text/html")
.expect("Content-Type", /html/)
.expect(404, done);
});
it("Get all Vehicle Types", (done) => {
request("http://localhost:4000")
.get("/vehicleType")
.set("Accept", "text/html")
.expect("Content-Type", /html/)
.expect(404, done);
});
});
import { Client } from "pg";
import { QueryTypes, Sequelize } from "sequelize";
it("has the database", async () => {
const client = new Client({
user: process.env.DB_USER,
host: process.env.DB_HOST,
password: process.env.DB_PASSWORD,
});
await client.connect();
const query = await client.query(
`SELECT * FROM pg_database WHERE datname='${process.env.DB_NAME}'`
);
expect(query.rows.length).toBeGreaterThan(0);
await client.end();
});
describe("Database Tests", () => {
it("has the database", () => {
const client = new Client({
let client: Client;
const databaseTables = [
{
table_name: "checkin",
columns: [
{ column_name: "id", data_type: "uuid" },
{ column_name: "startTime", data_type: "timestamp with time zone" },
{
column_name: "estimatedEndTime",
data_type: "timestamp with time zone",
},
{ column_name: "boatId", data_type: "uuid" },
{ column_name: "createdAt", data_type: "timestamp with time zone" },
{ column_name: "updatedAt", data_type: "timestamp with time zone" },
{ column_name: "email", data_type: "character varying" },
{
column_name: "fullNameOfResponsableClient",
data_type: "character varying",
},
{ column_name: "additionalClients", data_type: "ARRAY" },
],
},
{
table_name: "boat",
columns: [
{ column_name: "status", data_type: "boolean" },
{ column_name: "createdAt", data_type: "timestamp with time zone" },
{ column_name: "updatedAt", data_type: "timestamp with time zone" },
{ column_name: "boattype", data_type: "uuid" },
{ column_name: "id", data_type: "uuid" },
{ column_name: "name", data_type: "character varying" },
{ column_name: "tags", data_type: "ARRAY" },
],
},
{
table_name: "boattype",
columns: [
{ column_name: "id", data_type: "uuid" },
{ column_name: "seats", data_type: "integer" },
{ column_name: "createdAt", data_type: "timestamp with time zone" },
{ column_name: "updatedAt", data_type: "timestamp with time zone" },
{ column_name: "name", data_type: "character varying" },
],
},
{
table_name: "employee",
columns: [
{ column_name: "id", data_type: "uuid" },
{ column_name: "createdAt", data_type: "timestamp with time zone" },
{ column_name: "updatedAt", data_type: "timestamp with time zone" },
{ column_name: "last_name", data_type: "character varying" },
{ column_name: "password", data_type: "character varying" },
{ column_name: "role", data_type: "character varying" },
{ column_name: "email", data_type: "character varying" },
{ column_name: "first_name", data_type: "character varying" },
],
},
];
beforeAll(async () => {
client = new Client({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
});
client.connect();
await client.connect();
});
});
const showAllDBs = async (sequelizeConnection: Sequelize) => {
const DBs = await sequelizeConnection.query(
"SELECT datname FROM pg_database",
{ type: QueryTypes.SELECT }
);
console.log(
"----------Found Databases----------\n",
DBs,
"\n-----------------------------------\n"
afterAll(async () => await client.end());
it("has the correct tables", async () => {
const query = await client.query(
`SELECT table_name FROM information_schema.tables WHERE table_schema='public'`
);
const tableNames = query.rows.map((row) => row.table_name).sort();
const shouldHaveTableNames = databaseTables
.map((table) => table.table_name)
.sort();
expect(tableNames).toEqual(shouldHaveTableNames);
});
it.each(databaseTables)(
"has correct columns for each table",
async ({ table_name, columns }) => {
const query = await client.query(
`SELECT column_name, data_type FROM information_schema.columns WHERE table_name='${table_name}';`
);
const queryColumns = query.rows;
for (let queryColumn of queryColumns) {
expect(columns).toContainEqual(queryColumn);
}
}
);
};
it("has the initial employee", async () => {
const query = await client.query(
`SELECT * FROM employee WHERE email='${process.env.INITIAL_COORDINATOR_EMAIL}';`
);
expect(query.rows.length).toEqual(1);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment