~thomasvs/paisley/object-views

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

from paisley import couchdb

import sys
from twisted.internet import reactor, defer
from twisted.python import log
from twisted.web import client, error, http

client.HTTPClientFactory.noisy = False



def test():
    foo = couchdb.CouchDB('localhost')

    print "\nCreate database 'mydb':"
    d = foo.createDB('mydb')
    wfd = defer.waitForDeferred(d)
    yield wfd

    try:
        print wfd.getResult()
    except error.Error, e:
        # FIXME: not sure why Error.status is a str compared to http constants
        if e.status == str(http.UNAUTHORIZED):
            print "\nError: not allowed to create databases"
            reactor.stop()
            return
        else:
            raise

    print "\nList databases on server:"
    d = foo.listDB()
    wfd = defer.waitForDeferred(d)
    yield wfd
    print wfd.getResult()

    print "\nCreate a document 'mydoc' in database 'mydb':"
    doc = """
    {
        "value":
        {
            "Subject":"I like Planktion",
            "Author":"Rusty",
            "PostedDate":"2006-08-15T17:30:12-04:00",
            "Tags":["plankton", "baseball", "decisions"],
            "Body":"I decided today that I don't like baseball. I like plankton."
        }
    }
    """
    d = foo.saveDoc('mydb', doc, 'mydoc')
    wfd = defer.waitForDeferred(d)
    yield wfd
    mydoc = wfd.getResult()
    print mydoc

    print "\nCreate a document, using an assigned docId:"
    d = foo.saveDoc('mydb', doc)
    wfd = defer.waitForDeferred(d)
    yield wfd
    print wfd.getResult()

    print "\nList all documents in database 'mydb'"
    d = foo.listDoc('mydb')
    wfd = defer.waitForDeferred(d)
    yield wfd
    print wfd.getResult()

    print "\nRetrieve document 'mydoc' in database 'mydb':"
    d = foo.openDoc('mydb', 'mydoc')
    wfd = defer.waitForDeferred(d)
    yield wfd
    print wfd.getResult()

    print "\nDelete document 'mydoc' in database 'mydb':"
    d = foo.deleteDoc('mydb', 'mydoc', mydoc['rev'])
    wfd = defer.waitForDeferred(d)
    yield wfd
    print wfd.getResult()

    print "\nList all documents in database 'mydb'"
    d = foo.listDoc('mydb')
    wfd = defer.waitForDeferred(d)
    yield wfd
    print wfd.getResult()

    print "\nList info about database 'mydb':"
    d = foo.infoDB('mydb')
    wfd = defer.waitForDeferred(d)
    yield wfd
    print wfd.getResult()

    print "\nDelete database 'mydb':"
    d = foo.deleteDB('mydb')
    wfd = defer.waitForDeferred(d)
    yield wfd
    print wfd.getResult()

    print "\nList databases on server:"
    d = foo.listDB()
    wfd = defer.waitForDeferred(d)
    yield wfd
    print wfd.getResult()

    reactor.stop()
test = defer.deferredGenerator(test)


if __name__ == "__main__":
    log.startLogging(sys.stdout)
    reactor.callWhenRunning(test)
    reactor.run()