Skip to content

Commit

Permalink
Don't update a full taxon record with a partial one
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Jul 3, 2024
1 parent c81efd6 commit 7b1a911
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pyinaturalist_convert/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,18 @@ def to_model(self) -> Taxon:
)

def update(self, taxon: Taxon):
"""Update an existing record"""
"""Merge new values into an existing record"""
# Don't update a full taxon record with a partial one
if self.partial is False and taxon._partial is True:
return

new_taxon = self.from_model(taxon)
for col in [c.name for c in inspect(self).mapper.columns]:
if not (new_val := getattr(new_taxon, col)):
if (new_val := getattr(new_taxon, col)) is None:
continue
if col not in PRECOMPUTED_COLUMNS:
setattr(self, col, new_val)
# Precomputed columns: Don't overwrite non-null values
# Precomputed columns: Only overwrite null values
elif getattr(self, col) in [None, 0]:
setattr(self, col, new_val)

Expand Down
25 changes: 25 additions & 0 deletions test/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,28 @@ def test_save_taxa__preserve_precomputed_cols(tmp_path):
assert results[0].observations_count == 18017625
assert results[1].complete_species_count == 416
assert results[1].observations_count == 218279


def test_save_taxa__partial(tmp_path):
db_path = tmp_path / 'observations.db'
taxon_1 = Taxon(
id=3,
name='Aves',
rank='class',
preferred_common_name='Birds',
complete_species_count=10672,
observations_count=18017625,
)

create_tables(db_path)
save_taxa([taxon_1], db_path=db_path)

# Save with updated values for precomputed columns
taxon_1.preferred_common_name = 'updated!'
taxon_1._partial = True
save_taxa([taxon_1], db_path=db_path)

# Only previously null values (taxon_2.observations_count) in DB should be updated
results = list(get_db_taxa(db_path))
assert results[0].preferred_common_name == 'Birds'
assert results[0]._partial is False

0 comments on commit 7b1a911

Please sign in to comment.