~james-w/desktopcouch/0.4.1

« back to all changes in this revision

Viewing changes to desktopcouch/records/tests/test_field_registry.py

  • Committer: Bazaar Package Importer
  • Author(s): Ken VanDine
  • Date: 2009-07-31 13:44:45 UTC
  • Revision ID: james.westby@ubuntu.com-20090731134445-3eissn9me417ya5x
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2009 Canonical Ltd.
 
2
#
 
3
# This file is part of desktopcouch.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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>
 
17
 
 
18
"""Test cases for field mapping"""
 
19
 
 
20
import copy
 
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
 
25
 
 
26
TEST_RECORD = {
 
27
    'record_type': 'http://example.com/test',
 
28
    'simple_field': 23,
 
29
    'test_fields': {
 
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'}}}}
 
37
 
 
38
APP_RECORD = {
 
39
    'record_type': 'http://example.com/test',
 
40
    'simpleField': 23,
 
41
    'strawberryField': 'the value',}
 
42
 
 
43
field_registry = {
 
44
    'simpleField': SimpleFieldMapping('simple_field'),
 
45
    'strawberryField': MergeableListFieldMapping(
 
46
        'Test App', 'test_field', 'test_fields', 'the_field'),}
 
47
 
 
48
 
 
49
class AppTransformer(Transformer):
 
50
    """A test transformer class."""
 
51
 
 
52
    def __init__(self):
 
53
        super(AppTransformer, self).__init__('Test App', field_registry)
 
54
 
 
55
 
 
56
class TestFieldMapping(TwistedTestCase):
 
57
    """Test Case for FieldMapping objects."""
 
58
 
 
59
    def setUp(self):
 
60
        self.test_record = copy.deepcopy(TEST_RECORD)
 
61
 
 
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))
 
69
 
 
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))
 
78
 
 
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))
 
86
 
 
87
 
 
88
class TestTransformer(TwistedTestCase):
 
89
    """Test application specific transformer classes"""
 
90
 
 
91
    def setUp(self):
 
92
        """setup test fixtures"""
 
93
        self.transformer = AppTransformer()
 
94
 
 
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']
 
103
        self.assertEqual(
 
104
            {'the_field': 'the value'},
 
105
            underlying['test_fields'][the_uuid])
 
106
 
 
107
    def test_to_app(self):
 
108
        """Test transformation to app from Ubuntu One."""
 
109
        record = Record(TEST_RECORD)
 
110
        data = {}
 
111
        self.transformer.to_app(record, data)
 
112
        self.assertEqual(
 
113
            {'simpleField': 23, 'strawberryField': 'the value'}, data)