Exemplo n.º 1
0
class BasicOperations(TestCase):
    """Tests basic operations in manager.py."""

    @factory.django.mute_signals(signals.pre_save)
    def setUp(self):
        self.username = "******"
        self.password = "******"
        self.email = "*****@*****.**"
        self.user = User.objects.create_user(
            self.username, self.email, self.password)

        self.mock_connection = self.create_patch(
            'core.db.manager.DataHubConnection')

        self.manager = DataHubManager(user=self.username)

    def create_patch(self, name):
        # helper method for creating patches
        patcher = patch(name)
        thing = patcher.start()
        self.addCleanup(patcher.stop)
        return thing

    def test_set_search_paths(self):
        con_set_spaths = self.mock_connection.return_value.set_search_paths
        search_paths = ['reponame', 'username']
        self.manager.set_search_paths(search_paths)
        self.assertTrue(con_set_spaths.called)

    def test_create_table(self):
        con_create_table = self.mock_connection.return_value.create_table
        repo = 'repo'
        table = 'table'
        params = {'id': 'integer', 'words': 'text'}
        self.manager.create_table(repo, table, params)
        self.assertTrue(con_create_table.called)

    def test_list_tables(self):
        con_list_tables = self.mock_connection.return_value.list_tables
        self.manager.list_tables('repo')

        self.assertTrue(con_list_tables.called)

    def test_describe_table(self):
        con_describe_table = self.mock_connection.return_value.describe_table
        self.manager.describe_table(repo='repo', table='table', detail=False)

        self.assertTrue(con_describe_table.called)

    def test_list_table_permissions(self):
        con_tbl_perm = self.mock_connection.return_value.list_table_permissions
        self.manager.list_table_permissions(repo='repo', table='table')

        self.assertTrue(con_tbl_perm.called)

    def test_delete_table(self):
        con_delete_table = self.mock_connection.return_value.delete_table
        self.manager.delete_table(repo='repo', table='table', force=False)
        self.assertTrue(con_delete_table.called)

    def test_list_views(self):
        con_list_views = self.mock_connection.return_value.list_views
        self.manager.list_views('repo')

        self.assertTrue(con_list_views.called)

    def test_create_view(self):
        con_create_view = self.mock_connection.return_value.create_view
        repo = 'repo_name'
        view = 'view_name'
        sql = 'SELECT * FROM table_name'
        self.manager.create_view(repo, view, sql)
        self.assertTrue(con_create_view.called)

    def test_describe_view(self):
        con_describe_view = self.mock_connection.return_value.describe_view
        self.manager.describe_view(
            repo='repo_name', view='_name', detail=False)
        self.assertTrue(con_describe_view.called)

    def test_delete_view(self):
        con_delete_view = self.mock_connection.return_value.delete_view
        self.manager.delete_view(
            repo='repo_name', view='view_name', force=False)
        self.assertTrue(con_delete_view.called)

    def test_list_repos(self):
        con_list_repos = self.mock_connection.return_value.list_repos
        self.manager.list_repos()

        self.assertTrue(con_list_repos.called)

    def test_rename_repo(self):
        con_rename_repo = self.mock_connection.return_value.rename_repo
        # self.mock_Collaborator = self.create_patch(
        #     'inventory.models.Collaborator')

        self.manager = DataHubManager(
            user=self.username, repo_base=self.username)
        self.manager.Collaborator = MagicMock

        self.manager.rename_repo('old_name', 'new_name')
        self.assertTrue(con_rename_repo.called)

    def test_create_repo(self):
        con_create_repo = self.mock_connection.return_value.create_repo
        self.manager.create_repo('repo')

        self.assertTrue(con_create_repo.called)

    def test_delete_repo(self):
        con_delete_repo = self.mock_connection.return_value.delete_repo
        self.manager = DataHubManager(
            user=self.username, repo_base=self.username)
        self.manager.delete_repo('repo')

        self.assertTrue(con_delete_repo.called)
        self.assertEqual(con_delete_repo.call_args[1]['force'], False)

    def test_add_collaborator(self):
        con_add_collab = self.mock_connection.return_value.add_collaborator
        self.create_patch('core.db.manager.User')
        mock_Collaborator = self.create_patch(
            'core.db.manager.Collaborator')
        mock_Collaborator.objects.get_or_create.return_value = (
            MagicMock(), True)

        self.manager.add_collaborator(
            repo='reponame',
            collaborator='new_collaborator',
            db_privileges=['select'], file_privileges=['read', 'write'])

        self.assertTrue(con_add_collab.called)
        self.assertEqual(con_add_collab.call_args[1]['repo'], 'reponame')
        self.assertEqual(
            con_add_collab.call_args[1]['collaborator'], 'new_collaborator')
        self.assertEqual(
            con_add_collab.call_args[1]['db_privileges'], ['SELECT'])

    def test_delete_collaborator(self):
        self.mock_connection.return_value.list_collaborators.return_value = [
            {'username': '******', 'permissions': 'U'},
            {'username': self.username, 'permissions': 'UC'}
            ]
        self.mock_User = self.create_patch('core.db.manager.User')
        self.mock_Collaborator = self.create_patch(
            'core.db.manager.Collaborator')

        con_delete_collab = (self.mock_connection
                             .return_value.delete_collaborator)
        self.manager.delete_collaborator('reponame', 'old_collaborator')

        self.assertTrue(con_delete_collab.called)
        self.assertEqual(con_delete_collab.call_args[1]['repo'], 'reponame')
        self.assertEqual(
            con_delete_collab.call_args[1]['collaborator'], 'old_collaborator')

    def test_get_schema(self):
        con_get_schema = self.mock_connection.return_value.get_schema
        self.manager.get_schema('reponame', 'tablename')

        self.assertTrue(con_get_schema.called)
        self.assertEqual(con_get_schema.call_args[1]['repo'], 'reponame')
        self.assertEqual(con_get_schema.call_args[1]['table'], 'tablename')
