def test_draft3_schema_draft4_validator(self): stdout, stderr = NativeIO(), NativeIO() with self.assertRaises(SchemaError): cli.run( { "validator": Draft4Validator, "schema": { "anyOf": [ { "minimum": 20 }, { "type": "string" }, { "required": True }, ], }, "instances": [1], "error_format": "{error.message}", }, stdout=stdout, stderr=stderr, )
def test_draft3_schema_draft4_validator(self): stdout, stderr = StringIO(), StringIO() with self.assertRaises(SchemaError): cli.run( { "validator": Draft4Validator, "schema": {"anyOf": [{"minimum": 20}, {"type": "string"}, {"required": True}]}, "instances": [1], "error_format": "{error.message}", }, stdout=stdout, stderr=stderr, )
def test_successful_validation(self): stdout, stderr = StringIO(), StringIO() exit_code = cli.run( {"validator": fake_validator(), "schema": {}, "instances": [1], "error_format": "{error.message}"}, stdout=stdout, stderr=stderr, ) self.assertFalse(stdout.getvalue()) self.assertFalse(stderr.getvalue()) self.assertEqual(exit_code, 0)
def test_successful_validation(self): stdout, stderr = StringIO(), StringIO() exit_code = cli.run( { "validator": fake_validator(), "schema": {}, "instances": [1], "error_format": "{error.message}", }, stdout=stdout, stderr=stderr, ) self.assertFalse(stdout.getvalue()) self.assertFalse(stderr.getvalue()) self.assertEqual(exit_code, 0)
def test_unsuccessful_validation(self): error = ValidationError("I am an error!", instance=1) stdout, stderr = StringIO(), StringIO() exit_code = cli.run( { "validator": fake_validator([error]), "schema": {}, "instances": [1], "error_format": "{error.instance} - {error.message}", }, stdout=stdout, stderr=stderr, ) self.assertFalse(stdout.getvalue()) self.assertEqual(stderr.getvalue(), "1 - I am an error!") self.assertEqual(exit_code, 1)
def test_piping(self): stdout, stderr, stdin = NativeIO(), NativeIO(), NativeIO("{}") exit_code = cli.run( { "validator": fake_validator(), "schema": {}, "instances": [], "error_format": "{error.message}", }, stdout=stdout, stderr=stderr, stdin=stdin, ) self.assertFalse(stdout.getvalue()) self.assertFalse(stderr.getvalue()) self.assertEqual(exit_code, 0)
def test_unsuccessful_validation_multiple_instances(self): first_errors = [ValidationError("9", instance=1), ValidationError("8", instance=1)] second_errors = [ValidationError("7", instance=2)] stdout, stderr = StringIO(), StringIO() exit_code = cli.run( { "validator": fake_validator(first_errors, second_errors), "schema": {}, "instances": [1, 2], "error_format": "{error.instance} - {error.message}\t", }, stdout=stdout, stderr=stderr, ) self.assertFalse(stdout.getvalue()) self.assertEqual(stderr.getvalue(), "1 - 9\t1 - 8\t2 - 7\t") self.assertEqual(exit_code, 1)
def test_unsuccessful_validation_multiple_instances(self): first_errors = [ ValidationError("9", instance=1), ValidationError("8", instance=1), ] second_errors = [ValidationError("7", instance=2)] stdout, stderr = StringIO(), StringIO() exit_code = cli.run( { "validator": fake_validator(first_errors, second_errors), "schema": {}, "instances": [1, 2], "error_format": "{error.instance} - {error.message}\t", }, stdout=stdout, stderr=stderr, ) self.assertFalse(stdout.getvalue()) self.assertEqual(stderr.getvalue(), "1 - 9\t1 - 8\t2 - 7\t") self.assertEqual(exit_code, 1)
def run_cli(self, argv, files={}, stdin=StringIO(), exit_code=0, **override): arguments = cli.parse_args(argv) arguments.update(override) self.assertFalse(hasattr(cli, "open")) cli.open = fake_open(files) try: stdout, stderr = StringIO(), StringIO() actual_exit_code = cli.run( arguments, stdin=stdin, stdout=stdout, stderr=stderr, ) finally: del cli.open self.assertEqual( actual_exit_code, exit_code, msg=dedent( """ Expected an exit code of {} != {}. stdout: {} stderr: {} """.format( exit_code, actual_exit_code, stdout.getvalue(), stderr.getvalue(), ), ), ) return stdout.getvalue(), stderr.getvalue()