CourseInsights / test / controller / InsightFacadeC2.spec.ts
InsightFacadeC2.spec.ts
Raw
import {
	IInsightFacade,
	InsightDatasetKind,
	InsightError,
	InsightResult,
	NotFoundError,
	ResultTooLargeError,
} from "../../src/controller/IInsightFacade";
import InsightFacade from "../../src/controller/InsightFacade";
import { clearDisk, getContentFromArchives, loadTestQuery } from "../TestUtil";

import { expect, use } from "chai";
import chaiAsPromised from "chai-as-promised";

use(chaiAsPromised);

describe("InsightFacadeNam", function () {
	let facade: IInsightFacade;
	let sectionsQuery: string;
	let campus: string;

	// let fiveK: string;
	// let fiveKminusOne: string;
	// let fiveKone: string;

	before(async function () {
		// This block runs once and loads the datasets.

		// Declare datasets used in tests. You should add more datasets like this!
		//const sections: string = await getContentFromArchives("validSmallData.zip");
		sectionsQuery = await getContentFromArchives("pairCourses.zip");
		campus = await getContentFromArchives("campus.zip");
		//const noResKey: string = await getContentFromArchives("noResKey.zip");

		await clearDisk();
	});
	describe("PerformQuery", function () {
		/**
		 * Loads the TestQuery specified in the test name and asserts the behaviour of performQuery.
		 *
		 * Note: the 'this' parameter is automatically set by Mocha and contains information about the test.
		 */
		async function checkQuery(this: Mocha.Context): Promise<void> {
			if (!this.test) {
				throw new Error(
					"Invalid call to checkQuery." +
						"Usage: 'checkQuery' must be passed as the second parameter of Mocha's it(..) function." +
						"Do not invoke the function directly."
				);
			}
			// Destructuring assignment to reduce property accesses
			const { input, expected, errorExpected, orderExpected, orderBy } = await loadTestQuery(this.test.title);
			//console.log(typeof input); input is an Object
			let result: InsightResult[] = []; // dummy value before being reassigned
			try {
				result = await facade.performQuery(input);
			} catch (err) {
				if (!errorExpected) {
					expect.fail(`performQuery threw unexpected error: ${err}`);
				}
				const errorClassMap: Record<string, any> = {
					InsightError,
					NotFoundError,
					ResultTooLargeError,
				};
				const expectedError = errorClassMap[expected];
				expect(err).to.be.instanceOf(expectedError);
				return;
			}
			if (errorExpected) {
				expect.fail(`performQuery resolved when it should have rejected with ${expected}`);
			}
			//expect(result).to.deep.equal(expected);
			expect(result).to.have.deep.members(expected);
			expect(result).to.have.length(expected.length);
			if (orderExpected) {
				const resultAvg = result.map((item) => orderBy.map((key: any) => item[key]));
				const expectedAvg = expected.map((item: InsightResult) => orderBy.map((key: any) => item[key]));
				expect(resultAvg).to.deep.equal(expectedAvg);
			}
			return;
		}

		before(async function () {
			facade = new InsightFacade();

			// Add the datasets to InsightFacade once.
			// Will *fail* if there is a problem reading ANY dataset.
			const loadDatasetPromises: Promise<string[]>[] = [
				facade.addDataset("sections", sectionsQuery, InsightDatasetKind.Sections),
				facade.addDataset("rooms", campus, InsightDatasetKind.Rooms),
			];

			try {
				await Promise.all(loadDatasetPromises);
			} catch (err) {
				throw new Error(`In PerformQuery Before hook, dataset(s) failed to be added. \n${err}`);
			}
		});

		after(async function () {
			await clearDisk();
		});
		it("[invalid/apllyIsNull.json]", checkQuery);
		it("[invalid/apllyIsObject.json]", checkQuery);
		it("[invalid/applykeyIs__.json]", checkQuery);
		it("[invalid/applyRuleInvalidAvgString.json]", checkQuery);
		it("[invalid/applyRuleInvalidField.json]", checkQuery);
		it("[invalid/applyRuleInvalidKeyKind.json]", checkQuery);
		it("[invalid/applyRuleInvalidMaxString.json]", checkQuery);
		it("[invalid/applyRuleInvalidMinString.json]", checkQuery);
		it("[invalid/applyRuleInvalidSumString.json]", checkQuery);
		it("[invalid/applyRuleInvalidToken.json]", checkQuery);
		it("[invalid/applyRuleIsArray.json]", checkQuery);
		it("[invalid/applyRuleIsNull.json]", checkQuery);
		it("[invalid/directionIsArray.json]", checkQuery);
		it("[invalid/directionIsNull.json]", checkQuery);
		it("[invalid/directionIsNumber.json]", checkQuery);
		it("[invalid/directionIsWrong.json]", checkQuery);
		it("[invalid/duplicateKeyInApply.json]", checkQuery);
		it("[invalid/groupsHasKeyNotInColumnsOrApply.json]", checkQuery);
		it("[invalid/groupsIsEmpty.json]", checkQuery);
		it("[invalid/groupsIsNull.json]", checkQuery);
		it("[invalid/groupsIsObject.json]", checkQuery);
		it("[invalid/invalidKeyInApply.json]", checkQuery);
		it("[invalid/keyInColsNotInApplyOrGroup.json]", checkQuery);
		it("[invalid/keysIsNull.json]", checkQuery);
		it("[invalid/orderHasThreeKeys.json]", checkQuery);
		it("[invalid/orderHasZeroKeys.json]", checkQuery);
		it("[invalid/orderIsArray.json]", checkQuery);
		it("[invalid/orderMissingDirection.json]", checkQuery);
		it("[invalid/orderMissingKeys.json]", checkQuery);
		it("[invalid/transformationsHasThreeKeys.json]", checkQuery);
		it("[invalid/transformationsHasZeroKeys.json]", checkQuery);
		it("[invalid/transformationsIsNull.json]", checkQuery);
		it("[invalid/wrongFieldColumns.json]", checkQuery);
		it("[invalid/wrongFieldColumnsKind.json]", checkQuery);
		it("[invalid/wrongKeyWhere.json]", checkQuery);
		it("[invalid/groupedOver5000.json]", checkQuery);
		it("[valid/allRoomsFields.json]", checkQuery);
		it("[valid/applyIsEmpty.json]", checkQuery);
		it("[valid/applyKeyNotInCols.json]", checkQuery);
		it("[valid/countOnMkey.json]", checkQuery);
		it("[valid/groupBy1MaxMinAvgCount.json]", checkQuery);
		it("[valid/groupBy2MaxMinAvgCount.json]", checkQuery);
		it("[valid/groupBy3MaxMinAvgCount.json]", checkQuery);
		it("[valid/groupBy3SumMinAvgCount.json]", checkQuery);
		it("[valid/sortDown.json]", checkQuery);
		it("[valid/sortUp.json]", checkQuery);
		it("[valid/groupedNowUnder5000.json]", checkQuery);
		it("[valid/orderBy2.json]", checkQuery);
		it("[valid/orderBy3.json]", checkQuery);
		it("[valid/orderBy4.json]", checkQuery);
		it("[valid/orderBy2Sections.json]", checkQuery);
		it("[valid/orderBy3Sections.json]", checkQuery);
		it("[valid/orderBy2SectionsReverse.json]", checkQuery);
		it("[valid/orderBy3SectionsReverse.json]", checkQuery);
		it("[valid/orderBy2T.json]", checkQuery);
		it("[valid/orderBy3T.json]", checkQuery);
		it("[valid/orderBy2TR.json]", checkQuery);
		it("[valid/orderBy3TR.json]", checkQuery);
		it("[valid/allMaxcool.json]", checkQuery);
		it("[valid/emptyWhereButTransformationsGroup.json]", checkQuery);
	});
});