summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémi Duraffort <remi.duraffort@linaro.org>2016-08-09 15:54:37 +0200
committerRémi Duraffort <remi.duraffort@linaro.org>2016-08-09 16:02:13 +0200
commitdb3ea384b07c646dca30ddefd2c757338b2e3476 (patch)
tree4c4f52a492a514ecdf098be3c8051ef62040f47e
parent2eb61238956f7790742a9d3093ddb469bb1d3b41 (diff)
Port to Python3
Change-Id: I2bcf79cd1f5d37d3548a6eb8472e0a4f0cb4555a
-rw-r--r--django_restricted_resource/test_utils.py21
-rw-r--r--django_restricted_resource/tests.py48
2 files changed, 64 insertions, 5 deletions
diff --git a/django_restricted_resource/test_utils.py b/django_restricted_resource/test_utils.py
index 747a899..29bf4c5 100644
--- a/django_restricted_resource/test_utils.py
+++ b/django_restricted_resource/test_utils.py
@@ -54,9 +54,9 @@ class TestCaseWithInvariants(TestCaseWithScenarios):
return self.scenarios
def _dict_to_keys_and_values(self, d):
- pairs = d.items()
- keys = [first for (first, second) in pairs]
- values = [second for (first, second) in pairs]
+ items = list(d.items())
+ keys = [first for (first, second) in items]
+ values = [second for (first, second) in items]
return keys, values
def _get_all_possible_scenarios(self, invariants):
@@ -75,7 +75,7 @@ class TestCaseWithInvariants(TestCaseWithScenarios):
k, v = self._dict_to_keys_and_values(value)
scenario_ids_list.append(k)
scenario_params_list.append(v)
- for scenario_ids, scenario_params in itertools.izip(
+ for scenario_ids, scenario_params in zip(
itertools.product(*scenario_ids_list),
itertools.product(*scenario_params_list)):
parameters = dict(zip(invariant_keys, scenario_params))
@@ -90,10 +90,21 @@ class TestCaseWithInvariants(TestCaseWithScenarios):
def setUp(self):
super(TestCaseWithScenarios, self).setUp()
# Evaluate lazy invariants now that `self' is around
- for invariant in self._get_invariants().iterkeys():
+ for invariant in self._get_invariants().keys():
value = getattr(self, invariant)
if inspect.isfunction(value):
value = value(self)
+ # Some keys are lambda pointing to another key (which is
+ # sometime also a lambda). Depending on the order of the keys
+ # returned by .keys() the pointed value might still be a
+ # lambda.
+ # For instance with:
+ # invariants = {'owner': lambda self: ...,
+ # 'accessible_by_principal': lambda self: self.owner }
+ # when 'accessible_by_principal' is evaluated before 'owner',
+ # it should be evaluated twice.
+ if inspect.isfunction(value):
+ value = value(self)
setattr(self, invariant, value)
diff --git a/django_restricted_resource/tests.py b/django_restricted_resource/tests.py
index 5619eb0..8bf5b38 100644
--- a/django_restricted_resource/tests.py
+++ b/django_restricted_resource/tests.py
@@ -41,6 +41,9 @@ if hasattr(django, 'setup'):
class ResourceCleanTests(FixtureHelper, TestCase):
+ def __hash__(self):
+ return hash(repr(self))
+
def test_clean_raises_exception_when_owner_is_not_set(self):
resource = RestrictedResource()
self.assertRaises(ValidationError, resource.clean)
@@ -65,6 +68,9 @@ class ResourceCleanTests(FixtureHelper, TestCase):
class ResourceOwnerTest(FixtureHelper, TestCase):
""" Tests for the owner property """
+ def __hash__(self):
+ return hash(repr(self))
+
def test_user_is_owner(self):
user = self.getUniqueUser()
resource = ExampleRestrictedResource(user=user)
@@ -103,6 +109,9 @@ class PrincipalTypeIsChecked(
principal=[1, object(), {}, [], "string"],
)
+ def __hash__(self):
+ return hash(repr(self))
+
def test_owner_assignment(self):
resource = ExampleRestrictedResource()
self.assertRaises(TypeError, setattr, resource, "owner", self.principal)
@@ -142,6 +151,9 @@ class OthersDoNotOwnResource(
is_public=[True, False],
)
+ def __hash__(self):
+ return hash(repr(self))
+
def test(self):
resource = self.getUniqueResource(
owner=self.owner, is_public=self.is_public)
@@ -163,6 +175,9 @@ class OwnerOwnsResource(
is_public=[True, False],
)
+ def __hash__(self):
+ return hash(repr(self))
+
def test(self):
resource = self.getUniqueResource(
owner=self.owner, is_public=self.is_public)
@@ -204,6 +219,9 @@ class ResourceManagerOwnerSetFindsNoMatchesForOthers(
is_public=[True, False],
)
+ def __hash__(self):
+ return hash(repr(self))
+
def test(self):
manager = ExampleRestrictedResource.objects
resource = self.getUniqueResource(
@@ -224,6 +242,9 @@ class ResourceManagerOwnerSetFindsMatchesForOwner(
is_public=[True, False],
)
+ def __hash__(self):
+ return hash(repr(self))
+
def test(self):
for i in range(self.num_objects):
self.getUniqueResource(
@@ -243,6 +264,9 @@ class ResourceManagerOwnerSetFindsMatchesForOwnerGroupMember(
is_public=[True, False],
)
+ def __hash__(self):
+ return hash(repr(self))
+
def test(self):
group = self.getUniqueGroup()
user = self.getUniqueUser()
@@ -277,6 +301,9 @@ class EveryoneHasPublicAccessToPublicResources(
is_public=[True, False],
)
+ def __hash__(self):
+ return hash(repr(self))
+
def setUp(self):
super(EveryoneHasPublicAccessToPublicResources, self).setUp()
self.resource = self.getUniqueResource(
@@ -296,6 +323,9 @@ class NobodyButTheOwnerHasAccessToNonPublicUserResources(
FixtureHelper, TestCaseWithInvariants):
""" Tests for the get_access_type() method """
+ def __hash__(self):
+ return hash(repr(self))
+
invariants = dict(
accessing_principal=dict(
nothing=None,
@@ -318,6 +348,9 @@ class OwnerHasPrivateAccessToNonPublicUserResources(
FixtureHelper, TestCase):
""" Tests for the get_access_type() method """
+ def __hash__(self):
+ return hash(repr(self))
+
def test_owner(self):
owner = self.getUniqueUser()
resource = self.getUniqueResource(is_public=False, owner=owner)
@@ -329,6 +362,9 @@ class OwnerHasPrivateAccessToNonPublicUserResources(
class GroupMembersGetSharedAccessToNonPublicGroupResources(
FixtureHelper, TestCase):
+ def __hash__(self):
+ return hash(repr(self))
+
def test_get_access_type_for_owning_group(self):
owner = self.getUniqueGroup()
resource = self.getUniqueResource(owner=owner, is_public=False)
@@ -363,6 +399,9 @@ class ResourceManagerAccessibleSetFindsOnlyPublicElementsForNonOwners(
)
)
+ def __hash__(self):
+ return hash(repr(self))
+
def test(self):
manager = ExampleRestrictedResource.objects
self.add_resources(["a", "b", "c"], owner=self.owner, is_public=True)
@@ -383,6 +422,9 @@ class ResourceManagerAccessibleByAnyoneSetFindsOnlyPublicElements(
),
)
+ def __hash__(self):
+ return hash(repr(self))
+
def test(self):
self.add_resources(["a", "b", "c"], owner=self.owner, is_public=True)
self.add_resources(["x", "y", "z"], owner=self.owner, is_public=False)
@@ -403,6 +445,9 @@ class ResourceManagerAccessibleByPrincipalSetFindsAllOwnedlements(
),
)
+ def __hash__(self):
+ return hash(repr(self))
+
def test(self):
self.add_resources(["a", "b", "c"], owner=self.owner, is_public=True)
self.add_resources(["x", "y", "z"], owner=self.owner, is_public=False)
@@ -416,6 +461,9 @@ class ResourceManagerAccessibleByPrincipalSetFindsAllOwnedlements(
class ResourceManagerAccessibleSetTests(
FixtureHelper, TestCase):
+ def __hash__(self):
+ return hash(repr(self))
+
def setUp(self):
super(ResourceManagerAccessibleSetTests, self).setUp()
self.user = self.getUniqueUser()