Ejemplo n.º 1
0
 def test_render_error_to_thrift(self):
     self.assertEqual(
         types.RenderError(
             types.I18nMessage("foo", {}),
             [
                 types.QuickFix(
                     types.I18nMessage("click"),
                     types.QuickFixAction.PrependStep("filter", {"x": "y"}),
                 )
             ],
         ).to_thrift(),
         ttypes.RenderError(
             ttypes.I18nMessage("foo", {}, ttypes.I18nMessageSource()),
             [
                 ttypes.QuickFix(
                     ttypes.I18nMessage("click", {}, ttypes.I18nMessageSource()),
                     ttypes.QuickFixAction(
                         prepend_step=ttypes.PrependStepQuickFixAction(
                             "filter", ttypes.RawParams('{"x":"y"}')
                         )
                     ),
                 )
             ],
         ),
     )
Ejemplo n.º 2
0
 def test_render_error_to_dict(self):
     self.assertEqual(
         fields._render_error_to_dict(
             types.RenderError(
                 types.I18nMessage("err", {}),
                 [
                     types.QuickFix(
                         types.I18nMessage("click", {}, "cjwmodule"),
                         types.QuickFixAction.PrependStep("filter", {"x": "y"}),
                     )
                 ],
             )
         ),
         {
             "message": {"id": "err", "arguments": {}},
             "quickFixes": [
                 {
                     "buttonText": {
                         "id": "click",
                         "arguments": {},
                         "source": "cjwmodule",
                     },
                     "action": {
                         "type": "prependStep",
                         "moduleSlug": "filter",
                         "partialParams": {"x": "y"},
                     },
                 }
             ],
         },
     )
Ejemplo n.º 3
0
 def test_fetch_result_from_thrift_happy_path(self):
     with tempfile.NamedTemporaryFile(dir=str(self.basedir)) as tf:
         self.assertEqual(
             types.FetchResult.from_thrift(
                 ttypes.FetchResult(
                     Path(tf.name).name,
                     [types.RenderError(types.I18nMessage("hi")).to_thrift()],
                 ),
                 self.basedir,
             ),
             types.FetchResult(
                 Path(tf.name), [types.RenderError(types.I18nMessage("hi"))]
             ),
         )
Ejemplo n.º 4
0
 def test_i18n_message_from_dict_no_source(self):
     self.assertEqual(
         types.I18nMessage.from_dict(
             {"id": "modules.x.y", "arguments": ["s", 12345678, 0.123]}
         ),
         types.I18nMessage("modules.x.y", ["s", 12345678, 0.123]),
     )
Ejemplo n.º 5
0
 def test_i18n_message_from_dict_source_module(self):
     self.assertEqual(
         fields._dict_to_i18n_message(
             {"id": "modules.x.y", "arguments": {"foo": "bar"}, "source": "module"}
         ),
         types.I18nMessage("modules.x.y", {"foo": "bar"}, "module"),
     )
Ejemplo n.º 6
0
 def test_i18n_message_to_dict_source_module(self):
     self.assertEqual(
         fields._i18n_message_to_dict(
             types.I18nMessage("modules.x.y", {}, "module")
         ),
         {"id": "modules.x.y", "arguments": {}, "source": "module"},
     )
Ejemplo n.º 7
0
 def test_i18n_message_to_dict_no_source(self):
     self.assertEqual(
         fields._i18n_message_to_dict(
             types.I18nMessage("modules.x.y", ["s", 12345678, 0.123])
         ),
         {"id": "modules.x.y", "arguments": ["s", 12345678, 0.123]},
     )
Ejemplo n.º 8
0
 def test_i18n_message_to_dict(self):
     self.assertEqual(
         types.I18nMessage("modules.x.y", ["s", 12345678, 0.123]).to_dict(),
         {
             "id": "modules.x.y",
             "arguments": ["s", 12345678, 0.123]
         },
     )
Ejemplo n.º 9
0
 def test_i18n_message_to_dict_source_module(self):
     self.assertEqual(
         types.I18nMessage(
             "modules.x.y",
             ["s", 12345678, 0.123],
             types.I18nMessageSource.Module("testmodule"),
         ).to_dict(),
         {
             "id": "modules.x.y",
             "arguments": ["s", 12345678, 0.123],
             "source": {"module": "testmodule"},
         },
     )
Ejemplo n.º 10
0
 def test_quick_fix_to_thrift(self):
     self.assertEqual(
         types.QuickFix(
             types.I18nMessage("click"),
             types.QuickFixAction.PrependStep("filter", {"x": "y"}),
         ).to_thrift(),
         ttypes.QuickFix(
             ttypes.I18nMessage("click", {}),
             ttypes.QuickFixAction(
                 prepend_step=ttypes.PrependStepQuickFixAction(
                     "filter", ttypes.RawParams('{"x":"y"}'))),
         ),
     )
Ejemplo n.º 11
0
 def test_i18n_message_to_thrift_source_none(self):
     self.assertEqual(
         types.I18nMessage(
             "modules.x.y", {"a": "s", "b": 12345678, "c": 0.123}
         ).to_thrift(),
         ttypes.I18nMessage(
             "modules.x.y",
             {
                 "a": ttypes.I18nArgument(string_value="s"),
                 "b": ttypes.I18nArgument(i32_value=12345678),
                 "c": ttypes.I18nArgument(double_value=0.123),
             },
             ttypes.I18nMessageSource(),
         ),
     )
