Skip to content

Commit

Permalink
Merge pull request #2860 from Gkrumbach07/task/RHOAIENG-5510-add-test…
Browse files Browse the repository at this point in the history
…-coverage-for-pipeline-mlmd-hooks

Added tests for MLMD hooks
  • Loading branch information
openshift-merge-bot[bot] authored Jun 5, 2024
2 parents 2b19ddb + bfb4845 commit ac0fc61
Show file tree
Hide file tree
Showing 16 changed files with 1,358 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { testHook, standardUseFetchState } from '~/__tests__/unit/testUtils/hooks';
import { Artifact, Context, MetadataStoreServicePromiseClient } from '~/third_party/mlmd';
import { usePipelinesAPI } from '~/concepts/pipelines/context';
import { useArtifactsFromMlmdContext } from '~/concepts/pipelines/apiHooks/mlmd/useArtifactsFromMlmdContext';
import { GetArtifactsByContextResponse } from '~/third_party/mlmd/generated/ml_metadata/proto/metadata_store_service_pb';

// Mock the usePipelinesAPI hook
jest.mock('~/concepts/pipelines/context', () => ({
usePipelinesAPI: jest.fn(),
}));

// Mock the MetadataStoreServicePromiseClient
jest.mock('~/third_party/mlmd', () => {
const originalModule = jest.requireActual('~/third_party/mlmd');
return {
...originalModule,
MetadataStoreServicePromiseClient: jest.fn().mockImplementation(() => ({
getArtifactsByContext: jest.fn(),
})),
};
});

