def to_json(self): if not self.test or not self.test.expression: return {'error': 'Invalid test result'} test_result_json = { 'id': self.test.id, 'title': self.test.title, 'description': self.test.title, # for backwards compatibility 'expression': self.test.expression } if self.test.column: test_result_json['columnName'] = self.test.column if self.error: test_result_json['error'] = str(self.error) else: test_result_json['passed'] = self.passed test_result_json['values'] = JsonHelper.to_jsonnable(self.values) if self.group_values: test_result_json['groupValues'] = JsonHelper.to_jsonnable( self.group_values) return test_result_json
def to_dict(self) -> dict: if not self.test or not self.test.expression: return {"error": "Invalid test result"} test_result_json = { "id": self.test.id, "title": self.test.title, "description": self.test.title, # for backwards compatibility "expression": self.test.expression, } if self.test.column: test_result_json["columnName"] = self.test.column if self.test.source: test_result_json["source"] = self.test.source else: test_result_json["source"] = "soda-sql" if self.error: test_result_json["error"] = str(self.error) else: test_result_json["passed"] = self.passed test_result_json["skipped"] = self.skipped test_result_json["values"] = JsonHelper.to_jsonnable(self.values) if self.group_values: test_result_json["groupValues"] = JsonHelper.to_jsonnable( self.group_values) return test_result_json
def test_column_group_measurement(self): measurement = Measurement( metric=Metric.MIN, column_name='AGE', group_values=[GroupValue(group={'country': 'US'}, value=3.4)]) self.assertEqual("min(AGE): \n group{'country': 'US'} = 3.4", str(measurement)) JsonHelper.to_json(JsonHelper.to_jsonnable(measurement.to_dict()))
def __str__(self): column_str = f'({self.column_name})' if self.column_name else '' group_values_str = JsonHelper.to_json( JsonHelper.to_jsonnable( self.group_values)) if self.group_values else '' value_str = '' if self.value is None \ else (' = ' + (', '.join([str(e) for e in self.value]) if isinstance(self.value, list) else str(self.value))) return f'{self.metric}{column_str}{group_values_str}{value_str}'
def to_json(self): if not self.test or not self.test.expression: return { 'error': 'Invalid test result' } test_result_json = { 'id': self.test.id, 'description': self.test.description, 'expression': self.test.expression } if self.test.column: test_result_json['columnName'] = self.test.column if self.error: test_result_json['error'] = self.error else: test_result_json['passed'] = self.passed values = {} if isinstance(self.values, dict): for variable_name in self.values: values[variable_name] = JsonHelper.to_jsonnable(self.values[variable_name]) test_result_json['values'] = values if self.group_values: test_result_json['groupValues'] = self.group_values return test_result_json
def to_json(self): json = { 'metric': self.metric, } if self.group_values is None: json['value'] = JsonHelper.to_jsonnable(self.value) else: json['groupValues'] = [ group_value.to_json() for group_value in self.group_values ] if self.column_name is not None: json['columnName'] = self.column_name return json
def to_dict(self) -> dict: dictionary = { 'metric': self.metric, } if self.group_values is None: dictionary['value'] = JsonHelper.to_jsonnable(self.value) else: dictionary['groupValues'] = [ group_value.to_dict() for group_value in self.group_values ] if self.column_name is not None: dictionary['columnName'] = self.column_name return dictionary
def test_jsonize_time(self): self.assertEqual(JsonHelper.to_jsonnable(datetime.time(10, 5, 23)), '10:05:23')
def test_jsonize_datetime(self): self.assertEqual(JsonHelper.to_jsonnable(datetime.datetime(2021, 1, 2, 10, 5, 23)), '2021-01-02T10:05:23')
def test_jsonize_date(self): self.assertEqual(JsonHelper.to_jsonnable(datetime.date(2021, 1, 2)), '2021-01-02')
def test_column_group_measurement_empty_list(self): measurement = Measurement(metric=Metric.MIN, column_name='AGE', group_values=[]) self.assertEqual('min(AGE): no groups', str(measurement)) JsonHelper.to_json(JsonHelper.to_jsonnable(measurement.to_dict()))
def test_column_measurement_list_value(self): measurement = Measurement(metric=Metric.MINS, column_name='chars', value=['a', 'b']) self.assertEqual("mins(chars) = ['a', 'b']", str(measurement)) JsonHelper.to_json(JsonHelper.to_jsonnable(measurement.to_dict()))
def test_column_measurement(self): measurement = Measurement(metric=Metric.MIN, column_name='AGE', value=3.4) self.assertEqual('min(AGE) = 3.4', str(measurement)) JsonHelper.to_json(JsonHelper.to_jsonnable(measurement.to_dict()))
def test_row_count(self): measurement = Measurement(metric=Metric.ROW_COUNT, value=5) self.assertEqual('row_count = 5', str(measurement)) JsonHelper.to_json(JsonHelper.to_jsonnable(measurement.to_dict()))
def to_json(self): return { 'group': JsonHelper.to_jsonnable(self.group), 'value': JsonHelper.to_jsonnable(self.value) }