mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Use `self.assertRaises*()` as a context manager
This commit is contained in:
@@ -42,4 +42,5 @@ class ToolboxFiltersSeleniumIntegrationTestCase(SeleniumIntegrationTestCase):
|
||||
self.sleep_for(self.wait_types.UX_RENDER)
|
||||
self.home()
|
||||
# But now it should raise NoSuchElementException
|
||||
self.assertRaises(NoSuchElementException, lambda: self.driver.find_element(By.LINK_TEXT, "Test Section"))
|
||||
with self.assertRaises(NoSuchElementException):
|
||||
self.driver.find_element(By.LINK_TEXT, "Test Section")
|
||||
|
||||
@@ -97,17 +97,16 @@ class TestDynamicToolDestination(unittest.TestCase):
|
||||
|
||||
@log_capture()
|
||||
def test_brokenDestYML(self, lc):
|
||||
self.assertRaises(
|
||||
JobMappingException,
|
||||
map_tool_to_destination,
|
||||
runJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
broken_default_dest_path,
|
||||
job_conf_path,
|
||||
)
|
||||
with self.assertRaises(JobMappingException):
|
||||
map_tool_to_destination(
|
||||
runJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
broken_default_dest_path,
|
||||
job_conf_path,
|
||||
)
|
||||
|
||||
lc.check_present(
|
||||
("galaxy.jobs.dynamic_tool_destination", "DEBUG", "Running config validation..."),
|
||||
@@ -119,28 +118,26 @@ class TestDynamicToolDestination(unittest.TestCase):
|
||||
|
||||
@log_capture()
|
||||
def test_filesize_empty(self, lc):
|
||||
self.assertRaises(
|
||||
JobMappingException,
|
||||
map_tool_to_destination,
|
||||
emptyJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
path,
|
||||
job_conf_path,
|
||||
)
|
||||
self.assertRaises(
|
||||
JobMappingException,
|
||||
map_tool_to_destination,
|
||||
emptyJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
priority_path,
|
||||
job_conf_path,
|
||||
)
|
||||
with self.assertRaises(JobMappingException):
|
||||
map_tool_to_destination(
|
||||
emptyJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
path,
|
||||
job_conf_path,
|
||||
)
|
||||
with self.assertRaises(JobMappingException):
|
||||
map_tool_to_destination(
|
||||
emptyJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
priority_path,
|
||||
job_conf_path,
|
||||
)
|
||||
|
||||
lc.check_present(
|
||||
("galaxy.jobs.dynamic_tool_destination", "DEBUG", "Running config validation..."),
|
||||
@@ -162,28 +159,26 @@ class TestDynamicToolDestination(unittest.TestCase):
|
||||
|
||||
@log_capture()
|
||||
def test_filesize_zero(self, lc):
|
||||
self.assertRaises(
|
||||
JobMappingException,
|
||||
map_tool_to_destination,
|
||||
zeroJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
path,
|
||||
job_conf_path,
|
||||
)
|
||||
self.assertRaises(
|
||||
JobMappingException,
|
||||
map_tool_to_destination,
|
||||
zeroJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
priority_path,
|
||||
job_conf_path,
|
||||
)
|
||||
with self.assertRaises(JobMappingException):
|
||||
map_tool_to_destination(
|
||||
zeroJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
path,
|
||||
job_conf_path,
|
||||
)
|
||||
with self.assertRaises(JobMappingException):
|
||||
map_tool_to_destination(
|
||||
zeroJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
priority_path,
|
||||
job_conf_path,
|
||||
)
|
||||
|
||||
lc.check_present(
|
||||
("galaxy.jobs.dynamic_tool_destination", "DEBUG", "Running config validation..."),
|
||||
@@ -203,28 +198,26 @@ class TestDynamicToolDestination(unittest.TestCase):
|
||||
|
||||
@log_capture()
|
||||
def test_filesize_fail(self, lc):
|
||||
self.assertRaises(
|
||||
JobMappingException,
|
||||
map_tool_to_destination,
|
||||
failJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
path,
|
||||
job_conf_path,
|
||||
)
|
||||
self.assertRaises(
|
||||
JobMappingException,
|
||||
map_tool_to_destination,
|
||||
failJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
priority_path,
|
||||
job_conf_path,
|
||||
)
|
||||
with self.assertRaises(JobMappingException):
|
||||
map_tool_to_destination(
|
||||
failJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
path,
|
||||
job_conf_path,
|
||||
)
|
||||
with self.assertRaises(JobMappingException):
|
||||
map_tool_to_destination(
|
||||
failJob,
|
||||
theApp,
|
||||
vanillaTool,
|
||||
"user@email.com",
|
||||
True,
|
||||
priority_path,
|
||||
job_conf_path,
|
||||
)
|
||||
|
||||
lc.check_present(
|
||||
("galaxy.jobs.dynamic_tool_destination", "DEBUG", "Running config validation..."),
|
||||
@@ -473,7 +466,8 @@ class TestDynamicToolDestination(unittest.TestCase):
|
||||
# ================================Invalid yaml files==============================
|
||||
@log_capture()
|
||||
def test_no_file(self, lc):
|
||||
self.assertRaises(IOError, dt.parse_yaml, path="")
|
||||
with self.assertRaises(IOError):
|
||||
dt.parse_yaml(path="")
|
||||
lc.check_present()
|
||||
|
||||
@log_capture()
|
||||
@@ -1360,8 +1354,10 @@ class TestDynamicToolDestination(unittest.TestCase):
|
||||
|
||||
# ================================Testing str_to_bytes==========================
|
||||
def test_str_to_bytes_invalid(self):
|
||||
self.assertRaises(dt.MalformedYMLException, dt.str_to_bytes, "1d")
|
||||
self.assertRaises(dt.MalformedYMLException, dt.str_to_bytes, "1 d")
|
||||
with self.assertRaises(dt.MalformedYMLException):
|
||||
dt.str_to_bytes("1d")
|
||||
with self.assertRaises(dt.MalformedYMLException):
|
||||
dt.str_to_bytes("1 d")
|
||||
|
||||
def test_str_to_bytes_valid(self):
|
||||
assert dt.str_to_bytes("-1") == -1
|
||||
@@ -1380,12 +1376,16 @@ class TestDynamicToolDestination(unittest.TestCase):
|
||||
# ==============================Testing bytes_to_str=============================
|
||||
def test_bytes_to_str_invalid(self):
|
||||
testValue = ""
|
||||
self.assertRaises(ValueError, dt.bytes_to_str, testValue)
|
||||
with self.assertRaises(ValueError):
|
||||
dt.bytes_to_str(testValue)
|
||||
testValue = "5564fads"
|
||||
self.assertRaises(ValueError, dt.bytes_to_str, testValue)
|
||||
with self.assertRaises(ValueError):
|
||||
dt.bytes_to_str(testValue)
|
||||
testValue = "45.0.1"
|
||||
self.assertRaises(ValueError, dt.bytes_to_str, testValue)
|
||||
self.assertRaises(ValueError, dt.bytes_to_str, "1 024")
|
||||
with self.assertRaises(ValueError):
|
||||
dt.bytes_to_str(testValue)
|
||||
with self.assertRaises(ValueError):
|
||||
dt.bytes_to_str("1 024")
|
||||
|
||||
def test_bytes_to_str_valid(self):
|
||||
assert dt.bytes_to_str(-1) == "Infinity"
|
||||
|
||||
@@ -84,7 +84,8 @@ class DatasetManagerTestCase(BaseTestCase):
|
||||
|
||||
self.log("should raise an error when purging a dataset if config does not allow")
|
||||
assert not item1.purged
|
||||
self.assertRaises(exceptions.ConfigDoesNotAllowException, self.dataset_manager.purge, item1)
|
||||
with self.assertRaises(exceptions.ConfigDoesNotAllowException):
|
||||
self.dataset_manager.purge(item1)
|
||||
assert not item1.purged
|
||||
|
||||
def test_create_with_no_permissions(self):
|
||||
@@ -272,12 +273,12 @@ class DatasetSerializerTestCase(BaseTestCase):
|
||||
|
||||
self.log("permissions should be not returned for non-managing users")
|
||||
not_my_supervisor = self.user_manager.create(**user3_data)
|
||||
self.assertRaises(
|
||||
SkipAttribute, self.dataset_serializer.serialize_permissions, dataset, "perms", user=not_my_supervisor
|
||||
)
|
||||
with self.assertRaises(SkipAttribute):
|
||||
self.dataset_serializer.serialize_permissions(dataset, "perms", user=not_my_supervisor)
|
||||
|
||||
self.log("permissions should not be returned for anon users")
|
||||
self.assertRaises(SkipAttribute, self.dataset_serializer.serialize_permissions, dataset, "perms", user=None)
|
||||
with self.assertRaises(SkipAttribute):
|
||||
self.dataset_serializer.serialize_permissions(dataset, "perms", user=None)
|
||||
|
||||
self.log("permissions should be returned for admin users")
|
||||
permissions = self.dataset_serializer.serialize_permissions(dataset, "perms", user=self.admin_user)
|
||||
|
||||
@@ -189,7 +189,8 @@ class HDAManagerTestCase(HDATestCase):
|
||||
|
||||
self.log("should raise an error when purging an hda if config does not allow")
|
||||
assert not item1.purged
|
||||
self.assertRaises(exceptions.ConfigDoesNotAllowException, self.hda_manager.purge, item1)
|
||||
with self.assertRaises(exceptions.ConfigDoesNotAllowException):
|
||||
self.hda_manager.purge(item1)
|
||||
assert not item1.deleted
|
||||
assert not item1.purged
|
||||
|
||||
@@ -206,10 +207,12 @@ class HDAManagerTestCase(HDATestCase):
|
||||
assert not self.hda_manager.is_owner(item1, non_owner)
|
||||
|
||||
self.log("should raise an error when checking ownership with non-owner")
|
||||
self.assertRaises(exceptions.ItemOwnershipException, self.hda_manager.error_unless_owner, item1, non_owner)
|
||||
with self.assertRaises(exceptions.ItemOwnershipException):
|
||||
self.hda_manager.error_unless_owner(item1, non_owner)
|
||||
|
||||
self.log("should raise an error when checking ownership with anonymous")
|
||||
self.assertRaises(exceptions.ItemOwnershipException, self.hda_manager.error_unless_owner, item1, None)
|
||||
with self.assertRaises(exceptions.ItemOwnershipException):
|
||||
self.hda_manager.error_unless_owner(item1, None)
|
||||
|
||||
self.log("should not raise an error when checking ownership with owner")
|
||||
assert self.hda_manager.error_unless_owner(item1, owner) == item1
|
||||
@@ -240,13 +243,12 @@ class HDAManagerTestCase(HDATestCase):
|
||||
"after setting a dataset to private (one user) permissions, "
|
||||
+ "access should be not allowed for other users"
|
||||
)
|
||||
self.assertRaises(
|
||||
exceptions.ItemAccessibilityException,
|
||||
self.hda_manager.get_accessible,
|
||||
item1.id,
|
||||
non_owner,
|
||||
current_history=self.trans.history,
|
||||
)
|
||||
with self.assertRaises(exceptions.ItemAccessibilityException):
|
||||
self.hda_manager.get_accessible(
|
||||
item1.id,
|
||||
non_owner,
|
||||
current_history=self.trans.history,
|
||||
)
|
||||
|
||||
self.log(
|
||||
"a copy of a restricted dataset in another users history should be inaccessible even to "
|
||||
@@ -256,26 +258,24 @@ class HDAManagerTestCase(HDATestCase):
|
||||
self.trans.set_history(history2)
|
||||
item2 = self.hda_manager.copy(item1, history=history2)
|
||||
assert isinstance(item2, model.HistoryDatasetAssociation)
|
||||
self.assertRaises(
|
||||
exceptions.ItemAccessibilityException,
|
||||
self.hda_manager.get_accessible,
|
||||
item2.id,
|
||||
non_owner,
|
||||
current_history=self.trans.history,
|
||||
)
|
||||
with self.assertRaises(exceptions.ItemAccessibilityException):
|
||||
self.hda_manager.get_accessible(
|
||||
item2.id,
|
||||
non_owner,
|
||||
current_history=self.trans.history,
|
||||
)
|
||||
|
||||
self.log("a restricted dataset cannot be accessed by anonymous users")
|
||||
anon_user = None
|
||||
self.trans.set_user(anon_user)
|
||||
history3 = self.history_manager.create(name="anon_history", user=anon_user)
|
||||
self.trans.set_history(history3)
|
||||
self.assertRaises(
|
||||
exceptions.ItemAccessibilityException,
|
||||
self.hda_manager.get_accessible,
|
||||
item1.id,
|
||||
anon_user,
|
||||
current_history=self.trans.history,
|
||||
)
|
||||
with self.assertRaises(exceptions.ItemAccessibilityException):
|
||||
self.hda_manager.get_accessible(
|
||||
item1.id,
|
||||
anon_user,
|
||||
current_history=self.trans.history,
|
||||
)
|
||||
|
||||
def test_anon_ownership(self):
|
||||
anon_user = None
|
||||
@@ -297,8 +297,10 @@ class HDAManagerTestCase(HDATestCase):
|
||||
self.log("should raise an error when checking ownership on anonymous' dataset with other user")
|
||||
non_owner = self.user_manager.create(**user3_data)
|
||||
assert not self.hda_manager.is_owner(item1, non_owner)
|
||||
self.assertRaises(exceptions.ItemOwnershipException, self.hda_manager.error_unless_owner, item1, non_owner)
|
||||
self.assertRaises(exceptions.ItemOwnershipException, self.hda_manager.get_owned, item1.id, non_owner)
|
||||
with self.assertRaises(exceptions.ItemOwnershipException):
|
||||
self.hda_manager.error_unless_owner(item1, non_owner)
|
||||
with self.assertRaises(exceptions.ItemOwnershipException):
|
||||
self.hda_manager.get_owned(item1.id, non_owner)
|
||||
|
||||
def test_anon_accessibility(self):
|
||||
anon_user = None
|
||||
@@ -320,9 +322,8 @@ class HDAManagerTestCase(HDATestCase):
|
||||
+ "permissions do not allow"
|
||||
)
|
||||
assert not self.hda_manager.is_accessible(item1, anon_user)
|
||||
self.assertRaises(
|
||||
exceptions.ItemAccessibilityException, self.hda_manager.error_unless_accessible, item1, anon_user
|
||||
)
|
||||
with self.assertRaises(exceptions.ItemAccessibilityException):
|
||||
self.hda_manager.error_unless_accessible(item1, anon_user)
|
||||
|
||||
self.log(
|
||||
"those users with access permissions should still be allowed access to datasets "
|
||||
@@ -339,7 +340,8 @@ class HDAManagerTestCase(HDATestCase):
|
||||
|
||||
hda.state = model.Dataset.states.UPLOAD
|
||||
self.log("should raise an error when calling error_if_uploading and in the uploading state")
|
||||
self.assertRaises(exceptions.Conflict, self.hda_manager.error_if_uploading, hda)
|
||||
with self.assertRaises(exceptions.Conflict):
|
||||
self.hda_manager.error_if_uploading(hda)
|
||||
|
||||
def test_data_conversion_status(self):
|
||||
hda = self._create_vanilla_hda()
|
||||
@@ -535,9 +537,8 @@ class HDADeserializerTestCase(HDATestCase):
|
||||
|
||||
self.log("should raise when deserializing deleted from non-bool")
|
||||
assert not hda.deleted
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException, self.hda_deserializer.deserialize, hda, {"deleted": None}
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.hda_deserializer.deserialize(hda, {"deleted": None})
|
||||
assert not hda.deleted
|
||||
self.log("should be able to deserialize deleted from True")
|
||||
self.hda_deserializer.deserialize(hda, {"deleted": True})
|
||||
@@ -550,9 +551,8 @@ class HDADeserializerTestCase(HDATestCase):
|
||||
hda = self._create_vanilla_hda()
|
||||
|
||||
self.log("should raise when deserializing purged from non-bool")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException, self.hda_deserializer.deserialize, hda, {"purged": None}
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.hda_deserializer.deserialize(hda, {"purged": None})
|
||||
assert not hda.purged
|
||||
self.log("should be able to deserialize purged from True")
|
||||
self.hda_deserializer.deserialize(hda, {"purged": True})
|
||||
@@ -567,9 +567,8 @@ class HDADeserializerTestCase(HDATestCase):
|
||||
|
||||
self.log("should raise when deserializing from non-bool")
|
||||
assert hda.visible
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException, self.hda_deserializer.deserialize, hda, {"visible": "None"}
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.hda_deserializer.deserialize(hda, {"visible": "None"})
|
||||
assert hda.visible
|
||||
self.log("should be able to deserialize from False")
|
||||
self.hda_deserializer.deserialize(hda, {"visible": False})
|
||||
@@ -586,9 +585,8 @@ class HDADeserializerTestCase(HDATestCase):
|
||||
self.hda_deserializer.deserialize(hda, {"genome_build": None})
|
||||
assert hda.dbkey == "?"
|
||||
self.log("should raise when deserializing from non-string")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException, self.hda_deserializer.deserialize, hda, {"genome_build": 12}
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.hda_deserializer.deserialize(hda, {"genome_build": 12})
|
||||
self.log("should be able to deserialize from unicode")
|
||||
date_palm = "نخيل التمر"
|
||||
self.hda_deserializer.deserialize(hda, {"genome_build": date_palm})
|
||||
@@ -601,13 +599,11 @@ class HDADeserializerTestCase(HDATestCase):
|
||||
hda = self._create_vanilla_hda()
|
||||
|
||||
self.log("should raise when deserializing from non-string")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException, self.hda_deserializer.deserialize, hda, {"name": True}
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.hda_deserializer.deserialize(hda, {"name": True})
|
||||
self.log("should raise when deserializing from None")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException, self.hda_deserializer.deserialize, hda, {"name": None}
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.hda_deserializer.deserialize(hda, {"name": None})
|
||||
# self.log( 'should deserialize to empty string from None' )
|
||||
# self.hda_deserializer.deserialize( hda, { 'name': None } )
|
||||
# assert hda.name == ""
|
||||
@@ -623,13 +619,11 @@ class HDADeserializerTestCase(HDATestCase):
|
||||
hda = self._create_vanilla_hda()
|
||||
|
||||
self.log("should raise when deserializing from non-string")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException, self.hda_deserializer.deserialize, hda, {"info": True}
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.hda_deserializer.deserialize(hda, {"info": True})
|
||||
self.log("should raise when deserializing from None")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException, self.hda_deserializer.deserialize, hda, {"info": None}
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.hda_deserializer.deserialize(hda, {"info": None})
|
||||
self.log("should be able to deserialize from unicode")
|
||||
rice = "飯"
|
||||
self.hda_deserializer.deserialize(hda, {"info": rice})
|
||||
|
||||
@@ -347,7 +347,8 @@ class HistoryContentsFilterParserTestCase(HistoryAsContainerBaseTestCase):
|
||||
).isoformat(sep=" ")
|
||||
|
||||
self.log("should error if bad epoch is used")
|
||||
self.assertRaises(ValueError, self.filter_parser.parse_date, "0x000234")
|
||||
with self.assertRaises(ValueError):
|
||||
self.filter_parser.parse_date("0x000234")
|
||||
|
||||
# -- datetime strings
|
||||
self.log("should allow date alone")
|
||||
@@ -364,11 +365,15 @@ class HistoryContentsFilterParserTestCase(HistoryAsContainerBaseTestCase):
|
||||
assert self.filter_parser.parse_date("2009-02-13T18:13:00.123Z") == "2009-02-13 18:13:00.123"
|
||||
|
||||
self.log("should error if timezone is added")
|
||||
self.assertRaises(ValueError, self.filter_parser.parse_date, "2009-02-13T18:13:00.123+0700")
|
||||
with self.assertRaises(ValueError):
|
||||
self.filter_parser.parse_date("2009-02-13T18:13:00.123+0700")
|
||||
|
||||
self.log("should error if locale is used")
|
||||
self.assertRaises(ValueError, self.filter_parser.parse_date, "Fri Feb 13 18:31:30 2009")
|
||||
with self.assertRaises(ValueError):
|
||||
self.filter_parser.parse_date("Fri Feb 13 18:31:30 2009")
|
||||
|
||||
self.log("should error if wrong milliseconds format is used")
|
||||
self.assertRaises(ValueError, self.filter_parser.parse_date, "2009-02-13 18:13:00.")
|
||||
self.assertRaises(ValueError, self.filter_parser.parse_date, "2009-02-13 18:13:00.1234567")
|
||||
with self.assertRaises(ValueError):
|
||||
self.filter_parser.parse_date("2009-02-13 18:13:00.")
|
||||
with self.assertRaises(ValueError):
|
||||
self.filter_parser.parse_date("2009-02-13 18:13:00.1234567")
|
||||
|
||||
@@ -127,8 +127,10 @@ class HistoryManagerTestCase(BaseTestCase):
|
||||
assert not self.history_manager.is_owner(item1, non_owner)
|
||||
|
||||
self.log("should raise an error when checking ownership with non-owner")
|
||||
self.assertRaises(exceptions.ItemOwnershipException, self.history_manager.error_unless_owner, item1, non_owner)
|
||||
self.assertRaises(exceptions.ItemOwnershipException, self.history_manager.get_owned, item1.id, non_owner)
|
||||
with self.assertRaises(exceptions.ItemOwnershipException):
|
||||
self.history_manager.error_unless_owner(item1, non_owner)
|
||||
with self.assertRaises(exceptions.ItemOwnershipException):
|
||||
self.history_manager.get_owned(item1.id, non_owner)
|
||||
|
||||
self.log("should not raise an error when checking ownership with owner")
|
||||
assert self.history_manager.error_unless_owner(item1, owner) == item1
|
||||
@@ -151,12 +153,10 @@ class HistoryManagerTestCase(BaseTestCase):
|
||||
assert not self.history_manager.is_accessible(item1, non_owner)
|
||||
|
||||
self.log("should raise an error when checking accessibility with non-owner")
|
||||
self.assertRaises(
|
||||
exceptions.ItemAccessibilityException, self.history_manager.error_unless_accessible, item1, non_owner
|
||||
)
|
||||
self.assertRaises(
|
||||
exceptions.ItemAccessibilityException, self.history_manager.get_accessible, item1.id, non_owner
|
||||
)
|
||||
with self.assertRaises(exceptions.ItemAccessibilityException):
|
||||
self.history_manager.error_unless_accessible(item1, non_owner)
|
||||
with self.assertRaises(exceptions.ItemAccessibilityException):
|
||||
self.history_manager.get_accessible(item1.id, non_owner)
|
||||
|
||||
self.log("should not raise an error when checking ownership with owner")
|
||||
assert self.history_manager.error_unless_accessible(item1, owner) == item1
|
||||
@@ -603,7 +603,8 @@ class HistorySerializerTestCase(BaseTestCase):
|
||||
self.assertIsJsonifyable(serialized)
|
||||
|
||||
self.log("serialization of user_rating without user should error")
|
||||
self.assertRaises(base.ModelSerializingError, serializer.serialize, item, ["user_rating"])
|
||||
with self.assertRaises(base.ModelSerializingError):
|
||||
serializer.serialize(item, ["user_rating"])
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -657,9 +658,8 @@ class HistoryDeserializerTestCase(BaseTestCase):
|
||||
assert len(user_shares) == 0
|
||||
|
||||
self.log("adding a bad user id should error")
|
||||
self.assertRaises(
|
||||
exceptions.MalformedId, deserializer.deserialize, item, {"users_shared_with": [None]}, user=user2
|
||||
)
|
||||
with self.assertRaises(exceptions.MalformedId):
|
||||
deserializer.deserialize(item, {"users_shared_with": [None]}, user=user2)
|
||||
|
||||
self.log("adding a non-existing user id should do nothing")
|
||||
non_user_id = self.app.security.encode_id(99)
|
||||
@@ -688,45 +688,40 @@ class HistoryFiltersTestCase(BaseTestCase):
|
||||
|
||||
def test_parse_filters_invalid_filters(self):
|
||||
self.log("should error on non-column attr")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException,
|
||||
self.filter_parser.parse_filters,
|
||||
[
|
||||
("merp", "eq", "wot"),
|
||||
],
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.filter_parser.parse_filters(
|
||||
[
|
||||
("merp", "eq", "wot"),
|
||||
],
|
||||
)
|
||||
self.log("should error on non-allowlisted attr")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException,
|
||||
self.filter_parser.parse_filters,
|
||||
[
|
||||
("user_id", "eq", "wot"),
|
||||
],
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.filter_parser.parse_filters(
|
||||
[
|
||||
("user_id", "eq", "wot"),
|
||||
],
|
||||
)
|
||||
self.log("should error on non-allowlisted op")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException,
|
||||
self.filter_parser.parse_filters,
|
||||
[
|
||||
("name", "lt", "wot"),
|
||||
],
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.filter_parser.parse_filters(
|
||||
[
|
||||
("name", "lt", "wot"),
|
||||
],
|
||||
)
|
||||
self.log("should error on non-listed fn op")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException,
|
||||
self.filter_parser.parse_filters,
|
||||
[
|
||||
("annotation", "like", "wot"),
|
||||
],
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.filter_parser.parse_filters(
|
||||
[
|
||||
("annotation", "like", "wot"),
|
||||
],
|
||||
)
|
||||
self.log("should error on val parsing error")
|
||||
self.assertRaises(
|
||||
exceptions.RequestParameterInvalidException,
|
||||
self.filter_parser.parse_filters,
|
||||
[
|
||||
("deleted", "eq", "wot"),
|
||||
],
|
||||
)
|
||||
with self.assertRaises(exceptions.RequestParameterInvalidException):
|
||||
self.filter_parser.parse_filters(
|
||||
[
|
||||
("deleted", "eq", "wot"),
|
||||
],
|
||||
)
|
||||
|
||||
def test_orm_filter_parsing(self):
|
||||
user2 = self.user_manager.create(**user2_data)
|
||||
|
||||
@@ -67,17 +67,15 @@ class UserManagerTestCase(BaseTestCase):
|
||||
self.user_manager.create(**user2_data)
|
||||
|
||||
self.log("emails must be unique")
|
||||
self.assertRaises(
|
||||
exceptions.Conflict,
|
||||
self.user_manager.create,
|
||||
**dict(email="user2@user2.user2", username="user2a", password=default_password),
|
||||
)
|
||||
with self.assertRaises(exceptions.Conflict):
|
||||
self.user_manager.create(
|
||||
**dict(email="user2@user2.user2", username="user2a", password=default_password),
|
||||
)
|
||||
self.log("usernames must be unique")
|
||||
self.assertRaises(
|
||||
exceptions.Conflict,
|
||||
self.user_manager.create,
|
||||
**dict(email="user2a@user2.user2", username="user2", password=default_password),
|
||||
)
|
||||
with self.assertRaises(exceptions.Conflict):
|
||||
self.user_manager.create(
|
||||
**dict(email="user2a@user2.user2", username="user2", password=default_password),
|
||||
)
|
||||
|
||||
def test_trimming(self):
|
||||
self.log("emails must be trimmed")
|
||||
@@ -114,7 +112,8 @@ class UserManagerTestCase(BaseTestCase):
|
||||
assert self.user_manager.is_admin(self.admin_user)
|
||||
assert not self.user_manager.is_admin(user2)
|
||||
assert self.user_manager.admins() == [self.admin_user]
|
||||
self.assertRaises(exceptions.AdminRequiredException, self.user_manager.error_unless_admin, user2)
|
||||
with self.assertRaises(exceptions.AdminRequiredException):
|
||||
self.user_manager.error_unless_admin(user2)
|
||||
assert self.user_manager.error_unless_admin(self.admin_user) == self.admin_user
|
||||
|
||||
def test_anonymous(self):
|
||||
@@ -333,23 +332,21 @@ class UserDeserializerTestCase(BaseTestCase):
|
||||
trans=self.trans,
|
||||
)
|
||||
assert "Public name cannot be empty" in str(exception)
|
||||
self.assertRaises(
|
||||
base_manager.ModelDeserializingError,
|
||||
self.deserializer.deserialize,
|
||||
user,
|
||||
{"username": "f,d,r,"},
|
||||
trans=self.trans,
|
||||
)
|
||||
with self.assertRaises(base_manager.ModelDeserializingError):
|
||||
self.deserializer.deserialize(
|
||||
user,
|
||||
{"username": "f,d,r,"},
|
||||
trans=self.trans,
|
||||
)
|
||||
|
||||
self.log("usernames must be unique")
|
||||
self.user_manager.create(**user3_data)
|
||||
self.assertRaises(
|
||||
base_manager.ModelDeserializingError,
|
||||
self.deserializer.deserialize,
|
||||
user,
|
||||
{"username": "user3"},
|
||||
trans=self.trans,
|
||||
)
|
||||
with self.assertRaises(base_manager.ModelDeserializingError):
|
||||
self.deserializer.deserialize(
|
||||
user,
|
||||
{"username": "user3"},
|
||||
trans=self.trans,
|
||||
)
|
||||
|
||||
self.log("username should be updatable")
|
||||
new_name = "double-plus-good"
|
||||
|
||||
@@ -144,7 +144,8 @@ class ParameterParsingTestCase(BaseParameterTestCase):
|
||||
assert param.value == "9"
|
||||
assert param.type == "integer"
|
||||
param.validate(8)
|
||||
self.assertRaises(Exception, lambda: param.validate(10))
|
||||
with self.assertRaises(ValueError):
|
||||
param.validate(10)
|
||||
|
||||
def test_float_params(self):
|
||||
param = self._parameter_for(
|
||||
@@ -156,7 +157,8 @@ class ParameterParsingTestCase(BaseParameterTestCase):
|
||||
assert param.value == "9"
|
||||
assert param.type == "float"
|
||||
param.validate(8.1)
|
||||
self.assertRaises(Exception, lambda: param.validate(10.0))
|
||||
with self.assertRaises(ValueError):
|
||||
param.validate(10.0)
|
||||
|
||||
def test_boolean_params(self):
|
||||
param = self._parameter_for(
|
||||
|
||||
@@ -104,8 +104,10 @@ class Test_BaseDataProvider(BaseTestCase):
|
||||
return self.provider_class(source)
|
||||
|
||||
# two objects without __iter__ method: build in function and int
|
||||
self.assertRaises(exceptions.InvalidDataProviderSource, non_iterator_dprov, sum)
|
||||
self.assertRaises(exceptions.InvalidDataProviderSource, non_iterator_dprov, 40)
|
||||
with self.assertRaises(exceptions.InvalidDataProviderSource):
|
||||
non_iterator_dprov(sum)
|
||||
with self.assertRaises(exceptions.InvalidDataProviderSource):
|
||||
non_iterator_dprov(40)
|
||||
|
||||
def test_writemethods(self):
|
||||
"""should throw an error if any write methods are called"""
|
||||
@@ -117,9 +119,12 @@ class Test_BaseDataProvider(BaseTestCase):
|
||||
method = getattr(provider, method_name)
|
||||
return method(*args)
|
||||
|
||||
self.assertRaises(NotImplementedError, call_method, provider, "truncate", 20)
|
||||
self.assertRaises(NotImplementedError, call_method, provider, "write", "bler")
|
||||
self.assertRaises(NotImplementedError, call_method, provider, "writelines", ["one", "two"])
|
||||
with self.assertRaises(NotImplementedError):
|
||||
call_method(provider, "truncate", 20)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
call_method(provider, "write", "bler")
|
||||
with self.assertRaises(NotImplementedError):
|
||||
call_method(provider, "writelines", ["one", "two"])
|
||||
|
||||
def test_readlines(self):
|
||||
"""readlines should return all the data in list form"""
|
||||
|
||||
Reference in New Issue
Block a user