Exemplo n.º 1
0
    def test_create_repo(self, mock_describerepo, mock_request):
        """
        Test create_repo
        """
        mock_describerepo.return_value = MockRequestResponse(
            "", json=self.json_repo)
        mock_request.post.return_value = MockRequestResponse(
            "aaa", json=self.json_repo)
        repo = self.datary.create_repo('repo_name', 'category_test', amount=1)
        repo2 = self.datary.create_repo('repo_nane',
                                        'category_test',
                                        repo_uuid='123',
                                        amount=1)

        mock_request.post.side_effect = Exception('Amount not introduced')
        mock_describerepo.return_value = {}
        repo4 = self.datary.create_repo('repo_nane',
                                        'category_test',
                                        repo_uuid='123')

        self.assertEqual(repo.json().get('uuid'),
                         '47eq5s12-5el1-2hq2-2ss1-egx517b1w967')
        self.assertEqual(repo2.json().get('uuid'),
                         '47eq5s12-5el1-2hq2-2ss1-egx517b1w967')
        self.assertEqual(mock_request.post.call_count, 1)
        self.assertEqual(mock_describerepo.call_count, 3)
        self.assertEqual(repo4, {})
Exemplo n.º 2
0
    def test_get_commited_dataset_uuid(self, mock_request):
        """
        Test Datary get_commited_dataset_uuid
        """

        # no args path and basename introduced
        mock_request.return_value = MockRequestResponse("",
                                                        json=self.dataset_uuid)
        result_no_pathname = self.datary.get_commited_dataset_uuid(
            self.wdir_uuid)
        self.assertEqual(result_no_pathname, {})
        self.assertEqual(mock_request.call_count, 0)

        # good case
        result = self.datary.get_commited_dataset_uuid(self.wdir_uuid, 'path',
                                                       'basename')
        self.assertEqual(result, self.dataset_uuid)
        self.assertEqual(mock_request.call_count, 1)

        # datary request return None
        mock_request.reset_mock()
        mock_request.return_value = MockRequestResponse("", status_code=500)

        no_response_result = self.datary.get_commited_dataset_uuid(
            self.wdir_uuid, 'path', 'basename')
        self.assertEqual(no_response_result, {})
        self.assertEqual(mock_request.call_count, 1)
Exemplo n.º 3
0
    def test_get_wdir_changes(self, mock_describerepo, mock_request):
        """
        Test get_wdir_changes
        """
        mock_request.return_value = MockRequestResponse(
            "", json=self.wdir_json.get('workdir'))
        workdir = self.datary.get_wdir_changes(self.wdir_uuid)
        self.assertEqual(mock_request.call_count, 1)
        assert isinstance(workdir, dict)

        mock_describerepo.return_value = self.json_repo
        mock_request.return_value = MockRequestResponse(
            "", json=self.wdir_json.get('workdir'))
        workdir = self.datary.get_wdir_changes(repo_uuid=self.repo_uuid)
        self.assertEqual(mock_request.call_count, 2)
        assert isinstance(workdir, dict)
Exemplo n.º 4
0
 def test(self):
     """
     Test MockRequestsResponse
     """
     test = MockRequestResponse('aaaa', path='test_path')
     self.assertEqual(test.text, 'aaaa')
     self.assertEqual(test.path(), 'test_path')
     self.assertEqual(test.encoding(), 'utf-8')