Exemplo n.º 2
0
class BasicOperations(TestCase):
    """Tests basic operations in manager.py."""

    @factory.django.mute_signals(signals.pre_save)
    def setUp(self):
        self.username = "******"
        self.password = "******"
        self.email = "*****@*****.**"
        self.user = User.objects.create_user(
            self.username, self.email, self.password)

        self.mock_connection = self.create_patch(
            'core.db.manager.DataHubConnection')

        self.manager = DataHubManager(user=self.username)

    def create_patch(self, name):
        # helper method for creating patches
        patcher = patch(name)
        thing = patcher.start()
        self.addCleanup(patcher.stop)
        return thing

    def test_create_table(self):
        con_create_table = self.mock_connection.return_value.create_table
        repo = 'repo'
        table = 'table'
        params = {'id': 'integer', 'words': 'text'}
        self.manager.create_table(repo, table, params)
        self.assertTrue(con_create_table.called)

    def test_list_tables(self):
        con_list_tables = self.mock_connection.return_value.list_tables
        self.manager.list_tables('repo')

        self.assertTrue(con_list_tables.called)

    def test_describe_table(self):
        con_describe_table = self.mock_connection.return_value.describe_table
        self.manager.describe_table(repo='repo', table='table', detail=False)

        self.assertTrue(con_describe_table.called)

    def test_list_table_permissions(self):
        con_tbl_perm = self.mock_connection.return_value.list_table_permissions
        self.manager.list_table_permissions(repo='repo', table='table')

        self.assertTrue(con_tbl_perm.called)

    def test_delete_table(self):
        con_delete_table = self.mock_connection.return_value.delete_table
        self.manager.delete_table(repo='repo', table='table', force=False)
        self.assertTrue(con_delete_table.called)

    def test_clone_table(self):
        con_clone_table = self.mock_connection.return_value.clone_table
        self.manager.clone_table(
            repo='repo_name', table='table_name', new_table='new_table_name')
        self.assertTrue(con_clone_table.called)

    def test_list_views(self):
        con_list_views = self.mock_connection.return_value.list_views
        self.manager.list_views('repo')

        self.assertTrue(con_list_views.called)

    def test_create_view(self):
        con_create_view = self.mock_connection.return_value.create_view
        repo = 'repo_name'
        view = 'view_name'
        sql = 'SELECT * FROM table_name'
        self.manager.create_view(repo, view, sql)
        self.assertTrue(con_create_view.called)

    def test_describe_view(self):
        con_describe_view = self.mock_connection.return_value.describe_view
        self.manager.describe_view(
            repo='repo_name', view='_name', detail=False)
        self.assertTrue(con_describe_view.called)

    def test_delete_view(self):
        con_delete_view = self.mock_connection.return_value.delete_view
        self.manager.delete_view(
            repo='repo_name', view='view_name', force=False)
        self.assertTrue(con_delete_view.called)

    def test_list_repos(self):
        con_list_repos = self.mock_connection.return_value.list_repos
        self.manager.list_repos()

        self.assertTrue(con_list_repos.called)

    def test_rename_repo(self):
        con_rename_repo = self.mock_connection.return_value.rename_repo
        # self.mock_Collaborator = self.create_patch(
        #     'inventory.models.Collaborator')

        self.manager = DataHubManager(
            user=self.username, repo_base=self.username)
        self.manager.Collaborator = MagicMock

        self.manager.rename_repo('old_name', 'new_name')
        self.assertTrue(con_rename_repo.called)

    def test_create_repo(self):
        con_create_repo = self.mock_connection.return_value.create_repo
        self.manager.create_repo('repo')

        self.assertTrue(con_create_repo.called)

    def test_delete_repo(self):
        con_delete_repo = self.mock_connection.return_value.delete_repo
        self.manager = DataHubManager(
            user=self.username, repo_base=self.username)
        self.manager.delete_repo('repo')

        self.assertTrue(con_delete_repo.called)
        self.assertEqual(con_delete_repo.call_args[1]['force'], False)

    def test_add_collaborator(self):
        con_add_collab = self.mock_connection.return_value.add_collaborator
        self.create_patch('core.db.manager.User')
        mock_Collaborator = self.create_patch(
            'core.db.manager.Collaborator')
        mock_Collaborator.objects.get_or_create.return_value = (
            MagicMock(), True)

        self.manager.add_collaborator(
            repo='reponame',
            collaborator='new_collaborator',
            db_privileges=['select'], file_privileges=['read', 'write'])

        self.assertTrue(con_add_collab.called)
        self.assertEqual(con_add_collab.call_args[1]['repo'], 'reponame')
        self.assertEqual(
            con_add_collab.call_args[1]['collaborator'], 'new_collaborator')
        self.assertEqual(
            con_add_collab.call_args[1]['db_privileges'], ['SELECT'])

    def test_delete_collaborator(self):
        self.mock_connection.return_value.list_collaborators.return_value = [
            {'username': '******', 'permissions': 'U'},
            {'username': self.username, 'permissions': 'UC'}
            ]
        self.mock_User = self.create_patch('core.db.manager.User')
        self.mock_Collaborator = self.create_patch(
            'core.db.manager.Collaborator')

        con_delete_collab = (self.mock_connection
                             .return_value.delete_collaborator)
        self.manager.delete_collaborator('reponame', 'old_collaborator')

        self.assertTrue(con_delete_collab.called)
        self.assertEqual(con_delete_collab.call_args[1]['repo'], 'reponame')
        self.assertEqual(
            con_delete_collab.call_args[1]['collaborator'], 'old_collaborator')

    def test_get_schema(self):
        con_get_schema = self.mock_connection.return_value.get_schema
        self.manager.get_schema('reponame', 'tablename')

        self.assertTrue(con_get_schema.called)
        self.assertEqual(con_get_schema.call_args[1]['repo'], 'reponame')
        self.assertEqual(con_get_schema.call_args[1]['table'], 'tablename')