def main(argv): """ Start with:: python -m restorm.examples.mock.library_serv [port or address:port] """ ip_address = '127.0.0.1' port = 8000 # This is an example. Your should do argument checking. if len(argv) == 1: ip_address_port = argv[0].split(':', 1) if len(ip_address_port) == 1: port = ip_address_port[0] else: ip_address, port = ip_address_port # Create a playground HTTP server that handles requests from the # ``LibraryApiClient``. api = LibraryApiClient('http://%s:%s/api/' % (ip_address, port)) server = api.create_server(ip_address, int(port)) print 'Mock library webservice is running at http://%s:%s/api/' % ( ip_address, port) print 'Quit the server with CTRL-C.' try: server.serve_forever() except KeyboardInterrupt: print 'Closing server...' server.socket.close()
def main(argv): """ Start with:: python -m restorm.examples.mock.serv [port or address:port] """ ip_address = '127.0.0.1' port = 8000 # This is an example. Your should do argument checking. if len(argv) == 1: ip_address_port = argv[0].split(':', 1) if len(ip_address_port) == 1: port = ip_address_port[0] else: ip_address, port = ip_address_port # Create a playground HTTP server that handles requests from the # ``LibraryApiClient``. api = LibraryApiClient('http://%s:%s/api/' % (ip_address, port)) server = api.create_server(ip_address, int(port)) print 'Mock library webservice is running at http://%s:%s' % (ip_address, port) print 'Quit the server with CTRL-C.' try: server.serve_forever() except KeyboardInterrupt: print 'Closing server...' server.socket.close()
class MockApiClientTests(TestCase): def setUp(self): self.client = LibraryApiClient() def test_get(self): response = self.client.get('book/') self.assertEqual(response.status_code, 200) self.assertEqual(response.raw_content, self.client.responses['book/']['GET'][1]) def test_post(self): response = self.client.post('search/', {'q': 'Python'}) self.assertEqual(response.status_code, 200) self.assertEqual( response['X-Cache'], self.client.responses['search/']['POST'][0]['X-Cache']) self.assertEqual(response.raw_content, self.client.responses['search/']['POST'][1]) def test_page_not_found(self): uri = 'book/2' self.assertTrue(uri not in self.client.responses) response = self.client.get(uri) self.assertEqual(response.status_code, 404) def test_method_not_allowed(self): response = self.client.post('author/1', {}) self.assertEqual(response.status_code, 405) def test_related(self): class Book(Resource): class Meta: list = r'^book/$' item = r'^book/(?P<isbn>\d)$' class Author(Resource): class Meta: list = (r'^author/$', 'author_set') item = r'^author/(?P<id>\d)$' book = Book.objects.get(isbn='978-1441413024', client=self.client) self.assertEqual(book.data['title'], 'Dive into Python') self.assertEqual(book.data['author'], '%sauthor/1' % self.client.root_uri) author = book.data.author self.assertEqual(author.data['name'], 'Mark Pilgrim')
class MockApiClientTests(TestCase): def setUp(self): self.client = LibraryApiClient() def test_get(self): response = self.client.get('book/') self.assertEqual(response.status_code, 200) self.assertEqual(response.raw_content, self.client.responses['book/']['GET'][1]) def test_post(self): response = self.client.post('search/', {'q': 'Python'}) self.assertEqual(response.status_code, 200) self.assertEqual(response['X-Cache'], self.client.responses['search/']['POST'][0]['X-Cache']) self.assertEqual(response.raw_content, self.client.responses['search/']['POST'][1]) def test_page_not_found(self): uri = 'book/2' self.assertTrue(uri not in self.client.responses) response = self.client.get(uri) self.assertEqual(response.status_code, 404) def test_method_not_allowed(self): response = self.client.post('author/1', {}) self.assertEqual(response.status_code, 405) def test_related(self): class Book(Resource): class Meta: list = r'^book/$' item = r'^book/(?P<isbn>\d)$' class Author(Resource): class Meta: list = (r'^author/$', 'author_set') item = r'^author/(?P<id>\d)$' book = Book.objects.get(isbn='978-1441413024', client=self.client) self.assertEqual(book.data['title'], 'Dive into Python') self.assertEqual(book.data['author'], '%sauthor/1' % self.client.root_uri) author = book.data.author self.assertEqual(author.data['name'], 'Mark Pilgrim')
def setUp(self): self.client = LibraryApiClient()
def setUp(self): RestormAppSetup() self.client = LibraryApiClient()
def setUp(self): RestormAppSetup() self.client = LibraryApiClient() class Author(Resource): id = fields.IntegerField(primary_key=True) name = fields.CharField() class Meta: resource_name = 'author' list = (r'^author/$', 'author_set') item = r'^author/(?P<id>\d)$' client = self.client verbose_name = 'Author' verbose_name_plural = 'Authors' self.author_resource = Author class BookManager(ResourceManager): def filter_on_author(self, author_resource): return self.all(query=[('author', author_resource), ]) self.book_manager = BookManager def get_author_id(data, resource): return { 'id': data.split('/')[-1] } class Book(Resource): some_class_attribute = 'foobar' isbn = fields.CharField(primary_key=True) title = fields.CharField() author = fields.ToOneField('author', Author, get_author_id) objects = BookManager() class Meta: resource_name = 'book' list = r'^book/$' item = r'^book/(?P<isbn>\d)$' client = self.client def __init__(self, *args, **kwargs): self.some_instance_attribute_before_init = 'foobar' super(Book, self).__init__(*args, **kwargs) self.some_instance_attribute_after_init = 'foobar' def get_title(self): return self.title.title() self.book_resource = Book class Store(Resource): id = fields.IntegerField(primary_key=True) name = fields.CharField() class Meta: resource_name = 'store' list = r'^store/$' item = r'^store/(?P<id>\d)$' client = self.client verbose_name = 'Storage' verbose_name_plural = 'Storages' self.store_resource = Store
class MockApiClientTests(TestCase): def setUp(self): self.client = LibraryApiClient() def test_get(self): response = self.client.get('book/') self.assertEqual(response.status_code, 200) self.assertEqual(response.raw_content, self.client.responses['book/']['GET'][1]) def test_post(self): response = self.client.post('search/', {'q': 'Python'}) self.assertEqual(response.status_code, 200) self.assertEqual( response['X-Cache'], self.client.responses['search/']['POST'][0]['X-Cache']) self.assertEqual(response.raw_content, self.client.responses['search/']['POST'][1]) def test_page_not_found(self): uri = 'book/2' self.assertTrue(uri not in self.client.responses) response = self.client.get(uri) self.assertEqual(response.status_code, 404) def test_method_not_allowed(self): response = self.client.post('author/1', {}) self.assertEqual(response.status_code, 405) def test_related(self): class Author(Resource): id = fields.IntegerField(primary_key=True) name = fields.CharField() class Meta: list = (r'^author/$', 'author_set') item = r'^author/(?P<id>\d)$' client = self.client def get_author_id(data, resource): return {'id': data.split('/')[-1]} class Book(Resource): isbn = fields.CharField(primary_key=True) title = fields.CharField() author = fields.ToOneField('author', Author, get_author_id) class Meta: list = r'^book/$' item = r'^book/(?P<isbn>\d)$' client = self.client book = Book.objects.get(isbn='978-1441413024', client=self.client) self.assertEqual(book.title, 'Dive into Python') self.assertEqual(book.author.absolute_url, '%sauthor/1' % self.client.root_uri) author = book.author self.assertEqual(author.name, 'Mark Pilgrim')