Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DHSCFT-50: Unit testing AI search #33

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions search-setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ npm install
```
npm run create-index
```

## Testing

This project uses Jest for unit testing.

### Running the Unit tests

```bash
npm run test
```
99 changes: 99 additions & 0 deletions search-setup/__tests__/search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { config } from "dotenv";
import { DocumentResponse, SearchIndexResponse, IndexField } from "../types";
import { getEnvironmentVariable } from "../utils/helpers";

config();

const searchEndpoint = getEnvironmentVariable("AI_SEARCH_SERVICE_ENDPOINT");
const indexName = getEnvironmentVariable("AI_SEARCH_INDEX_NAME");
const apiKey = getEnvironmentVariable("AI_SEARCH_API_KEY");

describe("AI search index creation and data loading", () => {
const URL_PREFIX = `${searchEndpoint}/indexes('${indexName}')`;
const URL_SUFFIX = "?api-version=2024-07-01";

const expectFieldToMatch = (
field: IndexField | undefined,
name: string,
type: string,
retrievable: boolean,
searchable: boolean,
sortable: boolean,
filterable: boolean
) => {
expect(field?.name).toBe(name);
expect(field?.type).toBe(type);
expect(field?.retrievable).toBe(retrievable);
expect(field?.searchable).toBe(searchable);
expect(field?.sortable).toBe(sortable);
expect(field?.filterable).toBe(filterable);
};

const expectComplexFieldToMatch = (
field: IndexField | undefined,
name: string,
fieldLength: number
) => {
expect(field?.name).toBe(name);
expect(field?.type).toBe("Edm.ComplexType");
expect(field?.fields?.length).toBe(fieldLength);
};

test("should create index with expected fields", async () => {
const url = `${URL_PREFIX}${URL_SUFFIX}`;

const response = await fetch(url, {
headers: {
"api-key": apiKey,
},
});

const index: SearchIndexResponse = await response.json();

expect(index.name).toBe(indexName);
expect(index.fields.length).toBe(2);
expectFieldToMatch(
index.fields.at(0),
"IID",
"Edm.String",
true,
true,
true,
true
);
expect(index.fields.at(0)?.key).toBe(true);
expectComplexFieldToMatch(index.fields.at(1), "Descriptive", 2);
expectFieldToMatch(
index.fields.at(1)?.fields?.at(0),
"Name",
"Edm.String",
true,
true,
true,
true
);
expectFieldToMatch(
index.fields.at(1)?.fields?.at(1),
"Definition",
"Edm.String",
true,
true,
true,
true
);
});

test("should populate index with data", async () => {
const url = `${URL_PREFIX}/docs${URL_SUFFIX}`;

const response = await fetch(url, {
headers: {
"api-key": apiKey,
},
});

const documents: DocumentResponse = await response.json();

expect(documents.value.length).toBeGreaterThan(0);
});
});
7 changes: 7 additions & 0 deletions search-setup/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
transform: { "^.+\\.ts?$": "ts-jest" },
testEnvironment: "node",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
testMatch: ["<rootDir>/**/*.test.ts"],
transformIgnorePatterns: ["^.+\\.js$"],
};
11 changes: 1 addition & 10 deletions search-setup/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ import { SearchIndexClient, AzureKeyCredential } from "@azure/search-documents";
import { createSearchIndex, populateIndex } from "./indexOperations.js";
import { Data } from "./types.js";
import { sampleData } from "./sample-data.js";

function getEnvironmentVariable(variableName: string): string {
const variableValue = process.env[variableName];

if (!variableValue) {
throw new Error(`Could not load environment variable ${variableName}!`);
}

return variableValue;
}
import { getEnvironmentVariable } from "./utils/helpers.js";

async function main(): Promise<void> {
const endpoint = getEnvironmentVariable("AI_SEARCH_SERVICE_ENDPOINT");
Expand Down
Loading
Loading