Exemplo n.º 5
0
    def test_describerepo(self, mock_get_member_repo, mock_request):
        """
        Test describerepo
        """
        mock_get_member_repo.return_value = self.json_repo
        mock_request.get.return_value = MockRequestResponse(
            "aaa", json=self.json_repo)

        repo = self.datary.get_describerepo(self.repo_uuid)
        self.assertEqual(mock_request.get.call_count, 1)
        assert isinstance(repo, dict)
        self.assertEqual(repo.get('name'), 'test_repo')
        self.assertEqual(mock_get_member_repo.call_count, 0)

        repo = self.datary.get_describerepo(member_uuid=self.member_uuid)
        self.assertEqual(mock_request.get.call_count, 1)
        assert isinstance(repo, dict)
        self.assertEqual(repo.get('name'), 'test_repo')
        self.assertEqual(mock_get_member_repo.call_count, 1)

        mock_request.get.return_value = MockRequestResponse(
            "", status_code=204, json=self.json_repo)
        repo2 = self.datary.get_describerepo(self.repo_uuid)
        assert isinstance(repo, dict)
        self.assertEqual(repo2.get('name'), 'test_repo')

        mock_request.get.return_value = MockRequestResponse(
            "aaa", json=[self.json_repo, self.json_repo2])
        repo3 = self.datary.get_describerepo(
            '0dc6379e-741d-11e6-8b77-86f30ca893d3')
        assert isinstance(repo, dict)
        self.assertEqual(repo3.get('name'), 'test_repo2')

        repo4 = self.datary.get_describerepo(repo_name='test_repo2')
        assert isinstance(repo, dict)
        self.assertEqual(repo4.get('name'), 'test_repo2')

        mock_request.get.return_value = MockRequestResponse("a", json=[])
        repo5 = self.datary.get_describerepo(repo_name='test_repo2')
        self.assertEqual(repo5, {})

        # check fail requests returns None
        mock_request.get.return_value = MockRequestResponse("Break test",
                                                            status_code=500)
        repo6 = self.datary.get_describerepo(repo_name='test_repo2')
        self.assertEqual(repo6, {})
Exemplo n.º 6
0
    def test_get_metadata(self, mock_request):
        """
        Test Datary datasets get_metadata
        """
        mock_request.return_value = MockRequestResponse("",
                                                        json=self.element.get(
                                                            'data',
                                                            {}).get('meta'))
        metadata = self.datary.get_metadata(self.dataset_uuid, self.repo_uuid)
        self.assertEqual(mock_request.call_count, 1)
        self.assertTrue(isinstance(metadata, dict))
        self.assertEqual(metadata, self.element.get('data', {}).get('meta'))

        mock_request.return_value = MockRequestResponse("", status_code=500)
        metadata2 = self.datary.get_metadata(self.dataset_uuid, self.repo_uuid)
        self.assertTrue(isinstance(metadata2, dict))
        self.assertEqual(metadata2, {})
Exemplo n.º 7
0
 def test_get_wdir_filetree(self, mock_request):
     """
     Test get_wdir_filetree
     """
     mock_request.return_value = MockRequestResponse(
         "", json=self.wdir_json.get('changes'))
     changes = self.datary.get_wdir_filetree(self.repo_uuid)
     self.assertEqual(mock_request.call_count, 1)
     assert isinstance(changes, dict)
Exemplo n.º 8
0
    def test_override_file(self, mock_modify_requests):
        """
        Test datary operation modify override_file
        """

        mock_modify_requests.return_value = MockRequestResponse("")
        self.datary.override_file(
            self.json_repo.get('workdir', {}).get('uuid'), self.element)
        self.assertEqual(mock_modify_requests.call_count, 1)
Exemplo n.º 9
0
 def test_get_commit_filetree(self, mock_request):
     """
     Test get_commit_filetree
     """
     mock_request.return_value = MockRequestResponse(
         "", json=self.wdir_json.get('workdir'))
     workdir = self.datary.get_commit_filetree(
         self.repo_uuid, self.commit_sha1)
     self.assertEqual(mock_request.call_count, 1)
     assert isinstance(workdir, dict)
Exemplo n.º 10
0
 def test_delete_file(self, mock_request):
     """
     Test operation remove delete_file
     """
     mock_request.return_value = MockRequestResponse("")
     self.datary.delete_file(
         self.json_repo.get('workdir', {}).get('uuid'), self.element)
     mock_request.return_value = None
     self.datary.delete_file(
         self.json_repo.get('workdir', {}).get('uuid'), self.element)
     self.assertEqual(mock_request.call_count, 2)
