Skip to content

Commit

Permalink
Reset self.instance after each load
Browse files Browse the repository at this point in the history
Fix #78
  • Loading branch information
sloria committed Jan 5, 2017
1 parent 26fa8f0 commit 1531189
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion marshmallow_sqlalchemy/schema.py
Expand Up @@ -183,7 +183,9 @@ def load(self, data, session=None, instance=None, *args, **kwargs):
self.instance = instance or self.instance
if not self.session:
raise ValueError('Deserialization requires a session')
return super(ModelSchema, self).load(data, *args, **kwargs)
ret = super(ModelSchema, self).load(data, *args, **kwargs)
self.instance = None
return ret

def validate(self, data, session=None, *args, **kwargs):
self.session = session or self.session
Expand Down
17 changes: 17 additions & 0 deletions tests/test_marshmallow_sqlalchemy.py
Expand Up @@ -668,6 +668,23 @@ def test_model_schema_loading_custom_instance(self, models, schemas, student, se
assert result.data is student
assert result.data.current_school == student.current_school

# Regression test for https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/78
def test_model_schema_loading_resets_instance(self, models, schemas, student):
schema = schemas.StudentSchema()
result1 = schema.load({'full_name': 'new name'}, instance=student)
assert not result1.errors
data1 = result1.data
assert data1.id == student.id
assert data1.full_name == student.full_name

result2 = schema.load({'full_name': 'new name2'})
assert not result2.errors
data2 = result2.data
assert isinstance(data2, models.Student)
# loaded data is different from first instance (student)
assert data2 != student
assert data2.full_name == 'new name2'

def test_model_schema_loading_no_instance_or_pk(self, models, schemas, student, session):
schema = schemas.StudentSchema()
dump_data = {'full_name': 'Terry Gilliam'}
Expand Down

0 comments on commit 1531189

Please sign in to comment.