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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
#
# This copy of Ice is licensed to you under the terms described in the
# ICE_LICENSE file included in this distribution.
#
# **********************************************************************
import pexpect, sys
def dequote(s):
cur = 0
end = len(s)
havequote = False
quote = None
items = []
while cur < len(s):
if not quote:
if s[cur] == "'" or s[cur] == '"':
quote = s[cur]
start = cur+1
else:
if s[cur] == quote:
items.append(s[start:cur])
quote = None
cur = cur + 1
return items
def mkregexp(s):
s = s.replace('(', '\\(')
s = s.replace(')', '\\)')
s = s.replace('.', '\\.')
s = s.replace('+', '\\+')
s = s.replace('*', '\\*')
return s
def run(client, server):
print "populating database... ",
sys.stdout.flush()
f = open("books", "r")
books = []
for l in f:
client.sendline(l)
#server.expect('added object')
client.expect('added new book')
isbn, title, author = dequote(l)
books.append((isbn, title, author))
print "ok"
byauthor = {}
for b in books:
isbn, title, author = b
if not byauthor.has_key(author):
byauthor[author] = []
byauthor[author].append(b)
print "testing isbn... ",
sys.stdout.flush()
for b in books:
isbn, title, author = b
client.sendline('isbn %s' %(isbn))
#server.expect('locate found')
#server.expect('locate found')
client.expect('current book is:')
client.expect('isbn: %s' %(isbn))
client.expect('title: %s' %(mkregexp(title)))
client.expect('authors: %s' %(author))
client.sendline('isbn 1000')
client.expect('no book with that')
print "ok"
print "testing authors... ",
sys.stdout.flush()
for a, bl in byauthor.iteritems():
client.sendline('authors "%s"' %(a))
client.expect('number of books found: ([0-9]+)')
n = int(client.match.group(1))
assert len(bl) == n
for i in range(0, n):
#server.expect('locate found')
# Consume evicting messages otherwise its possible to get
# hangs.
#try:
#server.expect('evicting', timeout=0)
#except pexpect.TIMEOUT:
#pass
client.expect('current book is:')
client.expect('isbn: ([a-zA-Z0-9]+)')
findisbn = client.match.group(1)
nbl = []
for b in bl:
isbn, title, author = b
if not findisbn:
nbl.append(b)
if isbn == findisbn:
findisbn = None
client.expect('title: %s' %(mkregexp(title)))
client.expect('authors: %s' %(author))
bl = nbl
assert not findisbn
client.sendline('next')
client.expect('no current book')
assert len(bl) == 0
client.sendline('authors foo')
client.expect('number of books found: 0')
client.expect('no current book')
print "ok"
print "testing rent/return... ",
sys.stdout.flush()
isbn, title, author = books[0]
client.sendline('isbn %s' % (isbn))
#server.expect('locate')
client.expect('current book is:.*isbn.*\r{1,2}\ntitle.*\r{1,2}\nauthors')
client.sendline('rent matthew')
#server.expect('locate')
client.expect("the book is now rented by `matthew'")
client.sendline('current')
#server.expect('locate')
client.expect('rented: matthew')
client.sendline('rent john')
#server.expect('locate')
client.expect('the book has already been rented')
client.sendline('return')
#server.expect('locate')
client.expect('the book has been returned')
client.sendline('current')
try:
client.expect('rented:', timeout=2)
except pexpect.TIMEOUT:
pass
print "ok"
print "testing remove... ",
sys.stdout.flush()
isbn, title, author = books[0]
client.sendline('isbn %s' % (isbn))
#server.expect('locate')
client.expect('current book is:.*isbn.*\r{1,2}\ntitle.*\r{1,2}\nauthors')
client.sendline('remove')
#server.expect('locate')
#server.expect('removed')
client.expect('removed current book')
client.sendline('remove')
#server.expect(['locate could not find', 'locate found.*but it was dead or destroyed'])
client.expect('current book no longer exists')
print "ok"
|