def bulk_complete(task): """Process bulk complete""" flask.session['credentials'] = task.parameters.get("credentials") with benchmark("Create CsvBuilder"): builder = csvbuilder.CsvBuilder(task.parameters.get("data", {})) with benchmark("Prepare import data for attributes update"): update_data = builder.attributes_update_to_csv() with benchmark("Update assessments attributes"): update_attrs = converters.make_import(csv_data=update_data, dry_run=False, bulk_import=True) _log_import(update_attrs["data"]) upd_errors = set(update_attrs["failed_slugs"]) with benchmark("Prepare import data for attributes update"): complete_data = builder.assessments_complete_to_csv(upd_errors) complete_errors = [] if complete_data: with benchmark("Update assessments attributes"): complete_assmts = converters.make_import(csv_data=complete_data, dry_run=False, bulk_import=True) complete_errors = set(complete_assmts["failed_slugs"]) bulk_notifications.send_notification(update_errors=upd_errors, partial_errors=complete_errors, asmnt_ids=builder.assessment_ids) return app.make_response(('success', 200, [("Content-Type", "text/json")]))
def test_bulk_verify_csv(self): """Test building csv for bulk verify""" builder = csvbuilder.CsvBuilder({}) builder.assessments[1].slug = "slug-1" builder.assessments[2].slug = "slug-2" builded_list = builder.assessments_verify_to_csv() expected_list = [ [u"Object type"], [u"Assessment", u"Code", u"State", u"Verified Date"], [u"", u"slug-1", u"Completed", u"10/21/2019"], [u"", u"slug-2", u"Completed", u"10/21/2019"], ] self.assertEqual(expected_list, builded_list)
def test_needs_verification_assessment(self): """Test assessment needs verification""" with factories.single_commit(): asmt = factories.AssessmentFactory() asmt.add_person_with_role_name(factories.PersonFactory(), "Verifiers") cad_text = factories.CustomAttributeDefinitionFactory( title="test_LCA", definition_type="assessment", definition_id=asmt.id, attribute_type="Text", ) data = { "assessments_ids": [asmt.id], "attributes": [{ "attribute_value": "cav_value", "attribute_title": cad_text.title, "attribute_type": "Text", "extra": { "comment": {}, "urls": [], "files": [], }, "bulk_update": [ { "assessment_id": asmt.id, "attribute_definition_id": cad_text.id, "slug": asmt.slug, }, ] }] } builder = csvbuilder.CsvBuilder(data) expected_data = { asmt.id: { "files": [], "urls": [], "cavs": { "test_LCA": "cav_value" }, "slug": asmt.slug, "verification": True, "comments": [] } } self.assert_assessments(builder, expected_data) self.assertEqual(builder.assessment_ids, [asmt.id])
def test_attributes_extra_null(self, extra_data): """Test attributes extra data empty""" with factories.single_commit(): asmt = factories.AssessmentFactory() cad = factories.CustomAttributeDefinitionFactory( title="Test_LCA", definition_type="assessment", definition_id=asmt.id, attribute_type="Text", ) data = { "assessments_ids": [asmt.id], "attributes": [{ "attribute_value": "cav_value", "attribute_title": cad.title, "attribute_type": "Text", "extra": extra_data, "bulk_update": [ { "assessment_id": asmt.id, "attribute_definition_id": cad.id, "slug": asmt.slug, }, ] }] } builder = csvbuilder.CsvBuilder(data) expected_data = { asmt.id: { "files": [], "urls": [], "cavs": { "Test_LCA": "cav_value" }, "slug": asmt.slug, "verification": False, "comments": [] } } self.assert_assessments(builder, expected_data) self.assertEqual(builder.assessment_ids, [asmt.id])
def bulk_verify(task): """Process bulk complete""" with benchmark("Create CsvBuilder"): builder = csvbuilder.CsvBuilder(task.parameters.get("data", {})) with benchmark("Prepare import data for verification"): update_data = builder.assessments_verify_to_csv() with benchmark("Verify assessments"): verify_assmts = converters.make_import(csv_data=update_data, dry_run=False, bulk_import=True) _log_import(verify_assmts["data"]) verify_errors = set(verify_assmts["failed_slugs"]) bulk_notifications.send_notification(update_errors=verify_errors, partial_errors={}, asmnt_ids=builder.assessment_ids) return app.make_response(('success', 200, [("Content-Type", "text/json")]))
def test_needs_verification_two_diff_cads(self): """Test two cads from two assessments""" with factories.single_commit(): asmt1 = factories.AssessmentFactory() asmt2 = factories.AssessmentFactory() asmt1.add_person_with_role_name(factories.PersonFactory(), "Verifiers") cad1 = factories.CustomAttributeDefinitionFactory( title="test_LCA_1", definition_type="assessment", definition_id=asmt1.id, attribute_type="Text", ) cad2 = factories.CustomAttributeDefinitionFactory( title="test_LCA_2", definition_type="assessment", definition_id=asmt2.id, attribute_type="Text", ) asmt_ids = [asmt1.id, asmt2.id] data = { "assessments_ids": asmt_ids, "attributes": [{ "attribute_value": "cav_value_1", "attribute_title": cad1.title, "attribute_type": "Text", "extra": { "comment": {}, "urls": [], "files": [], }, "bulk_update": [ { "assessment_id": asmt1.id, "attribute_definition_id": cad1.id, "slug": asmt1.slug, }, ] }, { "attribute_value": "cav_value_2", "attribute_title": cad2.title, "attribute_type": "Text", "extra": { "comment": {}, "urls": [], "files": [], }, "bulk_update": [ { "assessment_id": asmt2.id, "attribute_definition_id": cad2.id, "slug": asmt2.slug, }, ] }] } builder = csvbuilder.CsvBuilder(data) expected_data = { asmt1.id: { "files": [], "urls": [], "cavs": { "test_LCA_1": "cav_value_1" }, "slug": asmt1.slug, "verification": True, "comments": [] }, asmt2.id: { "files": [], "urls": [], "cavs": { "test_LCA_2": "cav_value_2" }, "slug": asmt2.slug, "verification": False, "comments": [] }, } self.assert_assessments(builder, expected_data) self.assertEqual(builder.assessment_ids, asmt_ids)
def test_convert_data(self, data, expected_result): """Test convert_data method""" builder = csvbuilder.CsvBuilder(create_input_data(data)) self.assert_assessments(builder, create_assert_data(expected_result))