Ejemplo n.º 12
0
 def test_quick_fix_to_dict(self):
     self.assertEqual(
         types.QuickFix(
             types.I18nMessage("click"),
             types.QuickFixAction.PrependStep("filter", {"x": "y"}),
         ).to_dict(),
         {
             "buttonText": {"id": "click", "arguments": {}},
             "action": {
                 "type": "prependStep",
                 "moduleSlug": "filter",
                 "partialParams": {"x": "y"},
             },
         },
     )
Ejemplo n.º 13
0
 def test_i18n_message_from_dict_source_library(self):
     self.assertEqual(
         types.I18nMessage.from_dict(
             {
                 "id": "modules.x.y",
                 "arguments": ["s", 12345678, 0.123],
                 "source": {"library": "cjwmodule"},
             }
         ),
         types.I18nMessage(
             "modules.x.y",
             ["s", 12345678, 0.123],
             types.I18nMessageSource.Library("cjwmodule"),
         ),
     )
Ejemplo n.º 14
0
def __render_arrow(
    *,
    table: types.ArrowTable,
    params: Dict[str, Any],
    tab_name: str,
    fetch_result: Optional[types.FetchResult],
    output_path: Path,
) -> types.RenderResult:
    """
    Render using `cjwkernel.types` data types.

    Write to `output_path`.

    This will typically call `render()`.
    """
    # call render()
    raw_result = render(
        table.table,
        params,
        output_path,
        columns=table.metadata.columns,
        settings=settings,
        tab_name=tab_name,
        fetch_result=fetch_result,
    )

    # coerce result
    # TODO let module output column types. (Currently, the lack of column types
    # means this is only useful for fetch modules that don't output number
    # formats.)
    table = types.ArrowTable.from_arrow_file_with_inferred_metadata(
        output_path,
        fallback_column_types={c.name: c.type
                               for c in table.metadata.columns},
    )
    # TODO support more output types? Or develop the One True Types (maybe
    # types.RenderResult) and force modules to output it.
    if isinstance(raw_result, list):
        # List of I18nMessage errors
        errors = [
            types.RenderError(types.I18nMessage(*message))
            for message in raw_result
        ]
    elif raw_result is None:
        errors = []

    return types.RenderResult(table, errors)
Ejemplo n.º 15
0
 def test_i18n_message_from_thrift(self):
     self.assertEqual(
         types.I18nMessage.from_thrift(
             ttypes.I18nMessage(
                 "modules.x.y",
                 {
                     "a": ttypes.I18nArgument(string_value="s"),
                     "b": ttypes.I18nArgument(i32_value=12345678),
                     "c": ttypes.I18nArgument(double_value=0.123),
                 },
             )),
         types.I18nMessage("modules.x.y", {
             "a": "s",
             "b": 12345678,
             "c": 0.123
         }),
     )
Ejemplo n.º 16
0
 def test_i18n_message_to_thrift_source_library(self):
     self.assertEqual(
         types.arrow_i18n_message_to_thrift(
             types.I18nMessage("modules.x.y", {
                 "a": "s",
                 "b": 12345678,
                 "c": 0.123
             }, "cjwmodule")),
         ttypes.I18nMessage(
             "modules.x.y",
             {
                 "a": ttypes.I18nArgument(string_value="s"),
                 "b": ttypes.I18nArgument(i32_value=12345678),
                 "c": ttypes.I18nArgument(double_value=0.123),
             },
             "cjwmodule",
         ),
     )
Ejemplo n.º 17
0
 def test_fetch_result_from_thrift_happy_path(self):
     with tempfile.NamedTemporaryFile(dir=str(self.basedir)) as tf:
         filename = Path(tf.name).name
         self.assertEqual(
             types.thrift_fetch_result_to_arrow(
                 ttypes.FetchResult(
                     filename,
                     [
                         ttypes.FetchError(
                             ttypes.I18nMessage("hi", {}, None))
                     ],
                 ),
                 self.basedir,
             ),
             types.FetchResult(
                 Path(tf.name),
                 [types.FetchError(types.I18nMessage("hi", {}, None))],
             ),
         )
Ejemplo n.º 18
0
 def test_i18n_message_from_dict_best_wanted_type_datetime_becomes_timestamp(self):
     # Compatibility for https://www.pivotaltracker.com/story/show/174865394
     # DELETEME when there are no CachedRenderResults from before 2020-10-01
     self.assertEqual(
         fields._dict_to_i18n_message(
             {
                 "id": "py.renderer.execute.types.PromptingError.WrongColumnType.as_quick_fixes.general",
                 "arguments": {
                     "found_type": "text",
                     "best_wanted_type": "datetime",
                 },
             }
         ),
         types.I18nMessage(
             "py.renderer.execute.types.PromptingError.WrongColumnType.as_quick_fixes.general",
             {"found_type": "text", "best_wanted_type": "timestamp"},
             None,
         ),
     )
Ejemplo n.º 19
0
 def test_i18n_message_to_thrift_source_none(self):
     self.assertEqual(
         types.arrow_i18n_message_to_thrift(
             types.I18nMessage("modules.x.y")),
         ttypes.I18nMessage("modules.x.y", {}, None),
     )