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 cd117fa
Show file tree
Hide file tree
Showing 10 changed files with 135 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 to define builder methods to be implemented."""

@abstractmethod
def build(self) -> object:
"""
To be implemented by each subclass and build the respective object.
:return: an instance of the object to be built
"""
pass
47 changes: 47 additions & 0 deletions hive_metastore_client/builders/database_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Database Builder Class."""
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):
"""Builder class for 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,
):
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:
"""
Builds Database object given builder parameters.
:return: Database object built
"""
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
18 changes: 18 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,18 @@
from hive_metastore_client.builders.database_builder import DatabaseBuilder
from thrift_files.libraries.thrift_hive_metastore_client.ttypes import Database


class TestDatabaseBuilder:
def test_build_with_name(self):
database_builder = DatabaseBuilder(name="database_name")
database = Database(name="database_name")

assert database == database_builder.build()

def test_build_with_description(self):
database_builder = DatabaseBuilder(
name="database_name", description="description"
)
database = Database(name="database_name", description="description")

assert database == database_builder.build()

0 comments on commit cd117fa

Please sign in to comment.