Exemplo n.º 11
0
    def test_deleterepo(self, mock_request):
        """
        Test deleterepo
        """
        mock_request.delete.return_value = MockRequestResponse(
            "Repo {} deleted".format('123'))
        result = self.datary.delete_repo(repo_uuid='123')
        self.assertEqual(result, 'Repo 123 deleted')

        with self.assertRaises(Exception):
            self.datary.delete_repo()
Exemplo n.º 12
0
    def test_modify_request(self, mock_request):
        """
        Test datary operation modify modify_request
        """
        mock_request.return_value = MockRequestResponse("")
        self.datary.modify_request(
            self.json_repo.get('workdir', {}).get('uuid'), self.element)

        mock_request.return_value = None

        self.datary.modify_request(
            self.json_repo.get('workdir', {}).get('uuid'), self.big_element)
        self.assertEqual(mock_request.call_count, 2)
Exemplo n.º 13
0
    def test_delete_inode(self, mock_request):
        """
        Test operation remove delete_inode
        """
        mock_request.return_value = MockRequestResponse("")
        self.datary.delete_inode(
            self.json_repo.get('workdir', {}).get('uuid'), self.inode)
        mock_request.return_value = None
        self.datary.delete_inode(
            self.json_repo.get('workdir', {}).get('uuid'), self.inode)
        self.assertEqual(mock_request.call_count, 2)

        mock_request.side_effect = Exception('Test Err exception')
        with self.assertRaises(Exception):
            self.datary.delete_inode(
                self.json_repo.get('workdir', {}).get('uuid'), self.inode)
Exemplo n.º 14
0
    def test_rename_file(self, mock_request):
        """
        Test add_dir
        """
        mock_request.return_value = MockRequestResponse("")
        self.datary.rename_file(
            self.json_repo.get('workdir', {}).get('uuid'),
            {'path': 'test_path/path', 'basename': 'test_basename'},
            {'path': 'test_path/new_path', 'basename': 'test_new_basename'},
            )
        self.assertEqual(mock_request.call_count, 1)

        mock_request.reset_mock()

        mock_request.return_value = None
        self.datary.rename_file(
            self.json_repo.get('workdir', {}).get('uuid'),
            {'path': 'test_path/path', 'basename': 'test_basename'},
            {'path': 'test_path/new_path', 'basename': 'test_new_basename'},
            )
        self.assertEqual(mock_request.call_count, 1)
Exemplo n.º 15
0
    def test_request(self, mock_time, mock_requests):
        """
        Test get_request
        =============   =============   =======================================
        Parameter       Type            Description
        =============   =============   =======================================
        mock_request    mock            Mock datary.requests.requests.request
                                        function
        =============   =============   =======================================
        """

        mock_requests.get.return_value = MockRequestResponse(
            "ok", headers={'x-set-token': self.test_token})
        mock_requests.post.return_value = MockRequestResponse(
            "ok", headers={'x-set-token': self.test_token})
        mock_requests.put.return_value = MockRequestResponse(
            "ok", headers={'x-set-token': self.test_token})
        mock_requests.delete.return_value = MockRequestResponse(
            "ok", headers={'x-set-token': self.test_token})

        # test GET
        result1 = self.datary.request(self.url, 'GET')
        self.assertEqual(result1.text, 'ok')

        # test POST
        result2 = self.datary.request(self.url, 'POST')
        self.assertEqual(result2.text, 'ok')

        # test PUT
        result3 = self.datary.request(self.url, 'PUT')
        self.assertEqual(result3.text, 'ok')

        # test DELETED
        result4 = self.datary.request(self.url, 'DELETE')
        self.assertEqual(result4.text, 'ok')

        # test UNKNOWK http method
        with self.assertRaises(Exception):
            self.datary.request(self.url, 'UNKWOWN')

        # test status code wrong
        mock_requests.get.return_value = MockRequestResponse("err",
                                                             status_code=500)
        result5 = self.datary.request(self.url, 'GET')
        self.assertEqual(result5, None)

        # test status code 429
        responses_429_test = [
            MockRequestResponse(
                "Request limit exceeded, must wait for XX seconds",
                status_code=429),
            MockRequestResponse(
                "Request limit exceeded, must wait for XX seconds",
                status_code=429),
            MockRequestResponse(
                "Request limit exceeded, must wait for XX seconds",
                status_code=429),
            MockRequestResponse("Everything OK", status_code=200)
        ]

        mock_requests.get.side_effect = iter(responses_429_test)

        result7 = self.datary.request(self.url, 'GET')
        self.assertEqual(result7, None)

        # upgrade tries limit
        self.datary.tries_limit = 4
        mock_requests.get.side_effect = iter(responses_429_test)

        result8 = self.datary.request(self.url, 'GET')
        self.assertEqual(result8.text, 'Everything OK')

        self.assertEqual(mock_time.sleep.call_count, 3 + 3)

        mock_requests.get.side_effect = requests.RequestException('err')
        result6 = self.datary.request(self.url, 'GET')
        self.assertEqual(result6, None)

        self.assertEqual(mock_requests.get.call_count, 10)
        self.assertEqual(mock_requests.post.call_count, 1)
        self.assertEqual(mock_requests.put.call_count, 1)
        self.assertEqual(mock_requests.delete.call_count, 1)
