Commit 542187df authored by Enrico Zini's avatar Enrico Zini

Flake8 updates

parent 1d046a6f
import backend.models as bmodels
from backend.models import Person, Process, AM, Fingerprint
from backend.models import Person, AM, Fingerprint
from backend import const
from django.utils.timezone import now
from django.test import Client
......@@ -7,8 +7,6 @@ from rest_framework.test import APIClient
from collections import defaultdict
import contextlib
import datetime
import os
import io
import re
import six
......@@ -30,7 +28,8 @@ class NamedObjects(dict):
This allows to use __getitem__ with already resolved objects, just to have
functions that can take either objects or their fixture names.
"""
if not isinstance(key, str): return key
if not isinstance(key, str):
return key
return super(NamedObjects, self).__getitem__(key)
def __getattr__(self, key):
......@@ -38,7 +37,8 @@ class NamedObjects(dict):
Make dict elements also appear as class members
"""
res = self.get(key, None)
if res is not None: return res
if res is not None:
return res
raise AttributeError("member {} not found".format(key))
def _update_kwargs_with_defaults(self, _name, kw):
......@@ -75,7 +75,6 @@ class NamedObjects(dict):
except self._model.DoesNotExist:
del self[name]
def delete_all(self):
"""
Call delete() on all model objects registered in this dict.
......@@ -83,7 +82,8 @@ class NamedObjects(dict):
This can be used in methods like tearDownClass to remove objects common
to all tests.
"""
if self._skip_delete_all: return
if self._skip_delete_all:
return
for o in self.values():
o.delete()
......@@ -224,11 +224,14 @@ class TestBase(object):
def assertFormErrorMatches(self, response, form_name, field_name, regex):
form = response.context[form_name]
errors = form.errors
if not errors: self.fail("Form {} has no errors".format(form_name))
if field_name not in errors: self.fail("Form {} has no errors in field {}".format(form_name, field_name))
if not errors:
self.fail("Form {} has no errors".format(form_name))
if field_name not in errors:
self.fail("Form {} has no errors in field {}".format(form_name, field_name))
match = re.compile(regex)
for errmsg in errors[field_name]:
if match.search(errmsg): return
if match.search(errmsg):
return
self.fail("{} dit not match any in {}".format(regex, repr(errors)))
def assertContainsElements(self, response, elements, *names):
......@@ -237,7 +240,8 @@ class TestBase(object):
"""
want = set(names)
extras = want - set(elements.keys())
if extras: raise RuntimeError("Wanted elements not found in the list of possible ones: {}".format(", ".join(extras)))
if extras:
raise RuntimeError("Wanted elements not found in the list of possible ones: {}".format(", ".join(extras)))
should_have = []
should_not_have = []
content = response.content.decode("utf-8")
......@@ -250,8 +254,10 @@ class TestBase(object):
should_not_have.append(name)
if should_have or should_not_have:
msg = []
if should_have: msg.append("should have element(s) {}".format(", ".join(should_have)))
if should_not_have: msg.append("should not have element(s) {}".format(", ".join(should_not_have)))
if should_have:
msg.append("should have element(s) {}".format(", ".join(should_have)))
if should_not_have:
msg.append("should not have element(s) {}".format(", ".join(should_not_have)))
self.fail("page " + " and ".join(msg))
......@@ -295,10 +301,10 @@ class OpFixtureMixin(BaseFixtureMixin):
When run without check_contents, it acts as a decorator, allowing a
shorter invocation, like this:
@self.assertOperationSerializes(operation_to_check)
def _(o):
self.assertEqual(o.audit_author, self.persons.fd)
# ...more checks on o...
@self.assertOperationSerializes(operation_to_check)
def _(o):
self.assertEqual(o.audit_author, self.persons.fd)
# ...more checks on o...
"""
if check_contents is None:
# Act as a decorator
......@@ -312,7 +318,7 @@ class OpFixtureMixin(BaseFixtureMixin):
self.assertIsInstance(o.audit_time, datetime.datetime)
check_contents(o)
d = o.to_dict()
o = cls(**d)
self.assertIsInstance(o.audit_time, datetime.datetime)
......@@ -339,7 +345,9 @@ class PersonFixtureMixin(OpFixtureMixin):
def setUpClass(cls):
super(PersonFixtureMixin, cls).setUpClass()
# pending account
cls.persons.create("pending", status=const.STATUS_DC, expires=now() + datetime.timedelta(days=1), pending="12345", alioth=True)
cls.persons.create(
"pending", status=const.STATUS_DC, expires=now() +
datetime.timedelta(days=1), pending="12345", alioth=True)
# debian contributor
cls.persons.create("dc", status=const.STATUS_DC, alioth=True)
# debian contributor with guest account
......@@ -376,7 +384,8 @@ class TestSet(set):
changed with simple text patches.
"""
def __init__(self, initial=""):
if initial: self.update(initial.split())
if initial:
self.update(initial.split())
def set(self, vals):
self.clear()
......@@ -389,7 +398,8 @@ class TestSet(set):
elif change[0] == "-":
self.discard(change[1:])
else:
raise RuntimeError("Changes {} contain {} that is nether an add nor a remove".format(repr(text), repr(change)))
raise RuntimeError("Changes {} contain {} that is nether an add nor a remove".format(
repr(diff), repr(change)))
def clone(self):
res = TestSet()
......@@ -405,7 +415,8 @@ class PatchExact(object):
self.items = set()
def apply(self, cur):
if self.items: return set(self.items)
if self.items:
return set(self.items)
return None
......@@ -426,7 +437,8 @@ class PatchDiff(object):
cur = set(self.added)
else:
cur = (cur - self.removed) | self.added
if not cur: return None
if not cur:
return None
return cur
......@@ -474,18 +486,24 @@ class ExpectedSets(defaultdict):
def assertEqual(self, visitor, got):
got = set(got)
wanted = self.get(visitor, set())
if got == wanted: return
if got == wanted:
return
extra = got - wanted
missing = wanted - got
msgs = []
if missing: msgs.append(self.issue_msg.format(problem="misses", mismatch=", ".join(sorted(missing))))
if extra: msgs.append(self.issue_msg.format(problem="has extra", mismatch=", ".join(sorted(extra))))
if missing:
msgs.append(self.issue_msg.format(problem="misses", mismatch=", ".join(sorted(missing))))
if extra:
msgs.append(self.issue_msg.format(problem="has extra", mismatch=", ".join(sorted(extra))))
self.testcase.fail(self.action_msg.format(visitor=visitor) + " " + " and ".join(msgs))
def assertEmpty(self, visitor, got):
extra = set(got)
if not extra: return
self.testcase.fail(self.action_msg.format(visitor=visitor) + " " + self.issue_msg.format(problem="has", mismatch=", ".join(sorted(extra))))
if not extra:
return
self.testcase.fail(
self.action_msg.format(visitor=visitor) + " " +
self.issue_msg.format(problem="has", mismatch=", ".join(sorted(extra))))
def assertMatches(self, visited):
for visitor in self.visitors:
......@@ -501,7 +519,8 @@ class ExpectedPerms(object):
Store the permissions expected out of a *VisitorPermissions object
"""
def __init__(self, perms=None):
if perms is None: perms = {}
if perms is None:
perms = {}
self.perms = {}
for visitors, expected_perms in perms.items():
for visitor in visitors.split():
......@@ -520,10 +539,10 @@ class ExpectedPerms(object):
self._apply_diff(self.perms, diff)
def set_perms(self, visitors, text):
self.update_perms({ visitors: PatchExact(text) })
self.update_perms({visitors: PatchExact(text)})
def patch_perms(self, visitors, text):
self.update_perms({ visitors: PatchDiff(text) })
self.update_perms({visitors: PatchDiff(text)})
class PageElements(dict):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment