Skip to content

Commit

Permalink
Merge pull request #38 from Northeastern-Electric-Racing/Reverting-Pr…
Browse files Browse the repository at this point in the history
…isma-Changes

Revert Odyssey Base Change
  • Loading branch information
Peyton-McKee authored Sep 23, 2023
2 parents 2d3326a + 766dd80 commit 0e89b6c
Show file tree
Hide file tree
Showing 14 changed files with 338 additions and 7 deletions.
3 changes: 2 additions & 1 deletion scylla-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
"ws": "8.14.1"
},
"prisma": {
"schema": "./src/odyssey-base/src/prisma/schema.prisma"
"schema": "./src/prisma/schema.prisma"
},
"devDependencies": {
"@types/jest": "^29.5.4",
"@types/ws": "8.5.5",
"prisma": "^5.2.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-- CreateTable
CREATE TABLE "Run" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"locationName" TEXT NOT NULL,
"driverId" TEXT,
"systemId" TEXT,
"time" DATETIME NOT NULL,
CONSTRAINT "Run_locationName_fkey" FOREIGN KEY ("locationName") REFERENCES "Location" ("name") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Run_driverId_fkey" FOREIGN KEY ("driverId") REFERENCES "Driver" ("username") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "Run_systemId_fkey" FOREIGN KEY ("systemId") REFERENCES "System" ("name") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Location" (
"name" TEXT NOT NULL PRIMARY KEY,
"latitude" REAL NOT NULL,
"longitude" REAL NOT NULL,
"radius" REAL NOT NULL
);

-- CreateTable
CREATE TABLE "Driver" (
"username" TEXT NOT NULL PRIMARY KEY
);

-- CreateTable
CREATE TABLE "System" (
"name" TEXT NOT NULL PRIMARY KEY
);

-- CreateTable
CREATE TABLE "Data" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"value" INTEGER NOT NULL,
"dataTypeName" TEXT NOT NULL,
"time" INTEGER NOT NULL,
"runId" INTEGER NOT NULL,
CONSTRAINT "Data_dataTypeName_fkey" FOREIGN KEY ("dataTypeName") REFERENCES "DataType" ("name") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Data_runId_fkey" FOREIGN KEY ("runId") REFERENCES "Run" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "DataType" (
"name" TEXT NOT NULL PRIMARY KEY,
"unit" TEXT NOT NULL,
"nodeName" TEXT NOT NULL,
CONSTRAINT "DataType_nodeName_fkey" FOREIGN KEY ("nodeName") REFERENCES "Node" ("name") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Node" (
"name" TEXT NOT NULL PRIMARY KEY
);

-- CreateIndex
CREATE UNIQUE INDEX "Run_id_key" ON "Run"("id");

-- CreateIndex
CREATE UNIQUE INDEX "Location_name_key" ON "Location"("name");

-- CreateIndex
CREATE UNIQUE INDEX "Driver_username_key" ON "Driver"("username");

-- CreateIndex
CREATE UNIQUE INDEX "System_name_key" ON "System"("name");

-- CreateIndex
CREATE UNIQUE INDEX "Data_id_key" ON "Data"("id");

-- CreateIndex
CREATE UNIQUE INDEX "DataType_name_key" ON "DataType"("name");

-- CreateIndex
CREATE UNIQUE INDEX "Node_name_key" ON "Node"("name");
3 changes: 3 additions & 0 deletions scylla-server/src/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
4 changes: 4 additions & 0 deletions scylla-server/src/prisma/prisma-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
export default prisma;
62 changes: 62 additions & 0 deletions scylla-server/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-1.1.x", "linux-arm64-openssl-1.1.x"]
}

datasource db {
provider = "sqlite"
url = "file:./mydatabase.db"
}

model Run {
id Int @id @unique @default(autoincrement())
locationName String
location Location @relation(fields: [locationName], references: [name])
driverId String?
driver Driver? @relation(fields: [driverId], references: [username])
systemId String?
system System? @relation(fields: [systemId], references: [name])
data Data[]
time DateTime
}

model Location {
name String @id @unique
latitude Float
longitude Float
radius Float
runs Run[]
}

model Driver {
username String @id @unique
runs Run[]
}

model System {
name String @id @unique
runs Run[]
}

model Data {
id Int @id @unique @default(autoincrement())
value Int
dataTypeName String
dataType DataType @relation(fields: [dataTypeName], references: [name])
time Int
runId Int
run Run @relation(fields: [runId], references: [id])
}

model DataType {
name String @id @unique
unit String
data Data[]
node Node @relation(fields: [nodeName], references: [name])
nodeName String
}

model Node {
name String @id @unique
dataTypes DataType[]
}
2 changes: 1 addition & 1 deletion scylla-server/src/proxy/proxy-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Socket } from 'socket.io';
import { ClientMessage } from '../utils/message.utils';
import { ResponseFunction } from '../odyssey-base/src/types/message.types';
import { ResponseFunction } from '../utils/message-maps.utils';

/**
* Proxy for handling Inputting and Outputting Messages to a Client
Expand Down
11 changes: 11 additions & 0 deletions scylla-server/src/services/dataTypes.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import prisma from '../prisma/prisma-client';
import { ResponseFunction } from '../utils/message-maps.utils';

/**
* CRUD operation to get all dataTypes
* @returns string containing all the dataTypes
*/
export const getAllDataTypes: ResponseFunction = async () => {
const data = await prisma.dataType.findMany();
return JSON.stringify(data);
};
27 changes: 27 additions & 0 deletions scylla-server/src/services/driver.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import prisma from '../prisma/prisma-client';
import { ResponseFunction } from '../utils/message-maps.utils';

