Skip to content

Commit

Permalink
add test and tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
diversemix committed Oct 3, 2023
1 parent 8c7d6be commit d3e01f0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
18 changes: 15 additions & 3 deletions app/db/models/app/counters.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ class EntityCounter(Base):
counter = sa.Column(sa.Integer, default=0)

def get_next_count(self) -> str:
"""Gets the next counter value"""
"""
Gets the next counter value and updates the row.
:return str: The next counter value.
"""
try:
db = object_session(self)
cmd = self._get_and_increment.bindparams(id=self.id)
Expand All @@ -81,8 +85,16 @@ def get_next_count(self) -> str:
_LOGGER.exception(f"When generating counter for {self.prefix}")
raise

def get_import_id(self, entity: CountedEntity) -> str:
"""gets an import id"""
def create_import_id(self, entity: CountedEntity) -> str:
"""
Creates a unique import id.
This uses the n-value of zero to conform to existing format.
:param CountedEntity entity: The entity you want counted
:raises RuntimeError: raised when the prefix is not an organisation.
:return str: The fully formatted import_id
"""
# Validation
prefix_ok = (
self.prefix == ORGANISATION_CCLW or self.prefix == ORGANISATION_UNFCCC
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/app/models/test_counters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from app.data_migrations import populate_counters
from app.db.models.app.counters import CountedEntity, EntityCounter


def test_import_id_generation(test_db):
populate_counters(test_db)
rows = test_db.query(EntityCounter).count()
assert rows > 0

row: EntityCounter = (
test_db.query(EntityCounter).filter(EntityCounter.prefix == "CCLW").one()
)
assert row is not None

assert row.prefix == "CCLW"
assert row.counter == 0

import_id = row.create_import_id(CountedEntity.Family)
assert import_id == "CCLW.family.i00000001.n0001"

row: EntityCounter = (
test_db.query(EntityCounter).filter(EntityCounter.prefix == "CCLW").one()
)
assert row.counter == 1

0 comments on commit d3e01f0

Please sign in to comment.