def test_render_using_tab_output(self): def render(table, params): self.assertEqual(params["tabparam"].slug, "tab-1") self.assertEqual(params["tabparam"].name, "Tab 1") self.assertEqual( params["tabparam"].columns, { "X": ptypes.RenderColumn("X", "number", "{:,d}"), "Y": ptypes.RenderColumn("Y", "text", None), }, ) assert_frame_equal(params["tabparam"].dataframe, pd.DataFrame({ "X": [1], "Y": ["y"] })) with arrow_table_context( { "X": [1], "Y": ["y"] }, columns=[ Column("X", ColumnType.Number("{:,d}")), Column("Y", ColumnType.Text()), ], dir=self.basedir, ) as atable: self._test_render( render, params={"tabparam": TabOutput(Tab("tab-1", "Tab 1"), atable)})
def test_render_using_tab_output(self): def render(table, params): self.assertEqual(params["tabparam"].name, "Tab 1") self.assertEqual( params["tabparam"].columns, { "X": ptypes.RenderColumn("X", "number", "{:,d}"), "Y": ptypes.RenderColumn("Y", "text", None), }, ) assert_frame_equal(params["tabparam"].dataframe, pd.DataFrame({ "X": [1], "Y": ["y"] })) param_schema = ParamSchema.Dict({"tabparam": ParamSchema.Tab()}) with ModuleTestEnv(param_schema=param_schema, render=render) as env: with arrow_table_context( make_column("X", [1], format="{:,d}"), make_column("Y", ["y"]), dir=env.basedir, ) as (path, _): env.call_render( make_table(), params={"tabparam": "tab-1"}, tab_outputs={ "tab-1": TabOutput(tab_name="Tab 1", table_filename=path.name) }, )
def test_clean_tabs_happy_path(self): tab2 = Tab("tab-2", "Tab 2") tab2_output = arrow_table({"B": [1]}) tab3 = Tab("tab-3", "Tab 3") tab3_output = arrow_table({"C": [1]}) context = self._render_context(tab_results={ tab2: RenderResult(tab2_output), tab3: RenderResult(tab3_output), }) result = clean_value(ParamDType.Multitab(), ["tab-2", "tab-3"], context) self.assertEqual( result, [TabOutput(tab2, tab2_output), TabOutput(tab3, tab3_output)])
def test_render_tab_outputs(self): def render_arrow_v1(table, params, *, tab_outputs, **kwargs): self.assertEqual(params["tab"], "tab-x") self.assertEqual(tab_outputs["tab-x"].tab_name, "Tab X") assert_arrow_table_equals( tab_outputs["tab-x"].table, make_table( make_column("X", [1], format="{:,d}"), make_column("Y", ["y"]), ), ) return ArrowRenderResult(make_table()) param_schema = ParamSchema.Dict({"tab": ParamSchema.Tab()}) with ModuleTestEnv(param_schema=param_schema, render_arrow_v1=render_arrow_v1) as env: with arrow_table_context( make_column("X", [1], format="{:,d}"), make_column("Y", ["y"]), dir=env.basedir, ) as (path, _): env.call_render( make_table(), params={"tab": "tab-x"}, tab_outputs={ "tab-x": TabOutput(tab_name="Tab X", table_filename=path.name) }, )
def test_clean_tabs_happy_path(self): self.assertEqual( self._call_prep_params( ParamSchema.Dict({"x": ParamSchema.Multitab()}), {"x": ["tab-2", "tab-3"]}, tab_results={ Tab("tab-2", "Tab 2"): StepResult(Path("tab-2.arrow"), [NUMBER("B")]), Tab("tab-3", "Tab 3"): StepResult(Path("tab-3.arrow"), [NUMBER("C")]), }, ), PrepParamsResult( {"x": ["tab-2", "tab-3"]}, { "tab-2": TabOutput("Tab 2", "tab-2.arrow"), "tab-3": TabOutput("Tab 3", "tab-3.arrow"), }, uploaded_files={}, ), )
def clean(self): if self.result is not None: raise RuntimeError( "You cannot call clean() twice on the same _Cleaner") cleaned_params = self.clean_value(self.schema, self.params) tab_outputs = { td.slug: TabOutput(td.tab.name, td.result.path.name) for td in self.tabs.values() if td.slug in self.used_tab_slugs } self.result = PrepParamsResult(cleaned_params, tab_outputs, self.uploaded_files) return self.result
def test_clean_tabs_preserve_ordering(self): # "x" gives wrongly-ordered tabs; renderprep should reorder them. result = self._call_prep_params( ParamSchema.Dict({"x": ParamSchema.Multitab()}), {"x": ["tab-2", "tab-3"]}, tab_results={ Tab("tab-3", "Tab 3"): StepResult(Path("tab-3.arrow"), [NUMBER("C")]), Tab("tab-2", "Tab 2"): StepResult(Path("tab-2.arrow"), [NUMBER("B")]), }, ) self.assertEqual( result, PrepParamsResult( {"x": ["tab-3", "tab-2"]}, { "tab-3": TabOutput("Tab 3", "tab-3.arrow"), "tab-2": TabOutput("Tab 2", "tab-2.arrow"), }, uploaded_files={}, ), )
def test_clean_tab_omit_unused_tabs_from_tab_outputs(self): result = self._call_prep_params( ParamSchema.Dict({"x": ParamSchema.Tab()}), {"x": "tab-1"}, tab_results={ Tab("tab-1", "Tab 1"): StepResult(Path("tab-1.arrow"), [TEXT("A")]), Tab("tab-2", "Tab 2"): StepResult(Path("tab-2.arrow"), [TEXT("A")]), Tab("tab-3", "Tab 3"): StepResult(Path("tab-3.arrow"), [TEXT("A")]), }, ) self.assertEqual(result.tab_outputs, {"tab-1": TabOutput("Tab 1", "tab-1.arrow")})
def _(dtype: ParamDType.Tab, value: str, context: RenderContext) -> TabOutput: tab_slug = value try: tab_data = context.tabs[tab_slug] except KeyError: # It's a tab that doesn't exist. return None tab_result = tab_data.result if tab_result is None: # It's an un-rendered tab. Or at least, the executor _tells_ us it's # un-rendered. That means there's a tab-cycle. raise TabCycleError if tab_result.status != "ok": raise TabOutputUnreachableError return TabOutput(tab_data.tab, tab_result.table)
def test_clean_tab_happy_path(self): result = self._call_prep_params( ParamSchema.Dict({"x": ParamSchema.Tab()}), {"x": "tab-1"}, tab_results={ Tab("tab-1", "Tab 1"): StepResult(Path("tab-1.arrow"), [TEXT("A")]) }, ) self.assertEqual( result, PrepParamsResult( {"x": "tab-1"}, tab_outputs={"tab-1": TabOutput("Tab 1", "tab-1.arrow")}, uploaded_files={}, ), )
def test_clean_tab_happy_path(self): tab = Tab("tab-1", "Tab 1") table = arrow_table({"A": [1, 2]}) context = self._render_context(tab_results={tab: RenderResult(table)}) result = clean_value(ParamDType.Tab(), "tab-1", context) self.assertEqual(result, TabOutput(tab, table))