def test_dictionary_constraints(self): """Test the dictionary constraints.""" checker = StorageChecker() checker.add_constraint("x", {"a": 1, "b": 2, "c": 3}) self.assertIn("x", checker.constraints) self.assertEqual(checker.constraints["x"], {"a": 1, "b": 2, "c": 3}) checker.set_constraint("x", {"e": 4, "f": 5}) self.assertIn("x", checker.constraints) self.assertEqual(checker.constraints["x"], {"e": 4, "f": 5})
def test_dictionary_constraints(self): """Test the dictionary constraints.""" checker = StorageChecker() checker.add_constraint("x", {"a": 1, "b": 2, "c": 3}) assert "x" in checker.constraints assert checker.constraints["x"] == {"a": 1, "b": 2, "c": 3} checker.set_constraint("x", {"e": 4, "f": 5}) assert "x" in checker.constraints assert checker.constraints["x"] == {"e": 4, "f": 5}
def test_simple_constraints(self): """Test simple constraint adding.""" checker = StorageChecker() # Try to add a new constraint with a wrong method. with pytest.raises(KeyError): checker.set_constraint("x", None) # Try to add a new constraint two times. checker.add_constraint("x", None) with pytest.raises(KeyError): checker.add_constraint("x", None)
def test_check_constraints(self): """Test constraints checking.""" checker = StorageChecker() def check(storage, constraints, report_error, report_warning): report_warning("%s" % constraints) checker.add_check(check) report = checker.check(None) self.assertListEqual(report.warnings, ["{}"]) checker.add_constraint("x", 1) report = checker.check(None) self.assertListEqual(report.warnings, ["{'x': 1}"]) checker.set_constraint("x", 0) report = checker.check(None) self.assertListEqual(report.warnings, ["{'x': 0}"])
def test_complicated(self): """Run a complicated check.""" checker = StorageChecker() # Set the checks, def check_x(storage, constraints, report_error, report_warning): if constraints["x"] != 1: report_error("x is not equal to 1") def check_y(storage, constraints, report_error, report_warning): if constraints["y"] != 2: report_error("y is not equal to 2") def check_z(storage, constraints, report_error, report_warning): if constraints["z"] != 3: report_error("z is not equal to 3") checker.add_check(check_x) checker.add_check(check_y) checker.add_check(check_z) # Set the constraints. checker.add_constraint("x", 1) checker.add_constraint("y", 2) checker.add_constraint("z", 3) # Run the checker. OK report = checker.check(None) self.assertEqual(report.success, True) self.assertListEqual(report.errors, []) self.assertListEqual(report.warnings, []) # Set constraints to different values. checker.set_constraint("x", 0) checker.set_constraint("y", 1) checker.set_constraint("z", 2) # Run the checker. FAIL report = checker.check(None) self.assertEqual(report.success, False) self.assertListEqual(report.errors, [ "x is not equal to 1", "y is not equal to 2", "z is not equal to 3" ]) self.assertListEqual(report.warnings, []) # Run the checker. Test SKIP. report = checker.check(None, skip=(check_y,)) self.assertEqual(report.success, False) self.assertListEqual(report.errors, [ "x is not equal to 1", "z is not equal to 3" ]) self.assertListEqual(report.warnings, []) # Run the checker. Test CONSTRAINTS. constraints = {"x": 1, "y": 2, "z": 3} report = checker.check(None, constraints=constraints) self.assertEqual(report.success, True) self.assertListEqual(report.errors, []) self.assertListEqual(report.warnings, []) # Remove checks. checker.remove_check(check_x) checker.remove_check(check_y) checker.remove_check(check_z) report = checker.check(None) self.assertEqual(report.success, True) self.assertListEqual(report.errors, []) self.assertListEqual(report.warnings, [])