def post_metric_copy(metric_uuid: MetricId, subject_uuid: SubjectId, database: Database): """Add a copy of the metric to the subject (new in v3).""" data_model, reports = latest_datamodel(database), latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, subject_uuid) target.subject["metrics"][(metric_copy_uuid := uuid())] = copy_metric(source.metric, source.datamodel) description = ( f"{{user}} copied the metric '{source.metric_name}' of subject '{source.subject_name}' from report " f"'{source.report_name}' to subject '{target.subject_name}' in report '{target.report_name}'." ) uuids = [target.report_uuid, target.subject_uuid, metric_copy_uuid] result = insert_new_report(database, description, (target.report, uuids)) result["new_metric_uuid"] = metric_copy_uuid return result
def post_metric_copy(metric_uuid: MetricId, subject_uuid: SubjectId, database: Database): """Add a copy of the metric to the subject (new in v3).""" data_model = latest_datamodel(database) reports = latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, subject_uuid) target.subject["metrics"][(metric_copy_uuid := uuid())] = copy_metric(source.metric, source.datamodel) user = sessions.user(database) target.report["delta"] = dict( uuids=[target.report_uuid, target.subject_uuid, metric_copy_uuid], email=user["email"], description= f"{user['user']} copied the metric '{source.metric_name}' of subject " f"'{source.subject_name}' from report '{source.report_name}' to subject '{target.subject_name}' " f"in report '{target.report_name}'.") result = insert_new_report(database, target.report) result["new_metric_uuid"] = metric_copy_uuid return result
def test_copy_sources(self): """Test that the sources are copied too.""" metric_copy = copy_metric(self.metric, self.data_model) self.assertEqual("Source", list(metric_copy["sources"].values())[0]["name"])
def test_copy_without_name_change(self): """Test that the copy name can be left unchanged.""" metric_copy = copy_metric(self.metric, self.data_model, change_name=False) self.assertEqual("Metric", metric_copy["name"])
def test_copy_without_name(self): """Test that the copy name is based on the data model if the original metric doesn't have a name.""" self.metric["name"] = "" metric_copy = copy_metric(self.metric, self.data_model) self.assertEqual("Metric type (copy)", metric_copy["name"])
def test_copy_name(self): """Test that the copy name is changed.""" metric_copy = copy_metric(self.metric, self.data_model) self.assertEqual("Metric (copy)", metric_copy["name"])