~ubuntu-branches/ubuntu/lucid/desktopcouch/lucid-proposed

« back to all changes in this revision

Viewing changes to desktopcouch/pair/tests/test_couchdb_io.py

  • Committer: James Westby
  • Date: 2009-10-14 13:43:23 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@canonical.com-20091014134323-yvov1mrx9xvs1i01
Merging shared upstream rev into target branch.

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
 
 
17
 
 
18
import pygtk
 
19
pygtk.require('2.0')
 
20
 
 
21
import desktopcouch.tests as dctests
 
22
 
 
23
from desktopcouch.pair.couchdb_pairing import couchdb_io
 
24
from desktopcouch.records.server import CouchDatabase
 
25
from desktopcouch.records.record import Record
 
26
import unittest
 
27
import uuid
 
28
import os
 
29
import httplib2
 
30
URI = None  # use autodiscovery that desktopcouch.tests permits.
 
31
 
 
32
class TestCouchdbIo(unittest.TestCase):
 
33
 
 
34
    def setUp(self):
 
35
        """setup each test"""
 
36
        self.mgt_database = CouchDatabase('management', create=True, uri=URI)
 
37
        self.foo_database = CouchDatabase('foo', create=True, uri=URI)
 
38
        #create some records to pull out and test
 
39
        self.foo_database.put_record(Record({
 
40
            "key1_1": "val1_1", "key1_2": "val1_2", "key1_3": "val1_3",
 
41
            "record_type": "test.com"}))
 
42
        self.foo_database.put_record(Record({
 
43
            "key2_1": "val2_1", "key2_2": "val2_2", "key2_3": "val2_3",
 
44
            "record_type": "test.com"}))
 
45
        self.foo_database.put_record(Record({
 
46
            "key13_1": "va31_1", "key3_2": "val3_2", "key3_3": "val3_3",
 
47
            "record_type": "test.com"}))
 
48
 
 
49
    def tearDown(self):
 
50
        """tear down each test"""
 
51
        del self.mgt_database._server['management']
 
52
        del self.mgt_database._server['foo']
 
53
 
 
54
    def test_put_static_paired_service(self):
 
55
        service_name = "dummyfortest"
 
56
        oauth_data = {
 
57
                "consumer_key": str("abcdef"),
 
58
                "consumer_secret": str("ghighjklm"),
 
59
                "token": str("opqrst"),
 
60
                "token_secret": str("uvwxyz"),
 
61
            }
 
62
        couchdb_io.put_static_paired_service(oauth_data, service_name, uri=URI)
 
63
        pairings = list(couchdb_io.get_pairings())
 
64
 
 
65
    def test_put_dynamic_paired_host(self):
 
66
        hostname = "host%d" % (os.getpid(),)
 
67
        remote_uuid = str(uuid.uuid4())
 
68
        oauth_data = {
 
69
                "consumer_key": str("abcdef"),
 
70
                "consumer_secret": str("ghighjklm"),
 
71
                "token": str("opqrst"),
 
72
                "token_secret": str("uvwxyz"),
 
73
            }
 
74
 
 
75
        couchdb_io.put_dynamic_paired_host(hostname, remote_uuid, oauth_data,
 
76
                uri=URI)
 
77
        couchdb_io.put_dynamic_paired_host(hostname, remote_uuid, oauth_data,
 
78
                uri=URI)
 
79
        couchdb_io.put_dynamic_paired_host(hostname, remote_uuid, oauth_data,
 
80
                uri=URI)
 
81
 
 
82
        pairings = list(couchdb_io.get_pairings())
 
83
        self.assertEqual(3, len(pairings))
 
84
        self.assertEqual(pairings[0].value["oauth"], oauth_data)
 
85
        self.assertEqual(pairings[0].value["server"], hostname)
 
86
        self.assertEqual(pairings[0].value["pairing_identifier"], remote_uuid)
 
87
 
 
88
        for i, row in enumerate(pairings):
 
89
            couchdb_io.remove_pairing(row.id, i == 1)
 
90
 
 
91
        pairings = list(couchdb_io.get_pairings())
 
92
        self.assertEqual(0, len(pairings))
 
93
 
 
94
 
 
95
    def test_get_database_names_replicatable_bad_server(self):
 
96
        # If this resolves, FIRE YOUR DNS PROVIDER.
 
97
 
 
98
        try:
 
99
            names = couchdb_io.get_database_names_replicatable(
 
100
                uri='http://test.desktopcouch.example.com:9/')
 
101
            self.assertEqual(set(), names)
 
102
        except httplib2.ServerNotFoundError:
 
103
            pass
 
104
 
 
105
    def test_get_database_names_replicatable(self):
 
106
        names = couchdb_io.get_database_names_replicatable(uri=URI)
 
107
        self.assertFalse('management' in names)
 
108
        self.assertTrue('foo' in names)
 
109
 
 
110
    def test_get_my_host_unique_id(self):
 
111
        got = couchdb_io.get_my_host_unique_id(uri=URI)
 
112
        again = couchdb_io.get_my_host_unique_id(uri=URI)
 
113
        self.assertEquals(len(got), 1)
 
114
        self.assertEquals(got, again)
 
115
 
 
116
    def test_mkuri(self):
 
117
        uri = couchdb_io.mkuri(
 
118
            'fnord.org', 55241, has_ssl=True, path='a/b/c',
 
119
            auth_pair=('f o o', 'b=a=r'))
 
120
        self.assertEquals(
 
121
            'https://f%20o%20o:b%3Da%3Dr@fnord.org:55241/a/b/c', uri)
 
122
 
 
123
    def Xtest_replication_good(self):
 
124
        pass
 
125
 
 
126
    def Xtest_replication_no_oauth_remote(self):
 
127
        pass
 
128
 
 
129
    def Xtest_replication_bad_oauth_remote(self):
 
130
        pass
 
131
 
 
132
    def Xtest_replication_no_oauth_local(self):
 
133
        pass
 
134
 
 
135
    def Xtest_replication_bad_oauth_local(self):
 
136
        pass
 
137
 
 
138
 
 
139
if __name__ == "__main__":
 
140
    unittest.main()