From 6f02cbe3a3fef25437d6aeb0da6ed4e09750dbbd Mon Sep 17 00:00:00 2001 From: "luca.boel" Date: Mon, 9 Oct 2023 13:25:05 +0200 Subject: [PATCH 1/3] charger validation test rewriten + more tests --- backend-app/.env.example | 27 -------- .../tests/charger-validation.test.js | 64 +++++++++++-------- 2 files changed, 38 insertions(+), 53 deletions(-) delete mode 100644 backend-app/.env.example diff --git a/backend-app/.env.example b/backend-app/.env.example deleted file mode 100644 index 699d5fbe..00000000 --- a/backend-app/.env.example +++ /dev/null @@ -1,27 +0,0 @@ -PORT=8080 - -#OCPP -RUN_OCPP_TEST=0 -OCPP_TEST_INTERVAL_MULTIPLIER=1 -LIVEMETRICS_DB_UPDATE_INTERVAL=10000 - -#Use the local dev database instead of the production database -USE_LOCAL_DATABASE=1 - -#AWS -AWS_REGION= -USER_POOL= -USER_POOL_ID= -USER_POOL_SECRET= - -#Database -DATABASE_NAME= -DATABASE_USERNAME= -DATABASE_PASSWORD= -DATABASE_HOST= - -#Klarna -KLARNA_TOKEN= - -#Ignore Klarna, only for testing -BYPASS_KLARNA=1 diff --git a/backend-app/src/database-Interface/tests/charger-validation.test.js b/backend-app/src/database-Interface/tests/charger-validation.test.js index 553855a7..4d27d7b4 100644 --- a/backend-app/src/database-Interface/tests/charger-validation.test.js +++ b/backend-app/src/database-Interface/tests/charger-validation.test.js @@ -3,34 +3,34 @@ const { describe, expect, test } = require("@jest/globals"); const VALID_LONGITUDE = 120; const VALID_LATITUDE = 20; -describe("Charger By SerialNumber Validation with input:", () => { - test("empty string", () => { +describe("Charger By SerialNumber Validation", () => { + test("should return an error when input is an empty string", () => { const errors = chargerValidation.getChargerBySerialNumberValidation(""); expect(errors.length).toBe(1); }); - test("null", () => { + test("should return an error when input is null", () => { const errors = chargerValidation.getChargerBySerialNumberValidation(null); expect(errors.length).toBe(1); }); - test("undefined", () => { + test("should return an error when input is undefined", () => { const valErrors = chargerValidation.getChargerBySerialNumberValidation(undefined); expect(valErrors.length).toBe(1); }); - test("int", () => { + test("should return an error when input is an integer", () => { const errors = chargerValidation.getChargerBySerialNumberValidation(1); expect(errors.length).toBe(1); }); - test("float", () => { + test("should return an error when input is a float", () => { const errors = chargerValidation.getChargerBySerialNumberValidation(11.11); expect(errors.length).toBe(1); }); - test("long string", () => { + test("should return an error when input is a long string", () => { const errors = chargerValidation.getChargerBySerialNumberValidation( "ashdfjasdfj;laksdjflkasjdf;lkjsalkdfjlkajdfljadhfaoiuwoejasklflha;sjdsljdfasdfhjalshdfkja" ); @@ -38,8 +38,8 @@ describe("Charger By SerialNumber Validation with input:", () => { }); }); -describe("Add Charger Validation with input:", () => { - test("all undefined", () => { +describe("Add Charger Validation", () => { + test("should return errors when all inputs are undefined", () => { const errors = chargerValidation.getAddChargerValidation( undefined, undefined, @@ -47,15 +47,18 @@ describe("Add Charger Validation with input:", () => { ); expect(errors.length).toBe(3); }); - test("all null", () => { + + test("should return errors when all inputs are null", () => { const errors = chargerValidation.getAddChargerValidation(null, null, null); expect(errors.length).toBe(3); }); - test("all empty string", () => { + + test("should return errors when all inputs are empty strings", () => { const errors = chargerValidation.getAddChargerValidation("", "", ""); expect(errors.length).toBe(3); }); - test("location as array of string", () => { + + test("should return errors when location is an array of strings", () => { const location = ["123", "213"]; const errors = chargerValidation.getAddChargerValidation( location, @@ -64,8 +67,9 @@ describe("Add Charger Validation with input:", () => { ); expect(errors.length).toBe(3); }); - test("valid input", () => { - const location = [80, 123]; + + test("should not return errors when input is valid", () => { + const location = [VALID_LATITUDE, VALID_LONGITUDE]; const errors = chargerValidation.getAddChargerValidation( location, "serialNumber", @@ -73,7 +77,8 @@ describe("Add Charger Validation with input:", () => { ); expect(errors.length).toBe(0); }); - test("location as array of int and string", () => { + + test("should return errors when location is an array of integers and strings", () => { const location = [VALID_LATITUDE, "213"]; const errors = chargerValidation.getAddChargerValidation( location, @@ -82,7 +87,8 @@ describe("Add Charger Validation with input:", () => { ); expect(errors.length).toBe(2); }); - test("location array of length 3", () => { + + test("should return an error when location array has length 3", () => { const location = [12, 12, 12]; const errors = chargerValidation.getAddChargerValidation( location, @@ -91,7 +97,8 @@ describe("Add Charger Validation with input:", () => { ); expect(errors.length).toBe(1); }); - test("location array of length 1", () => { + + test("should return an error when location array has length 1", () => { const location = [12]; const errors = chargerValidation.getAddChargerValidation( location, @@ -102,38 +109,43 @@ describe("Add Charger Validation with input:", () => { }); }); -describe("Update Charger Status Validation with input:", () => { - test("null", () => { +describe("Update Charger Status Validation", () => { + test("should return an error when input is null", () => { const errors = chargerValidation.getUpdateChargerStatusValidation(null); expect(errors.length).toBe(1); }); - test("undefined", () => { + + test("should return an error when input is undefined", () => { const errors = chargerValidation.getUpdateChargerStatusValidation(undefined); expect(errors.length).toBe(1); }); - test("empty string", () => { + + test("should return an error when input is an empty string", () => { const errors = chargerValidation.getUpdateChargerStatusValidation(""); expect(errors.length).toBe(1); }); - test("Available", () => { + + test("should not return an error when input is 'Available'", () => { const errors = chargerValidation.getUpdateChargerStatusValidation("Available"); expect(errors.length).toBe(0); }); - test("Availableqweqwe", () => { + + test("should return an error when input is 'Availableqweqwe'", () => { const errors = chargerValidation.getUpdateChargerStatusValidation("Availableqweqwe"); expect(errors.length).toBe(1); }); - test("Available qweqwe", () => { + + test("should return an error when input is 'Available qweqwe'", () => { const errors = chargerValidation.getUpdateChargerStatusValidation("Available qweqwe"); expect(errors.length).toBe(1); }); - test("int", () => { + test("should return errors when input is an integer", () => { const errors = chargerValidation.getUpdateChargerStatusValidation(123); expect(errors.length).toBe(2); }); -}); +}); \ No newline at end of file From 9dd49a679e834c1dbcfc8a1d7196b615fb0a1f7a Mon Sep 17 00:00:00 2001 From: "luca.boel" Date: Mon, 9 Oct 2023 13:34:26 +0200 Subject: [PATCH 2/3] small fixes --- .../tests/charge-point-validation.test.js | 11 +++++++---- .../tests/database-interface-chargers.test.js | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/backend-app/src/database-Interface/tests/charge-point-validation.test.js b/backend-app/src/database-Interface/tests/charge-point-validation.test.js index c1c1fe0d..7c857244 100644 --- a/backend-app/src/database-Interface/tests/charge-point-validation.test.js +++ b/backend-app/src/database-Interface/tests/charge-point-validation.test.js @@ -1,6 +1,4 @@ -const chargePointValidation = require("../validation/charge-point-validation")( - {} -); +const chargePointValidation = require("../validation/charge-point-validation")({}); const { describe, expect, test } = require("@jest/globals"); const VALID_LONGITUDE = 120; const VALID_LATITUDE = 20; @@ -11,6 +9,7 @@ describe("Charge Point Validation with input:", () => { const errors = chargePointValidation.chargePointValidation("", "", ""); expect(errors.length).toBe(3); }); + test("valid input", () => { const errors = chargePointValidation.chargePointValidation( "tst", @@ -19,6 +18,7 @@ describe("Charge Point Validation with input:", () => { ); expect(errors.length).toBe(0); }); + test("location array length 3", () => { const location = [20, 20, 20]; const errors = chargePointValidation.chargePointValidation( @@ -28,6 +28,7 @@ describe("Charge Point Validation with input:", () => { ); expect(errors.length).toBe(1); }); + test("price null", () => { const errors = chargePointValidation.chargePointValidation( "tst", @@ -36,6 +37,7 @@ describe("Charge Point Validation with input:", () => { ); expect(errors.length).toBe(1); }); + test("price undefined", () => { const errors = chargePointValidation.chargePointValidation( "tst", @@ -44,6 +46,7 @@ describe("Charge Point Validation with input:", () => { ); expect(errors.length).toBe(1); }); + test("negative price", () => { const errors = chargePointValidation.chargePointValidation( "tst", @@ -52,4 +55,4 @@ describe("Charge Point Validation with input:", () => { ); expect(errors.length).toBe(1); }); -}); +}); \ No newline at end of file diff --git a/backend-app/src/database-Interface/tests/database-interface-chargers.test.js b/backend-app/src/database-Interface/tests/database-interface-chargers.test.js index 8e8f7f7b..00670ab1 100644 --- a/backend-app/src/database-Interface/tests/database-interface-chargers.test.js +++ b/backend-app/src/database-Interface/tests/database-interface-chargers.test.js @@ -63,7 +63,7 @@ describe("addCharger", () => { const chargePointID = 100001 const serialNumber = "bca245" const coordinates = [23.12, 55.43] - test("Get all Chargers with status of 'Available'", (done) => { + test("Add a Charger", (done) => { databaseInterfaceChargers.addCharger(chargePointID, serialNumber, coordinates, (error, charger) => { if (Object.keys(error).length > 0) { done(error); @@ -102,4 +102,4 @@ describe("updateChargerStatus", () => { done() }) }); -}); +}); \ No newline at end of file From 86cfdfc886f6209f9bfff8d9d832442e8ed13019 Mon Sep 17 00:00:00 2001 From: "luca.boel" Date: Wed, 11 Oct 2023 13:03:52 +0200 Subject: [PATCH 3/3] tests passing --- .../tests/invoices-validation.test.js | 2 +- .../tests/transaction-validation.test.js | 225 +++++++++--------- 2 files changed, 110 insertions(+), 117 deletions(-) diff --git a/backend-app/src/database-Interface/tests/invoices-validation.test.js b/backend-app/src/database-Interface/tests/invoices-validation.test.js index d62f698f..2dd23887 100644 --- a/backend-app/src/database-Interface/tests/invoices-validation.test.js +++ b/backend-app/src/database-Interface/tests/invoices-validation.test.js @@ -156,5 +156,5 @@ describe("Invoice date", () => { "2022-11-11" ); expect(errors.length).toEqual(0); - }); + }); }); diff --git a/backend-app/src/database-Interface/tests/transaction-validation.test.js b/backend-app/src/database-Interface/tests/transaction-validation.test.js index 51a43d05..1336590a 100644 --- a/backend-app/src/database-Interface/tests/transaction-validation.test.js +++ b/backend-app/src/database-Interface/tests/transaction-validation.test.js @@ -1,129 +1,122 @@ -const transactionValidation = require('../validation/transaction-validation')({}) -const {describe, expect, test} = require('@jest/globals') +const transactionValidation = require('../validation/transaction-validation')({}); +const { describe, expect, test } = require('@jest/globals'); -const validChargeMeterInput = 0 -const validCurrentChargePercentageInput = 0 -const validClientId = "123" -const validSessionId = "123" -describe('Transaction by pricePerKwh with input:', () => { - - test('integer below 0', () => { - const errors = transactionValidation.getAddTransactionValidation(-1); - expect(errors.length).toBe(2); - }); - - test('string', () => { - const errors = transactionValidation.getAddTransactionValidation("1"); - expect(errors.length).toBe(2); - }); - - test('null', () => { - const errors = transactionValidation.getAddTransactionValidation(null); - expect(errors.length).toBe(3); - }); - - test('undefined', () => { - const valErrors = transactionValidation.getAddTransactionValidation(undefined); - expect(valErrors.length).toBe(3); - }); +const validChargeMeterInput = 0; +const validCurrentChargePercentageInput = 0; +const validClientId = "123"; +const validSessionId = "123"; +describe('Transaction by pricePerKwh with input:', () => { + test('integer below 0', () => { + const errors = transactionValidation.getAddTransactionValidation(-1); + expect(errors.length).toBe(2); + }); + + test('string', () => { + const errors = transactionValidation.getAddTransactionValidation("1"); + expect(errors.length).toBe(2); + }); + + test('null', () => { + const errors = transactionValidation.getAddTransactionValidation(null); + expect(errors.length).toBe(3); + }); + + test('undefined', () => { + const valErrors = transactionValidation.getAddTransactionValidation(undefined); + expect(valErrors.length).toBe(3); + }); }); describe('Update Charger by chargeMeterValue input', () => { - - test('null', () => { - const errors = transactionValidation.getUpdateTransactionChargingStatus(null,validCurrentChargePercentageInput); - expect(errors.length).toBe(1); - }); - - test('undefined', () => { - const valErrors = transactionValidation.getUpdateTransactionChargingStatus(undefined,validCurrentChargePercentageInput); - expect(valErrors.length).toBe(1); - }); - - test('integer below 0', () => { - const errors = transactionValidation.getUpdateTransactionChargingStatus(-1,validCurrentChargePercentageInput); - expect(errors.length).toBe(1); - }); - + test('null', () => { + const errors = transactionValidation.getUpdateTransactionChargingStatus(null, validCurrentChargePercentageInput); + expect(errors.length).toBe(1); + }); + + test('undefined', () => { + const valErrors = transactionValidation.getUpdateTransactionChargingStatus(undefined, validCurrentChargePercentageInput); + expect(valErrors.length).toBe(1); + }); + + test('integer below 0', () => { + const errors = transactionValidation.getUpdateTransactionChargingStatus(-1, validCurrentChargePercentageInput); + expect(errors.length).toBe(1); + }); }); describe('Update Charger by currentChargePercentage input', () => { - - test('null', () => { - const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput,null); - expect(errors.length).toBe(1); - }); - - test('undefined', () => { - const valErrors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput,undefined); - expect(valErrors.length).toBe(1); - }); - - test('integer below 0 string input', () => { - const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput,"test"); - expect(errors.length).toBe(1); - }); - - test('integer below 0 array input', () => { - const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput,[1,2,3]); - expect(errors.length).toBe(1); - }); - - test('integer below 0', () => { - const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput,-1); - expect(errors.length).toBe(1); - }); - - test('integer over 100', () => { - const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput,101); - expect(errors.length).toBe(1); - }); - + test('null', () => { + const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput, null); + expect(errors.length).toBe(1); + }); + + test('undefined', () => { + const valErrors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput, undefined); + expect(valErrors.length).toBe(1); + }); + + test('integer below 0 string input', () => { + const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput, "test"); + expect(errors.length).toBe(1); + }); + + test('integer below 0 array input', () => { + const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput, [1, 2, 3]); + expect(errors.length).toBe(1); + }); + + test('integer below 0', () => { + const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput, -1); + expect(errors.length).toBe(1); + }); + + test('integer over 100', () => { + const errors = transactionValidation.getUpdateTransactionChargingStatus(validChargeMeterInput, 101); + expect(errors.length).toBe(1); + }); }); describe('Test klarna by session_id input', () => { - - test('null', () => { - const errors = transactionValidation.addKlarnaTransactionValidation(null, validClientId); - expect(errors.length).toBe(1); - }); - - test('undefined', () => { - const errors = transactionValidation.addKlarnaTransactionValidation(undefined, validClientId); - expect(errors.length).toBe(1); - }); - - test('datatype not string', () => { - const errors = transactionValidation.addKlarnaTransactionValidation(1, validClientId); - expect(errors.length).toBe(1); - }); - - test('empty string', () => { - const errors = transactionValidation.addKlarnaTransactionValidation("", validClientId); - expect(errors.length).toBe(1); - }); -}) + test('null', () => { + const errors = transactionValidation.addKlarnaTransactionValidation(null, validClientId); + expect(errors.length).toBe(1); + }); + + test('undefined', () => { + const errors = transactionValidation.addKlarnaTransactionValidation(undefined, validClientId); + expect(errors.length).toBe(1); + }); + + test('datatype not string', () => { + const errors = transactionValidation.addKlarnaTransactionValidation(1, validClientId); + expect(errors.length).toBe(1); + }); + + test('empty string', () => { + const errors = transactionValidation.addKlarnaTransactionValidation("", validClientId); + expect(errors.length).toBe(1); + }); +}); describe('Test klarna by client_id input', () => { - - test('null', () => { - const errors = transactionValidation.addKlarnaTransactionValidation(validSessionId, null); - expect(errors.length).toBe(1); - }); - - test('undefined', () => { - const errors = transactionValidation.addKlarnaTransactionValidation(validSessionId, undefined); - expect(errors.length).toBe(1); - }); - - test('datatype not string', () => { - const errors = transactionValidation.addKlarnaTransactionValidation(validSessionId, 1); - expect(errors.length).toBe(1); - }); - - test('empty string', () => { - const errors = transactionValidation.addKlarnaTransactionValidation(validSessionId, ""); - expect(errors.length).toBe(1); - }); -}); + test('null', () => { + const errors = transactionValidation.addKlarnaTransactionValidation(validSessionId, null); + expect(errors.length).toBe(1); + }); + + test('undefined', () => { + const errors = transactionValidation.addKlarnaTransactionValidation(validSessionId, undefined); + expect(errors.length).toBe(1); + }); + + test('datatype not string', () => { + const errors = transactionValidation.addKlarnaTransactionValidation(validSessionId, 1); + expect(errors.length).toBe(1); + }); + + test('empty string', () => { + const errors = transactionValidation.addKlarnaTransactionValidation(validSessionId, ""); + expect(errors.length).toBe(1); + }); +}); \ No newline at end of file