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
150
151
|
#!/usr/bin/env python
# encoding: utf-8
"""
test_rpcserver.py
This file is part of sugarbot.
sugarbot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sugarbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with sugarbot. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import unittest
from sys import path
from random import randint
from xmlrpclib import ServerProxy
path.append("..")
try:
import sbconfig
except ImportError:
import sbconfig_sample
from sbrpcserver import *
class TestRPCServer(unittest.TestCase):
files = ["fileOne.py", "fileTwo.py"]
activityNames = ["activityOne", "activityTwo"]
basicScript = \
"""
def sugarbot_main(var):
assert var
"""
scripts = [basicScript + "sugarActivityName = '%s'" % activityNames[0],
basicScript + "sugarActivityName = '%s'" % activityNames[1]]
def __init__(self):
self.is_setup = False
def setUp(self):
assert not self.is_setup
# Generate a random ID
self.clientID = randint(1,1000)
# Create the server object.
self.server = sbRpcServer([], None)
# Create the script file and add it...
self.createScriptFiles()
for file in self.files:
self.server.addScript(file)
self.is_setup = True
def tearDown(self):
assert self.is_setup
self.server.server_close()
self.deleteScriptFiles()
self.is_setup = False
def createScriptFiles(self):
for i in range(0, len(self.scripts)):
f = open(self.files[i],"w+")
f.write(self.scripts[i])
f.close()
def deleteScriptFiles(self):
for f in self.files:
os.remove(f)
def test_StartScript_Reset(self):
self.server.startScript(self.clientID)
client = self.server.clients[self.clientID]
client.currentScript = 999999
self.server.startScript(self.clientID)
assert client.currentScript == 0
assert client.responses[client.scriptName] == False
def test_StartScript_Bounds(self):
# Check to see if we go beyond the bounds
for count in range(0,1+len(self.server.listOfScripts)):
self.server.startScript()
assert self.server.clients[0].currentScript == 0
def test_RegisteredFunctions(self):
expectedMethods = ['addScript',
'completionStatus',
'fail',
'generateSessionID',
'getActivityName',
'getRestartFlag',
'getScript',
'numberOfScripts',
'startScript',
'success']
listedMethods = self.server.system_listMethods()
for method in expectedMethods:
if not method in listedMethods:
print method
assert 0
assert not 'NOTREGISTERED' in listedMethods
def test_getActivityName_IndexError(self):
try:
self.server.startScript(self.clientID)
self.server.getActivityName(self.clientID, 9999)
assert 0
except IndexError:
pass
def test_getActivityName_WithoutStartingScript(self):
try:
self.server.getActivityName(self.clientID)
assert 0
except IndexError:
pass
def test_getActivityName(self):
for activity in self.activityNames:
self.server.startScript()
assert self.server.getActivityName() == activity
def test_multipleCallsToGetScript(self):
for script in self.scripts:
self.server.startScript()
assert self.server.getScript() == script
if __name__ == "__main__":
unittest.main()
|