Skip to content

Commit

Permalink
adding database builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliana Freire committed Nov 30, 2020
1 parent 18e04fe commit df73584
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exclude_lines =
raise AssertionError
raise NotImplementedError
super

pass

[run]
omit =
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ Running all the checks at once:
To fix style (_with black_):
- run `make apply-style`

### Documentation generation
Updating generated documentation:
- First install requirements running `make requirements-docs`.
- To recreate .rst files run `make update-docs`. If a new module was added, edit docs/source/index.rst file to add the rst file for the module manually.
- To test the documentation generated run `make docs`. It will generate html documentation files in docs/build/html folder.

## License
[Apache License 2.0](https://github.com/quintoandar/hive-metastore-client/blob/staging/LICENSE)

Expand Down
26 changes: 26 additions & 0 deletions docs/source/hive_metastore_client.builders.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
hive\_metastore\_client.builders package
========================================

Submodules
----------


.. automodule:: hive_metastore_client.builders.abstract_builder
:members:
:undoc-members:
:show-inheritance:


.. automodule:: hive_metastore_client.builders.database_builder
:members:
:undoc-members:
:show-inheritance:


Module contents
---------------

.. automodule:: hive_metastore_client.builders
:members:
:undoc-members:
:show-inheritance:
8 changes: 8 additions & 0 deletions docs/source/hive_metastore_client.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
hive\_metastore\_client package
===============================

Subpackages
-----------

.. toctree::
:maxdepth: 4

hive_metastore_client.builders

Submodules
----------

Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Welcome to Hive Metastore Client's documentation!
:caption: Contents:

hive_metastore_client.rst
hive_metastore_client.builders.rst

Indices and tables
==================
Expand Down
12 changes: 12 additions & 0 deletions examples/create_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from hive_metastore_client.builders.database_builder import DatabaseBuilder
from hive_metastore_client.hive_mestastore_client import HiveMetastoreClient

HIVE_HOST = "<ADD_HIVE_HOST_HERE>"
HIVE_PORT = 9083

with HiveMetastoreClient(HIVE_HOST, HIVE_PORT) as hive_client:

database_builder = DatabaseBuilder("database_name")
database = database_builder.build()

hive_client.create_database(database)
1 change: 1 addition & 0 deletions hive_metastore_client/builders/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Builder Classes."""
15 changes: 15 additions & 0 deletions hive_metastore_client/builders/abstract_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Abstract Builder Class."""
from abc import ABCMeta, abstractmethod


class AbstractBuilder(metaclass=ABCMeta):
"""Abstract Builder class with builder methods."""

@abstractmethod
def build(self) -> object:
"""
Builds the respective Thrift object.
:return: an instance of the built object
"""
pass
55 changes: 55 additions & 0 deletions hive_metastore_client/builders/database_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""DatabaseBuilder."""
from typing import Mapping

from hive_metastore_client.builders.abstract_builder import AbstractBuilder
from thrift_files.libraries.thrift_hive_metastore_client.ttypes import Database, PrincipalPrivilegeSet, PrincipalType # type: ignore # noqa: E501


class DatabaseBuilder(AbstractBuilder):
"""Builds thrift Database object."""

def __init__(
self,
name: str,
description: str = None,
location_uri: str = None,
parameters: Mapping[str, str] = None,
privileges: PrincipalPrivilegeSet = None,
owner_name: str = None,
owner_type: PrincipalType = None,
catalog_name: str = None,
):
"""
Constructor.
:param name: name of the database
:param description: description of the database
:param location_uri: location for the database
:param parameters: properties associated with the database
:param privileges: privilege grant info for the database
:param owner_name: owner name for the database
:param owner_type: owner type for the database
:param catalog_name: catalog name for the database
"""
self.name = name
self.description = description
self.location_uri = location_uri
self.parameters = parameters
self.privileges = privileges
self.owner_name = owner_name
self.owner_type = owner_type
self.catalog_name = catalog_name

def build(self) -> Database:
"""Returns the thrift Database object."""
database = Database(
name=self.name,
description=self.description,
locationUri=self.location_uri,
parameters=self.parameters,
privileges=self.privileges,
ownerName=self.owner_name,
ownerType=self.owner_type,
catalogName=self.catalog_name,
)
return database
29 changes: 29 additions & 0 deletions tests/unit/hive_metastore_client/builders/test_database_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from unittest import mock
from unittest.mock import Mock
from hive_metastore_client.builders.database_builder import DatabaseBuilder


class TestDatabaseBuilder:
@mock.patch("hive_metastore_client.builders.database_builder.Database")
def test_build(self, mocked_database):
# arrange
mocked_database_name = "database_name"

mocked_obj = Mock()
mocked_database.return_value = mocked_obj

# act
returned_value = DatabaseBuilder(name=mocked_database_name).build()

# assert
assert returned_value == mocked_obj
mocked_database.assert_called_once_with(
name=mocked_database_name,
description=None,
locationUri=None,
parameters=None,
privileges=None,
ownerName=None,
ownerType=None,
catalogName=None,
)

0 comments on commit df73584

Please sign in to comment.