Skip to content

Commit

Permalink
Add validation for empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Fonseca committed Jan 22, 2021
1 parent 30ca6c2 commit a2ba8c8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
14 changes: 13 additions & 1 deletion hive_metastore_client/hive_metastore_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,20 @@ def add_partitions_to_table(
"""
Add partitions to a table.
If the user tries to add a partition twice, the method handles the
AlreadyExistsException returning false and indicating the operation
result: True for new partition values added or False when nothing
was done.
:param db_name: database name where the table is at
:param table_name: table name which the partitions belong to
:param partition_list: list of partitions to be added to the table
"""
if not partition_list:
raise ValueError(
"m=add_partitions_to_table, msg=The partition list is empty."
)

table = self.get_table(dbname=db_name, tbl_name=table_name)

partition_list_with_correct_location = self._format_partitions_location(
Expand Down Expand Up @@ -202,4 +212,6 @@ def _validate_lists_length(list_a: List[Any], list_b: List[Any]) -> None:
:param list_b: second list to be compared
"""
if len(list_a) != len(list_b):
raise ValueError("The length of the two provided lists does not match")
raise ValueError(
"m=_validate_lists_length, msg=The length of the two provided lists does not match"
)
23 changes: 22 additions & 1 deletion tests/unit/hive_metastore_client/test_hive_metastore_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from unittest import mock
from unittest.mock import Mock
from unittest.mock import Mock, ANY

import pytest
from pytest import raises

from hive_metastore_client import HiveMetastoreClient
from thrift_files.libraries.thrift_hive_metastore_client.ThriftHiveMetastore import (
Expand Down Expand Up @@ -225,6 +226,26 @@ def test_add_partitions_to_table_with_duplicated_partitions(
)
mocked_add_partitions.assert_called_once_with(formatted_partitions_location)

@mock.patch.object(HiveMetastoreClient, "get_table")
@mock.patch.object(HiveMetastoreClient, "_format_partitions_location")
@mock.patch.object(HiveMetastoreClient, "add_partitions")
def test_add_partitions_to_table_with_invalid_partitions(
self,
mocked_add_partitions,
mocked__format_partitions,
mocked_get_table,
hive_metastore_client,
):
# assert
with raises(ValueError):
# act
hive_metastore_client.add_partitions_to_table(
db_name=ANY, table_name=ANY, partition_list=[]
)
mocked_get_table.assert_not_called()
mocked__format_partitions.assert_not_called()
mocked_add_partitions.assert_not_called()

@mock.patch.object(HiveMetastoreClient, "_validate_lists_length")
def test_format_partitions_location(
self, mocked_validate_lists_length, hive_metastore_client
Expand Down

0 comments on commit a2ba8c8

Please sign in to comment.