def setUp(self):
     patcher = patch('requests.post')
     mock_post = patcher.start()
     mock_post.json.side_effect = [
         {'id': {'someNumber': []}},
     ]
     super(TestCKANDatastore, self).setUp()
     self.ckan_loader = CKANLoader(self.ckan_config)
     patcher.stop()
class TestCKANDatastore(TestCKANDatastoreBase):
    def setUp(self):
        patcher = patch('requests.post')
        mock_post = patcher.start()
        mock_post.json.side_effect = [
            {'id': {'someNumber': []}},
        ]
        super(TestCKANDatastore, self).setUp()
        self.ckan_loader = CKANLoader(self.ckan_config)
        patcher.stop()

    def test_datapusher_init(self):
        self.assertIsNotNone(self.ckan_loader)
        self.assertEquals(self.ckan_loader.ckan_url, 'localhost:9000/api/3/')
        self.assertEquals(self.ckan_loader.dump_url, 'localhost:9000/datastore/dump/')

    @patch('requests.post')
    def test_get_resource_id(self, post):
        mock_post = Mock()
        mock_post.json.side_effect = [
            {'result': {'resources': []}},
            {'result': {'resources': [{'name': 'NOT EXIST', 'id': 'who cares'}]}},
            {'result': {'resources': [{'name': 'exists', 'id': 'anID'}]}},
        ]
        post.return_value = mock_post

        self.assertIsNone(self.ckan_loader.get_resource_id(None, 'anything'))
        self.assertIsNone(self.ckan_loader.get_resource_id(None, 'exists'))
        self.assertEqual(self.ckan_loader.get_resource_id(None, 'exists'), 'anID')

    @patch('requests.post')
    def test_resource_exists(self, post):
        mock_post = Mock()
        mock_post.json.side_effect = [
            {'result': {'resources': []}},
            {'result': {'resources': [{'name': 'NOT EXIST', 'id': 'who cares'}]}},
            {'result': {'resources': [{'name': 'exists', 'id': 'anID'}]}},
        ]
        post.return_value = mock_post

        self.assertFalse(self.ckan_loader.resource_exists(None, 'exists'))
        self.assertFalse(self.ckan_loader.resource_exists(None, 'exists'))
        self.assertTrue(self.ckan_loader.resource_exists(None, 'exists'))

    @patch('requests.post')
    def test_create_resource(self, post):
        mock_post = Mock()
        mock_post.json.side_effect = [
            {'success': True, 'result': {'id': 1}},
            {'error': {'__type': ['not successful']}}
        ]
        post.return_value = mock_post

        self.assertEquals(self.ckan_loader.create_resource(None, None), 1)
        with self.assertRaises(CKANException):
            self.ckan_loader.create_resource(None, None)

    @patch('requests.post')
    def test_create_datastore(self, post):
        mock_post = Mock()
        mock_post.json.side_effect = [
            {'success': True, 'result': {'resource_id': 1}},
            {'error': {'name': ['not successful']}}
        ]
        post.return_value = mock_post

        self.assertEquals(self.ckan_loader.create_datastore(None, []), 1)
        with self.assertRaises(CKANException):
            self.ckan_loader.create_datastore(None, [])

    @patch('requests.post')
    def test_generate_datastore(self, post):
        mock_post = Mock()
        mock_post.json.side_effect = [
            {'success': True, 'result': {'id': 1}},
            {'success': True, 'result': {'resource_id': 1}},
        ]
        post.return_value = mock_post

        self.assertEquals(self.ckan_loader.generate_datastore([]), 1)

    @patch('requests.post')
    def test_delete_datastore(self, post):
        type(post.return_value).status_code = PropertyMock(return_value=204)
        self.assertEquals(self.ckan_loader.delete_datastore(None), 204)

    @patch('requests.post')
    def test_upsert(self, post):
        type(post.return_value).status_code = PropertyMock(return_value=200)
        self.assertEquals(self.ckan_loader.upsert(None, None), 200)

    @patch('requests.post')
    def test_update_metadata(self, post):
        type(post.return_value).status_code = PropertyMock(return_value=200)
        self.assertEquals(self.ckan_loader.update_metadata(None), 200)