/**
* CRUD operation to get all dataTypes
* @returns string containing all the dataTypes
*/
export const getAllDrivers: ResponseFunction = async () => {
const data = await prisma.driver.findMany();
return JSON.stringify(data);
};

/**
* CRUD operation to create a driver in the database if it doesn't already exist, does nothing otherwise.
*
*/
export const upsertDriver = async (driverName: string) => {
await prisma.driver.upsert({
where: {
username: driverName
},
update: {},
create: {
username: driverName
}
});
};
29 changes: 29 additions & 0 deletions scylla-server/src/services/systems.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import prisma from '../prisma/prisma-client';
import { ResponseFunction } from '../utils/message-maps.utils';

/**
* CRUD operation to get all systems with ResponseFunction type
* @returns Promise<string> contianing all the systems in the db
*/
export const getAllSystems: ResponseFunction = async () => {
const data = await prisma.system.findMany();
return JSON.stringify(data);
};

/**
* CRUD opertation that creates system if it doesn't exist, otherwise does nothing.
* Currently designated private so not hooked up to server.
* @param systemName name of the system as string
* @returns Promise<void>
*/
export const upsertSystem = async (systemName: string) => {
await prisma.system.upsert({
where: {
name: systemName
},
update: {},
create: {
name: systemName
}
});
};
9 changes: 5 additions & 4 deletions scylla-server/src/utils/message-maps.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getAllDataTypes } from '../odyssey-base/src/services/dataTypes.services';
import { getAllDrivers } from '../odyssey-base/src/services/driver.services';
import { getAllSystems } from '../odyssey-base/src/services/systems.services';
import { ResponseFunction } from '../odyssey-base/src/types/message.types';
import { getAllDataTypes } from '../services/dataTypes.services';
import { getAllDrivers } from '../services/driver.services';
import { getAllSystems } from '../services/systems.services';

export type ResponseFunction = (data?: JSON) => Promise<string>;

/**
* Creates a map of messages received from the client to functions that handle the messages
Expand Down
15 changes: 15 additions & 0 deletions scylla-server/tests/dataTypes-services.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, test, expect } from 'vitest';
import { getAllDataTypes } from '../src/services/dataTypes.services';

describe('Data Type', () => {
test('Get All Data Types Works', async () => {
const expected = [];
const result = await getAllDataTypes();

// Parse result to a JavaScript object from the JSON string
const parsedResult = JSON.parse(result);

// Use toEqual to compare parsedResult with the expected array
expect(parsedResult).toEqual(expected);
});
});
44 changes: 44 additions & 0 deletions scylla-server/tests/driver-services.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, test, expect, afterEach } from 'vitest';
import { getAllDrivers, upsertDriver } from '../src/services/driver.services';
import prisma from '../src/prisma/prisma-client';

/**
* Tests for CRUD Service functions
*/
describe('CRUD Driver', () => {
afterEach(async () => {
try {
await prisma.driver.delete({
where: {
username: 'test'
}
});
} catch (err) {}
});

/**
* unit test for get all drivers
*/
test('Get All Data Types Works', async () => {
const expected = [];
const result = await getAllDrivers();

// Parse result to a JavaScript object from the JSON string
const parsedResult = JSON.parse(result);

// Use toEqual to compare parsedResult with the expected array
expect(parsedResult).toEqual(expected);
});

/**
* Unit testing for upsert driver
* test driver creation if the name doesn't already exist
* */
test('Upsert Driver Creates', async () => {
const expected = [{ username: 'test' }];
await upsertDriver('test');
const result = JSON.parse(await getAllDrivers());

expect(result).toEqual(expected);
});
});
60 changes: 60 additions & 0 deletions scylla-server/tests/systems-services.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, test, expect, afterEach } from 'vitest';
import { getAllSystems, upsertSystem } from '../src/services/systems.services';

import prisma from '../src/prisma/prisma-client';

/**
* Tests for CRUD Service functions
*/
describe('CRUD Systems', () => {
//cleaning up
afterEach(async () => {
try {
await prisma.system.delete({
where: {
name: 'test'
}
});
} catch (err) {}
});
/**
* unit test for upsert system
* testing creating system if doesn't exist
*/
test('Upsert System Creates', async () => {
const expected = [{ name: 'test' }];
await upsertSystem('test');
const result = JSON.parse(await getAllSystems());

// Use toEqual to compare parsedResult with the expected array
expect(result).toEqual(expected);
});

/**
* updated unit test for get all systems
*/
test('Get All Systems Works', async () => {
await upsertSystem('test');
const expected = [{ name: 'test' }];
const result = await getAllSystems();

// Parse result to a JavaScript object from the JSON string
const parsedResult = JSON.parse(result);

// Use toEqual to compare parsedResult with the expected array
expect(parsedResult).toEqual(expected);
});

/**
* unit test for upsert system
* testing does nothing if system does exist
*/
test('Upsert System Does Nothing', async () => {
const expected = [{ name: 'test' }];
await upsertSystem('test');
const result = JSON.parse(await getAllSystems());

// Use toEqual to compare result with the expected array
expect(result).toEqual(expected);
});
});

0 comments on commit 0e89b6c

Please sign in to comment.