Exemplo n.º 16
0
    def test_get_original(self, mock_request):
        """
        Test Datary datasets get_original
        """

        mock_request.return_value = MockRequestResponse("", json=self.original)
        original = self.datary.get_original(self.dataset_uuid, self.repo_uuid)
        self.assertEqual(mock_request.call_count, 1)
        self.assertTrue(isinstance(original, dict))
        self.assertEqual(original, self.original)
        mock_request.reset_mock()

        # not dataset_uuid, introduced
        original2 = self.datary.get_original(self.dataset_uuid, self.repo_uuid,
                                             self.wdir_uuid)
        self.assertEqual(mock_request.call_count, 1)
        self.assertTrue(isinstance(original2, dict))
        self.assertEqual(original2, self.original)
        mock_request.reset_mock()

        # not dataset_uuid, introduced
        original3 = self.datary.get_original(self.dataset_uuid,
                                             wdir_uuid=self.wdir_uuid)
        self.assertEqual(mock_request.call_count, 1)
        self.assertTrue(isinstance(original3, dict))
        self.assertEqual(original3, self.original)
        mock_request.reset_mock()

        mock_request.side_effect = iter([
            MockRequestResponse("", status_code=500),
            MockRequestResponse("", json=self.original)
        ])

        original4 = self.datary.get_original(self.dataset_uuid, self.repo_uuid)
        self.assertEqual(mock_request.call_count, 2)
        self.assertTrue(isinstance(original4, dict))
        self.assertEqual(original4, self.original)
        mock_request.reset_mock()

        mock_request.side_effect = iter([
            MockRequestResponse("", status_code=500),
            MockRequestResponse("", status_code=500)
        ])

        original4b = self.datary.get_original(self.dataset_uuid,
                                              self.repo_uuid)
        self.assertEqual(mock_request.call_count, 2)
        self.assertTrue(isinstance(original4b, dict))
        self.assertEqual(original4b, {})
        mock_request.reset_mock()

        # not dataset_uuid, introduced
        original5 = self.datary.get_original(
            MockRequestResponse("", status_code=500))

        self.assertEqual(mock_request.call_count, 0)
        self.assertTrue(isinstance(original5, dict))
        self.assertEqual(original5, {})
        mock_request.reset_mock()

        # scope
        mock_request.side_effect = iter([
            MockRequestResponse("", json=self.original),
            MockRequestResponse("", json=self.original)
        ])
        original6 = self.datary.get_original(self.dataset_uuid,
                                             self.repo_uuid,
                                             scope='repo')
        self.assertEqual(mock_request.call_count, 1)
        self.assertTrue(isinstance(original6, dict))
        self.assertEqual(original6, self.original)