describe('useArtifactsFromMlmdContext', () => {
const mockClient = new MetadataStoreServicePromiseClient('');
const mockUsePipelinesAPI = jest.mocked(
usePipelinesAPI as () => Partial<ReturnType<typeof usePipelinesAPI>>,
);
const mockGetArtifactsByContext = jest.mocked(mockClient.getArtifactsByContext);

const mockedContext = new Context();
mockedContext.setId(1);

const artifact1 = new Artifact();
artifact1.setId(1);
const artifact2 = new Artifact();
artifact2.setId(2);
const mockArtifacts = [artifact1, artifact2];

beforeEach(() => {
jest.clearAllMocks();
mockUsePipelinesAPI.mockReturnValue({
metadataStoreServiceClient: mockClient,
});
});

it('should fetch and return artifacts', async () => {
mockGetArtifactsByContext.mockResolvedValue({
getArtifactsList: () => mockArtifacts,
} as GetArtifactsByContextResponse);

const renderResult = testHook(useArtifactsFromMlmdContext)(mockedContext);

expect(renderResult).hookToStrictEqual(standardUseFetchState([]));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();

expect(renderResult.result.current).toStrictEqual(standardUseFetchState(mockArtifacts, true));
expect(renderResult).hookToHaveUpdateCount(2);

// Check that mockGetArtifactsByContext was called with the correct context ID
const [request] = mockGetArtifactsByContext.mock.calls[0];
expect(request.getContextId()).toBe(mockedContext.getId());
});

it('should handle errors from getArtifactsByContext', async () => {
const error = new Error('Cannot find artifacts');
mockGetArtifactsByContext.mockRejectedValue(error);

const renderResult = testHook(useArtifactsFromMlmdContext)(mockedContext);

expect(renderResult).hookToStrictEqual(standardUseFetchState([]));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();

expect(renderResult.result.current).toStrictEqual(standardUseFetchState([], false, error));
expect(renderResult).hookToHaveUpdateCount(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { testHook, standardUseFetchState } from '~/__tests__/unit/testUtils/hooks';
import { Execution, Context, MetadataStoreServicePromiseClient } from '~/third_party/mlmd';
import { usePipelinesAPI } from '~/concepts/pipelines/context';
import { useExecutionsFromMlmdContext } from '~/concepts/pipelines/apiHooks/mlmd/useExecutionsFromMlmdContext';
import { GetExecutionsByContextResponse } from '~/third_party/mlmd/generated/ml_metadata/proto/metadata_store_service_pb';

// Mock the usePipelinesAPI hook
jest.mock('~/concepts/pipelines/context', () => ({
usePipelinesAPI: jest.fn(),
}));

// Mock the MetadataStoreServicePromiseClient
jest.mock('~/third_party/mlmd', () => {
const originalModule = jest.requireActual('~/third_party/mlmd');
return {
...originalModule,
MetadataStoreServicePromiseClient: jest.fn().mockImplementation(() => ({
getExecutionsByContext: jest.fn(),
})),
};
});

describe('useExecutionsFromMlmdContext', () => {
const mockClient = new MetadataStoreServicePromiseClient('');
const mockUsePipelinesAPI = jest.mocked(
usePipelinesAPI as () => Partial<ReturnType<typeof usePipelinesAPI>>,
);
const mockGetExecutionsByContext = jest.mocked(mockClient.getExecutionsByContext);

const mockedContext = new Context();
mockedContext.setId(1);

const execution1 = new Execution();
execution1.setId(1);
const execution2 = new Execution();
execution2.setId(2);
const mockExecutions = [execution1, execution2];

beforeEach(() => {
jest.clearAllMocks();
mockUsePipelinesAPI.mockReturnValue({
metadataStoreServiceClient: mockClient,
});
});

it('should fetch and return executions', async () => {
mockGetExecutionsByContext.mockResolvedValue({
getExecutionsList: () => mockExecutions,
} as GetExecutionsByContextResponse);

const renderResult = testHook(useExecutionsFromMlmdContext)(mockedContext);

expect(renderResult).hookToStrictEqual(standardUseFetchState([]));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();

expect(renderResult.result.current).toStrictEqual(standardUseFetchState(mockExecutions, true));
expect(renderResult).hookToHaveUpdateCount(2);

// Check that mockGetExecutionsByContext was called with the correct context ID
const [request] = mockGetExecutionsByContext.mock.calls[0];
expect(request.getContextId()).toBe(mockedContext.getId());
});

it('should handle errors from getExecutionsByContext', async () => {
const error = new Error('Cannot find executions');
mockGetExecutionsByContext.mockRejectedValue(error);

const renderResult = testHook(useExecutionsFromMlmdContext)(mockedContext);

expect(renderResult).hookToStrictEqual(standardUseFetchState([]));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();

expect(renderResult.result.current).toStrictEqual(standardUseFetchState([], false, error));
expect(renderResult).hookToHaveUpdateCount(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { testHook, standardUseFetchState } from '~/__tests__/unit/testUtils/hooks';
import { Artifact, MetadataStoreServicePromiseClient } from '~/third_party/mlmd';
import { usePipelinesAPI } from '~/concepts/pipelines/context';
import { useGetArtifactById } from '~/concepts/pipelines/apiHooks/mlmd/useGetArtifactById';
import { GetArtifactsByIDResponse } from '~/third_party/mlmd/generated/ml_metadata/proto/metadata_store_service_pb';

// Mock the usePipelinesAPI hook
jest.mock('~/concepts/pipelines/context', () => ({
usePipelinesAPI: jest.fn(),
}));

// Mock the MetadataStoreServicePromiseClient
jest.mock('~/third_party/mlmd', () => {
const originalModule = jest.requireActual('~/third_party/mlmd');
return {
...originalModule,
MetadataStoreServicePromiseClient: jest.fn().mockImplementation(() => ({
getArtifactsByID: jest.fn(),
})),
GetArtifactsByIDRequest: originalModule.GetArtifactsByIDRequest,
};
});

describe('useGetArtifactById', () => {
const mockClient = new MetadataStoreServicePromiseClient('');
const mockUsePipelinesAPI = jest.mocked(
usePipelinesAPI as () => Partial<ReturnType<typeof usePipelinesAPI>>,
);
const mockGetArtifactsByID = jest.mocked(mockClient.getArtifactsByID);

const mockArtifact = new Artifact();
mockArtifact.setId(1);
mockArtifact.setName('test-artifact');

beforeEach(() => {
jest.clearAllMocks();
mockUsePipelinesAPI.mockReturnValue({
metadataStoreServiceClient: mockClient,
});
});

it('should fetch and return the artifact', async () => {
mockGetArtifactsByID.mockResolvedValue({
getArtifactsList: () => [mockArtifact],
} as GetArtifactsByIDResponse);

const renderResult = testHook(useGetArtifactById)(1);

expect(renderResult.result.current).toStrictEqual(standardUseFetchState(undefined));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();

expect(renderResult.result.current).toStrictEqual(standardUseFetchState(mockArtifact, true));
expect(renderResult).hookToHaveUpdateCount(2);

// Check that mockGetArtifactsByID was called with the correct ID
const [request] = mockGetArtifactsByID.mock.calls[0];
expect(request.getArtifactIdsList()).toContain(1);
});

it('should handle errors from getArtifactsByID', async () => {
const error = new Error('Cannot find specified artifact');
mockGetArtifactsByID.mockRejectedValue(error);

const renderResult = testHook(useGetArtifactById)(1);

expect(renderResult.result.current).toStrictEqual(standardUseFetchState(undefined));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();

expect(renderResult.result.current).toStrictEqual(
standardUseFetchState(undefined, false, error),
);
expect(renderResult).hookToHaveUpdateCount(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { testHook, standardUseFetchState } from '~/__tests__/unit/testUtils/hooks';
import { ArtifactType, MetadataStoreServicePromiseClient } from '~/third_party/mlmd';
import { usePipelinesAPI } from '~/concepts/pipelines/context';
import { useGetArtifactTypes } from '~/concepts/pipelines/apiHooks/mlmd/useGetArtifactTypes';
import { GetArtifactTypesResponse } from '~/third_party/mlmd/generated/ml_metadata/proto/metadata_store_service_pb';

// Mock the usePipelinesAPI hook
jest.mock('~/concepts/pipelines/context', () => ({
usePipelinesAPI: jest.fn(),
}));

// Mock the MetadataStoreServicePromiseClient
jest.mock('~/third_party/mlmd', () => {
const originalModule = jest.requireActual('~/third_party/mlmd');
return {
...originalModule,
MetadataStoreServicePromiseClient: jest.fn().mockImplementation(() => ({
getArtifactTypes: jest.fn(),
})),
GetArtifactTypesRequest: originalModule.GetArtifactTypesRequest,
};
});

describe('useGetArtifactTypes', () => {
const mockClient = new MetadataStoreServicePromiseClient('');
const mockUsePipelinesAPI = jest.mocked(
usePipelinesAPI as () => Partial<ReturnType<typeof usePipelinesAPI>>,
);
const mockGetArtifactTypes = jest.mocked(mockClient.getArtifactTypes);

const mockArtifactType1 = new ArtifactType();
mockArtifactType1.setId(1);
mockArtifactType1.setName('artifactType1');

const mockArtifactType2 = new ArtifactType();
mockArtifactType2.setId(2);
mockArtifactType2.setName('artifactType2');

const mockArtifactTypes = [mockArtifactType1, mockArtifactType2];

beforeEach(() => {
jest.clearAllMocks();
mockUsePipelinesAPI.mockReturnValue({
metadataStoreServiceClient: mockClient,
});
});

it('should fetch and return the artifact types', async () => {
mockGetArtifactTypes.mockResolvedValue({
getArtifactTypesList: () => mockArtifactTypes,
} as GetArtifactTypesResponse);

const renderResult = testHook(useGetArtifactTypes)();

expect(renderResult.result.current).toStrictEqual(standardUseFetchState([]));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();

expect(renderResult.result.current).toStrictEqual(
standardUseFetchState(mockArtifactTypes, true),
);
expect(renderResult).hookToHaveUpdateCount(2);
});

it('should handle errors from getArtifactTypes', async () => {
const error = new Error('Cannot fetch artifact types');
mockGetArtifactTypes.mockRejectedValue(error);

const renderResult = testHook(useGetArtifactTypes)();

expect(renderResult.result.current).toStrictEqual(standardUseFetchState([]));
expect(renderResult).hookToHaveUpdateCount(1);

// wait for update
await renderResult.waitForNextUpdate();

expect(renderResult.result.current).toStrictEqual(standardUseFetchState([], false, error));
expect(renderResult).hookToHaveUpdateCount(2);
});
});
Loading

0 comments on commit ac0fc61

Please sign in to comment.