def _commands_from_ndjson(lines): """Turn multiple lines of JSON into Command objects. :param lines: An iterable of JSON strings :return: A generator of `Command` objects :rtype: Iterator[Command] :raise InvalidJSONError: when invalid JSON is encountered """ for line_number, line in enumerate(lines): if not line: continue try: data = json.loads(line) except JSONDecodeError as err: raise InvalidJSONError( f"Invalid JSON on line {line_number}: {err.args[0]}") # Try catch JSON errors here yield CommandBuilder.from_data(data)
def test_we_get_schema_errors_with_malformed_commands(self, data): with pytest.raises(SchemaValidationError): CommandBuilder.from_data(data)
def test_deserialise_configure_can_fail(self, configuration_body): configuration_body["random"] = "new_value" with pytest.raises(SchemaValidationError): CommandBuilder.from_data([CONFIGURE, configuration_body])
def test_deserialise_configure_ok(self, configuration_body): command = CommandBuilder.from_data([CONFIGURE, configuration_body]) assert isinstance(command.body, Configuration)
def test_deserialise_group_membership_can_fail( self, create_group_membership_body): del create_group_membership_body["data"]["relationships"]["group"] with pytest.raises(SchemaValidationError): CommandBuilder.from_data([CREATE, create_group_membership_body])
def test_deserialise_group_membership_ok(self, create_group_membership_body): command = CommandBuilder.from_data( [CREATE, create_group_membership_body]) assert isinstance(command.body, CreateGroupMembership)
def test_deserialise_group_can_fail(self, upsert_group_body): del upsert_group_body["data"]["attributes"]["name"] with pytest.raises(SchemaValidationError): CommandBuilder.from_data([UPSERT, upsert_group_body])
def test_deserialise_group_ok(self, upsert_group_body): command = CommandBuilder.from_data([UPSERT, upsert_group_body]) assert isinstance(command.body, UpsertGroup)