Skip to content

Commit

Permalink
fix: extra column in metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Dec 22, 2021
1 parent c49545a commit 8baf887
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
11 changes: 11 additions & 0 deletions superset/datasets/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ def fix_extra(self, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:


class ImportV1MetricSchema(Schema):
# pylint: disable=no-self-use, unused-argument
@pre_load
def fix_extra(self, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]:
"""
Fix for extra initially beeing exported as a string.
"""
if isinstance(data.get("extra"), str):
data["extra"] = json.loads(data["extra"])

return data

metric_name = fields.String(required=True)
verbose_name = fields.String(allow_none=True)
metric_type = fields.String(allow_none=True)
Expand Down
9 changes: 7 additions & 2 deletions tests/unit_tests/datasets/commands/export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def test_export(app_context: None, session: Session) -> None:
),
]
metrics = [
SqlMetric(metric_name="cnt", expression="COUNT(*)"),
SqlMetric(
metric_name="cnt",
expression="COUNT(*)",
extra=json.dumps({"warning_markdown": None}),
),
]

sqla_table = SqlaTable(
Expand Down Expand Up @@ -108,7 +112,8 @@ def test_export(app_context: None, session: Session) -> None:
expression: COUNT(*)
description: null
d3format: null
extra: null
extra:
warning_markdown: null
warning_text: null
columns:
- column_name: profit
Expand Down
18 changes: 15 additions & 3 deletions tests/unit_tests/datasets/commands/importers/v1/import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_import_(app_context: None, session: Session) -> None:
"expression": "COUNT(*)",
"description": None,
"d3format": None,
"extra": None,
"extra": {"warning_markdown": None},
"warning_text": None,
}
],
Expand Down Expand Up @@ -115,7 +115,7 @@ def test_import_(app_context: None, session: Session) -> None:
assert sqla_table.metrics[0].expression == "COUNT(*)"
assert sqla_table.metrics[0].description is None
assert sqla_table.metrics[0].d3format is None
assert sqla_table.metrics[0].extra is None
assert sqla_table.metrics[0].extra == '{"warning_markdown": null}'
assert sqla_table.metrics[0].warning_text is None
assert len(sqla_table.columns) == 1
assert sqla_table.columns[0].column_name == "profit"
Expand Down Expand Up @@ -169,7 +169,18 @@ def test_import_column_extra_is_string(app_context: None, session: Session) -> N
"fetch_values_predicate": "foo IN (1, 2)",
"extra": '{"warning_markdown": "*WARNING*"}',
"uuid": dataset_uuid,
"metrics": [],
"metrics": [
{
"metric_name": "cnt",
"verbose_name": None,
"metric_type": None,
"expression": "COUNT(*)",
"description": None,
"d3format": None,
"extra": '{"warning_markdown": null}',
"warning_text": None,
}
],
"columns": [
{
"column_name": "profit",
Expand All @@ -193,5 +204,6 @@ def test_import_column_extra_is_string(app_context: None, session: Session) -> N
dataset_config["database_id"] = database.id
sqla_table = import_dataset(session, dataset_config)

assert sqla_table.metrics[0].extra == '{"warning_markdown": null}'
assert sqla_table.columns[0].extra == '{"certified_by": "User"}'
assert sqla_table.extra == '{"warning_markdown": "*WARNING*"}'

0 comments on commit 8baf887

Please sign in to comment.