Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

Commit

Permalink
YACHT-1256: Extracting logic validating source table and backup empti…
Browse files Browse the repository at this point in the history
…ness.

Adding test: Should make backup if source table is empty and existing backup is also empty (eg. schema change but without adding any data).
  • Loading branch information
radkomateusz committed Apr 15, 2019
1 parent a95630e commit 6519f61
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
22 changes: 10 additions & 12 deletions src/backup/default_backup_predicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ def test(self, big_query_table_metadata, table_entity):
if not self._is_possible_to_copy_table(big_query_table_metadata):
return False

if self.__table_has_up_to_date_backup(big_query_table_metadata, table_entity):
logging.info('Backup is up to date')
return False

return True

def __table_has_up_to_date_backup(self, big_query_table_metadata, table_entity):
last_backup = self.__get_last_table_backup_if_any(table_entity)

if not last_backup:
return True

if big_query_table_metadata.is_empty() and last_backup.numBytes > 0:
logging.info("Source table is empty. Not empty backup exists.")
return False

return self.__is_table_backup_up_to_date(big_query_table_metadata,
last_backup)
if self.__is_table_backup_up_to_date(big_query_table_metadata, last_backup):
logging.info('Backup is up to date')
return False

return True

def __get_last_table_backup_if_any(self, table_entity):
if table_entity is None:
Expand All @@ -45,9 +46,6 @@ def __is_table_backup_up_to_date(self, big_query_table_metadata, last_backup):
)
if source_table_last_modified_time > last_backup.last_modified:
logging.info("Backup time is older than table metadata")
if big_query_table_metadata.is_empty() and last_backup.numBytes > 0:
logging.info("Source table is empty. Not empty backup exists.")
return True
return False
return True

Expand Down
25 changes: 23 additions & 2 deletions tests/backup/test_default_backup_predicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_should_return_false_if_schema_is_not_defined(self, _):
@patch.object(BigQueryTableMetadata, 'is_empty', return_value=True)
@patch.object(BigQueryTableMetadata, 'get_last_modified_datetime',
return_value=datetime(2016, 11, 13, 15, 00))
def test_should_return_true_if_table_is_empty(self, _, _1):
def test_should_return_true_if_table_is_empty_and_there_is_no_backup(self, _, _1):
# given
predicate = DefaultBackupPredicate()
# when
Expand Down Expand Up @@ -171,4 +171,25 @@ def test_should_return_false_if_changed_table_is_empty_and_last_backup_is_not_em
# then
self.assertFalse(result, "ShouldBackupPredicate should return FALSE "
"if table was changed after last backup was made,"
"but source table is empty and bbq has not empty last backup")
"but source table is empty and bbq has not empty last backup")

@patch.object(BigQueryTableMetadata, 'is_empty', return_value=True)
@patch.object(BigQueryTableMetadata, 'get_last_modified_datetime',
return_value=datetime(2018, 11, 13, 17, 00))
def test_should_return_true_if_changed_table_is_empty_and_last_backup_is_also_empty(self, _1, _2): # nopep8 pylint: disable=C0301
# given
backup = Backup(
parent=self.table.key,
last_modified=datetime(2016, 11, 13, 15, 00),
numBytes=0
)
backup.put()
predicate = DefaultBackupPredicate()

# when
result = predicate.test(self.big_query_table_metadata, self.table)

# then
self.assertTrue(result, "ShouldBackupPredicate should return True "
"if table was changed after last backup was made,"
"but source table is empty and bbq has also empty last backup")

0 comments on commit 6519f61

Please sign in to comment.