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 11 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
```
64 changes: 64 additions & 0 deletions search-setup/__tests__/search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { config } from "dotenv";
import { DocumentResponse, SearchIndexResponse } from "../types";

config();

const searchEndpoint = process.env["AI_SEARCH_SERVICE_ENDPOINT"];
const indexName = process.env["AI_SEARCH_INDEX_NAME"];
const apiKey = process.env["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";

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);
expect(index.fields.at(0)?.name).toBe("IID");
expect(index.fields.at(0)?.type).toBe("Edm.String");
expect(index.fields.at(0)?.key).toBe(true);
expect(index.fields.at(0)?.retrievable).toBe(true);
gareth-allan-bjss marked this conversation as resolved.
Show resolved Hide resolved
expect(index.fields.at(0)?.searchable).toBe(true);
expect(index.fields.at(0)?.sortable).toBe(true);
expect(index.fields.at(0)?.filterable).toBe(true);
expect(index.fields.at(1)?.name).toBe("Descriptive");
expect(index.fields.at(1)?.type).toBe("Edm.ComplexType");
expect(index.fields.at(1)?.fields?.length).toBe(2);
expect(index.fields.at(1)?.fields?.at(0)?.name).toBe("Name");
expect(index.fields.at(1)?.fields?.at(0)?.type).toBe("Edm.String");
expect(index.fields.at(1)?.fields?.at(0)?.retrievable).toBe(true);
expect(index.fields.at(1)?.fields?.at(0)?.searchable).toBe(true);
expect(index.fields.at(1)?.fields?.at(0)?.sortable).toBe(true);
expect(index.fields.at(1)?.fields?.at(0)?.filterable).toBe(true);
expect(index.fields.at(1)?.fields?.at(1)?.name).toBe("Definition");
expect(index.fields.at(1)?.fields?.at(1)?.type).toBe("Edm.String");
expect(index.fields.at(1)?.fields?.at(1)?.retrievable).toBe(true);
expect(index.fields.at(1)?.fields?.at(1)?.searchable).toBe(true);
expect(index.fields.at(1)?.fields?.at(1)?.sortable).toBe(true);
expect(index.fields.at(1)?.fields?.at(1)?.filterable).toBe(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list of assertions is already long, and our index only has three fields. I think we need to look at whether we can compare this against an object, or if that doesn't work at least having a function to check each field is as expected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using helper methods to fix this as the object returned by the fetch call contains plenty fields that are not being used. This way, only the necessary fields are being tested.

});

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$"],
};
192 changes: 191 additions & 1 deletion search-setup/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion search-setup/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"type": "module",
"scripts": {
"create-index": "tsc && node --env-file=.env main.js"
"create-index": "tsc && node --env-file=.env main.js",
"test": "jest"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.10.2",
"dotenv": "^16.4.7",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"typescript": "^5.7.2"
Expand Down
2 changes: 1 addition & 1 deletion search-setup/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"resolveJsonModule": true
},
"include": ["./**/*"],
"exclude": ["node_modules"]
"exclude": ["node_modules", "__tests__/*", "*.config.ts"]
}
Loading
Loading