Skip to content

Commit

Permalink
Add unit tests for file explorer url to pandas conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
aswallace committed May 13, 2024
1 parent 4032c48 commit b390d1d
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("<CodeSnippet />", () => {
it("displays snippet when present in state", async () => {
// Arrange
const setup = "pip install pandas";
const code = "TODO";
const code = "#No options selected";
const { store } = configureMockStore({ state: visibleDialogState });
const { findByText } = render(
<Provider store={store}>
Expand Down
121 changes: 121 additions & 0 deletions packages/core/entity/FileExplorerURL/test/fileexplorerurl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("FileExplorerURL", () => {
private: true,
created: new Date(),
createdBy: "test",
uri: "test/file.csv",
};

describe("encode", () => {
Expand Down Expand Up @@ -196,4 +197,124 @@ describe("FileExplorerURL", () => {
expect(() => FileExplorerURL.decode(encodedUrl)).to.throw();
});
});

describe("convert to python pandas string", () => {
it("converts groupings", () => {
// Arrange
const expectedAnnotationNames = ["Cell Line", "Donor Plasmid", "Lifting?"];
const components: Partial<FileExplorerURLComponents> = {
hierarchy: expectedAnnotationNames,
};
const expectedPandasGroups = expectedAnnotationNames.map(
(annotation) => `.groupby('${annotation}', group_keys=True).apply(lambda x: x)`
);
const expectedResult = `df${expectedPandasGroups.join("")}`;

// Act
const result = FileExplorerURL.convertToPython(components);

// Assert
expect(result).to.contain(expectedResult);
});

it("converts filters", () => {
// Arrange
const expectedFilters = [
{ name: "Cas9", value: "spCas9" },
{ name: "Donor Plasmid", value: "ACTB-mEGFP" },
];
const components: Partial<FileExplorerURLComponents> = {
filters: expectedFilters.map(({ name, value }) => new FileFilter(name, value)),
};
const expectedPandasQueries = expectedFilters.map(
(filter) => `\`${filter.name}\`=="${filter.value}"`
);
const expectedResult = `df.query('${expectedPandasQueries[0]}').query('${expectedPandasQueries[1]}')`;

// Act
const result = FileExplorerURL.convertToPython(components);

// Assert
expect(result).to.contain(expectedResult);
});

it("converts same filter with multiple values", () => {
// Arrange
const expectedFilters = [
{ name: "Gene", value: "AAVS1" },
{ name: "Gene", value: "ACTB" },
];
const components: Partial<FileExplorerURLComponents> = {
filters: expectedFilters.map(({ name, value }) => new FileFilter(name, value)),
};
const expectedPandasQueries = expectedFilters.map(
(filter) => `\`${filter.name}\`=="${filter.value}"`
);
const expectedResult = `df.query('${expectedPandasQueries[0]} | ${expectedPandasQueries[1]}')`;

// Act
const result = FileExplorerURL.convertToPython(components);

// Assert
expect(result).to.contain(expectedResult);
});

it("converts sorts", () => {
// Arrange
const components: Partial<FileExplorerURLComponents> = {
sortColumn: new FileSort(AnnotationName.UPLOADED, SortOrder.DESC),
};
const expectedPandasSort = `.sort_values(by='${AnnotationName.UPLOADED}', ascending=False`;
const expectedResult = `df${expectedPandasSort}`;

// Act
const result = FileExplorerURL.convertToPython(components);

// Assert
expect(result).to.contain(expectedResult);
});

it("provides info on converting external data source to pandas dataframe", () => {
// Arrange
const components: Partial<FileExplorerURLComponents> = {
collection: {
name: mockCollection.name,
version: mockCollection.version,
uri: mockCollection.uri,
},
};
const expectedResult = `df = pandas.read_csv('${mockCollection.uri}').astype('str')`;

// Act
const result = FileExplorerURL.convertToPython(components);

// Assert
expect(result).to.contain(expectedResult);
});

it("arranges query elements in correct order", () => {
// Arrange
const expectedAnnotationNames = ["Plate Barcode"];
const expectedFilters = [
{ name: "Cas9", value: "spCas9" },
{ name: "Donor Plasmid", value: "ACTB-mEGFP" },
];
const components: Partial<FileExplorerURLComponents> = {
hierarchy: expectedAnnotationNames,
filters: expectedFilters.map(({ name, value }) => new FileFilter(name, value)),
sortColumn: new FileSort(AnnotationName.UPLOADED, SortOrder.DESC),
collection: {
name: mockCollection.name,
version: mockCollection.version,
},
};
const expectedResult = /df\.groupby\(.*\)\.query\(.*\)\.query\(.*\)\.sort_values\(.*\)/i;

// Act
const result = FileExplorerURL.convertToPython(components);

// Assert
expect(result).to.match(expectedResult);
});
});
});

0 comments on commit b390d1d

Please sign in to comment.