Skip to content

Commit

Permalink
CMDCT-3960: fixing tests, making year an int from the start
Browse files Browse the repository at this point in the history
  • Loading branch information
angelaco11 committed Sep 17, 2024
1 parent 3df7020 commit 5e2020e
Show file tree
Hide file tree
Showing 19 changed files with 63 additions and 165 deletions.
18 changes: 6 additions & 12 deletions services/app-api/handlers/coreSets/create.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import { getCoreSet } from "./get";
import {
createCoreSetKey,
createMeasureKey,
} from "../dynamoUtils/createCompoundKey";
import { MeasureMetaData, measures } from "../dynamoUtils/measureList";
import {
hasRolePermissions,
Expand Down Expand Up @@ -45,7 +41,6 @@ export const createCoreSet = handler(async (event, context) => {
body: Errors.CORESET_ALREADY_EXISTS,
};
}
const dynamoKey = createCoreSetKey({ state, year, coreSet });

await createDependentMeasures(
state,
Expand All @@ -56,7 +51,7 @@ export const createCoreSet = handler(async (event, context) => {

// filter out qualifier and account for autocomplete measures on creation
let autoCompletedMeasures = 0;
const measuresLengthWithoutQualifiers = measures[parseInt(year)].filter(
const measuresLengthWithoutQualifiers = measures[year].filter(
(measure: MeasureMetaData) => {
if (measure.autocompleteOnCreation && measure.type === type) {
autoCompletedMeasures++;
Expand All @@ -73,9 +68,9 @@ export const createCoreSet = handler(async (event, context) => {
const params = {
TableName: process.env.coreSetTableName!,
Item: {
compoundKey: dynamoKey,
compoundKey: `${state}${year}${coreSet}`,
state: state,
year: parseInt(year),
year: year,
coreSet: coreSet,
createdAt: Date.now(),
lastAltered: Date.now(),
Expand All @@ -94,11 +89,11 @@ export const createCoreSet = handler(async (event, context) => {

const createDependentMeasures = async (
state: string,
year: string,
year: number,
coreSet: Types.CoreSetAbbr,
type: string
) => {
const filteredMeasures = measures[parseInt(year)].filter(
const filteredMeasures = measures[year].filter(
(measure: MeasureMetaData) => measure.type === type
);

Expand All @@ -107,11 +102,10 @@ const createDependentMeasures = async (
for await (const measure of filteredMeasures) {
// The State Year and ID are all part of the path
const measureId = measure["measure"];
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTable!,
Item: {
compoundKey: dynamoKey,
compoundKey: `${state}${year}${coreSet}`,
state: state,
year: year,
coreSet: coreSet,
Expand Down
18 changes: 5 additions & 13 deletions services/app-api/handlers/coreSets/delete.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import {
createCoreSetKey,
createMeasureKey,
} from "../dynamoUtils/createCompoundKey";
import { convertToDynamoExpression } from "../dynamoUtils/convertToDynamoExpressionVars";
import { hasStatePermissions } from "../../libs/authorization";
import { Measure } from "../../types";
import { Errors, StatusCodes } from "../../utils/constants/constants";
Expand All @@ -27,11 +22,10 @@ export const deleteCoreSet = handler(async (event, context) => {
};
}

const dynamoKey = createCoreSetKey({ state, year, coreSet });
const params = {
TableName: process.env.coreSetTableName!,
Key: {
compoundKey: dynamoKey,
compoundKey: `${state}${year}${coreSet}`,
coreSet: coreSet,
},
};
Expand All @@ -47,18 +41,17 @@ export const deleteCoreSet = handler(async (event, context) => {

const deleteDependentMeasures = async (
state: string,
year: string,
year: number,
coreSet: string
) => {
const measures = await getMeasures(state, year, coreSet);
const Items = measures;

for await (const { measure } of Items) {
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTable!,
Key: {
compoundKey: dynamoKey,
compoundKey: `${state}${year}${coreSet}`,
coreSet: coreSet,
},
};
Expand All @@ -67,13 +60,12 @@ const deleteDependentMeasures = async (
}
};

const getMeasures = async (state: string, year: string, coreSet: string) => {
const dynamoKey = createMeasureKey({ state, year, coreSet });
const getMeasures = async (state: string, year: number, coreSet: string) => {
const params = {
TableName: process.env.measureTable!,
KeyConditionExpression: "compoundKey = :compoundKey",
ExpressionAttributeValues: {
":compoundKey": dynamoKey,
":compoundKey": `${state}${year}${coreSet}`,
},
};
const queryValue = await dynamoDb.queryAll<Measure>(params);
Expand Down
6 changes: 2 additions & 4 deletions services/app-api/handlers/coreSets/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import { updateCoreSetProgress } from "../../libs/updateCoreProgress";
import { convertToDynamoExpression } from "../dynamoUtils/convertToDynamoExpressionVars";
import { createCoreSetKey } from "../dynamoUtils/createCompoundKey";
import { createCoreSet } from "./create";
import {
hasRolePermissions,
Expand Down Expand Up @@ -41,7 +40,7 @@ export const coreSetList = handler(async (event, context) => {
...convertToDynamoExpression(
{
state: state,
year: parseInt(year),
year: year,
},
"list"
),
Expand Down Expand Up @@ -121,11 +120,10 @@ export const getCoreSet = handler(async (event, context) => {
}
} // if not state user, can safely assume admin type user due to baseline handler protections

const dynamoKey = createCoreSetKey({ state, year, coreSet });
const params = {
TableName: process.env.coreSetTableName!,
Key: {
compoundKey: dynamoKey,
compoundKey: `${state}${year}${coreSet}`,
coreSet: coreSet,
},
};
Expand Down
5 changes: 0 additions & 5 deletions services/app-api/handlers/coreSets/tests/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ jest.mock("../../../libs/authorization", () => ({
hasStatePermissions: () => mockHasStatePermissions(),
}));

jest.mock("../../dynamoUtils/createCompoundKey", () => ({
__esModule: true,
createCoreSetKey: jest.fn().mockReturnValue("FL2021ACSFUA-AD"),
}));

jest.mock("../get", () => ({
__esModule: true,
getCoreSet: jest.fn(),
Expand Down
17 changes: 6 additions & 11 deletions services/app-api/handlers/coreSets/tests/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CoreSetAbbr } from "../../../types";

jest.mock("../../../libs/dynamodb-lib", () => ({
delete: jest.fn(),
scanAll: jest.fn(),
queryAll: jest.fn(),
}));

const mockHasStatePermissions = jest.fn();
Expand All @@ -15,11 +15,6 @@ jest.mock("../../../libs/authorization", () => ({
hasStatePermissions: () => mockHasStatePermissions(),
}));

jest.mock("../../dynamoUtils/createCompoundKey", () => ({
__esModule: true,
createCoreSetKey: jest.fn().mockReturnValue("FL2020ACSFUA-AD"),
}));

jest.mock("../../../libs/updateCoreProgress", () => ({
__esModule: true,
updateCoreSetProgress: jest.fn(),
Expand All @@ -29,7 +24,7 @@ const event = { ...testEvent };

describe("Testing Delete Core Set Functions", () => {
beforeEach(() => {
(db.scanAll as jest.Mock).mockReset();
(db.queryAll as jest.Mock).mockReset();
(db.delete as jest.Mock).mockReset();
mockHasStatePermissions.mockImplementation(() => true);
event.pathParameters = {
Expand Down Expand Up @@ -67,24 +62,24 @@ describe("Testing Delete Core Set Functions", () => {
});

test("Test deleteCoreSet with associated measures", async () => {
(db.scanAll as jest.Mock).mockReturnValue([
(db.queryAll as jest.Mock).mockReturnValue([
testMeasure,
testMeasure,
testMeasure,
]);

await deleteCoreSet(event, null);

expect(db.scanAll).toHaveBeenCalled();
expect(db.queryAll).toHaveBeenCalled();
expect(db.delete).toHaveBeenCalledTimes(4);
});

test("Test deleteCoreSet with no associated measures", async () => {
(db.scanAll as jest.Mock).mockReturnValue([]);
(db.queryAll as jest.Mock).mockReturnValue([]);

await deleteCoreSet(event, null);

expect(db.scanAll).toHaveBeenCalled();
expect(db.queryAll).toHaveBeenCalled();
expect(db.delete).toHaveBeenCalled();
});
});
12 changes: 0 additions & 12 deletions services/app-api/handlers/coreSets/tests/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { testEvent } from "../../../test-util/testEvents";

import dynamodbLib from "../../../libs/dynamodb-lib";
import { updateCoreSetProgress } from "../../../libs/updateCoreProgress";
import { createCoreSetKey } from "../../dynamoUtils/createCompoundKey";
import { CoreSetAbbr } from "../../../types";
import { createCoreSet } from "../create";
import { Errors, StatusCodes } from "../../../utils/constants/constants";
Expand Down Expand Up @@ -35,11 +34,6 @@ jest.mock("../../../libs/updateCoreProgress", () => ({
updateCoreSetProgress: jest.fn(),
}));

jest.mock("../../dynamoUtils/createCompoundKey", () => ({
__esModule: true,
createCoreSetKey: jest.fn().mockReturnValue("FL2020ACSFUA-AD"),
}));

jest.mock("../../dynamoUtils/convertToDynamoExpressionVars", () => ({
__esModule: true,
convertToDynamoExpression: jest.fn().mockReturnValue({ testValue: "test" }),
Expand Down Expand Up @@ -96,12 +90,6 @@ describe("Test Get Core Set Functions", () => {
await getCoreSet(event, null);

expect(dynamodbLib.get).toHaveBeenCalled();
expect(createCoreSetKey).toHaveBeenCalled();
expect(createCoreSetKey).toHaveBeenCalledWith({
state: "AL",
year: "2021",
coreSet: "ACS",
});
});

test("Test coreSetList unauthorized user attempt (incorrect state)", async () => {
Expand Down
12 changes: 2 additions & 10 deletions services/app-api/handlers/coreSets/tests/update.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import dynamodbLib from "../../../libs/dynamodb-lib";
import { testEvent } from "../../../test-util/testEvents";
import { convertToDynamoExpression } from "../../dynamoUtils/convertToDynamoExpressionVars";
import { createCoreSetKey } from "../../dynamoUtils/createCompoundKey";
import { editCoreSet } from "../update";
import { Errors, StatusCodes } from "../../../utils/constants/constants";
import { CoreSetAbbr } from "../../../types";
Expand All @@ -17,11 +16,6 @@ jest.mock("../../../libs/authorization", () => ({
hasStatePermissions: () => mockHasStatePermissions(),
}));

jest.mock("../../dynamoUtils/createCompoundKey", () => ({
__esModule: true,
createCoreSetKey: jest.fn().mockReturnValue("FL2020ACSFUA-AD"),
}));

jest.mock("../../dynamoUtils/convertToDynamoExpressionVars", () => ({
__esModule: true,
convertToDynamoExpression: jest.fn().mockReturnValue({ testValue: "test" }),
Expand Down Expand Up @@ -73,7 +67,6 @@ describe("Testing Updating Core Set Functions", () => {
Date.now = jest.fn(() => 20);
const res = await editCoreSet(event, null);

expect(createCoreSetKey).toHaveBeenCalled();
expect(dynamodbLib.update).toHaveBeenCalled();
expect(convertToDynamoExpression).toHaveBeenCalledWith(
{
Expand All @@ -84,14 +77,13 @@ describe("Testing Updating Core Set Functions", () => {
"post"
);
expect(res.statusCode).toBe(StatusCodes.SUCCESS);
expect(res.body).toContain("FL2020ACSFUA-AD");
expect(res.body).toContain("WA2021ACS");
});

test("Test with no user id", async () => {
Date.now = jest.fn(() => 20);
const res = await editCoreSet(event, null);

expect(createCoreSetKey).toHaveBeenCalled();
expect(dynamodbLib.update).toHaveBeenCalled();
expect(convertToDynamoExpression).toHaveBeenCalledWith(
{
Expand All @@ -102,6 +94,6 @@ describe("Testing Updating Core Set Functions", () => {
"post"
);
expect(res.statusCode).toBe(StatusCodes.SUCCESS);
expect(res.body).toContain("FL2020ACSFUA-AD");
expect(res.body).toContain("WA2021ACS");
});
});
4 changes: 1 addition & 3 deletions services/app-api/handlers/coreSets/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import { convertToDynamoExpression } from "../dynamoUtils/convertToDynamoExpressionVars";
import { createCoreSetKey } from "../dynamoUtils/createCompoundKey";
import {
getUserNameFromJwt,
hasStatePermissions,
Expand All @@ -27,12 +26,11 @@ export const editCoreSet = handler(async (event, context) => {
}

const { submitted, status } = JSON.parse(event!.body!);
const dynamoKey = createCoreSetKey({ state, year, coreSet });
const lastAlteredBy = getUserNameFromJwt(event);
const params = {
TableName: process.env.coreSetTableName!,
Key: {
compoundKey: dynamoKey,
compoundKey: `${state}${year}${coreSet}`,
coreSet: coreSet,
},
...convertToDynamoExpression(
Expand Down
13 changes: 0 additions & 13 deletions services/app-api/handlers/dynamoUtils/createCompoundKey.ts

This file was deleted.

This file was deleted.

6 changes: 2 additions & 4 deletions services/app-api/handlers/measures/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import { createMeasureKey } from "../dynamoUtils/createCompoundKey";
import {
hasRolePermissions,
hasStatePermissions,
Expand Down Expand Up @@ -31,13 +30,12 @@ export const createMeasure = handler(async (event, context) => {
} // if not state user, can safely assume admin type user due to baseline handler protections

const body = JSON.parse(event!.body!);
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTable!,
Item: {
compoundKey: dynamoKey,
compoundKey: `${state}${year}${coreSet}`,
state: state,
year: parseInt(year),
year: year,
coreSet: coreSet,
measure: measure,
createdAt: Date.now(),
Expand Down
Loading

0 comments on commit 5e2020e

Please sign in to comment.