1
# Copyright 2009 Canonical Ltd.
3
# This file is part of desktopcouch.
5
# desktopcouch is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3
7
# as published by the Free Software Foundation.
9
# desktopcouch is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with desktopcouch. If not, see <http://www.gnu.org/licenses/>.
16
# Authors: Eric Casteleijn <eric.casteleijn@canonical.com>
18
"""Test cases for field mapping"""
21
from twisted.trial.unittest import TestCase as TwistedTestCase
22
from desktopcouch.records.field_registry import (
23
SimpleFieldMapping, MergeableListFieldMapping, Transformer)
24
from desktopcouch.records.record import Record
27
'record_type': 'http://example.com/test',
30
'e47455fb-da05-481e-a2c7-88f14d5cc163':
31
{'the_field': 'the value',
32
'not_the_field': 'different value'},
33
'd6d2c23b-279c-45c8-afb2-ec84ee7c81c3': {
34
'the field': 'another value'}},
35
'application_annotations': {'Test App': {'private_application_annotations':{
36
'test_field': 'e47455fb-da05-481e-a2c7-88f14d5cc163'}}}}
39
'record_type': 'http://example.com/test',
41
'strawberryField': 'the value',}
44
'simpleField': SimpleFieldMapping('simple_field'),
45
'strawberryField': MergeableListFieldMapping(
46
'Test App', 'test_field', 'test_fields', 'the_field'),}
49
class AppTransformer(Transformer):
50
"""A test transformer class."""
53
super(AppTransformer, self).__init__('Test App', field_registry)
56
class TestFieldMapping(TwistedTestCase):
57
"""Test Case for FieldMapping objects."""
60
self.test_record = copy.deepcopy(TEST_RECORD)
62
def test_simple_field_mapping(self):
63
"""Test the simple field mapping object."""
64
r = Record(self.test_record)
65
mapping = SimpleFieldMapping('simple_field')
66
self.assertEqual(23, mapping.getValue(r))
67
mapping.setValue(r, 'Fnord')
68
self.assertEqual('Fnord', mapping.getValue(r))
70
def test_mergeable_list_field_mapping(self):
71
"""Test the MergeableListFieldMapping object."""
72
record = Record(self.test_record)
73
mapping = MergeableListFieldMapping(
74
'Test App', 'test_field', 'test_fields', 'the_field')
75
self.assertEqual('the value', mapping.getValue(record))
76
mapping.setValue(record, 'a new value')
77
self.assertEqual('a new value', mapping.getValue(record))
79
def test_mergeable_list_field_mapping_empty_field(self):
80
"""Test setting empty values in the MergeableListFieldMapping object."""
81
record = Record(self.test_record)
82
mapping = MergeableListFieldMapping(
83
'Test App', 'test_field', 'test_fields', 'the_field')
84
mapping.setValue(record, '')
85
self.assertEqual(None, mapping.getValue(record))
88
class TestTransformer(TwistedTestCase):
89
"""Test application specific transformer classes"""
92
"""setup test fixtures"""
93
self.transformer = AppTransformer()
95
def test_from_app(self):
96
"""Test transformation from app to Ubuntu One."""
97
record = Record(record_type="http://example.com/test")
98
self.transformer.from_app(APP_RECORD, record)
99
underlying = record._data
100
self.assertEqual(23, record['simple_field'])
101
the_uuid = record.application_annotations['Test App']\
102
['private_application_annotations']['test_field']
104
{'the_field': 'the value'},
105
underlying['test_fields'][the_uuid])
107
def test_to_app(self):
108
"""Test transformation to app from Ubuntu One."""
109
record = Record(TEST_RECORD)
111
self.transformer.to_app(record, data)
113
{'simpleField': 23, 'strawberryField': 'the value'}, data)