Skip to content

Commit

Permalink
feat: adding tests to check for behaviour if one of the model fails, …
Browse files Browse the repository at this point in the history
…undoing some changes in previous commit regarding mockResolvedValueOnce (#3150)
  • Loading branch information
nutrina authored Dec 18, 2024
1 parent 90765e8 commit 7780837
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
7 changes: 2 additions & 5 deletions platforms/src/ETH/Providers/accountAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,8 @@ export async function fetchModelData<T>(address: string, url_subpath: string, da
}
const url = `http://${dataScienceEndpoint}/${url_subpath}`;
const response = await axios.post<T>(url, payload);

if (response.status !== 200) {
throw new Error(`Failed to fetch data for model ${url}`);
}
return response.data;

return response.data;
} catch (e) {
handleProviderAxiosError(e, "model data (" + url_subpath + ")", [dataScienceEndpoint]);
}
Expand Down
36 changes: 35 additions & 1 deletion platforms/src/ETH/__tests__/accountAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe("AccountAnalysis Providers", () => {
describe("should check human_probability", () => {
it.each(scoreTestCases)("for score %i should return %s for %p", async (score, expected, provider) => {
const mockedResponse = mockResponse({ score });
// Mock the same response for all axios requests
mockedAxios.post.mockResolvedValue(mockedResponse);
const ethAdvocateProvider = new provider();
const payload = await ethAdvocateProvider.verify({ address: mockAddress } as RequestPayload, mockContext);
Expand All @@ -78,6 +79,39 @@ describe("AccountAnalysis Providers", () => {
});
});

describe("should fail the human_probability check if one of the simple models fails", () => {
it.each(scoreTestCases)("for score %i should return %s for %p", async (score, expected, provider) => {
const mockedResponse = mockResponse({ score });
let counter = 0;
mockedAxios.post.mockImplementation(() => {
counter += 1;
if(counter === 2) {
// We just fail the 2-nd request
return Promise.reject(new Error("Error with model!"));
}
return Promise.resolve(mockedResponse);
});
const providerInstance = new provider();
await expect(providerInstance.verify({ address: mockAddress } as RequestPayload, mockContext)).rejects.toThrow(new Error("Error with model!"));
});
});

describe("should fail the human_probability check if the call to aggregate model fails", () => {
it.each(scoreTestCases)("for score %i should return %s for %p", async (score, expected, provider) => {
const mockedResponse = mockResponse({ score });
mockedAxios.post.mockImplementation((url) => {
const model = url.split("/").pop();
if (model === "aggregate-model-predict") {
// We just fail the request to aggregate-model-predict
return Promise.reject(new Error("Error with model!"));
}
return Promise.resolve(mockedResponse);
});
const providerInstance = new provider();
await expect(providerInstance.verify({ address: mockAddress } as RequestPayload, mockContext)).rejects.toThrow(new Error("Error with model!"));
});
});

it("should validate inputs for EthDaysActiveProvider", async () => {
const mockedResponse = mockResponse({ numberDaysActive: 50 });
mockedAxios.post.mockResolvedValueOnce(mockedResponse);
Expand Down Expand Up @@ -144,7 +178,7 @@ describe("AccountAnalysis Providers", () => {
describe("getETHAnalysis", () => {
it("should use value from context if present", async () => {
const mockedResponse = mockResponse({ score: 80 });
mockedAxios.post.mockResolvedValue(mockedResponse);
mockedAxios.post.mockResolvedValueOnce(mockedResponse);
mockContext = {};
const response1 = await getETHAnalysis(mockAddress, mockContext);
const response2 = await getETHAnalysis(mockAddress, mockContext);
Expand Down
2 changes: 1 addition & 1 deletion platforms/src/ZkSync/__tests__/accountAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("AccountAnalysis Providers", () => {
describe("should return valid/invalid based on score", () => {
it.each(scoreTestCases)("score %i should return %s for %p", async (score, expected, provider) => {
const mockedResponse = mockResponse(score);
mockedAxios.post.mockResolvedValue(mockedResponse);
mockedAxios.post.mockResolvedValueOnce(mockedResponse);
const ethAdvocateProvider = new provider();
const payload = await ethAdvocateProvider.verify({ address: mockAddress } as RequestPayload, mockContext);

Expand Down

0 comments on commit 7780837

Please sign in to comment.