def test_base_instance_relationship_add_child(db_session): """Test adding a child when loading with a base instance.""" track = db_session.query(Track).filter(Track.track_id == 1).first() instance = Album(album_id=9999) instance.tracks.append(track) data = {"album_id": 1, "tracks": [{"track_id": 1}]} schema = AlbumSchema(session=db_session, partial=True) result = schema.load(data, instance=instance) assert result.tracks[0].track_id == 1
def test_instance_relationship_nested_opts(db_session): """Test nested opts enable complete relation replacement.""" data = {"album_id": 2, "tracks": [{"track_id": 1}]} nested_opts = {"tracks": NestedOpts(partial=False)} schema = AlbumSchema(session=db_session, nested_opts=nested_opts, partial=True) result = schema.load(data) assert result.tracks[0].track_id == 1 assert len(result.tracks) == 1
def test_schema_relationship_bad_data(db_session): """Test bad data supplied to a relationship fails properly.""" schema = AlbumSchema(session=db_session, partial=True) data = {"tracks": [{"bytes": "TEST"}]} errors = {} try: schema.load(data=data) except ValidationError as exc: errors = exc.messages assert errors["tracks"][0]["bytes"]
def test_schema_fail_missing_key(): """Test schema failure missing key error message.""" schema = AlbumSchema() with raises(AssertionError): schema.make_error(key="test")
def test_permission_denied(db_session): """Test permission denied errors work as expected.""" data = {"album_id": 340, "title": "Denied"} schema = AlbumSchema(session=db_session, partial=True) with raises(PermissionValidationError): schema.load(data)
def test_relationship_invalid_op(db_session): """Test an invalid operation on a relationship fails.""" data = {"album_id": 1, "tracks": [{"track_id": 1, "$op": "test"}]} schema = AlbumSchema(session=db_session, partial=True) with raises(ValidationError): schema.load(data)
def test_relationship_invalid_remove(db_session): """Test trying to remove a child from the wrong parent fails.""" data = {"album_id": 1, "tracks": [{"track_id": 597, "$op": "remove"}]} schema = AlbumSchema(session=db_session, partial=True) with raises(ValidationError): schema.load(data)