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

feat: adding tests to check for behaviour if one of the model fails, … #3150

Merged
Merged
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
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
Loading