From 32a95b33e2e7f8a1eb19a613f602af0aeaa66c0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Urba=C5=84czyk?= Date: Sat, 22 Jun 2019 16:32:53 +0200 Subject: [PATCH] PEP8 fixes for data migrator --- onadata/apps/data_migration/decisioner.py | 1 - onadata/apps/data_migration/migrate_data.py | 2 +- onadata/apps/data_migration/models/version.py | 2 +- onadata/apps/data_migration/restore_backup.py | 2 +- onadata/apps/data_migration/setup.cfg | 11 +++++++++++ onadata/apps/data_migration/tests/common.py | 10 +++++----- onadata/apps/data_migration/tests/test_backup.py | 9 +++------ onadata/apps/data_migration/tests/test_compare_xml.py | 1 - .../apps/data_migration/tests/test_data_migration.py | 2 +- onadata/apps/data_migration/tests/test_decisioner.py | 11 +++++------ onadata/apps/data_migration/tests/test_tree.py | 5 +---- onadata/apps/data_migration/tests/test_views.py | 5 ++--- onadata/apps/data_migration/tests/test_xformtree.py | 1 - onadata/apps/data_migration/views.py | 4 +--- onadata/apps/data_migration/xformtree.py | 4 ++-- onadata/apps/data_migration/xmltree.py | 2 +- 16 files changed, 35 insertions(+), 37 deletions(-) create mode 100644 onadata/apps/data_migration/setup.cfg diff --git a/onadata/apps/data_migration/decisioner.py b/onadata/apps/data_migration/decisioner.py index 13f6f2be0..dc9a7e16f 100644 --- a/onadata/apps/data_migration/decisioner.py +++ b/onadata/apps/data_migration/decisioner.py @@ -149,7 +149,6 @@ def fields_groups_prev(self): return self.xforms_comparator.fields_groups_prev() def changed_fields_groups(self): - fields_changes = self.fields_changes fields_groups_new = self.fields_groups_new() migrated_fields_groups_prev = self.prev_fields_groups_migrated() return { diff --git a/onadata/apps/data_migration/migrate_data.py b/onadata/apps/data_migration/migrate_data.py index cf448e65d..34dc02a8b 100644 --- a/onadata/apps/data_migration/migrate_data.py +++ b/onadata/apps/data_migration/migrate_data.py @@ -54,7 +54,7 @@ def migrate(self): def save_survey(self, survey): survey.save() - survey.parsed_instance.save(async=True) + survey.parsed_instance.save(async=True) # noqa: W606 class SurveyFieldsHandler(object): diff --git a/onadata/apps/data_migration/models/version.py b/onadata/apps/data_migration/models/version.py index b5303b659..fed064af1 100644 --- a/onadata/apps/data_migration/models/version.py +++ b/onadata/apps/data_migration/models/version.py @@ -41,7 +41,7 @@ def last_common_item(xs, ys): """Search for index of last common item in two lists.""" max_i = min(len(xs), len(ys)) - 1 for i, (x, y) in enumerate(zip(xs, ys)): - if x == y and (i == max_i or xs[i+1] != ys[i+1]): + if x == y and (i == max_i or xs[i + 1] != ys[i + 1]): return i return -1 diff --git a/onadata/apps/data_migration/restore_backup.py b/onadata/apps/data_migration/restore_backup.py index 681a93026..0e6d08118 100644 --- a/onadata/apps/data_migration/restore_backup.py +++ b/onadata/apps/data_migration/restore_backup.py @@ -67,7 +67,7 @@ def _merge_changes(cls, curr_changes, new_changes): new_changes[MigrationDecisioner.RM_FIELDS_KEY]), modified=cls._transitive_merge(curr_changes[MigrationDecisioner.MOD_FIELDS_KEY], new_changes[MigrationDecisioner.MOD_FIELDS_KEY]), - ) + ) @staticmethod def _transitive_merge(curr_changes, new_changes): diff --git a/onadata/apps/data_migration/setup.cfg b/onadata/apps/data_migration/setup.cfg new file mode 100644 index 000000000..aac1c3f17 --- /dev/null +++ b/onadata/apps/data_migration/setup.cfg @@ -0,0 +1,11 @@ +[flake8] +exclude = migrations, settings, __init__.py, fixtures +max-line-length = 100 +ignore = + # E731 do not assign a lambda expression, use a def + E731 + # W503 line break before binary operator + W503 + # W503 line break after binary operator + W504 + diff --git a/onadata/apps/data_migration/tests/common.py b/onadata/apps/data_migration/tests/common.py index 854f441e4..cf3708763 100644 --- a/onadata/apps/data_migration/tests/common.py +++ b/onadata/apps/data_migration/tests/common.py @@ -57,11 +57,11 @@ def _are_trees_isomorphic(cls, e1, e2): cmp_tails = lambda e1, e2: ( remove_whitespaces(e1.tail or '') != remove_whitespaces(e2.tail or '') ) - if e1.tag != e2.tag: return False, get_msg(e1, e2, 'tag') - if e1.text != e2.text: return False, get_msg(e1, e2, 'text') - if cmp_tails(e1, e2): return False, get_msg(e1, e2, 'tail') - if e1.attrib != e2.attrib: return False, get_msg(e1, e2, 'attrib') - if len(e1) != len(e2): return False, "len({}) != len({})".format(str(e1), str(e2)) + if e1.tag != e2.tag: return False, get_msg(e1, e2, 'tag') # noqa + if e1.text != e2.text: return False, get_msg(e1, e2, 'text') # noqa + if cmp_tails(e1, e2): return False, get_msg(e1, e2, 'tail') # noqa + if e1.attrib != e2.attrib: return False, get_msg(e1, e2, 'attrib') # noqa + if len(e1) != len(e2): return False, "len({}) != len({})".format(str(e1), str(e2)) # noqa for c1, c2 in zip(sort_elems(e1), sort_elems(e2)): are_equal, info = cls._are_trees_isomorphic(c1, c2) diff --git a/onadata/apps/data_migration/tests/test_backup.py b/onadata/apps/data_migration/tests/test_backup.py index 5f4986889..5902f17e2 100644 --- a/onadata/apps/data_migration/tests/test_backup.py +++ b/onadata/apps/data_migration/tests/test_backup.py @@ -1,9 +1,6 @@ from onadata.apps.logger.models import Instance -from onadata.apps.data_migration.models import ( - BackupInstance, BackupXForm, XFormVersion) -from onadata.apps.data_migration.backup_data import ( - backup_xform, backup_survey -) +from onadata.apps.data_migration.models import BackupInstance, BackupXForm, XFormVersion +from onadata.apps.data_migration.backup_data import backup_xform, backup_survey from .common import MigrationTestCase @@ -56,7 +53,7 @@ def test_backup_xform__bind_twice(self): def test_multiple_xform_backups(self): exp = [backup_xform(self.xform).backup_version for _ in range(10)] actual = BackupXForm.objects.filter(xform_id=self.xform.id)\ - .values_list('backup_version', flat=True) + .values_list('backup_version', flat=True) self.assertCountEqual(exp, actual) def test_backup_survey(self): diff --git a/onadata/apps/data_migration/tests/test_compare_xml.py b/onadata/apps/data_migration/tests/test_compare_xml.py index 1d3b1e041..14633a55d 100644 --- a/onadata/apps/data_migration/tests/test_compare_xml.py +++ b/onadata/apps/data_migration/tests/test_compare_xml.py @@ -96,4 +96,3 @@ def test_fields_groups(self): 'current_date': [], } ) - diff --git a/onadata/apps/data_migration/tests/test_data_migration.py b/onadata/apps/data_migration/tests/test_data_migration.py index 8efc94f89..6cf0fc778 100644 --- a/onadata/apps/data_migration/tests/test_data_migration.py +++ b/onadata/apps/data_migration/tests/test_data_migration.py @@ -92,7 +92,7 @@ def test_migration__grouped_case(self): ) -class DataMigratorIntegrationSecondTestsCase(test_case.ThirdMigrationTestCase): +class DataMigratorIntegrationThirdTestsCase(test_case.ThirdMigrationTestCase): def test_migration__third_case(self): self.data_migrator.migrate() self.xform_new.refresh_from_db() diff --git a/onadata/apps/data_migration/tests/test_decisioner.py b/onadata/apps/data_migration/tests/test_decisioner.py index f46bd1d1c..3a9445bae 100644 --- a/onadata/apps/data_migration/tests/test_decisioner.py +++ b/onadata/apps/data_migration/tests/test_decisioner.py @@ -115,11 +115,11 @@ def test_prev_fields_groups_migrated(self): prev_fields_groups = self.migration_decisioner\ .prev_fields_groups_migrated() self.assertEqual({ - 'first_name': [], - 'gender': [], - 'photo': [], - 'age': [], - 'location': [], + 'first_name': [], + 'gender': [], + 'photo': [], + 'age': [], + 'location': [], }, prev_fields_groups) def test_changed_fields_groups(self): @@ -131,7 +131,6 @@ def test_changed_fields_groups(self): }, changed_fields_groups) - class GroupedMigrationDecisionerUnitTests(GroupedMigrationTestCase): def setUp(self): super(GroupedMigrationDecisionerUnitTests, self).setUp() diff --git a/onadata/apps/data_migration/tests/test_tree.py b/onadata/apps/data_migration/tests/test_tree.py index fac74282e..450b1a24f 100644 --- a/onadata/apps/data_migration/tests/test_tree.py +++ b/onadata/apps/data_migration/tests/test_tree.py @@ -53,8 +53,5 @@ def test_extract_ancestors_labels(self): 'a_parents': ['root'], }, { 'c31_parents': self.tree.extract_ancestors_labels(self.c31_node), - 'a_parents': self.tree.extract_ancestors_labels( - self.tree.find_child_by_label('a') - ) + 'a_parents': self.tree.extract_ancestors_labels(self.tree.find_child_by_label('a')) }) - diff --git a/onadata/apps/data_migration/tests/test_views.py b/onadata/apps/data_migration/tests/test_views.py index 9a2f2e231..ac271b0b1 100644 --- a/onadata/apps/data_migration/tests/test_views.py +++ b/onadata/apps/data_migration/tests/test_views.py @@ -1,4 +1,3 @@ -from unittest import skip from mock import patch from django.test import Client @@ -80,7 +79,7 @@ def test_migration_process(self): url = reverse('migrate-xform-data', kwargs=self.get_data__both_id_strings()) response = self.client.post(url, self.get_migration_decisions()) - self.assertEqual(response.status_code, 302) + self.assertEqual(response.status_code, 302) self.assertEqual(XForm.objects.count(), 1) self.assertEqual(Instance.objects.count(), 1) @@ -92,4 +91,4 @@ def test_restore_backup_view(self, restore_backup_mock, *args): response = self.client.post(url, {'restore_last': True}) restore_backup_mock.assert_called_once() - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 200) diff --git a/onadata/apps/data_migration/tests/test_xformtree.py b/onadata/apps/data_migration/tests/test_xformtree.py index a11d81a4b..0ad83ca04 100644 --- a/onadata/apps/data_migration/tests/test_xformtree.py +++ b/onadata/apps/data_migration/tests/test_xformtree.py @@ -131,4 +131,3 @@ def test_get_structured_fields(self): 'math_degree', new_group] self.assertCountEqual(expected_prev, self.prev_tree.get_structured_fields()) self.assertCountEqual(expected_new, self.new_tree.get_structured_fields()) - diff --git a/onadata/apps/data_migration/views.py b/onadata/apps/data_migration/views.py index 4f068a641..c77f7d9d0 100644 --- a/onadata/apps/data_migration/views.py +++ b/onadata/apps/data_migration/views.py @@ -92,9 +92,7 @@ def set_form(): return { 'type': 'alert-success', 'text': _(u'Successfully updated %(form_id)s.' - u' Please proceed now in data migration') % { - 'form_id': id_string, - } + u' Please proceed now in data migration') % {'form_id': id_string} } message = publish_form(set_form) diff --git a/onadata/apps/data_migration/xformtree.py b/onadata/apps/data_migration/xformtree.py index 54501f6d2..abe6fe549 100644 --- a/onadata/apps/data_migration/xformtree.py +++ b/onadata/apps/data_migration/xformtree.py @@ -114,7 +114,7 @@ def get_cleaned_nodeset(self, bind): output: 'name' """ nodeset = bind.attrib['nodeset'] - return nodeset[nodeset.find('/', 2)+1:] + return nodeset[nodeset.find('/', 2) + 1:] def get_inputs_as_dict(self): """ @@ -129,7 +129,7 @@ def get_inputs_as_dict(self): def get_input_name(self, input): ref = input.attrib['ref'] - return ref[ref.find('/', 2)+1:] + return ref[ref.find('/', 2) + 1:] def get_id_string(self): return self.get_head_instance().attrib['id'] diff --git a/onadata/apps/data_migration/xmltree.py b/onadata/apps/data_migration/xmltree.py index 522709447..c263e34d9 100644 --- a/onadata/apps/data_migration/xmltree.py +++ b/onadata/apps/data_migration/xmltree.py @@ -69,7 +69,7 @@ def clean_tag(tag): Example: '{http://www.w3.org/1999/xhtml}head' """ header_end = tag.find('}') - return tag[header_end+1:] if header_end != -1 else tag + return tag[header_end + 1:] if header_end != -1 else tag @classmethod def field_tag(cls, field):