Skip to content

Commit

Permalink
Creating the test server
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfalcao committed Aug 7, 2009
1 parent 3fda374 commit 168e63b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
6 changes: 4 additions & 2 deletions deadparrot/models/managers.py
Expand Up @@ -56,9 +56,11 @@ def _fullpath(self):
return join(self.base_path, self._filename)

def create(self, **kw):
ModelSetClass = self.model.Set()

model = self.model(**kw)
return self.add(model)

def add(self, model):
ModelSetClass = self.model.Set()
if not os.path.exists(self._fullpath):
f = codecs.open(self._fullpath, 'w', 'utf-8')
f.write('')
Expand Down
102 changes: 102 additions & 0 deletions tests/functional/parrot_server.py
@@ -0,0 +1,102 @@
#!/usr/bin/env python
# -*- coding: utf-8; -*-
#
# Copyright (C) 2009 Gabriel Falcão <gabriel@nacaolivre.org>
#
# This program 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 2 of the
# License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import os
import socket
import cherrypy
from deadparrot import models

class Actor(models.Model):
name = models.CharField(max_length=100)
objects = models.FileSystemModelManager(base_path=os.path.abspath('.'))
def __unicode__(self):
return u'Actor %r' % self.name

Actor.objects.create(name='Eric Idle')
Actor.objects.create(name='Terry Jones')
Actor.objects.create(name='John Cleese')
Actor.objects.create(name='Graham Chapman')
Actor.objects.create(name='Michael Palin')

class ParrotController:
@cherrypy.expose
def actor(self, attribute=None, value=None, action=None):
cherrypy.response.status = 201
if not attribute:
if cherrypy.request.method == 'POST':
try:
actor = Actor.deserialize(cherrypy.request.body.read(), 'json')
except TypeError, e:
cherrypy.response.status = 500
return unicode(e)

Actor.objects.add(actor)
return actor.serialize('json')

if attribute and not hasattr(Actor, attribute):
cherrypy.response.status = 404
return 'Invalid attribute: %s' % attribute

if value:
filtered = [p for p in actors.items \
if hasattr(p, attribute) \
and value in getattr(p, attribute)]
first = filtered[0]

if cherrypy.request.method == 'PUT':
try:
actor = Actor.deserialize(cherrypy.request.body.read(), 'json')
except TypeError, e:
cherrypy.response.status = 500
return unicode(e)

actors.remove(first)
actors.add(actor)
return actor.serialize('json')

if action == 'delete':
actors.remove(first)
return first.serialize('json')

return ActorSet(*filtered).serialize('json')

return actors.serialize('json')

def port_is_free(server, port):
connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
connection.bind((server, port))
connection.close()
del connection
return True
except socket.error, e:
if e.args[0] is 98:
return False
else:
raise e

if __name__ == '__main__':
if not port_is_free('localhost', 6060):
print 'The port 6060 in localhost is not free, Bolacha ' \
'functional tests can not proceed'
raise SystemExit(1)

cherrypy.config['server.socket_port'] = 6060
cherrypy.quickstart(ParrotController(), '/', config={})

0 comments on commit 168e63b

Please sign in to comment.