import unittest
from mock import patch, PropertyMock, MagicMock
import vim_bootstrap_updater as sut


class VimBootstrapUpdaterTests(unittest.TestCase):

    def test_generate_params(self):
        langs = 'hy clojure'.split()
        result = sut._generate_params(langs)

        self.assertEqual('langs=hy&langs=clojure', result)

    @patch('vim_bootstrap_updater.HTTPConnection')
    @patch('vim_bootstrap_updater.vimrc_path', return_value='/tmp/.vimrc')
    @patch('vim_bootstrap_updater._generate_params', return_value='langs=hy')
    def test_generate_vimrc_200(self, mock_generate_params, mock_VIMRC_PATH, mock_HTTPConnection):
        mock_conn = MagicMock()
        mock_HTTPConnection.return_value = mock_conn
        mock_response = MagicMock()
        type(mock_response).status = PropertyMock(return_value=200)
        mock_response.read.return_value = 'fake response'

        mock_conn.getresponse.return_value = mock_response

        result = sut._generate_vimrc(['hy'])

        self.assertEqual('fake response', result)

    @patch('vim_bootstrap_updater.HTTPConnection')
    @patch('vim_bootstrap_updater.vimrc_path', return_value='/tmp/.vimrc')
    @patch('vim_bootstrap_updater._generate_params', return_value='langs=hy')
    def test_generate_vimrc_NOT_200(self, mock_generate_params, mock_VIMRC_PATH, mock_HTTPConnection):
        mock_conn = MagicMock()
        mock_HTTPConnection.return_value = mock_conn
        mock_response = MagicMock()
        type(mock_response).status = PropertyMock(return_value=404)
        mock_response.read.return_value = 'fake response'

        mock_conn.getresponse.return_value = mock_response

        with self.assertRaises(Exception):
            sut._generate_vimrc(['hy'])

    @patch('vim_bootstrap_updater.vimrc_path', return_value='/tmp/.vimrc')
    @patch('vim_bootstrap_updater._generate_vimrc', return_value='fake vimrc')
    def test_update(self, mock_generate_vimrc, mock_vimrc_path):
        result = sut.update(['hy'])

        self.assertEqual('fake vimrc', result)

        with open('/tmp/.vimrc', 'r') as fh:
            content = fh.read()
            self.assertEqual('fake vimrc', content)

@@ -13,55 +13,34 @@
 est%0A
-from
+import
  mock
- 
+%0A
 import 
-patch, PropertyMock, MagicMock
+urllib2
 %0Aimp
@@ -134,188 +134,14 @@
     
-def test_generate_params(self):%0A        langs = 'hy clojure'.split()%0A        result = sut._generate_params(langs)%0A%0A        self.assertEqual('langs=hy&langs=clojure', result)%0A%0A    @
+@mock.
 patc
@@ -161,37 +161,38 @@
 rap_updater.
-HTTPConnectio
+urllib2.urlope
 n')%0A    @pat
@@ -180,32 +180,37 @@
 .urlopen')%0A    @
+mock.
 patch('vim_boots
@@ -271,265 +271,73 @@
     
-@patch('vim_bootstrap_updater._generate_params', return_value='langs=hy')%0A    def test_generate_vimrc_200(self, mock_generate_params, mock_VIMRC_PATH, mock_HTTPConnection):%0A        mock_conn = MagicMock()%0A        mock_HTTPConnection.return_value = mock_conn
+def test_generate_vimrc_200(self, mock_VIMRC_PATH, mock_urlopen):
 %0A   
@@ -349,32 +349,37 @@
 mock_response = 
+mock.
 MagicMock()%0A    
@@ -374,37 +374,32 @@
 cMock()%0A        
-type(
 mock_response).s
@@ -399,49 +399,19 @@
 onse
-).status = PropertyMock(return_value=
+.code = 
 200
-)
 %0A   
@@ -457,33 +457,32 @@
 'fake response'%0A
-%0A
         mock_con
@@ -478,32 +478,23 @@
    mock_
-conn.getresponse
+urlopen
 .return_
@@ -554,28 +554,200 @@
 _vimrc(%5B'hy'
+, 'closure'
 %5D)%0A%0A
+        self.assertEqual(mock_urlopen.call_args, mock.call(%0A            'https://vim-bootstrap.appspot.com/generate.vim',%0A            'langs=hy&langs=closure'))%0A
         self
@@ -782,32 +782,37 @@
 , result)%0A%0A    @
+mock.
 patch('vim_boots
@@ -824,29 +824,30 @@
 updater.
-HTTPConnectio
+urllib2.urlope
 n')%0A    
@@ -839,32 +839,37 @@
 .urlopen')%0A    @
+mock.
 patch('vim_boots
@@ -926,86 +926,8 @@
 c')%0A
-    @patch('vim_bootstrap_updater._generate_params', return_value='langs=hy')%0A
     
@@ -967,30 +967,8 @@
 elf,
- mock_generate_params,
  moc
@@ -986,29 +986,22 @@
 H, mock_
-HTTPConnectio
+urlope
 n):%0A    
@@ -1013,301 +1013,173 @@
 ock_
-conn = MagicMock()%0A        mock_HTTPConnection.return_value = mock_conn%0A        mock_response = MagicMock()%0A        type(mock_response).status = PropertyMock(return_value=404)%0A        mock_response.read.return_value = 'fake response'%0A%0A        mock_conn.getresponse.return_value = mock_response
+urlopen.side_effect = urllib2.HTTPError(%0A            url=%22https://vim-bootstrap.appspot.com/generate.vim%22,%0A            code=404, msg=%22Not Found%22, hdrs=None, fp=None)
 %0A%0A  
@@ -1211,17 +1211,25 @@
 ses(
-Exception
+urllib2.HTTPError
 ):%0A 
@@ -1269,24 +1269,29 @@
 hy'%5D)%0A%0A    @
+mock.
 patch('vim_b
@@ -1353,16 +1353,21 @@
 ')%0A    @
+mock.
 patch('v

Update tests for urlopen