diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-03-19 11:05:22 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-03-19 11:05:22 +0100 |
commit | 3039fae5124c00a67283c9b707e4a411149d93b1 (patch) | |
tree | 5d7fde3628825aebeeef3d85d2dfcf09a52116de /ishtar_common/tests.py | |
parent | b38e35ad05ae5b7d1c3d45436921f573bc9e5ba6 (diff) | |
download | Ishtar-3039fae5124c00a67283c9b707e4a411149d93b1.tar.bz2 Ishtar-3039fae5124c00a67283c9b707e4a411149d93b1.zip |
Format - black: ishtar_common
Diffstat (limited to 'ishtar_common/tests.py')
-rw-r--r-- | ishtar_common/tests.py | 2018 |
1 files changed, 1090 insertions, 928 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 20f87b3bc..85d658160 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (C) 2015-2017 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> @@ -53,101 +53,109 @@ from django.test.runner import DiscoverRunner from ishtar_common import models, models_common from ishtar_common import views from ishtar_common.apps import admin_site -from ishtar_common.serializers import type_serialization, \ - SERIALIZATION_VERSION, \ - restore_serialized, conf_serialization, CONF_MODEL_LIST, \ - importer_serialization, IMPORT_MODEL_LIST, geo_serialization, \ - GEO_MODEL_LIST, directory_serialization, DIRECTORY_MODEL_LIST, \ - document_serialization, get_type_models, full_serialization +from ishtar_common.serializers import ( + type_serialization, + SERIALIZATION_VERSION, + restore_serialized, + conf_serialization, + CONF_MODEL_LIST, + importer_serialization, + IMPORT_MODEL_LIST, + geo_serialization, + GEO_MODEL_LIST, + directory_serialization, + DIRECTORY_MODEL_LIST, + document_serialization, + get_type_models, + full_serialization, +) from archaeological_operations.serializers import OPERATION_MODEL_LIST from archaeological_context_records.serializers import CR_MODEL_LIST from archaeological_finds.serializers import FIND_MODEL_LIST from archaeological_warehouse.serializers import WAREHOUSE_MODEL_LIST from ishtar_common.serializers_utils import serialization_info -from ishtar_common.utils import post_save_geo, update_data, move_dict_data, \ - rename_and_simplify_media_name, try_fix_file +from ishtar_common.utils import ( + post_save_geo, + update_data, + move_dict_data, + rename_and_simplify_media_name, + try_fix_file, +) from ishtar_common.tasks import launch_export from ishtar_common import utils_secretary COMMON_FIXTURES = [ - settings.ROOT_PATH + '../fixtures/initial_data-auth-fr.json', - settings.ROOT_PATH + '../ishtar_common/fixtures/initial_data-fr.json', - settings.ROOT_PATH + - '../ishtar_common/fixtures/initial_spatialrefsystem-fr.json', - settings.ROOT_PATH + - '../ishtar_common/fixtures/initial_importtypes-fr.json', - ] + settings.ROOT_PATH + "../fixtures/initial_data-auth-fr.json", + settings.ROOT_PATH + "../ishtar_common/fixtures/initial_data-fr.json", + settings.ROOT_PATH + "../ishtar_common/fixtures/initial_spatialrefsystem-fr.json", + settings.ROOT_PATH + "../ishtar_common/fixtures/initial_importtypes-fr.json", +] OPERATION_FIXTURES = COMMON_FIXTURES + [ - settings.ROOT_PATH + - '../archaeological_operations/fixtures/initial_data-fr.json', - settings.ROOT_PATH + - '../archaeological_operations/fixtures/initial_data_relation_type_norel-fr.json', - settings.ROOT_PATH + - '../archaeological_operations/fixtures/initial_data_relation_type-fr.json', + settings.ROOT_PATH + "../archaeological_operations/fixtures/initial_data-fr.json", + settings.ROOT_PATH + + "../archaeological_operations/fixtures/initial_data_relation_type_norel-fr.json", + settings.ROOT_PATH + + "../archaeological_operations/fixtures/initial_data_relation_type-fr.json", ] -OPERATION_TOWNS_FIXTURES = \ - OPERATION_FIXTURES + \ - [settings.ROOT_PATH + '../ishtar_common/fixtures/test_towns.json'] +OPERATION_TOWNS_FIXTURES = OPERATION_FIXTURES + [ + settings.ROOT_PATH + "../ishtar_common/fixtures/test_towns.json" +] FILE_FIXTURES = OPERATION_FIXTURES + [ - settings.ROOT_PATH + - '../archaeological_files/fixtures/initial_data-fr.json'] + settings.ROOT_PATH + "../archaeological_files/fixtures/initial_data-fr.json" +] FILE_TOWNS_FIXTURES = OPERATION_TOWNS_FIXTURES + [ - settings.ROOT_PATH + - '../archaeological_files/fixtures/initial_data-fr.json'] + settings.ROOT_PATH + "../archaeological_files/fixtures/initial_data-fr.json" +] CONTEXT_RECORD_FIXTURES = FILE_FIXTURES + [ - settings.ROOT_PATH + - '../archaeological_context_records/fixtures/initial_data-fr.json', - settings.ROOT_PATH + - '../archaeological_context_records/fixtures/initial_data_relation_type_norel-fr.json', - settings.ROOT_PATH + - '../archaeological_context_records/fixtures/initial_data_relation_type-fr.json', - ] + settings.ROOT_PATH + + "../archaeological_context_records/fixtures/initial_data-fr.json", + settings.ROOT_PATH + + "../archaeological_context_records/fixtures/initial_data_relation_type_norel-fr.json", + settings.ROOT_PATH + + "../archaeological_context_records/fixtures/initial_data_relation_type-fr.json", +] CONTEXT_RECORD_TOWNS_FIXTURES = FILE_TOWNS_FIXTURES + [ - settings.ROOT_PATH + - '../archaeological_context_records/fixtures/initial_data-fr.json', - settings.ROOT_PATH + - '../archaeological_context_records/fixtures/initial_data_relation_type_norel-fr.json', - settings.ROOT_PATH + - '../archaeological_context_records/fixtures/initial_data_relation_type-fr.json', - ] + settings.ROOT_PATH + + "../archaeological_context_records/fixtures/initial_data-fr.json", + settings.ROOT_PATH + + "../archaeological_context_records/fixtures/initial_data_relation_type_norel-fr.json", + settings.ROOT_PATH + + "../archaeological_context_records/fixtures/initial_data_relation_type-fr.json", +] FIND_FIXTURES = CONTEXT_RECORD_FIXTURES + [ - settings.ROOT_PATH + - '../archaeological_finds/fixtures/initial_data-fr.json', - ] + settings.ROOT_PATH + "../archaeological_finds/fixtures/initial_data-fr.json", +] FIND_TOWNS_FIXTURES = CONTEXT_RECORD_TOWNS_FIXTURES + [ - settings.ROOT_PATH + - '../archaeological_finds/fixtures/initial_data-fr.json', - ] + settings.ROOT_PATH + "../archaeological_finds/fixtures/initial_data-fr.json", +] WAREHOUSE_FIXTURES = FIND_FIXTURES + [ - settings.ROOT_PATH + - '../archaeological_warehouse/fixtures/initial_data-fr.json', - ] + settings.ROOT_PATH + "../archaeological_warehouse/fixtures/initial_data-fr.json", +] def create_superuser(): - username = 'username4277' - password = 'dcbqj756456!@%' + username = "username4277" + password = "dcbqj756456!@%" q = User.objects.filter(username=username) if q.count(): return username, password, q.all()[0] - user = User.objects.create_superuser(username, "nomail@nomail.com", - password) + user = User.objects.create_superuser(username, "nomail@nomail.com", password) user.set_password(password) user.save() return username, password, user -def create_user(username='username678', password='dcbqj756aaa456!@%'): +def create_user(username="username678", password="dcbqj756aaa456!@%"): q = User.objects.filter(username=username) if q.count(): return username, password, q.all()[0] @@ -163,62 +171,31 @@ class TestCase(BaseTestCase): class UtilsTest(TestCase): def test_update_data(self): - data_1 = { - 'old': - {'youpi': - {'plouf': 'tralalalère'}} - } - data_2 = { - 'tsoin_tsoin': 'hop', - 'old': - {'hoppy': 'hop'} - } + data_1 = {"old": {"youpi": {"plouf": "tralalalère"}}} + data_2 = {"tsoin_tsoin": "hop", "old": {"hoppy": "hop"}} expected = { - 'tsoin_tsoin': 'hop', - 'old': - {'youpi': - {'plouf': 'tralalalère'}, - 'hoppy': 'hop'} + "tsoin_tsoin": "hop", + "old": {"youpi": {"plouf": "tralalalère"}, "hoppy": "hop"}, } res = update_data(data_1, data_2) self.assertEqual(res, expected) def test_move_dict_data(self): - data = { - 'old': {'daté': "value"} - } - expected = { - 'old': {'date': "value"} - } - res = move_dict_data( - data, 'data__old__daté', "data__old__date") + data = {"old": {"daté": "value"}} + expected = {"old": {"date": "value"}} + res = move_dict_data(data, "data__old__daté", "data__old__date") self.assertEqual(res, expected) - data = { - '': {'hop': "value"} - } - expected = { - 'hop': "value", - '': {} - } - res = move_dict_data(data, 'data____hop', "data__hop") + data = {"": {"hop": "value"}} + expected = {"hop": "value", "": {}} + res = move_dict_data(data, "data____hop", "data__hop") self.assertEqual(res, expected) - data = { - 'old': { - 'traitement': { - 'constat_état': 'aaa' - } - } - } - expected = { - 'old': { - 'traitement': { - 'constat_etat': 'aaa' - } - } - } - res = move_dict_data(data, - 'data__old__traitement__constat_état', - 'data__old__traitement__constat_etat') + data = {"old": {"traitement": {"constat_état": "aaa"}}} + expected = {"old": {"traitement": {"constat_etat": "aaa"}}} + res = move_dict_data( + data, + "data__old__traitement__constat_état", + "data__old__traitement__constat_etat", + ) self.assertEqual(res, expected) def test_tinyfy_url(self): @@ -235,44 +212,44 @@ class UtilsTest(TestCase): ty = models.TinyUrl.objects.get(id=db_id) self.assertEqual(base_url, ty.link) c = Client() - response = c.get(reverse('tiny-redirect', args=[short_id])) - self.assertEqual(response['Location'], base_url) + response = c.get(reverse("tiny-redirect", args=[short_id])) + self.assertEqual(response["Location"], base_url) self.assertEqual(response.status_code, 302) class SearchText: - SEARCH_URL = '' + SEARCH_URL = "" def _test_search(self, client, result, context=""): assert self.SEARCH_URL for q, expected_result in result: - search = {'search_vector': q} + search = {"search_vector": q} response = client.get(reverse(self.SEARCH_URL), search) self.assertEqual(response.status_code, 200) res = json.loads(response.content.decode()) - msg = "{} result(s) where expected for search: {} - found {}" \ - "".format(expected_result, q, res['recordsTotal']) + msg = "{} result(s) where expected for search: {} - found {}" "".format( + expected_result, q, res["recordsTotal"] + ) if context: msg = context + " - " + msg - self.assertEqual(res['recordsTotal'], expected_result, - msg=msg) + self.assertEqual(res["recordsTotal"], expected_result, msg=msg) class CommandsTestCase(TestCase): - fixtures = [settings.ROOT_PATH + - '../ishtar_common/fixtures/test_towns.json'] + fixtures = [settings.ROOT_PATH + "../ishtar_common/fixtures/test_towns.json"] def test_clean_ishtar(self): """ Clean ishtar db """ from archaeological_operations.models import Parcel + p = Parcel.objects.create( - town=models.Town.objects.create(name='test', numero_insee='25000'), + town=models.Town.objects.create(name="test", numero_insee="25000"), ) parcel_nb = Parcel.objects.count() out = StringIO() - call_command('clean_ishtar', stdout=out) + call_command("clean_ishtar", stdout=out) # no operation or file attached - the parcel should have disappear self.assertEqual(parcel_nb - 1, Parcel.objects.count()) self.assertEqual(Parcel.objects.filter(pk=p.pk).count(), 0) @@ -281,56 +258,75 @@ class CommandsTestCase(TestCase): q = models.Town.objects town_nb = q.count() out = StringIO() - call_command('import_geofla_csv', - settings.ROOT_PATH + - '../ishtar_common/tests/geofla-test.csv', '--quiet', - stdout=out) + call_command( + "import_geofla_csv", + settings.ROOT_PATH + "../ishtar_common/tests/geofla-test.csv", + "--quiet", + stdout=out, + ) self.assertEqual(town_nb + 9, models.Town.objects.count()) - call_command('import_geofla_csv', - settings.ROOT_PATH + - '../ishtar_common/tests/geofla-test.csv', '--quiet', - stdout=out) + call_command( + "import_geofla_csv", + settings.ROOT_PATH + "../ishtar_common/tests/geofla-test.csv", + "--quiet", + stdout=out, + ) # no new town self.assertEqual(town_nb + 9, models.Town.objects.count()) def test_import_insee(self): q = models.Town.objects town_nb = q.count() - first, union_start, union_end = '', '', [] + first, union_start, union_end = "", "", [] new_nums = [] for idx, town in enumerate(q.all()): x1 = float(idx) / 10 if not x1: x1 = 0 x2 = float(idx) / 10 + 0.1 - l = 'MULTIPOLYGON((({x1} 0.1,{x2} 0.1,{x2} 0,{x1} 0,' \ - '{x1} 0.1)))'.format(x1=x1, x2=x2) + l = "MULTIPOLYGON((({x1} 0.1,{x2} 0.1,{x2} 0,{x1} 0," "{x1} 0.1)))".format( + x1=x1, x2=x2 + ) if union_start: union_start += ", " else: - first = '{x1} 0.1'.format(x1=x1) - union_start += '{x2} 0.1'.format(x1=x1, x2=x2) - union_end.append('{x2} 0'.format(x1=x1, x2=x2)) + first = "{x1} 0.1".format(x1=x1) + union_start += "{x2} 0.1".format(x1=x1, x2=x2) + union_end.append("{x2} 0".format(x1=x1, x2=x2)) town.limit = l town.year = 1792 new_nums.append("{}-{}".format(town.numero_insee, town.year)) town.save() - union = 'MULTIPOLYGON (((' + first + ", " + union_start + \ - ", " + ", ".join(reversed(union_end)) + ", 0 0, " + first + ")))" + union = ( + "MULTIPOLYGON (((" + + first + + ", " + + union_start + + ", " + + ", ".join(reversed(union_end)) + + ", 0 0, " + + first + + ")))" + ) out = StringIO() - call_command('import_insee_comm_csv', - settings.ROOT_PATH + - '../ishtar_common/tests/insee-test.csv', '--quiet', - stdout=out) + call_command( + "import_insee_comm_csv", + settings.ROOT_PATH + "../ishtar_common/tests/insee-test.csv", + "--quiet", + stdout=out, + ) self.assertEqual(town_nb + 1, models.Town.objects.count()) - new = models.Town.objects.order_by('-pk').all()[0] + new = models.Town.objects.order_by("-pk").all()[0] self.assertEqual(new.parents.count(), 2) self.assertEqual(new.limit.wkt, union) - call_command('import_insee_comm_csv', '--quiet', - settings.ROOT_PATH + - '../ishtar_common/tests/insee-test.csv', stdout=out) + call_command( + "import_insee_comm_csv", + "--quiet", + settings.ROOT_PATH + "../ishtar_common/tests/insee-test.csv", + stdout=out, + ) # no new town self.assertEqual(town_nb + 1, models.Town.objects.count()) @@ -342,8 +338,16 @@ class WizardTestFormData(object): """ Test set to simulate wizard steps """ - def __init__(self, name, form_datas=None, ignored=None, pre_tests=None, - extra_tests=None, error_expected=None): + + def __init__( + self, + name, + form_datas=None, + ignored=None, + pre_tests=None, + extra_tests=None, + error_expected=None, + ): """ :param name: explicit name of the test :param form_datas: dict with data for each step - dict key are wizard @@ -392,7 +396,7 @@ class WizardTestFormData(object): Initialisations before the wizard. """ - suffix = '-' + test_object.url_name + suffix = "-" + test_object.url_name # if form names are defined without url_name fix it for form_name in list(self.form_datas.keys()): if suffix in form_name: @@ -412,7 +416,6 @@ class WizardTestFormData(object): class TimedTextTestResult(TextTestResult): - def __init__(self, *args, **kwargs): super(TimedTextTestResult, self).__init__(*args, **kwargs) self.clocks = {} @@ -430,7 +433,7 @@ class TimedTextTestResult(TextTestResult): if self.showAll: self.stream.writeln("OK (%.6fs)" % (time() - self.clocks[test])) elif self.dots: - self.stream.write('.') + self.stream.write(".") self.stream.flush() @@ -444,20 +447,19 @@ class ManagedModelTestRunner(DiscoverRunner): project managed for the duration of the test run, so that one doesn't need to execute the SQL manually to create them. """ + test_runner = TimedTextTestRunner def setup_test_environment(self, *args, **kwargs): from django.apps import apps - self.unmanaged_models = [m for m in apps.get_models() - if not m._meta.managed] + + self.unmanaged_models = [m for m in apps.get_models() if not m._meta.managed] for m in self.unmanaged_models: m._meta.managed = True - super(ManagedModelTestRunner, self).setup_test_environment(*args, - **kwargs) + super(ManagedModelTestRunner, self).setup_test_environment(*args, **kwargs) def teardown_test_environment(self, *args, **kwargs): - super(ManagedModelTestRunner, self).teardown_test_environment(*args, - **kwargs) + super(ManagedModelTestRunner, self).teardown_test_environment(*args, **kwargs) # reset unmanaged models for m in self.unmanaged_models: m._meta.managed = False @@ -466,7 +468,7 @@ class ManagedModelTestRunner(DiscoverRunner): class WizardTest(object): url_name = None url_uri = None - wizard_name = '' + wizard_name = "" steps = None condition_dict = None form_datas = [] @@ -479,8 +481,7 @@ class WizardTest(object): self.username, self.password, self.user = create_superuser() def pre_wizard(self): - self.client.login(**{'username': self.username, - 'password': self.password}) + self.client.login(**{"username": self.username, "password": self.password}) def post_wizard(self): pass @@ -491,22 +492,24 @@ class WizardTest(object): def check_response(self, response, current_step, data_idx): if "errorlist" in response.content.decode(): soup = Soup(response.content.decode(), "lxml") - errorlist = soup.findAll( - "ul", {"class": "errorlist"}) + errorlist = soup.findAll("ul", {"class": "errorlist"}) errors = [] for li in errorlist: lbl = li.findParent().findParent().findChild().text errors.append("{} - {}".format(lbl, li.text)) - raise ValidationError("Errors: {} on {} - dataset {}.".format( - " ".join(errors), current_step, data_idx + 1)) + raise ValidationError( + "Errors: {} on {} - dataset {}.".format( + " ".join(errors), current_step, data_idx + 1 + ) + ) - def wizard_post(self, client, url, current_step, form_data=None, - follow=True, extra_data=None): + def wizard_post( + self, client, url, current_step, form_data=None, follow=True, extra_data=None + ): if not url: url = reverse(self.url_name) data = { - '{}{}-current_step'.format(self.url_name, - self.wizard_name): [current_step], + "{}{}-current_step".format(self.url_name, self.wizard_name): [current_step], } if not form_data: form_data = [] @@ -515,34 +518,36 @@ class WizardTest(object): if type(form_data) in (list, tuple): # is a formset for d_idx, item in enumerate(form_data): for k in item: - data['{}-{}-{}'.format( - current_step, d_idx, k)] = item[k] + data["{}-{}-{}".format(current_step, d_idx, k)] = item[k] else: for k in form_data: - data['{}-{}'.format(current_step, k)] = form_data[k] + data["{}-{}".format(current_step, k)] = form_data[k] if extra_data: data.update(extra_data) try: response = client.post(url, data, follow=follow) except ValidationError as e: - msg = "Errors: {} on {}. On \"ManagementForm data is " \ - "missing or...\" error verify the wizard_name or " \ - "step name".format(" - ".join(e.messages), - current_step) + msg = ( + 'Errors: {} on {}. On "ManagementForm data is ' + 'missing or..." error verify the wizard_name or ' + "step name".format(" - ".join(e.messages), current_step) + ) raise ValidationError(msg) return response - def wizard_post_test(self, idx, data_idx, url, current_step, form_data, - test_form_data, ignored): - next_form_is_checked = len(self.steps) > idx + 1 and \ - self.steps[idx + 1][0] not in ignored + def wizard_post_test( + self, idx, data_idx, url, current_step, form_data, test_form_data, ignored + ): + next_form_is_checked = ( + len(self.steps) > idx + 1 and self.steps[idx + 1][0] not in ignored + ) data = [] if current_step in form_data: data = form_data[current_step] response = self.wizard_post( - self.client, url, current_step, data, - not next_form_is_checked) + self.client, url, current_step, data, not next_form_is_checked + ) if current_step == test_form_data.error_expected: with self.assertRaises(ValidationError): @@ -554,23 +559,20 @@ class WizardTest(object): next_form = self.steps[idx + 1][0] self.assertRedirects( response, - '/{}/{}'.format(self.url_uri, next_form), + "/{}/{}".format(self.url_uri, next_form), msg_prefix="Dataset n{} Redirection to {} has failed - " - "Error on previous form ({})?".format( - data_idx + 1, next_form, current_step) + "Error on previous form ({})?".format( + data_idx + 1, next_form, current_step + ), ) if idx == len(self.steps) - 1: # last form if not self.redirect_url: - redirect_url = '/{}/done'.format(self.url_uri) + redirect_url = "/{}/done".format(self.url_uri) else: - dct = { - "url_name": self.url_name, - "url_uri": self.url_uri - } - form_key = 'selec-' + self.url_name - if form_key in form_data and self.current_id_key in form_data[ - form_key]: + dct = {"url_name": self.url_name, "url_uri": self.url_uri} + form_key = "selec-" + self.url_name + if form_key in form_data and self.current_id_key in form_data[form_key]: dct["current_id"] = form_data[form_key][self.current_id_key] if self.model: q = self.model.objects @@ -601,35 +603,37 @@ class WizardTest(object): elif self.test_back and not back_tested: # test going back on a form response = self.wizard_post( - self.client, url, current_step, None, - extra_data={"form_prev_step": previous_step} + self.client, + url, + current_step, + None, + extra_data={"form_prev_step": previous_step}, ) self.assertEqual(response.status_code, 200) back_tested = True response = self.wizard_post_test( - idx, data_idx, url, current_step, form_data, test_form_data, - ignored) + idx, data_idx, url, current_step, form_data, test_form_data, ignored + ) test_form_data.tests(self, response) self.post_wizard() class CacheTest(TestCase): - fixtures = [settings.ROOT_PATH + - '../fixtures/initial_data-auth-fr.json', - settings.ROOT_PATH + - '../ishtar_common/fixtures/initial_data-fr.json',] + fixtures = [ + settings.ROOT_PATH + "../fixtures/initial_data-auth-fr.json", + settings.ROOT_PATH + "../ishtar_common/fixtures/initial_data-fr.json", + ] def test_add(self): models.OrganizationType.refresh_cache() - cached = models.OrganizationType.get_cache('test') + cached = models.OrganizationType.get_cache("test") self.assertEqual(cached, None) - orga = models.OrganizationType.objects.create( - txt_idx='test', label='testy') - cached = models.OrganizationType.get_cache('test') + orga = models.OrganizationType.objects.create(txt_idx="test", label="testy") + cached = models.OrganizationType.get_cache("test") self.assertEqual(cached.pk, orga.pk) - orga.txt_idx = 'testy' + orga.txt_idx = "testy" orga.save() - cached = models.OrganizationType.get_cache('testy') + cached = models.OrganizationType.get_cache("testy") self.assertEqual(cached.pk, orga.pk) def test_list(self): @@ -637,30 +641,30 @@ class CacheTest(TestCase): types = models.OrganizationType.get_types() # only empty self.assertTrue(len(types), 1) - org = models.OrganizationType.objects.create( - txt_idx='test', label='testy') - types = [ - str(lbl) for idx, lbl in models.OrganizationType.get_types()] - self.assertTrue('testy' in types) + org = models.OrganizationType.objects.create(txt_idx="test", label="testy") + types = [str(lbl) for idx, lbl in models.OrganizationType.get_types()] + self.assertTrue("testy" in types) org.delete() - types = [ - str(lbl) for idx, lbl in models.OrganizationType.get_types()] - self.assertFalse('testy' in types) + types = [str(lbl) for idx, lbl in models.OrganizationType.get_types()] + self.assertFalse("testy" in types) def test_menu_cache(self): - admin_username = 'username4277' - admin_password = 'dcbqj756456!@%' - User.objects.create_superuser(admin_username, "nomail@nomail.com", - admin_password) - readonly_user = 'username4456' - readonly_password = 'xshqu744456!@%' - user = User.objects.create_user(readonly_user, "nomail@nomail.com", - readonly_password) + admin_username = "username4277" + admin_password = "dcbqj756456!@%" + User.objects.create_superuser( + admin_username, "nomail@nomail.com", admin_password + ) + readonly_user = "username4456" + readonly_password = "xshqu744456!@%" + user = User.objects.create_user( + readonly_user, "nomail@nomail.com", readonly_password + ) ishtuser = models.IshtarUser.objects.get(user_ptr=user) models.UserProfile.objects.get_or_create( - current=True, person=ishtuser.person, - profile_type=models.ProfileType.objects.get( - txt_idx='reader_access')) + current=True, + person=ishtuser.person, + profile_type=models.ProfileType.objects.get(txt_idx="reader_access"), + ) c = Client() c.login(username=admin_username, password=admin_password) @@ -680,17 +684,22 @@ class CacheTest(TestCase): class GenericSerializationTest: def create_document_default(self): - image_path = os.path.join(settings.ROOT_PATH, "..", "ishtar_common", - "tests", "test.png") + image_path = os.path.join( + settings.ROOT_PATH, "..", "ishtar_common", "tests", "test.png" + ) self.documents = [] for idx in range(12): - self.documents.append(models.Document.objects.create( - title="Test{}".format(idx), - associated_file=SimpleUploadedFile( - 'test.txt', b'no real content'), - image=SimpleUploadedFile( - name='test.png', content=open(image_path, 'rb').read(), - content_type='image/png'))) + self.documents.append( + models.Document.objects.create( + title="Test{}".format(idx), + associated_file=SimpleUploadedFile("test.txt", b"no real content"), + image=SimpleUploadedFile( + name="test.png", + content=open(image_path, "rb").read(), + content_type="image/png", + ), + ) + ) def generic_serialization_test(self, serialize, no_test=False, kwargs=None): if not kwargs: @@ -703,11 +712,11 @@ class GenericSerializationTest: module_name, model_name = k.split("__") if module_name == "django": if model_name in ("Group", "Permission"): - module = importlib.import_module( - "django.contrib.auth.models") + module = importlib.import_module("django.contrib.auth.models") elif model_name in ("ContentType",): module = importlib.import_module( - "django.contrib.contenttypes.models") + "django.contrib.contenttypes.models" + ) else: return else: @@ -720,62 +729,87 @@ class GenericSerializationTest: result = json.loads(json_result[key]) serialization_count = len(result) # all serialization have to be tested - self.assertTrue(serialization_count, - msg="No data to test for {}".format(key)) + self.assertTrue( + serialization_count, msg="No data to test for {}".format(key) + ) # only "natural" serialization self.assertNotIn( - "pk", result[0], - msg="Serialization for {} do not use natural keys".format(key)) + "pk", + result[0], + msg="Serialization for {} do not use natural keys".format(key), + ) self.assertNotIn( - "id", result[0], - msg="Serialization for {} do not use natural keys".format(key)) + "id", + result[0], + msg="Serialization for {} do not use natural keys".format(key), + ) # has to be at least equal (can be superior for model with # recursion) self.assertTrue( serialization_count >= current_count, msg="Serialization for model {}.{} failed. {} serialized {} " - "expected".format(module.__name__, model_name, - serialization_count, current_count)) + "expected".format( + module.__name__, model_name, serialization_count, current_count + ), + ) return json_result - def generic_restore_test_genzip(self, model_list, serialization, - kwargs=None): + def generic_restore_test_genzip(self, model_list, serialization, kwargs=None): current_number = {} for model in model_list: - current_number[(model.__module__, model.__name__)] = \ - model.objects.count() + current_number[(model.__module__, model.__name__)] = model.objects.count() if not kwargs: kwargs = {} kwargs["archive"] = True zip_filename = serialization(**kwargs) return current_number, zip_filename - def generic_restore_test(self, zip_filename, current_number, model_list, - release_locks=False, delete_existing=True): - restore_serialized(zip_filename, delete_existing=delete_existing, - release_locks=release_locks) + def generic_restore_test( + self, + zip_filename, + current_number, + model_list, + release_locks=False, + delete_existing=True, + ): + restore_serialized( + zip_filename, delete_existing=delete_existing, release_locks=release_locks + ) for model in model_list: previous_nb = current_number[(model.__module__, model.__name__)] current_nb = model.objects.count() self.assertEqual( - previous_nb, current_nb, + previous_nb, + current_nb, msg="Restore for model {}.{} failed. Initial: {}, restored: " - "{}.".format(model.__module__, model.__name__, - previous_nb, current_nb)) + "{}.".format(model.__module__, model.__name__, previous_nb, current_nb), + ) class SerializationTest(GenericSerializationTest, TestCase): fixtures = COMMON_FIXTURES + WAREHOUSE_FIXTURES def create_types(self): - from archaeological_finds.models import MaterialTypeQualityType, \ - ObjectTypeQualityType, AlterationType, AlterationCauseType, \ - TreatmentEmergencyType, CommunicabilityType + from archaeological_finds.models import ( + MaterialTypeQualityType, + ObjectTypeQualityType, + AlterationType, + AlterationCauseType, + TreatmentEmergencyType, + CommunicabilityType, + ) from archaeological_operations.models import CulturalAttributionType - for model in (models.LicenseType, MaterialTypeQualityType, - ObjectTypeQualityType, AlterationType, - AlterationCauseType, TreatmentEmergencyType, - CommunicabilityType, CulturalAttributionType): + + for model in ( + models.LicenseType, + MaterialTypeQualityType, + ObjectTypeQualityType, + AlterationType, + AlterationCauseType, + TreatmentEmergencyType, + CommunicabilityType, + CulturalAttributionType, + ): model.objects.create(txt_idx="test", label="Test") def test_type_serialization(self): @@ -787,9 +821,9 @@ class SerializationTest(GenericSerializationTest, TestCase): models.get_current_profile(force=True) # create a default profile models.GlobalVar.objects.create(slug="test") cform = models.CustomForm.objects.create( - name="Test", form='ishtar_common.forms.TestForm') - models.ExcludedField.objects.create(custom_form=cform, - field="ExcludedField") + name="Test", form="ishtar_common.forms.TestForm" + ) + models.ExcludedField.objects.create(custom_form=cform, field="ExcludedField") CT = ContentType.objects.get_for_model(models.OrganizationType) models.JsonDataSection.objects.create( content_type=CT, @@ -805,8 +839,10 @@ class SerializationTest(GenericSerializationTest, TestCase): klass="ishtar_common.models.Organization" ) values["document_template"] = models.DocumentTemplate.objects.create( - name="Test", slug="test", associated_model=mod, - template=SimpleUploadedFile('test.txt', b'no real content') + name="Test", + slug="test", + associated_model=mod, + template=SimpleUploadedFile("test.txt", b"no real content"), ) return values @@ -825,20 +861,21 @@ class SerializationTest(GenericSerializationTest, TestCase): def create_geo_default(self): s = models_common.State.objects.create(label="test", number="999") - d = models.Department.objects.create(label="test", number="999", - state=s) + d = models.Department.objects.create(label="test", number="999", state=s) t1 = models.Town.objects.create( name="Test town", center="SRID=4326;POINT(-44.3 60.1)", - numero_insee="12345", departement=d + numero_insee="12345", + departement=d, ) t2 = models.Town.objects.create( name="Test town 2", center="SRID=4326;POINT(-44.2 60.2)", - numero_insee="12346", departement=d + numero_insee="12346", + departement=d, ) t2.children.add(t1) - a = models.Area.objects.create(label="Test", txt_idx='test') + a = models.Area.objects.create(label="Test", txt_idx="test") a.towns.add(t1) def test_geo_serialization(self): @@ -847,13 +884,12 @@ class SerializationTest(GenericSerializationTest, TestCase): def create_directory_default(self): org = models.Organization.objects.create( - name="Test", - organization_type=models.OrganizationType.objects.all()[0]) - person = models.Person.objects.create( - name="Test", attached_to=org + name="Test", organization_type=models.OrganizationType.objects.all()[0] ) + person = models.Person.objects.create(name="Test", attached_to=org) models.Author.objects.create( - person=person, author_type=models.AuthorType.objects.all()[0]) + person=person, author_type=models.AuthorType.objects.all()[0] + ) def test_directory_serialization(self): self.create_directory_default() @@ -861,17 +897,29 @@ class SerializationTest(GenericSerializationTest, TestCase): def create_document_default(self): super(SerializationTest, self).create_document_default() - from archaeological_operations.models import Operation, \ - ArchaeologicalSite, OperationType + from archaeological_operations.models import ( + Operation, + ArchaeologicalSite, + OperationType, + ) from archaeological_context_records.models import ContextRecord from archaeological_finds.models import Find, BaseFind - from archaeological_warehouse.models import Warehouse, Container, \ - ContainerLocalisation, WarehouseDivision, WarehouseDivisionLink, \ - WarehouseType, ContainerType + from archaeological_warehouse.models import ( + Warehouse, + Container, + ContainerLocalisation, + WarehouseDivision, + WarehouseDivisionLink, + WarehouseType, + ContainerType, + ) operation_type = OperationType.objects.all()[0] - dct = {'year': 2010, 'operation_type_id': operation_type.pk, - "code_patriarche": "66666"} + dct = { + "year": 2010, + "operation_type_id": operation_type.pk, + "code_patriarche": "66666", + } operation1 = Operation.objects.create(**dct) operation1.documents.add(self.documents[0]) dct["code_patriarche"] = "66667" @@ -885,23 +933,23 @@ class SerializationTest(GenericSerializationTest, TestCase): operation2.archaeological_sites.add(site2) site2.documents.add(self.documents[3]) - dct = {'label': "Context record1", "operation": operation1} + dct = {"label": "Context record1", "operation": operation1} cr1 = ContextRecord.objects.create(**dct) cr1.documents.add(self.documents[4]) - dct = {'label': "Context record2", "operation": operation2} + dct = {"label": "Context record2", "operation": operation2} cr2 = ContextRecord.objects.create(**dct) cr2.documents.add(self.documents[5]) - dct = {'label': "Base find", "context_record": cr1} + dct = {"label": "Base find", "context_record": cr1} base_find1 = BaseFind.objects.create(**dct) - dct = {'label': "Base find2", "context_record": cr2} + dct = {"label": "Base find2", "context_record": cr2} base_find2 = BaseFind.objects.create(**dct) - dct = {'label': "Find1"} + dct = {"label": "Find1"} find1 = Find.objects.create(**dct) find1.documents.add(self.documents[6]) find1.base_finds.add(base_find1) - dct = {'label': "Find2"} + dct = {"label": "Find2"} find2 = Find.objects.create(**dct) find2.documents.add(self.documents[7]) find2.base_finds.add(base_find2) @@ -925,7 +973,7 @@ class SerializationTest(GenericSerializationTest, TestCase): container_type=ContainerType.objects.all()[0], reference="Réf1", index=1, - external_id="ref1-1" + external_id="ref1-1", ) c1.documents.add(self.documents[10]) c2 = Container.objects.create( @@ -934,7 +982,7 @@ class SerializationTest(GenericSerializationTest, TestCase): container_type=ContainerType.objects.all()[0], reference="Réf2", index=2, - external_id="ref2-2" + external_id="ref2-2", ) c2.documents.add(self.documents[11]) find1.container = c1 @@ -943,30 +991,18 @@ class SerializationTest(GenericSerializationTest, TestCase): find2.container = c2 find2.container_ref = c2 find2.save() - wd1 = WarehouseDivision.objects.create( - label="Étagère", txt_idx="etagere" - ) - wd2 = WarehouseDivision.objects.create( - label="Allée", txt_idx="allee" - ) + wd1 = WarehouseDivision.objects.create(label="Étagère", txt_idx="etagere") + wd2 = WarehouseDivision.objects.create(label="Allée", txt_idx="allee") wl1 = WarehouseDivisionLink.objects.create( warehouse=w1, container_type=ContainerType.objects.all()[0], ) wl2 = WarehouseDivisionLink.objects.create( warehouse=w2, - container_type = ContainerType.objects.all()[1], - ) - ContainerLocalisation.objects.create( - container=c1, - division=wl1, - reference="A1" - ) - ContainerLocalisation.objects.create( - container=c2, - division=wl2, - reference="A2" + container_type=ContainerType.objects.all()[1], ) + ContainerLocalisation.objects.create(container=c1, division=wl1, reference="A1") + ContainerLocalisation.objects.create(container=c2, division=wl2, reference="A2") def test_base_document_serialization(self): self.create_document_default() @@ -974,71 +1010,65 @@ class SerializationTest(GenericSerializationTest, TestCase): def test_document_serialization(self): self.create_document_default() - res = self.generic_serialization_test( - document_serialization) - docs = json.loads( - res[('documents', 'ishtar_common__Document')] - ) + res = self.generic_serialization_test(document_serialization) + docs = json.loads(res[("documents", "ishtar_common__Document")]) self.assertEqual(len(docs), 12) - from archaeological_operations.models import Operation, \ - ArchaeologicalSite - result_queryset = Operation.objects.filter( - code_patriarche="66666") + from archaeological_operations.models import Operation, ArchaeologicalSite + + result_queryset = Operation.objects.filter(code_patriarche="66666") res = self.generic_serialization_test( - document_serialization, no_test=True, - kwargs={"operation_queryset": result_queryset} - ) - docs = json.loads( - res[('documents', 'ishtar_common__Document')] + document_serialization, + no_test=True, + kwargs={"operation_queryset": result_queryset}, ) + docs = json.loads(res[("documents", "ishtar_common__Document")]) self.assertEqual(len(docs), 6) result_queryset = ArchaeologicalSite.objects.filter( - id=ArchaeologicalSite.objects.all()[0].id) - res = self.generic_serialization_test( - document_serialization, no_test=True, - kwargs={"site_queryset": result_queryset} + id=ArchaeologicalSite.objects.all()[0].id ) - docs = json.loads( - res[('documents', 'ishtar_common__Document')] + res = self.generic_serialization_test( + document_serialization, + no_test=True, + kwargs={"site_queryset": result_queryset}, ) + docs = json.loads(res[("documents", "ishtar_common__Document")]) self.assertEqual(len(docs), 6) from archaeological_context_records.models import ContextRecord + result_queryset = ContextRecord.objects.filter( - id=ContextRecord.objects.all()[0].id) - res = self.generic_serialization_test( - document_serialization, no_test=True, - kwargs={"cr_queryset": result_queryset} + id=ContextRecord.objects.all()[0].id ) - docs = json.loads( - res[('documents', 'ishtar_common__Document')] + res = self.generic_serialization_test( + document_serialization, + no_test=True, + kwargs={"cr_queryset": result_queryset}, ) + docs = json.loads(res[("documents", "ishtar_common__Document")]) self.assertEqual(len(docs), 6) from archaeological_finds.models import Find - result_queryset = Find.objects.filter( - id=Find.objects.all()[0].id) + + result_queryset = Find.objects.filter(id=Find.objects.all()[0].id) res = self.generic_serialization_test( - document_serialization, no_test=True, - kwargs={"find_queryset": result_queryset} - ) - docs = json.loads( - res[('documents', 'ishtar_common__Document')] + document_serialization, + no_test=True, + kwargs={"find_queryset": result_queryset}, ) + docs = json.loads(res[("documents", "ishtar_common__Document")]) self.assertEqual(len(docs), 6) from archaeological_warehouse.models import Warehouse - result_queryset = Warehouse.objects.filter( - id=Warehouse.objects.all()[0].id) + + result_queryset = Warehouse.objects.filter(id=Warehouse.objects.all()[0].id) res = self.generic_serialization_test( - document_serialization, no_test=True, - kwargs={"warehouse_queryset": result_queryset} - ) - docs = json.loads( - res[('documents', 'ishtar_common__Document')] + document_serialization, + no_test=True, + kwargs={"warehouse_queryset": result_queryset}, ) + docs = json.loads(res[("documents", "ishtar_common__Document")]) self.assertEqual(len(docs), 6) def test_serialization_zip(self): @@ -1067,6 +1097,7 @@ class SerializationTest(GenericSerializationTest, TestCase): def test_type_restore(self): from archaeological_context_records.models import RelationType as CRRT from archaeological_operations.models import RelationType as OperationRT + cr_rel_type_nb = CRRT.objects.count() ope_rel_type_nb = OperationRT.objects.count() self.create_types() @@ -1075,75 +1106,76 @@ class SerializationTest(GenericSerializationTest, TestCase): zip_filename = type_serialization(archive=True) models.AuthorType.objects.create( - label="Am I still here", txt_idx="am-i-still-here") + label="Am I still here", txt_idx="am-i-still-here" + ) models.AuthorType.objects.filter(txt_idx="test").delete() restore_serialized(zip_filename) + self.assertEqual(models.AuthorType.objects.filter(txt_idx="test").count(), 1) self.assertEqual( - models.AuthorType.objects.filter(txt_idx="test").count(), 1) - self.assertEqual( - models.AuthorType.objects.filter(txt_idx="am-i-still-here").count(), - 1) + models.AuthorType.objects.filter(txt_idx="am-i-still-here").count(), 1 + ) self.assertEqual(cr_rel_type_nb, CRRT.objects.count()) self.assertEqual(ope_rel_type_nb, OperationRT.objects.count()) - self.assertTrue(OperationRT.objects.filter( - inverse_relation__isnull=False).count()) + self.assertTrue( + OperationRT.objects.filter(inverse_relation__isnull=False).count() + ) models.AuthorType.objects.filter(txt_idx="am-i-still-here").delete() zip_filename = type_serialization(archive=True) models.AuthorType.objects.create( - label="Am I still here", txt_idx="am-i-still-here") + label="Am I still here", txt_idx="am-i-still-here" + ) models.AuthorType.objects.filter(txt_idx="test").delete() restore_serialized(zip_filename, delete_existing=True) + self.assertEqual(models.AuthorType.objects.filter(txt_idx="test").count(), 1) self.assertEqual( - models.AuthorType.objects.filter(txt_idx="test").count(), 1) - self.assertEqual( - models.AuthorType.objects.filter(txt_idx="am-i-still-here").count(), - 0) + models.AuthorType.objects.filter(txt_idx="am-i-still-here").count(), 0 + ) self.assertEqual(cr_rel_type_nb, CRRT.objects.count()) self.assertEqual(ope_rel_type_nb, OperationRT.objects.count()) - self.assertTrue(OperationRT.objects.filter( - inverse_relation__isnull=False).count()) + self.assertTrue( + OperationRT.objects.filter(inverse_relation__isnull=False).count() + ) def test_conf_restore(self): values = self.create_default_conf() current_number, zip_filename = self.generic_restore_test_genzip( - CONF_MODEL_LIST, conf_serialization) + CONF_MODEL_LIST, conf_serialization + ) os.remove(values["document_template"].template.path) self.generic_restore_test(zip_filename, current_number, CONF_MODEL_LIST) - self.assertTrue( - os.path.isfile(values["document_template"].template.path) - ) + self.assertTrue(os.path.isfile(values["document_template"].template.path)) def test_importer_restore(self): self.create_default_importer() current_number, zip_filename = self.generic_restore_test_genzip( - IMPORT_MODEL_LIST, importer_serialization) - self.generic_restore_test(zip_filename, current_number, - IMPORT_MODEL_LIST) + IMPORT_MODEL_LIST, importer_serialization + ) + self.generic_restore_test(zip_filename, current_number, IMPORT_MODEL_LIST) def test_geo_restore(self): self.create_geo_default() self.assertTrue(models.Town.objects.get(numero_insee="12345").center) current_number, zip_filename = self.generic_restore_test_genzip( - GEO_MODEL_LIST, geo_serialization) - self.generic_restore_test(zip_filename, current_number, - GEO_MODEL_LIST) + GEO_MODEL_LIST, geo_serialization + ) + self.generic_restore_test(zip_filename, current_number, GEO_MODEL_LIST) # no geo restore self.assertFalse(models.Town.objects.get(numero_insee="12345").center) def test_directory_restore(self): self.create_directory_default() current_number, zip_filename = self.generic_restore_test_genzip( - DIRECTORY_MODEL_LIST, directory_serialization) - self.generic_restore_test(zip_filename, current_number, - DIRECTORY_MODEL_LIST) + DIRECTORY_MODEL_LIST, directory_serialization + ) + self.generic_restore_test(zip_filename, current_number, DIRECTORY_MODEL_LIST) def test_document_restore(self): self.create_document_default() current_number, zip_filename = self.generic_restore_test_genzip( - [models.Document], document_serialization) - self.generic_restore_test(zip_filename, current_number, - [models.Document]) + [models.Document], document_serialization + ) + self.generic_restore_test(zip_filename, current_number, [models.Document]) def full_create(self): self.create_types() @@ -1155,31 +1187,46 @@ class SerializationTest(GenericSerializationTest, TestCase): def test_full_restore(self): self.full_create() - model_list = get_type_models() + CONF_MODEL_LIST + IMPORT_MODEL_LIST + \ - GEO_MODEL_LIST + DIRECTORY_MODEL_LIST + OPERATION_MODEL_LIST + \ - CR_MODEL_LIST + FIND_MODEL_LIST + WAREHOUSE_MODEL_LIST + model_list = ( + get_type_models() + + CONF_MODEL_LIST + + IMPORT_MODEL_LIST + + GEO_MODEL_LIST + + DIRECTORY_MODEL_LIST + + OPERATION_MODEL_LIST + + CR_MODEL_LIST + + FIND_MODEL_LIST + + WAREHOUSE_MODEL_LIST + ) current_number, zip_filename = self.generic_restore_test_genzip( - model_list, full_serialization) - self.generic_restore_test(zip_filename, current_number, - model_list) + model_list, full_serialization + ) + self.generic_restore_test(zip_filename, current_number, model_list) def test_export_action(self): self.full_create() - model_list = get_type_models() + CONF_MODEL_LIST + IMPORT_MODEL_LIST + \ - GEO_MODEL_LIST + DIRECTORY_MODEL_LIST + OPERATION_MODEL_LIST + \ - CR_MODEL_LIST + FIND_MODEL_LIST + WAREHOUSE_MODEL_LIST + model_list = ( + get_type_models() + + CONF_MODEL_LIST + + IMPORT_MODEL_LIST + + GEO_MODEL_LIST + + DIRECTORY_MODEL_LIST + + OPERATION_MODEL_LIST + + CR_MODEL_LIST + + FIND_MODEL_LIST + + WAREHOUSE_MODEL_LIST + ) task = models.ExportTask.objects.create(state="S") launch_export(task.pk) task = models.ExportTask.objects.get(pk=task.pk) current_number = {} for model in model_list: - current_number[(model.__module__, model.__name__)] = \ - model.objects.count() - self.generic_restore_test(task.result.path, current_number, - model_list) + current_number[(model.__module__, model.__name__)] = model.objects.count() + self.generic_restore_test(task.result.path, current_number, model_list) task = models.ExportTask.objects.create( - filter_type="O", filter_text="66666", state="S") + filter_type="O", filter_text="66666", state="S" + ) current_number.update( { ("archaeological_operations.models", "ArchaeologicalSite"): 1, @@ -1195,39 +1242,38 @@ class SerializationTest(GenericSerializationTest, TestCase): ) launch_export(task.pk) task = models.ExportTask.objects.get(pk=task.pk) - self.generic_restore_test(task.result.path, current_number, - model_list) + self.generic_restore_test(task.result.path, current_number, model_list) class AccessControlTest(TestCase): def test_administrator(self): admin, created = models.PersonType.objects.get_or_create( - txt_idx='administrator', defaults={'label': 'Admin'}) - user, created = User.objects.get_or_create(username='myusername') + txt_idx="administrator", defaults={"label": "Admin"} + ) + user, created = User.objects.get_or_create(username="myusername") user.is_superuser = True user.save() - ishtar_user = models.IshtarUser.objects.get( - user_ptr__username='myusername') + ishtar_user = models.IshtarUser.objects.get(user_ptr__username="myusername") self.assertEqual( - models.UserProfile.objects.filter( - person__ishtaruser=ishtar_user, - profile_type__txt_idx='administrator' - ).count(), 1 + models.UserProfile.objects.filter( + person__ishtaruser=ishtar_user, profile_type__txt_idx="administrator" + ).count(), + 1, ) user = ishtar_user.user_ptr user.is_superuser = False user.save() self.assertEqual( models.UserProfile.objects.filter( - person__ishtaruser=ishtar_user, - profile_type__txt_idx='administrator' - ).count(), 1 + person__ishtaruser=ishtar_user, profile_type__txt_idx="administrator" + ).count(), + 1, ) # no more automatic deletion of profile for admin self.assertEqual( models.UserProfile.objects.filter( - person__ishtaruser=ishtar_user, - profile_type__txt_idx='administrator' - ).count(), 1 + person__ishtaruser=ishtar_user, profile_type__txt_idx="administrator" + ).count(), + 1, ) def test_django_admin(self): @@ -1243,7 +1289,7 @@ class AccessControlTest(TestCase): response = client.get(url) self.assertRedirects(response, "/admin/login/?next={}".format(url)) - User.objects.filter(username='myusername').update(is_staff=True) + User.objects.filter(username="myusername").update(is_staff=True) client.logout() client.login(username=username, password=password) response = client.get(url) @@ -1253,8 +1299,7 @@ class AccessControlTest(TestCase): response = client.get(url) self.assertEqual(response.status_code, 403) - user.user_permissions.add(Permission.objects.get( - codename='change_persontype')) + user.user_permissions.add(Permission.objects.get(codename="change_persontype")) client.logout() client.login(username=username, password=password) response = client.get(url) @@ -1265,76 +1310,84 @@ class UserProfileTest(TestCase): fixtures = OPERATION_FIXTURES def setUp(self): - self.password = 'mypassword' + self.password = "mypassword" self.username = "myuser" self.user = User.objects.create_superuser( - self.username, 'myemail@test.com', self.password) + self.username, "myemail@test.com", self.password + ) self.user.set_password(self.password) self.user.save() self.client = Client() self.client.login(username=self.username, password=self.password) def test_profile_edit(self): - base_url = '/profile/' + base_url = "/profile/" base_profile = self.user.ishtaruser.current_profile response = self.client.get(base_url) self.assertEqual(response.status_code, 200) response = self.client.post( - base_url, {'name': "New name", "current_profile": base_profile.pk}) + base_url, {"name": "New name", "current_profile": base_profile.pk} + ) self.assertEqual(response.status_code, 302) base_profile = models.UserProfile.objects.get(pk=base_profile.pk) - self.assertEqual( - base_profile.name, - "New name" - ) + self.assertEqual(base_profile.name, "New name") self.client.post( - base_url, {'delete_profile': True, 'name': "New name", - "current_profile": base_profile.pk}) + base_url, + { + "delete_profile": True, + "name": "New name", + "current_profile": base_profile.pk, + }, + ) self.assertEqual(response.status_code, 302) # cannot delete a profile it is the last of his kind - self.assertEqual( - self.user.ishtaruser.person.profiles.count(), - 1 - ) + self.assertEqual(self.user.ishtaruser.person.profiles.count(), 1) self.client.post( - base_url, {'name': "New name", 'duplicate_profile': True, - "current_profile": base_profile.pk}) + base_url, + { + "name": "New name", + "duplicate_profile": True, + "current_profile": base_profile.pk, + }, + ) self.assertEqual(response.status_code, 302) # duplicate - self.assertEqual( - self.user.ishtaruser.person.profiles.count(), - 2 - ) + self.assertEqual(self.user.ishtaruser.person.profiles.count(), 2) # new current profile is the duplicated new_profile = self.user.ishtaruser.current_profile base_profile = models.UserProfile.objects.get(pk=base_profile.pk) - self.assertNotEqual(base_profile.pk, - new_profile.pk) - self.assertNotEqual(base_profile.name, - new_profile.name) + self.assertNotEqual(base_profile.pk, new_profile.pk) + self.assertNotEqual(base_profile.name, new_profile.name) response = self.client.post( - base_url, {'name': "New name", "current_profile": new_profile.pk}) + base_url, {"name": "New name", "current_profile": new_profile.pk} + ) self.assertIn( - b"errorlist nonfield", response.content, - msg="An error should be isplayed as this name is already taken" + b"errorlist nonfield", + response.content, + msg="An error should be isplayed as this name is already taken", ) # the deletion can now occurs self.client.post( - base_url, {'delete_profile': True, - "current_profile": base_profile.pk}) - self.assertEqual( - self.user.ishtaruser.person.profiles.count(), - 1 + base_url, {"delete_profile": True, "current_profile": base_profile.pk} ) + self.assertEqual(self.user.ishtaruser.person.profiles.count(), 1) class AcItem: - def __init__(self, model, url, lbl_key=None, prepare_func=None, - id_key="pk", one_word_search=False, default_values=None): + def __init__( + self, + model, + url, + lbl_key=None, + prepare_func=None, + id_key="pk", + one_word_search=False, + default_values=None, + ): self.model, self.url, self.lbl_key = model, url, lbl_key self.prepare_func, self.id_key = prepare_func, id_key self.one_word_search = one_word_search @@ -1345,10 +1398,11 @@ class AcItem: class AutocompleteTestBase: def setUp(self): - self.password = 'mypassword' + self.password = "mypassword" self.username = "myuser" user = User.objects.create_superuser( - self.username, 'myemail@test.com', self.password) + self.username, "myemail@test.com", self.password + ) user.set_password(self.password) user.save() self.user = user @@ -1373,57 +1427,75 @@ class AutocompleteTestBase: item, __ = model.objects.get_or_create(**create_dict) response = self.client.get(url, {"term": search_term}) self.assertEqual( - response.status_code, 200, - msg="Status code != 200 - {}".format(url)) + response.status_code, 200, msg="Status code != 200 - {}".format(url) + ) data = json.loads(response.content.decode()) self.assertEqual( - len(data), 1, + len(data), + 1, msg="{} result for '{}' expected 1 - {}".format( - len(data), search_term, url)) + len(data), search_term, url + ), + ) self.assertEqual( - data[0]['id'], getattr(item, mdl.id_key), + data[0]["id"], + getattr(item, mdl.id_key), msg="id: {} expected {} for '{}' - {}".format( - data[0]['id'], item.pk, search_term, url)) + data[0]["id"], item.pk, search_term, url + ), + ) if mdl.one_word_search: continue search_term = "ler " + search_term response = self.client.get(url, {"term": search_term}) self.assertEqual( - response.status_code, 200, - msg="Status code != 200 when reaching {}".format(url)) + response.status_code, + 200, + msg="Status code != 200 when reaching {}".format(url), + ) data = json.loads(response.content.decode()) self.assertEqual( - len(data), 1, + len(data), + 1, msg="{} result for '{}' expected 1 - {}".format( - len(data), search_term, url)) + len(data), search_term, url + ), + ) self.assertEqual( - data[0]['id'], getattr(item, mdl.id_key), + data[0]["id"], + getattr(item, mdl.id_key), msg="id: {} expected {} for '{}' - {}".format( - data[0]['id'], item.pk, search_term, url)) + data[0]["id"], item.pk, search_term, url + ), + ) class AutocompleteTest(AutocompleteTestBase, TestCase): fixtures = OPERATION_FIXTURES models = [ - AcItem(models.User, 'autocomplete-user', "username"), - AcItem(models.User, 'autocomplete-ishtaruser', "username"), - AcItem(models.Person, 'autocomplete-person', "name"), - AcItem(models.Person, 'autocomplete-person-permissive', "name"), - AcItem(models.Town, 'autocomplete-town', "name"), - AcItem(models.Town, 'autocomplete-advanced-town', - prepare_func="create_advanced_town"), + AcItem(models.User, "autocomplete-user", "username"), + AcItem(models.User, "autocomplete-ishtaruser", "username"), + AcItem(models.Person, "autocomplete-person", "name"), + AcItem(models.Person, "autocomplete-person-permissive", "name"), + AcItem(models.Town, "autocomplete-town", "name"), + AcItem( + models.Town, + "autocomplete-advanced-town", + prepare_func="create_advanced_town", + ), AcItem(models.Department, "autocomplete-department", "label"), - AcItem(models.Author, "autocomplete-author", - prepare_func="create_author"), - AcItem(models.Organization, 'autocomplete-organization', - prepare_func="create_orga"), + AcItem(models.Author, "autocomplete-author", prepare_func="create_author"), + AcItem( + models.Organization, "autocomplete-organization", prepare_func="create_orga" + ), ] def create_advanced_town(self, base_name): town, __ = models.Town.objects.get_or_create(name=base_name) - dep, __ = models.Department.objects.get_or_create(label="Mydepartment", - number=999) + dep, __ = models.Department.objects.get_or_create( + label="Mydepartment", number=999 + ) town.departement = dep town.save() return town, "Mydepart" @@ -1431,40 +1503,53 @@ class AutocompleteTest(AutocompleteTestBase, TestCase): def create_author(self, base_name): person, __ = models.Person.objects.get_or_create(name=base_name) author, __ = models.Author.objects.get_or_create( - person=person, author_type=models.AuthorType.objects.all()[0]) + person=person, author_type=models.AuthorType.objects.all()[0] + ) return author, None def create_orga(self, base_name): orga, __ = models.Organization.objects.get_or_create( - name=base_name, - organization_type=models.OrganizationType.objects.all()[0]) + name=base_name, organization_type=models.OrganizationType.objects.all()[0] + ) return orga, None class AdminGenTypeTest(TestCase): fixtures = OPERATION_FIXTURES gen_models = [ - models.OrganizationType, models.PersonType, models.TitleType, - models.AuthorType, models.SourceType, models.OperationType, - models.SpatialReferenceSystem, models.Format, models.SupportType, + models.OrganizationType, + models.PersonType, + models.TitleType, + models.AuthorType, + models.SourceType, + models.OperationType, + models.SpatialReferenceSystem, + models.Format, + models.SupportType, ] models_with_data = gen_models + [models.ImporterModel] models = models_with_data - module_name = 'ishtar_common' + module_name = "ishtar_common" ishtar_apps = [ - 'ishtar_common', 'archaeological_files', 'archaeological_operations', - 'archaeological_context_records', 'archaeological_warehouse', - 'archaeological_finds' + "ishtar_common", + "archaeological_files", + "archaeological_operations", + "archaeological_context_records", + "archaeological_warehouse", + "archaeological_finds", + ] + readonly_models = [ + "archaeological_finds.Property", + "archaeological_finds.Treatment", + "ishtar_common.ProfileTypeSummary", ] - readonly_models = ['archaeological_finds.Property', - 'archaeological_finds.Treatment', - 'ishtar_common.ProfileTypeSummary'] def setUp(self): - self.password = 'mypassword' + self.password = "mypassword" self.username = "myuser" user = User.objects.create_superuser( - self.username, 'myemail@test.com', self.password) + self.username, "myemail@test.com", self.password + ) user.set_password(self.password) user.save() self.client = Client() @@ -1479,113 +1564,134 @@ class AdminGenTypeTest(TestCase): models.append((app, model)) for app, model in models: # quick test to verify basic access to listing - base_url = '/admin/{}/{}/'.format(app, model.__name__.lower()) + base_url = "/admin/{}/{}/".format(app, model.__name__.lower()) url = base_url response = self.client.get(url) self.assertEqual( - response.status_code, 200, - msg="Can not access admin list for {}.".format(model)) + response.status_code, + 200, + msg="Can not access admin list for {}.".format(model), + ) nb = model.objects.count() url = base_url + "add/" response = self.client.get(url) if app + "." + model.__name__ in self.readonly_models: continue self.assertEqual( - response.status_code, 200, - msg="Can not access admin add page for {}.".format(model)) + response.status_code, + 200, + msg="Can not access admin add page for {}.".format(model), + ) self.assertEqual( - nb, model.objects.count(), + nb, + model.objects.count(), msg="A ghost object have been created on access to add page " - "for {}.".format(model)) + "for {}.".format(model), + ) if nb: url = base_url + "{}/change/".format(model.objects.all()[0].pk) response = self.client.get(url) self.assertEqual( - response.status_code, 200, - msg="Can not access admin detail for {}.".format(model)) + response.status_code, + 200, + msg="Can not access admin detail for {}.".format(model), + ) def test_csv_export(self): for model in self.gen_models: - url = '/admin/{}/{}/'.format(self.module_name, - model.__name__.lower()) + url = "/admin/{}/{}/".format(self.module_name, model.__name__.lower()) q = model.objects if not q.count(): continue response = self.client.post( - url, {'action': 'export_as_csv', - '_selected_action': [str(o.pk) for o in q.all()]}) + url, + { + "action": "export_as_csv", + "_selected_action": [str(o.pk) for o in q.all()], + }, + ) self.assertEqual( - response.status_code, 200, - msg="Can not export as CSV for {}.".format(model)) + response.status_code, + 200, + msg="Can not export as CSV for {}.".format(model), + ) try: f = io.StringIO(response.content.decode()) reader = csv.DictReader(f) for row in reader: - if 'txt_idx' in row: - slug_name = 'txt_idx' - elif 'slug' in row: - slug_name = 'slug' + if "txt_idx" in row: + slug_name = "txt_idx" + elif "slug" in row: + slug_name = "slug" else: continue obj = model.objects.get(**{slug_name: row[slug_name]}) for k in row: current_value = getattr(obj, k) if not row[k]: - self.assertIn(current_value, [None, ''], + self.assertIn( + current_value, + [None, ""], msg="Export CSV for {} - {}: CSV value is " - "null whereas value for object is {}" - ".".format(model, k, current_value)) + "null whereas value for object is {}" + ".".format(model, k, current_value), + ) continue field = model._meta.get_field(k) if isinstance(field, BooleanField): if current_value: self.assertEqual( - row[k], 'True', + row[k], + "True", msg="Export CSV for {} - {}: CSV value is " - "{} whereas value for " - "object is True.".format(model, k, - row[k])) + "{} whereas value for " + "object is True.".format(model, k, row[k]), + ) continue else: self.assertEqual( - row[k], 'False', + row[k], + "False", msg="Export CSV for {} - {}: CSV value is " - "{} whereas value for " - "object is False.".format( - model, k, row[k])) + "{} whereas value for " + "object is False.".format(model, k, row[k]), + ) continue elif isinstance(field, ForeignKey): fmodel = field.rel.to try: - value = fmodel.objects.get( - **{slug_name: row[k]} - ) + value = fmodel.objects.get(**{slug_name: row[k]}) except fmodel.DoesNotExist: - msg = "Export CSV for {} - {}: CSV value is "\ - "{} but it is not a vaild slug for {}" \ - ".".format(model, k, row[k], fmodel) + msg = ( + "Export CSV for {} - {}: CSV value is " + "{} but it is not a vaild slug for {}" + ".".format(model, k, row[k], fmodel) + ) raise ValidationError(msg) self.assertEqual( - value, current_value, + value, + current_value, msg="Export CSV for {} - {}: CSV value is " - "{} whereas value for " - "object is {}.".format( - model, k, value, current_value)) + "{} whereas value for " + "object is {}.".format(model, k, value, current_value), + ) elif type(current_value) == float: self.assertEqual( - float(row[k]), current_value, + float(row[k]), + current_value, msg="Export CSV for {} - {}: CSV value is " - "{} whereas value for " - "object is {}.".format( - model, k, row[k], current_value)) + "{} whereas value for " + "object is {}.".format(model, k, row[k], current_value), + ) elif type(current_value) == int: self.assertEqual( - int(row[k]), current_value, + int(row[k]), + current_value, msg="Export CSV for {} - {}: CSV value is " - "{} whereas value for " - "object is {}.".format( - model, k, row[k], current_value)) + "{} whereas value for " + "object is {}.".format(model, k, row[k], current_value), + ) finally: f.close() @@ -1596,22 +1702,27 @@ class AdminGenTypeTest(TestCase): nb = q.count() if not nb: continue - base_url = '/admin/{}/{}/'.format(self.module_name, - model.__name__.lower()) + base_url = "/admin/{}/{}/".format(self.module_name, model.__name__.lower()) response = self.client.post( - base_url, {'action': 'export_as_csv', - '_selected_action': [str(o.pk) for o in q.all()]}) + base_url, + { + "action": "export_as_csv", + "_selected_action": [str(o.pk) for o in q.all()], + }, + ) self.assertEqual( - response.status_code, 200, - msg="Can not export as CSV for {}.".format(model)) + response.status_code, + 200, + msg="Can not export as CSV for {}.".format(model), + ) for obj in q.all(): obj.delete() - url = base_url + 'import-from-csv/' + url = base_url + "import-from-csv/" try: f = io.BytesIO(response.content) - response = self.client.post(url, {'csv_file': f, 'apply': True}) + response = self.client.post(url, {"csv_file": f, "apply": True}) self.assertEqual(response.status_code, 302) self.assertRedirects(response, base_url) self.assertEqual(nb, model.objects.count()) @@ -1620,17 +1731,22 @@ class AdminGenTypeTest(TestCase): def test_json_export(self): for model in self.gen_models: - url = '/admin/{}/{}/'.format(self.module_name, - model.__name__.lower()) + url = "/admin/{}/{}/".format(self.module_name, model.__name__.lower()) q = model.objects if not q.count(): continue response = self.client.post( - url, {'action': '_serialize_action', - '_selected_action': [str(o.pk) for o in q.all()]}) + url, + { + "action": "_serialize_action", + "_selected_action": [str(o.pk) for o in q.all()], + }, + ) self.assertEqual( - response.status_code, 200, - msg="Can not export as JSON for {}.".format(model)) + response.status_code, + 200, + msg="Can not export as JSON for {}.".format(model), + ) # json content already tested on full export def test_json_import(self): @@ -1639,23 +1755,27 @@ class AdminGenTypeTest(TestCase): nb = q.count() if not nb: continue - base_url = '/admin/{}/{}/'.format(self.module_name, - model.__name__.lower()) + base_url = "/admin/{}/{}/".format(self.module_name, model.__name__.lower()) response = self.client.post( - base_url, {'action': '_serialize_action', - '_selected_action': [str(o.pk) for o in q.all()]}) + base_url, + { + "action": "_serialize_action", + "_selected_action": [str(o.pk) for o in q.all()], + }, + ) self.assertEqual( - response.status_code, 200, - msg="Can not export as CSV for {}.".format(model)) + response.status_code, + 200, + msg="Can not export as CSV for {}.".format(model), + ) for obj in q.all(): obj.delete() - url = base_url + 'import-from-json/' + url = base_url + "import-from-json/" try: f = io.BytesIO(response.content) - response = self.client.post(url, {'json_file': f, - 'apply': True}) + response = self.client.post(url, {"json_file": f, "apply": True}) self.assertEqual(response.status_code, 302) self.assertRedirects(response, base_url) self.assertEqual(nb, model.objects.count()) @@ -1664,71 +1784,67 @@ class AdminGenTypeTest(TestCase): def test_importer_type_duplicate(self): model = models.ImporterType - base_url = '/admin/{}/{}/'.format(self.module_name, - model.__name__.lower()) + base_url = "/admin/{}/{}/".format(self.module_name, model.__name__.lower()) ref = model.objects.all()[0] nb = model.objects.count() response = self.client.post( - base_url, {'action': 'duplicate_importertype', - '_selected_action': [str(ref.pk)]}) + base_url, + {"action": "duplicate_importertype", "_selected_action": [str(ref.pk)]}, + ) self.assertEqual(response.status_code, 302) self.assertEqual(nb + 1, model.objects.count()) - duplicate = model.objects.order_by('-pk').all()[0] - self.assertEqual(duplicate.columns.count(), - ref.columns.count()) + duplicate = model.objects.order_by("-pk").all()[0] + self.assertEqual(duplicate.columns.count(), ref.columns.count()) def test_importer_column_duplicate(self): model = models.ImporterColumn - base_url = '/admin/{}/{}/'.format(self.module_name, - model.__name__.lower()) + base_url = "/admin/{}/{}/".format(self.module_name, model.__name__.lower()) ref = model.objects.all()[0] nb = model.objects.count() response = self.client.post( - base_url, {'action': 'duplicate_importercolumn', - '_selected_action': [str(ref.pk)]}) + base_url, + {"action": "duplicate_importercolumn", "_selected_action": [str(ref.pk)]}, + ) self.assertEqual(response.status_code, 302) self.assertEqual(nb + 1, model.objects.count()) - duplicate = model.objects.order_by('-pk').all()[0] - self.assertEqual(duplicate.duplicate_fields.count(), - ref.duplicate_fields.count()) - self.assertEqual(duplicate.targets.count(), - ref.targets.count()) + duplicate = model.objects.order_by("-pk").all()[0] + self.assertEqual( + duplicate.duplicate_fields.count(), ref.duplicate_fields.count() + ) + self.assertEqual(duplicate.targets.count(), ref.targets.count()) def test_importer_column_shift(self): model = models.ImporterColumn - importer_type = models.ImporterType.objects.get( - slug='ishtar-operations') + importer_type = models.ImporterType.objects.get(slug="ishtar-operations") # col in fixture should be well ordered - for idx, col in enumerate( - importer_type.columns.order_by('col_number').all()): + for idx, col in enumerate(importer_type.columns.order_by("col_number").all()): self.assertEqual(col.col_number, idx + 1) - base_url = '/admin/{}/{}/'.format(self.module_name, - model.__name__.lower()) + base_url = "/admin/{}/{}/".format(self.module_name, model.__name__.lower()) response = self.client.post( - base_url, { - 'action': 'shift_right', - '_selected_action': [ - str(c.pk) for c in importer_type.columns.all() - ]}) + base_url, + { + "action": "shift_right", + "_selected_action": [str(c.pk) for c in importer_type.columns.all()], + }, + ) self.assertEqual(response.status_code, 302) # col shifted to the right - for idx, col in enumerate( - importer_type.columns.order_by('col_number').all()): + for idx, col in enumerate(importer_type.columns.order_by("col_number").all()): self.assertEqual(col.col_number, idx + 2) response = self.client.post( - base_url, { - 'action': 'shift_left', - '_selected_action': [ - str(c.pk) for c in importer_type.columns.all() - ]}) + base_url, + { + "action": "shift_left", + "_selected_action": [str(c.pk) for c in importer_type.columns.all()], + }, + ) self.assertEqual(response.status_code, 302) # col shifted back to the left - for idx, col in enumerate( - importer_type.columns.order_by('col_number').all()): + for idx, col in enumerate(importer_type.columns.order_by("col_number").all()): self.assertEqual(col.col_number, idx + 1) def test_str(self): @@ -1737,88 +1853,102 @@ class AdminGenTypeTest(TestCase): self.assertTrue(str(model.objects.all()[0])) def test_user_creation(self): - url = '/admin/auth/user/add/' - password = 'ishtar is the queen' + url = "/admin/auth/user/add/" + password = "ishtar is the queen" response = self.client.post( - url, {'username': 'test', 'password1': password, - 'password2': password}) + url, {"username": "test", "password1": password, "password2": password} + ) self.assertEqual(response.status_code, 302) - self.assertTrue(self.client.login(username='test', password=password)) + self.assertTrue(self.client.login(username="test", password=password)) class MergeTest(TestCase): def setUp(self): - self.user, created = User.objects.get_or_create(username='username') - self.organisation_types = \ - models.OrganizationType.create_default_for_test() + self.user, created = User.objects.get_or_create(username="username") + self.organisation_types = models.OrganizationType.create_default_for_test() - self.person_types = [models.PersonType.objects.create(label='Admin'), - models.PersonType.objects.create(label='User')] - self.author_types = [models.AuthorType.objects.create(label='1'), - models.AuthorType.objects.create(label='2')] + self.person_types = [ + models.PersonType.objects.create(label="Admin"), + models.PersonType.objects.create(label="User"), + ] + self.author_types = [ + models.AuthorType.objects.create(label="1"), + models.AuthorType.objects.create(label="2"), + ] self.company_1 = models.Organization.objects.create( - history_modifier=self.user, name='Franquin Comp.', - organization_type=self.organisation_types[0]) + history_modifier=self.user, + name="Franquin Comp.", + organization_type=self.organisation_types[0], + ) self.person_1 = models.Person.objects.create( - name='Boule', surname=' ', history_modifier=self.user, - attached_to=self.company_1) + name="Boule", + surname=" ", + history_modifier=self.user, + attached_to=self.company_1, + ) self.person_1.person_types.add(self.person_types[0]) self.author_1_pk = models.Author.objects.create( - person=self.person_1, author_type=self.author_types[0]).pk + person=self.person_1, author_type=self.author_types[0] + ).pk - self.title = models.TitleType.objects.create(label='Test') + self.title = models.TitleType.objects.create(label="Test") self.company_2 = models.Organization.objects.create( - history_modifier=self.user, name='Goscinny Corp.', - organization_type=self.organisation_types[1]) + history_modifier=self.user, + name="Goscinny Corp.", + organization_type=self.organisation_types[1], + ) self.person_2 = models.Person.objects.create( - name='Bill', history_modifier=self.user, surname='Peyo', - title=self.title, attached_to=self.company_2) + name="Bill", + history_modifier=self.user, + surname="Peyo", + title=self.title, + attached_to=self.company_2, + ) self.user.ishtaruser.person = self.person_2 self.user.ishtaruser.save() models.UserProfile.objects.create( - profile_type=models.ProfileType.objects.all()[0], - person=self.person_2 + profile_type=models.ProfileType.objects.all()[0], person=self.person_2 ) self.person_2.person_types.add(self.person_types[1]) self.author_2_pk = models.Author.objects.create( - person=self.person_2, author_type=self.author_types[1]).pk + person=self.person_2, author_type=self.author_types[1] + ).pk self.person_3 = models.Person.objects.create( - name='George', history_modifier=self.user, - attached_to=self.company_1) + name="George", history_modifier=self.user, attached_to=self.company_1 + ) def test_person_merge(self): self.person_1.merge(self.person_2) # preserve existing fields - self.assertEqual(self.person_1.name, 'Boule') + self.assertEqual(self.person_1.name, "Boule") # fill missing fields self.assertEqual(self.person_1.title, self.title) # string field with only spaces is an empty field - self.assertEqual(self.person_1.surname, 'Peyo') + self.assertEqual(self.person_1.surname, "Peyo") # preserve one to one field - user = User.objects.get(username='username') + user = User.objects.get(username="username") self.assertEqual(self.person_1, user.ishtaruser.person) # preserve existing foreign key self.assertEqual(self.person_1.attached_to, self.company_1) # preserve existing many to many - self.assertTrue(self.person_types[0] - in self.person_1.person_types.all()) + self.assertTrue(self.person_types[0] in self.person_1.person_types.all()) # add new many to many - self.assertTrue(self.person_types[1] - in self.person_1.person_types.all()) + self.assertTrue(self.person_types[1] in self.person_1.person_types.all()) # update reverse foreign key association and do not break the existing - self.assertEqual(models.Author.objects.get(pk=self.author_1_pk).person, - self.person_1) - self.assertEqual(models.Author.objects.get(pk=self.author_2_pk).person, - self.person_1) + self.assertEqual( + models.Author.objects.get(pk=self.author_1_pk).person, self.person_1 + ) + self.assertEqual( + models.Author.objects.get(pk=self.author_2_pk).person, self.person_1 + ) self.person_3.merge(self.person_1) # manage well empty many to many fields - self.assertTrue(self.person_types[1] - in self.person_3.person_types.all()) + self.assertTrue(self.person_types[1] in self.person_3.person_types.all()) def test_person_with_use_account_merge(self): # bug: merge when the target is not the one having a Ishtar user account @@ -1828,23 +1958,25 @@ class MergeTest(TestCase): init_mc = self.person_1.merge_candidate.count() person = models.Person.objects.create( name=self.person_1.name, - surname=self.person_1.surname, history_modifier=self.user, - attached_to=self.person_1.attached_to) - self.assertEqual(self.person_1.merge_candidate.count(), - init_mc + 1) + surname=self.person_1.surname, + history_modifier=self.user, + attached_to=self.person_1.attached_to, + ) + self.assertEqual(self.person_1.merge_candidate.count(), init_mc + 1) person.archive() - self.assertEqual(self.person_1.merge_candidate.count(), - init_mc) + self.assertEqual(self.person_1.merge_candidate.count(), init_mc) class ShortMenuTest(TestCase): def setUp(self): - self.username = 'username666' - self.password = 'dcbqj7xnjkxnjsknx!@%' + self.username = "username666" + self.password = "dcbqj7xnjkxnjsknx!@%" self.user = User.objects.create_superuser( - self.username, "nomail@nomail.com", self.password) + self.username, "nomail@nomail.com", self.password + ) self.other_user = User.objects.create_superuser( - 'John', "nomail@nomail.com", self.password) + "John", "nomail@nomail.com", self.password + ) profile = models.get_current_profile() profile.files = True profile.context_record = True @@ -1856,6 +1988,7 @@ class ShortMenuTest(TestCase): if not user: user = self.other_user from archaeological_operations.models import Operation, OperationType + ope_type, created = OperationType.objects.get_or_create(label="test") idx = 1 while Operation.objects.filter(code_patriarche=str(idx)).count(): @@ -1863,23 +1996,24 @@ class ShortMenuTest(TestCase): return Operation.objects.create( operation_type=ope_type, history_modifier=user, - year=2042, operation_code=54, - code_patriarche=str(idx) + year=2042, + operation_code=54, + code_patriarche=str(idx), ) def test_not_connected(self): c = Client() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) # no content if not logged self.assertFalse(b"shortcut-menu" in response.content) c = Client() c.login(username=self.username, password=self.password) # no content because the user owns no object - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertFalse(b"shortcut-menu" in response.content) self._create_ope(user=self.user) # content is here - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertTrue(b"shortcut-menu" in response.content) def test_operation(self): @@ -1887,14 +2021,14 @@ class ShortMenuTest(TestCase): c.login(username=self.username, password=self.password) ope = self._create_ope() # not available at first - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(ope.cached_label) in response.content.decode()) # available because is the creator ope.history_creator = self.user ope.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(ope.cached_label) in response.content.decode()) @@ -1902,7 +2036,7 @@ class ShortMenuTest(TestCase): ope.history_creator = self.other_user ope.in_charge = self.user.ishtaruser.person ope.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(ope.cached_label) in response.content.decode()) @@ -1911,14 +2045,14 @@ class ShortMenuTest(TestCase): ope.in_charge = None ope.scientist = self.user.ishtaruser.person ope.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(ope.cached_label) in response.content.decode()) # end date is reached - no more available ope.end_date = datetime.date(1900, 1, 1) ope.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(ope.cached_label) in response.content.decode()) @@ -1931,12 +2065,13 @@ class ShortMenuTest(TestCase): session = c.session session[ope.SLUG] = ope.pk session.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(ope.cached_label) in response.content.decode()) def testFile(self): from archaeological_files.models import File, FileType + c = Client() c.login(username=self.username, password=self.password) file_type = FileType.objects.create() @@ -1946,14 +2081,14 @@ class ShortMenuTest(TestCase): year=2043, ) # not available at first - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(fle.cached_label) in response.content.decode()) # available because is the creator fle.history_creator = self.user fle.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(fle.cached_label) in response.content.decode()) @@ -1961,27 +2096,25 @@ class ShortMenuTest(TestCase): fle.history_creator = self.other_user fle.in_charge = self.user.ishtaruser.person fle.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(fle.cached_label) in response.content.decode()) # end date is reached - no more available fle.end_date = datetime.date(1900, 1, 1) fle.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(fle.cached_label) in response.content.decode()) def _create_cr(self): from archaeological_context_records.models import ContextRecord from archaeological_operations.models import Parcel + ope = self._create_ope() town = models.Town.objects.create() parcel = Parcel.objects.create( - operation=ope, - town=town, - section="AA", - parcel_number=42 + operation=ope, town=town, section="AA", parcel_number=42 ) return ContextRecord.objects.create( parcel=parcel, @@ -1995,14 +2128,14 @@ class ShortMenuTest(TestCase): cr = self._create_cr() # not available at first - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(cr.cached_label) in response.content.decode()) # available because is the creator cr.history_creator = self.user cr.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(cr.cached_label) in response.content.decode()) @@ -2011,7 +2144,7 @@ class ShortMenuTest(TestCase): cr.save() cr.operation.in_charge = self.user.ishtaruser.person cr.operation.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(cr.cached_label) in response.content.decode()) @@ -2021,19 +2154,16 @@ class ShortMenuTest(TestCase): cr.operation.in_charge = None cr.operation.scientist = self.user.ishtaruser.person cr.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(cr.cached_label) in response.content.decode()) def _create_find(self): from archaeological_finds.models import BaseFind, Find + cr = self._create_cr() - base_find = BaseFind.objects.create( - context_record=cr - ) - find = Find.objects.create( - label="Where is my find?" - ) + base_find = BaseFind.objects.create(context_record=cr) + find = Find.objects.create(label="Where is my find?") find.base_finds.add(base_find) return base_find, find @@ -2043,24 +2173,23 @@ class ShortMenuTest(TestCase): base_find, find = self._create_find() # not available at first - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(find.cached_label) in response.content.decode()) # available because is the creator find.history_creator = self.user find.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(find.cached_label) in response.content.decode()) # available because is in charge find.history_creator = self.other_user find.save() - base_find.context_record.operation.in_charge = \ - self.user.ishtaruser.person + base_find.context_record.operation.in_charge = self.user.ishtaruser.person base_find.context_record.operation.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(find.cached_label) in response.content.decode()) @@ -2068,10 +2197,9 @@ class ShortMenuTest(TestCase): find.history_creator = self.other_user find.save() base_find.context_record.operation.in_charge = None - base_find.context_record.operation.scientist = \ - self.user.ishtaruser.person + base_find.context_record.operation.scientist = self.user.ishtaruser.person base_find.context_record.operation.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(find.cached_label) in response.content.decode()) @@ -2079,34 +2207,34 @@ class ShortMenuTest(TestCase): c = Client() c.login(username=self.username, password=self.password) from archaeological_finds.models import FindBasket + basket = FindBasket.objects.create( label="My basket", ) # not available at first - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(basket.label) in response.content.decode()) # available because is the owner basket.user = self.user.ishtaruser basket.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(basket.label) in response.content.decode()) def test_treatment_file(self): c = Client() c.login(username=self.username, password=self.password) - from archaeological_finds.models import TreatmentFile, \ - TreatmentFileType + from archaeological_finds.models import TreatmentFile, TreatmentFileType + tf = TreatmentFile.objects.create( - type=TreatmentFileType.objects.create(), - year=2050 + type=TreatmentFileType.objects.create(), year=2050 ) # not available at first - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(tf.cached_label) in response.content.decode()) @@ -2114,7 +2242,7 @@ class ShortMenuTest(TestCase): tf.history_creator = self.user tf.save() tf = TreatmentFile.objects.get(pk=tf.pk) - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(tf.cached_label) in response.content.decode()) @@ -2122,26 +2250,25 @@ class ShortMenuTest(TestCase): tf.history_creator = self.other_user tf.in_charge = self.user.ishtaruser.person tf.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(tf.cached_label) in response.content.decode()) # end date is reached - no more available tf.end_date = datetime.date(1900, 1, 1) tf.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(tf.cached_label) in response.content.decode()) def _create_treatment(self): from archaeological_finds.models import Treatment, TreatmentState + completed, created = TreatmentState.objects.get_or_create( - txt_idx='completed', defaults={"executed": True, "label": "Done"} + txt_idx="completed", defaults={"executed": True, "label": "Done"} ) return Treatment.objects.create( - label="My treatment", - year=2052, - treatment_state=completed + label="My treatment", year=2052, treatment_state=completed ) def test_treatment(self): @@ -2150,14 +2277,14 @@ class ShortMenuTest(TestCase): treat = self._create_treatment() # not available at first - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(treat.cached_label) in response.content.decode()) # available because is the creator treat.history_creator = self.user treat.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(treat.cached_label) in response.content.decode()) @@ -2165,14 +2292,14 @@ class ShortMenuTest(TestCase): treat.history_creator = self.other_user treat.person = self.user.ishtaruser.person treat.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertTrue(str(treat.cached_label) in response.content.decode()) # end date is reached - no more available treat.end_date = datetime.date(1900, 1, 1) treat.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self.assertFalse(str(treat.cached_label) in response.content.decode()) @@ -2182,55 +2309,52 @@ class ShortMenuTest(TestCase): base_find, find = self._create_find() response = c.post( - reverse('pin-search', args=['find']), - {'value': 'Where is my find'}, - **{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'} + reverse("pin-search", args=["find"]), + {"value": "Where is my find"}, + **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"} ) self.assertEqual(response.status_code, 200) # the selected find search is pined - self.assertEqual(c.session['pin-search-find'], 'Where is my find') + self.assertEqual(c.session["pin-search-find"], "Where is my find") # empty search save means empty dependant search - c.get( - reverse('pin', args=['contextrecord', - str(base_find.context_record.pk)]) - ) + c.get(reverse("pin", args=["contextrecord", str(base_find.context_record.pk)])) response = c.post( - reverse('pin-search', args=['find']), - {'value': ''}, - **{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'} + reverse("pin-search", args=["find"]), + {"value": ""}, + **{"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"} ) self.assertEqual(response.status_code, 200) - self.assertEqual(c.session['pin-search-find'], '') - self.assertEqual(c.session['contextrecord'], '') + self.assertEqual(c.session["pin-search-find"], "") + self.assertEqual(c.session["contextrecord"], "") def test_update_current_item(self): c = Client() c.login(username=self.username, password=self.password) base_find, find = self._create_find() - response = c.get(reverse('pin', args=['find', find.pk])) + response = c.get(reverse("pin", args=["find", find.pk])) self.assertEqual(response.status_code, 200) # the selected find is pined - self.assertEqual(c.session['find'], str(find.pk)) + self.assertEqual(c.session["find"], str(find.pk)) # dependant items are also pined - self.assertEqual(c.session['contextrecord'], - str(base_find.context_record.pk)) - self.assertEqual(c.session['operation'], - str(base_find.context_record.operation.pk)) + self.assertEqual(c.session["contextrecord"], str(base_find.context_record.pk)) + self.assertEqual( + c.session["operation"], str(base_find.context_record.operation.pk) + ) # pin another operation - dependant items are nullify ope = self._create_ope() - response = c.get(reverse('pin', args=['operation', ope.pk])) + response = c.get(reverse("pin", args=["operation", ope.pk])) self.assertEqual(response.status_code, 200) - self.assertFalse(c.session['find']) - self.assertFalse(c.session['contextrecord']) + self.assertFalse(c.session["find"]) + self.assertFalse(c.session["contextrecord"]) # current find is set as an integer session = c.session - session['find'] = find.id + session["find"] = find.id session.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) self._create_treatment() @@ -2239,16 +2363,14 @@ class ShortMenuTest(TestCase): c = Client() c.login(username=self.username, password=self.password) from archaeological_finds.models import FindBasket - basket = FindBasket.objects.create( - label="My basket", - user=self.user.ishtaruser - ) + + basket = FindBasket.objects.create(label="My basket", user=self.user.ishtaruser) session = c.session - session['find'] = 'basket-{}'.format(basket.pk) + session["find"] = "basket-{}".format(basket.pk) session.save() - response = c.get(reverse('shortcut-menu')) + response = c.get(reverse("shortcut-menu")) self.assertEqual(response.status_code, 200) - response = c.get(reverse('get-document')) + response = c.get(reverse("get-document")) self.assertEqual(response.status_code, 200) @@ -2256,22 +2378,23 @@ class ImportTest(TestCase): def create_import(self): create_user() imp_model = models.ImporterModel.objects.create( - klass='ishtar_common.models.Person', name='Person') - importer_type = models.ImporterType.objects.create( - associated_models=imp_model) + klass="ishtar_common.models.Person", name="Person" + ) + importer_type = models.ImporterType.objects.create(associated_models=imp_model) dest = os.path.join(settings.MEDIA_ROOT, "MCC-operations-example.csv") shutil.copy( - settings.ROOT_PATH + - '../archaeological_operations/tests/MCC-operations-example.csv', - dest + settings.ROOT_PATH + + "../archaeological_operations/tests/MCC-operations-example.csv", + dest, ) - with open(dest, 'rb') as f: + with open(dest, "rb") as f: mcc_operation_file = DjangoFile(f) imprt = models.Import.objects.create( user=models.IshtarUser.objects.all()[0], importer_type=importer_type, - imported_file=mcc_operation_file) + imported_file=mcc_operation_file, + ) return imprt def test_archive_import(self): @@ -2279,8 +2402,7 @@ class ImportTest(TestCase): with open(imprt.imported_file.path, "r") as f: csv_content = f.read() with tempfile.TemporaryDirectory() as tmpdir: - for k in ("error_file", "result_file", "match_file", - "imported_images"): + for k in ("error_file", "result_file", "match_file", "imported_images"): sample_file = os.path.join(tmpdir, "media_{}.zip".format(k)) with open(sample_file, "w") as m: m.write("test" + k) @@ -2299,7 +2421,7 @@ class ImportTest(TestCase): self.assertTrue(imprt.archive_file) self.assertTrue(zipfile.is_zipfile(imprt.archive_file)) with tempfile.TemporaryDirectory() as tmpdir: - current_zip = zipfile.ZipFile(imprt.archive_file.path, 'r') + current_zip = zipfile.ZipFile(imprt.archive_file.path, "r") name_list = current_zip.namelist() self.assertIn("content.json", name_list) current_zip.extract("content.json", tmpdir) @@ -2324,7 +2446,7 @@ class ImportTest(TestCase): with open(os.path.join(tmpdir, name), "r") as f: self.assertEqual(f.read(), csv_content) - imprt.unarchive('FE') + imprt.unarchive("FE") imprt = models.Import.objects.get(pk=imprt.pk) self.assertEqual(imprt.state, "FE") for k in ("error_file", "result_file", "match_file", "imported_images"): @@ -2353,43 +2475,53 @@ class ImportTest(TestCase): self.assertFalse(imprt.imported_images) def test_delete_related(self): - town = models.Town.objects.create(name='my-test') - self.assertEqual(models.Town.objects.filter(name='my-test').count(), 1) + town = models.Town.objects.create(name="my-test") + self.assertEqual(models.Town.objects.filter(name="my-test").count(), 1) imprt = self.create_import() town.imports.add(imprt) imprt.delete() # town should be deleted - self.assertEqual(models.Town.objects.filter(name='my-test').count(), - 0) + self.assertEqual(models.Town.objects.filter(name="my-test").count(), 0) def test_keys(self): - content_type = ContentType.objects.get_for_model( - models.OrganizationType) + content_type = ContentType.objects.get_for_model(models.OrganizationType) # creation label = "Ploufé" ot = models.OrganizationType.objects.create(label=label) - self.assertEqual(models.ItemKey.objects.filter( - object_id=ot.pk, key=slugify(label), - content_type=content_type).count(), 1) + self.assertEqual( + models.ItemKey.objects.filter( + object_id=ot.pk, key=slugify(label), content_type=content_type + ).count(), + 1, + ) label_2 = "Plif" ot_2 = models.OrganizationType.objects.create(label=label_2) - self.assertEqual(models.ItemKey.objects.filter( - object_id=ot_2.pk, key=slugify(label_2), - content_type=content_type).count(), 1) + self.assertEqual( + models.ItemKey.objects.filter( + object_id=ot_2.pk, key=slugify(label_2), content_type=content_type + ).count(), + 1, + ) # replace key ot_2.add_key(slugify(label), force=True) # one key point to only one item - self.assertEqual(models.ItemKey.objects.filter( - key=slugify(label), - content_type=content_type).count(), 1) + self.assertEqual( + models.ItemKey.objects.filter( + key=slugify(label), content_type=content_type + ).count(), + 1, + ) # this key point to the right item - self.assertEqual(models.ItemKey.objects.filter( - object_id=ot_2.pk, key=slugify(label), - content_type=content_type).count(), 1) + self.assertEqual( + models.ItemKey.objects.filter( + object_id=ot_2.pk, key=slugify(label), content_type=content_type + ).count(), + 1, + ) # modification label_3 = "Yop" @@ -2397,32 +2529,43 @@ class ImportTest(TestCase): ot_2.txt_idx = slugify(label_3) ot_2.save() # old label not referenced anymore - self.assertEqual(models.ItemKey.objects.filter( - object_id=ot_2.pk, key=slugify(label_2), - content_type=content_type).count(), 0) + self.assertEqual( + models.ItemKey.objects.filter( + object_id=ot_2.pk, key=slugify(label_2), content_type=content_type + ).count(), + 0, + ) # # forced key association is always here # new key is here - self.assertEqual(models.ItemKey.objects.filter( - object_id=ot_2.pk, key=slugify(label), - content_type=content_type).count(), 1) - self.assertEqual(models.ItemKey.objects.filter( - object_id=ot_2.pk, key=slugify(label_3), - content_type=content_type).count(), 1) + self.assertEqual( + models.ItemKey.objects.filter( + object_id=ot_2.pk, key=slugify(label), content_type=content_type + ).count(), + 1, + ) + self.assertEqual( + models.ItemKey.objects.filter( + object_id=ot_2.pk, key=slugify(label_3), content_type=content_type + ).count(), + 1, + ) class IshtarSiteProfileTest(TestCase): - fixtures = [settings.ROOT_PATH + - '../fixtures/initial_data-auth-fr.json', - settings.ROOT_PATH + - '../ishtar_common/fixtures/initial_data-fr.json',] + fixtures = [ + settings.ROOT_PATH + "../fixtures/initial_data-auth-fr.json", + settings.ROOT_PATH + "../ishtar_common/fixtures/initial_data-fr.json", + ] def testRelevance(self): - cache.set('default-ishtarsiteprofile-is-current-profile', None, - settings.CACHE_TIMEOUT) + cache.set( + "default-ishtarsiteprofile-is-current-profile", None, settings.CACHE_TIMEOUT + ) profile = models.get_current_profile() default_slug = profile.slug profile2 = models.IshtarSiteProfile.objects.create( - label="Test profile 2", slug='test-profile-2') + label="Test profile 2", slug="test-profile-2" + ) profile2.save() # when no profile is the current, activate by default the first created self.assertTrue(profile.active and not profile2.active) @@ -2443,52 +2586,53 @@ class IshtarSiteProfileTest(TestCase): self.assertTrue(profile2.context_record and profile2.find) def testDefaultProfile(self): - cache.set('default-ishtar_common-IshtarSiteProfile', None, - settings.CACHE_TIMEOUT) + cache.set( + "default-ishtar_common-IshtarSiteProfile", None, settings.CACHE_TIMEOUT + ) self.assertFalse(models.IshtarSiteProfile.objects.count()) profile = models.get_current_profile(force=True) self.assertTrue(profile) self.assertEqual(models.IshtarSiteProfile.objects.count(), 1) def test_menu_filtering(self): - cache.set('default-ishtarsiteprofile-is-current-profile', None, - settings.CACHE_TIMEOUT) - username = 'username4277' - password = 'dcbqj756456!@%' - User.objects.create_superuser(username, "nomail@nomail.com", - password) + cache.set( + "default-ishtarsiteprofile-is-current-profile", None, settings.CACHE_TIMEOUT + ) + username = "username4277" + password = "dcbqj756456!@%" + User.objects.create_superuser(username, "nomail@nomail.com", password) c = Client() c.login(username=username, password=password) - response = c.get(reverse('start')) + response = c.get(reverse("start")) self.assertNotIn(b'href="/file_search/"', response.content) profile = models.get_current_profile() profile.files = True profile.save() - response = c.get(reverse('start')) + response = c.get(reverse("start")) self.assertIn(b'href="/file_search/"', response.content) def testExternalKey(self): profile = models.get_current_profile() - p = models.Person.objects.create(name='plouf', surname='Tégada') + p = models.Person.objects.create(name="plouf", surname="Tégada") self.assertEqual(p.raw_name, "PLOUF Tégada") - profile.person_raw_name = '{surname|slug} {name}' + profile.person_raw_name = "{surname|slug} {name}" profile.save() - p.raw_name = '' + p.raw_name = "" p.save() self.assertEqual(p.raw_name, "tegada plouf") class IshtarBasicTest(TestCase): def setUp(self): - self.password = 'mypassword' + self.password = "mypassword" self.my_admin = User.objects.create_superuser( - 'myuser', 'myemail@test.com', self.password) + "myuser", "myemail@test.com", self.password + ) self.client = Client() - self.client.login(username=self.my_admin.username, - password=self.password) + self.client.login(username=self.my_admin.username, password=self.password) def test_status(self): - response = self.client.get(reverse('status')) + response = self.client.get(reverse("status")) self.assertEqual(response.status_code, 200) def test_person_rawname(self): @@ -2501,28 +2645,29 @@ class IshtarBasicTest(TestCase): def test_show(self): person = models.Person.objects.create(name="Weasley", surname="Bill") orga_type = models.OrganizationType.objects.create( - txt_idx='test', label='testy') + txt_idx="test", label="testy" + ) company = models.Organization.objects.create( - history_modifier=self.my_admin, name='Franquin Comp.', - organization_type=orga_type) + history_modifier=self.my_admin, + name="Franquin Comp.", + organization_type=orga_type, + ) c = Client() - response = c.get(reverse('show-person', kwargs={'pk': person.pk})) + response = c.get(reverse("show-person", kwargs={"pk": person.pk})) self.assertEqual(response.status_code, 200) # empty content when not allowed self.assertEqual(response.content, b"") - response = c.get(reverse('show-organization', - kwargs={'pk': company.pk})) + response = c.get(reverse("show-organization", kwargs={"pk": company.pk})) self.assertEqual(response.status_code, 200) # empty content when not allowed self.assertEqual(response.content, b"") c.login(username=self.my_admin.username, password=self.password) - response = c.get(reverse('show-person', kwargs={'pk': person.pk})) + response = c.get(reverse("show-person", kwargs={"pk": person.pk})) self.assertEqual(response.status_code, 200) self.assertIn(b'class="card sheet"', response.content) - response = c.get(reverse('show-organization', - kwargs={'pk': company.pk})) + response = c.get(reverse("show-organization", kwargs={"pk": company.pk})) self.assertEqual(response.status_code, 200) self.assertIn(b'class="card sheet"', response.content) @@ -2535,8 +2680,7 @@ class IshtarBasicTest(TestCase): town = models.Town.objects.get(numero_insee="99999") self.assertEqual(town.cached_label, "Sin City - 99") - models.Town.objects.create(name="Mega City", numero_insee="99999", - year=2051) + models.Town.objects.create(name="Mega City", numero_insee="99999", year=2051) mega_city = models.Town.objects.get(numero_insee="99999", year=2051) town.children.add(mega_city) town = models.Town.objects.get(numero_insee="99999-2050") @@ -2548,13 +2692,14 @@ class GeomaticTest(TestCase): class FakeGeomaticObject(object): _meta = models.GeoItem._meta - def __init__(self, x, y, z, spatial_reference_system, point=None, - point_2d=None): + def __init__( + self, x, y, z, spatial_reference_system, point=None, point_2d=None + ): self.x = x self.y = y self.z = z self.spatial_reference_system = spatial_reference_system - self.point_source = 'P' + self.point_source = "P" self.point_source_item = "" self.point = point self.point_2d = point_2d @@ -2562,16 +2707,15 @@ class GeomaticTest(TestCase): def save(self, *args, **kwargs): pass + profile = models.get_current_profile() profile.mapping = True profile.save() srs = models.SpatialReferenceSystem.objects.create( - label='WGS84', txt_idx='wgs84', srid=4326 + label="WGS84", txt_idx="wgs84", srid=4326 ) - obj = FakeGeomaticObject( - x=2, y=3, z=4, - spatial_reference_system=srs) + obj = FakeGeomaticObject(x=2, y=3, z=4, spatial_reference_system=srs) self.assertIsNone(obj.point_2d) post_save_geo(FakeGeomaticObject, instance=obj) self.assertIsNotNone(obj.point_2d) @@ -2585,10 +2729,8 @@ class NewItems(TestCase): self.username, self.password, self.user = create_superuser() def test_new_author(self): - url = 'new-author' - person = models.Person.objects.create( - name="Hop", surname="Oups" - ) + url = "new-author" + person = models.Person.objects.create(name="Hop", surname="Oups") c = Client() # TODO @@ -2600,30 +2742,29 @@ class NewItems(TestCase): response = c.post( reverse(url), - {"person": person.id, - "author_type": models.AuthorType.objects.all()[0].pk} + {"person": person.id, "author_type": models.AuthorType.objects.all()[0].pk}, ) self.assertEqual(response.status_code, 200) self.assertEqual(person.author.count(), 1) class AccountWizardTest(WizardTest, TestCase): - fixtures = [settings.ROOT_PATH + - '../fixtures/initial_data-auth-fr.json', - settings.ROOT_PATH + - '../ishtar_common/fixtures/initial_data-fr.json',] - url_name = 'account_management' - wizard_name = 'account_wizard' + fixtures = [ + settings.ROOT_PATH + "../fixtures/initial_data-auth-fr.json", + settings.ROOT_PATH + "../ishtar_common/fixtures/initial_data-fr.json", + ] + url_name = "account_management" + wizard_name = "account_wizard" steps = views.account_wizard_steps form_datas = [ WizardTestFormData( "Add an account", form_datas={ - 'account': { - 'username': "My username", - 'email': "test@example.com", - 'hidden_password': "my_pass", - 'hidden_password_confirm': "my_pass", + "account": { + "username": "My username", + "email": "test@example.com", + "hidden_password": "my_pass", + "hidden_password_confirm": "my_pass", } }, ), @@ -2631,10 +2772,11 @@ class AccountWizardTest(WizardTest, TestCase): def pre_wizard(self): self.person = models.Person.objects.create( - name='Boule', surname=' ', + name="Boule", + surname=" ", ) - self.form_datas[0].set('selec', 'pk', self.person.pk) - self.form_datas[0].set('account', 'pk', self.person.pk) + self.form_datas[0].set("selec", "pk", self.person.pk) + self.form_datas[0].set("account", "pk", self.person.pk) self.account_number = models.IshtarUser.objects.count() super(AccountWizardTest, self).pre_wizard() @@ -2644,15 +2786,15 @@ class AccountWizardTest(WizardTest, TestCase): user = person.ishtaruser.user_ptr self.assertEqual(user.username, "My username") self.assertEqual(user.email, "test@example.com") - self.assertEqual(models.IshtarUser.objects.count(), - self.account_number + 1) + self.assertEqual(models.IshtarUser.objects.count(), self.account_number + 1) class DashboardTest(TestCase): def setUp(self): self.username, self.password, self.user = create_superuser() profile, created = models.IshtarSiteProfile.objects.get_or_create( - slug='default', active=True) + slug="default", active=True + ) profile.files = True profile.context_record = True profile.find = True @@ -2663,33 +2805,46 @@ class DashboardTest(TestCase): c = Client() c.login(username=self.username, password=self.password) - url = 'dashboard-main-detail' - - response = c.get(reverse(url, kwargs={'item_name': "zorglub"})) - self.assertEqual( - response.status_code, 404) - - for item in ['users', 'files', 'treatmentfiles', 'treatments', - 'operations', 'contextrecords', 'finds']: - response = c.get(reverse(url, kwargs={'item_name': item})) + url = "dashboard-main-detail" + + response = c.get(reverse(url, kwargs={"item_name": "zorglub"})) + self.assertEqual(response.status_code, 404) + + for item in [ + "users", + "files", + "treatmentfiles", + "treatments", + "operations", + "contextrecords", + "finds", + ]: + response = c.get(reverse(url, kwargs={"item_name": item})) self.assertEqual( - response.status_code, 200, - "Reaching dashboard for item: {} return an error.".format(url)) + response.status_code, + 200, + "Reaching dashboard for item: {} return an error.".format(url), + ) class CleanMedia(TestCase): def test_rename(self): test_names = [ - ("éofficier2-12-02-04.93_gvK3hAr-1_2m7zZPn-1_nKhh2S2-1_"\ - "ONmUhfD-1_ymA3gGJ-1_XzJyRx3-1_PhvRcO8-1-thumb_ZwWMKBd.jpg", - "éofficier2-12-02-04.93-thumb.jpg"), + ( + "éofficier2-12-02-04.93_gvK3hAr-1_2m7zZPn-1_nKhh2S2-1_" + "ONmUhfD-1_ymA3gGJ-1_XzJyRx3-1_PhvRcO8-1-thumb_ZwWMKBd.jpg", + "éofficier2-12-02-04.93-thumb.jpg", + ), ("a_ZwWMKBd.jpg", False), # no rename because too short - ("hoplala_gvK3hAr_2m7zZPn_nKhh2S2_ZwWMKBd.jpg", - "hoplala_gvK3hAr_2m7zZPn_nKhh2S2.jpg",), # stop before because + ( + "hoplala_gvK3hAr_2m7zZPn_nKhh2S2_ZwWMKBd.jpg", + "hoplala_gvK3hAr_2m7zZPn_nKhh2S2.jpg", + ), # stop before because # another file exists ] - base_dir = os.sep.join([settings.ROOT_PATH, "..", "ishtar_common", - "tests", "rename"]) + base_dir = os.sep.join( + [settings.ROOT_PATH, "..", "ishtar_common", "tests", "rename"] + ) for name, expected in test_names: name = os.sep.join([base_dir, name]) new_name, modif = rename_and_simplify_media_name(name, rename=False) @@ -2701,12 +2856,15 @@ class CleanMedia(TestCase): def test_try_fix(self): test_names = [ - ("hoplala_gvK3hAr_2m7zZPn_nKhh2S2_ZwWMKBd_ZwWMKBd.jpg", - # non existing file - "hoplala_gvK3hAr_2m7zZPn.jpg",), + ( + "hoplala_gvK3hAr_2m7zZPn_nKhh2S2_ZwWMKBd_ZwWMKBd.jpg", + # non existing file + "hoplala_gvK3hAr_2m7zZPn.jpg", + ), ] - base_dir = os.sep.join([settings.ROOT_PATH, "..", "ishtar_common", - "tests", "rename"]) + base_dir = os.sep.join( + [settings.ROOT_PATH, "..", "ishtar_common", "tests", "rename"] + ) for name, expected in test_names: name = os.sep.join([base_dir, name]) @@ -2719,8 +2877,7 @@ class PersonQATest(TestCase): def setUp(self): self.username, self.password, self.user = create_superuser() - self.user.user_permissions.add(Permission.objects.get( - codename='change_person')) + self.user.user_permissions.add(Permission.objects.get(codename="change_person")) self.title_1 = models.TitleType.objects.create(label="T1", txt_idx="t1") self.title_2 = models.TitleType.objects.create(label="T2", txt_idx="t2") self.person_1 = models.Person.objects.create(title=self.title_1) @@ -2729,51 +2886,52 @@ class PersonQATest(TestCase): def test_bulk_update(self): c = Client() pks = "{}-{}".format(self.person_1.pk, self.person_2.pk) - response = c.get(reverse('person-qa-bulk-update', args=[pks])) - self.assertRedirects(response, '/') + response = c.get(reverse("person-qa-bulk-update", args=[pks])) + self.assertRedirects(response, "/") c = Client() c.login(username=self.username, password=self.password) - response = c.get(reverse('person-qa-bulk-update', args=[pks])) + response = c.get(reverse("person-qa-bulk-update", args=[pks])) self.assertEqual(response.status_code, 200) self.assertNotEqual(self.person_1.title, self.title_2) self.assertNotEqual(self.person_2.title, self.title_2) response = c.post( - reverse('person-qa-bulk-update-confirm', args=[pks]), - {'qa_title': self.title_2.pk} + reverse("person-qa-bulk-update-confirm", args=[pks]), + {"qa_title": self.title_2.pk}, ) if response.status_code != 200: - self.assertRedirects(response, '/success/') + self.assertRedirects(response, "/success/") self.assertEqual( - models.Person.objects.get(pk=self.person_1.pk).title, - self.title_2 + models.Person.objects.get(pk=self.person_1.pk).title, self.title_2 ) self.assertEqual( - models.Person.objects.get(pk=self.person_2.pk).title, - self.title_2 + models.Person.objects.get(pk=self.person_2.pk).title, self.title_2 ) class DocumentTest(TestCase): def setUp(self): Operation = apps.get_model("archaeological_operations", "Operation") - ContextRecord = apps.get_model("archaeological_context_records", - "ContextRecord") + ContextRecord = apps.get_model( + "archaeological_context_records", "ContextRecord" + ) Unit = apps.get_model("archaeological_context_records", "Unit") BaseFind = apps.get_model("archaeological_finds", "BaseFind") Find = apps.get_model("archaeological_finds", "Find") operation_type, __ = models.OperationType.objects.get_or_create( - txt_idx="arch_diagnostic", label="Diagnostic") + txt_idx="arch_diagnostic", label="Diagnostic" + ) self.ope1 = Operation.objects.create( - code_patriarche="001", - operation_type_id=operation_type.pk) + code_patriarche="001", operation_type_id=operation_type.pk + ) self.ope2 = Operation.objects.create( - code_patriarche="002", - operation_type_id=operation_type.pk) + code_patriarche="002", operation_type_id=operation_type.pk + ) su, __ = Unit.objects.get_or_create( - txt_idx='stratigraphic-unit', label="Stratigraphic unit", order=1) + txt_idx="stratigraphic-unit", label="Stratigraphic unit", order=1 + ) self.cr1 = ContextRecord.objects.create(operation=self.ope1, unit=su) self.cr2 = ContextRecord.objects.create(operation=self.ope2, unit=su) bf1 = BaseFind.objects.create(context_record=self.cr1) @@ -2783,31 +2941,35 @@ class DocumentTest(TestCase): self.find2 = Find.objects.create() self.find2.base_finds.add(bf2) self.st1 = models.SourceType.objects.create(label="Report", code="REP") - self.st2 = models.SourceType.objects.create(label="Illustration", - code="ILL") + self.st2 = models.SourceType.objects.create(label="Illustration", code="ILL") def test_custom_index(self): profile, created = models.IshtarSiteProfile.objects.get_or_create( - slug='default', active=True) - profile.document_complete_identifier = \ + slug="default", active=True + ) + profile.document_complete_identifier = ( "{operation_codes}-{source_type__code}-{custom_index}" + ) profile.document_custom_index = "operation" profile.save() - doc = models.Document.objects.create(source_type=self.st1, - title="Operation report") + doc = models.Document.objects.create( + source_type=self.st1, title="Operation report" + ) doc.operations.add(self.ope1) doc = models.Document.objects.get(pk=doc.pk) self.assertEqual(doc.complete_identifier, "001-REP-1") - doc2 = models.Document.objects.create(source_type=self.st2, - title="Illustration CR") + doc2 = models.Document.objects.create( + source_type=self.st2, title="Illustration CR" + ) doc2.context_records.add(self.cr1) doc2 = models.Document.objects.get(pk=doc2.pk) self.assertEqual(doc2.complete_identifier, "001-ILL-2") - doc3 = models.Document.objects.create(source_type=self.st1, - title="Operation report 2") + doc3 = models.Document.objects.create( + source_type=self.st1, title="Operation report 2" + ) doc3.operations.add(self.ope2) doc3 = models.Document.objects.get(pk=doc3.pk) self.assertEqual(doc3.complete_identifier, "002-REP-1") @@ -2817,12 +2979,13 @@ class DocumentTest(TestCase): doc3.save() doc3 = models.Document.objects.get(pk=doc3.pk) self.assertEqual(doc3.custom_index, None) # 2 operations - no index - self.assertEqual(doc3.complete_identifier, '001|002-REP-') + self.assertEqual(doc3.complete_identifier, "001|002-REP-") # complex jinja identifier - profile.document_complete_identifier = \ - "{% if custom_index %}{{operation_codes}}-{{source_type__code}}-" \ - "{{ \"%03d\" % (custom_index|int)}}{% else %}no-code{% endif %}" + profile.document_complete_identifier = ( + "{% if custom_index %}{{operation_codes}}-{{source_type__code}}-" + '{{ "%03d" % (custom_index|int)}}{% else %}no-code{% endif %}' + ) profile.save() doc3.operations.remove(self.ope1) @@ -2830,18 +2993,19 @@ class DocumentTest(TestCase): doc3.complete_identifier = "" doc3.save() doc3 = models.Document.objects.get(pk=doc3.pk) - self.assertEqual(doc3.complete_identifier, '002-REP-001') + self.assertEqual(doc3.complete_identifier, "002-REP-001") doc3.operations.remove(self.ope2) doc3.custom_index = None doc3.complete_identifier = "" doc3.save() doc3 = models.Document.objects.get(pk=doc3.pk) - self.assertEqual(doc3.complete_identifier, 'no-code') + self.assertEqual(doc3.complete_identifier, "no-code") def test_clean_duplicate_association(self): - doc = models.Document.objects.create(source_type=self.st1, - title="Operation report") + doc = models.Document.objects.create( + source_type=self.st1, title="Operation report" + ) doc.operations.add(self.ope1) doc.context_records.add(self.cr1) doc = models.Document.objects.get(pk=doc.pk) @@ -2882,33 +3046,35 @@ class JinjaFilterTest(TestCase): self.assertEqual(utils_secretary.splitpart("1,2,3", 10), "") self.assertEqual(utils_secretary.splitpart("", 10), "") self.assertEqual(utils_secretary.splitpart("1;2;3", 2, ";"), "3") - self.assertEqual(utils_secretary.splitpart( - "1;2;3;4", 1, ";", True), "2;3;4") + self.assertEqual(utils_secretary.splitpart("1;2;3;4", 1, ";", True), "2;3;4") def test_human_date(self): self.assertEqual(utils_secretary.human_date_filter("NODATE"), "") self.assertEqual( - utils_secretary.human_date_filter("2020-01-01"), - "1 janvier 2020") + utils_secretary.human_date_filter("2020-01-01"), "1 janvier 2020" + ) def test_capfirst(self): self.assertEqual( utils_secretary.capfirst_filter("saint georges d'oléron"), - "Saint georges d'oléron") + "Saint georges d'oléron", + ) self.assertEqual(utils_secretary.capfirst_filter("s"), "S") self.assertEqual(utils_secretary.capfirst_filter(""), "") def test_capitalize(self): self.assertEqual( utils_secretary.capitalize_filter("SAINT-GEORGES D'OLÉRON"), - "Saint-Georges d'Oléron") + "Saint-Georges d'Oléron", + ) self.assertEqual(utils_secretary.capitalize_filter("s"), "S") self.assertEqual(utils_secretary.capitalize_filter(""), "") def test_lowerfirst(self): self.assertEqual( utils_secretary.lowerfirst_filter("SAINT GEORGES D'OLÉRON"), - "sAINT GEORGES D'OLÉRON") + "sAINT GEORGES D'OLÉRON", + ) self.assertEqual(utils_secretary.lowerfirst_filter("S"), "s") self.assertEqual(utils_secretary.lowerfirst_filter(""), "") @@ -2916,49 +3082,45 @@ class JinjaFilterTest(TestCase): class TemplateGenerationTest(TestCase): def test_filters(self): model, __ = models.ImporterModel.objects.get_or_create( - klass='archaeological_finds.models.Find' + klass="archaeological_finds.models.Find" ) q = models.DocumentTemplate.objects.filter(slug="test") if q.count(): q.all()[0].delete() doc = models.DocumentTemplate.objects.create( - name="Test", - slug="test", - associated_model=model, - available=True) + name="Test", slug="test", associated_model=model, available=True + ) LABEL_EXPECTED_KEYS = [ - 'container_index', - 'context_record_operation_common_name', - 'context_record_town_name', - 'container_location_name', - 'context_record_operation_operation_type', - 'container_reference', - 'my_complete_id', - 'complete_idx', - 'complete_idxy', + "container_index", + "context_record_operation_common_name", + "context_record_town_name", + "container_location_name", + "context_record_operation_operation_type", + "container_reference", + "my_complete_id", + "complete_idx", + "complete_idxy", ] - tpl_label = settings.ROOT_PATH + \ - '../ishtar_common/tests/test-filters-label.odt' + tpl_label = settings.ROOT_PATH + "../ishtar_common/tests/test-filters-label.odt" BASE_EXPECTED_KEYS = [ - 'container_index', - 'context_record_operation_common_name', - 'context_record_town_name', - 'container_location_name', - 'context_record_operation_operation_type', - 'container_reference', - 'my_complete_id', - 'complete_idx', - 'complete_idxy', + "container_index", + "context_record_operation_common_name", + "context_record_town_name", + "container_location_name", + "context_record_operation_operation_type", + "container_reference", + "my_complete_id", + "complete_idx", + "complete_idxy", ] - tpl_base = settings.ROOT_PATH + \ - '../ishtar_common/tests/test-filters-base.odt' + tpl_base = settings.ROOT_PATH + "../ishtar_common/tests/test-filters-base.odt" tests = ( (LABEL_EXPECTED_KEYS, tpl_label, models.DocumentTemplate.LABEL_RE), (BASE_EXPECTED_KEYS, tpl_base, models.DocumentTemplate.BASE_RE), ) for expected_keys, tpl, filter_re in tests: - with open(tpl, 'rb') as tpl: + with open(tpl, "rb") as tpl: template = SimpleUploadedFile("template.odt", tpl.read()) filtr = doc.get_filter(template, filter_re) for key in expected_keys: |