Skip to content

#88 Support the addition of new fields to an existing schema. #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
#88 Support the addition of new fields to an existing schema.
 - Especially ListFields and DictFields
  • Loading branch information
JonathanWylie committed Mar 15, 2016
commit 26b34d36fa14379a2d57c1bbc5fa4468723274ac
29 changes: 28 additions & 1 deletion couchdb/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,34 @@ def build(cls, **d):

@classmethod
def wrap(cls, data):
instance = cls()
""" Wrap the data object with cls, so that any data changes are made to
the data object

cls may define schema for data that is not in data yet,
eg a document is saved to the DB
the document schema is changed to contain another kind of data
load the old document with the new schema
we want to be able to
write the data for the new schema
get a default value for the new schema

:param data: A dictionary object of data
:return: an instance of class which provides a nice interface to the
information in the data object
"""

# Build the instance, this means defaults are set for any new fields
instance = cls(**data)
# instance now has a _data it's own data object which has been created
# based on data, and any new schema which may not have been in
# the original doc

# We have to wrap the data object, but we don't want to loose the
# defaults created in instance._data
# Merge any changes of data from instance back into the main data object
data.update(instance._data)

# make the instance wrap the data object
instance._data = data
return instance

Expand Down
81 changes: 81 additions & 0 deletions couchdb/tests/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

import copy
from decimal import Decimal
import unittest

Expand Down Expand Up @@ -250,6 +251,86 @@ def test_query(self):
self.assertEqual(type(results.rows[0]), self.Item)


class TestWrap(testutil.TempDatabaseMixin, unittest.TestCase):

def test_simple_schema_change(self):
my_mapping = mapping.Mapping.build(
name=mapping.TextField(),
)
my_instance = my_mapping.wrap({})
my_instance.name = 'Harry'

data = copy.copy(my_instance._data)

my_new_mapping = mapping.Mapping.build(
name=mapping.TextField(),
age=mapping.IntegerField(),
)

instance = my_new_mapping.wrap(data)
instance.age = 32
assert data['age'] == 32

def test_defaults_for_new_schema_should_be_set(self):
my_mapping = mapping.Mapping.build(
name=mapping.TextField(),
)
my_instance = my_mapping.wrap({})
my_instance.name = 'Harry'

data = copy.copy(my_instance._data)

my_new_mapping = mapping.Mapping.build(
name=mapping.TextField(),
address=mapping.DictField(mapping.Mapping.build(
number=mapping.IntegerField(default=1),
street=mapping.TextField(default='street')
)),
pets=mapping.ListField(mapping.TextField(), default=[])
)

my_new_instance = my_new_mapping.wrap(data)

assert data['pets'] == []
assert data['address']['number'] == 1
assert data['address']['street'] == 'street'
assert my_new_instance.address.number == 1
assert my_new_instance.address.street == 'street'

def test_can_set_new_schema_data(self):
my_mapping = mapping.Mapping.build(
name=mapping.TextField(),
)
my_instance = my_mapping.wrap({})
my_instance.name = 'Sherlock'

data = copy.copy(my_instance._data)

my_new_mapping = mapping.Mapping.build(
name=mapping.TextField(),
address=mapping.DictField(mapping.Mapping.build(
number=mapping.TextField(),
street=mapping.TextField()
)),
pets=mapping.ListField(mapping.TextField(), default=[]),
)

my_new_instance = my_new_mapping.wrap(data)

my_new_instance.pets.append('dog')
my_new_instance.pets.append('cat')
my_new_instance.address.number = '221B'
my_new_instance.address.street = 'Baker Street'

assert data['address']['number'] == '221B'
assert data['address']['street'] == 'Baker Street'
assert data['pets'] == ['dog', 'cat']
assert my_new_instance.address.number == '221B'
assert my_new_instance.address.street == 'Baker Street'
assert my_new_instance.pets[0] == 'dog'
assert my_new_instance.pets[1] == 'cat'


def suite():
suite = unittest.TestSuite()
suite.addTest(testutil.doctest_suite(mapping))
Expand Down