From b390d1d872b776d89971e63cd7a43debad8afd3f Mon Sep 17 00:00:00 2001 From: Anya Wallace Date: Mon, 13 May 2024 10:34:23 -0700 Subject: [PATCH] Add unit tests for file explorer url to pandas conversion --- .../CodeSnippet/test/CodeSnippet.test.tsx | 2 +- .../test/fileexplorerurl.test.ts | 121 ++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/packages/core/components/Modal/CodeSnippet/test/CodeSnippet.test.tsx b/packages/core/components/Modal/CodeSnippet/test/CodeSnippet.test.tsx index 700fe0692..51ff9069f 100644 --- a/packages/core/components/Modal/CodeSnippet/test/CodeSnippet.test.tsx +++ b/packages/core/components/Modal/CodeSnippet/test/CodeSnippet.test.tsx @@ -31,7 +31,7 @@ describe("", () => { 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( diff --git a/packages/core/entity/FileExplorerURL/test/fileexplorerurl.test.ts b/packages/core/entity/FileExplorerURL/test/fileexplorerurl.test.ts index 1dc2a53cd..7703cc6e5 100644 --- a/packages/core/entity/FileExplorerURL/test/fileexplorerurl.test.ts +++ b/packages/core/entity/FileExplorerURL/test/fileexplorerurl.test.ts @@ -18,6 +18,7 @@ describe("FileExplorerURL", () => { private: true, created: new Date(), createdBy: "test", + uri: "test/file.csv", }; describe("encode", () => { @@ -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 = { + 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 = { + 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 = { + 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 = { + 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 = { + 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 = { + 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); + }); + }); });