from __future__ import absolute_import

import mock
import unittest

import servy.client
import servy.proto


class Service(unittest.TestCase):
    def test_url(self):
        service = servy.client.Service('serv', 'localhost')
        self.assertEqual(service.url, 'http://localhost/serv')

    def test_client_init(self):
        client = servy.client.Client({'name': 'serv', 'host': 'localhost'})
        self.assertEqual(client._Client__service.url, 'http://localhost/serv')


class RemoteExecution(unittest.TestCase):
    def setUp(self):
        self.service = servy.client.Service('serv', 'localhost')
        self.client = servy.client.Client(self.service)

    def test_remote_execution(self):
        content = servy.proto.Response.encode('content')
        with mock.patch('servy.client.Service.read') as read:
            read.return_value = content
            self.assertEqual(self.client.fn(), servy.proto.Response.decode(content))
        message = servy.proto.Request.encode('fn', (), {})
        read.assert_called_once_with(message)

    def test_failed_remote_execution(self):
        with self.assertRaises(TypeError):
            self.client()

@@ -40,20 +40,18 @@
 %0Aimport 
-mock
+io
 %0Aimport 
@@ -58,16 +58,44 @@
 unittest
+%0Aimport urllib2%0A%0Aimport mock
 %0A%0Aimport
@@ -108,16 +108,33 @@
 .client%0A
+import servy.exc%0A
 import s
@@ -1202,8 +1202,620 @@
 lient()%0A
+%0A    def test_http_exception_404(self):%0A        with mock.patch('servy.client.Service.read') as read:%0A            read.side_effect = urllib2.HTTPError(self.service.url, 404, 'Not Found', %5B%5D, io.StringIO())%0A            with self.assertRaises(servy.exc.ServiceNotFound):%0A                self.client.fn()%0A%0A    def test_http_exception_501(self):%0A        with mock.patch('servy.client.Service.read') as read:%0A            read.side_effect = urllib2.HTTPError(self.service.url, 501, 'Not Implemented', %5B%5D, io.StringIO())%0A            with self.assertRaises(servy.exc.ProcedureNotFound):%0A                self.client.fn()%0A

add tests for exceptions