From 8f6562ad090783fb64623fc81547a39e2c709ce5 Mon Sep 17 00:00:00 2001 From: Francesco D'Orlandi Date: Wed, 6 Mar 2024 12:18:39 +0100 Subject: [PATCH] fix: handle InMemoryDatabase find_schemas latest when no versions available for subject --- karapace/in_memory_database.py | 2 +- tests/unit/test_in_memory_database.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_in_memory_database.py diff --git a/karapace/in_memory_database.py b/karapace/in_memory_database.py index 222e38046..704a1eb6a 100644 --- a/karapace/in_memory_database.py +++ b/karapace/in_memory_database.py @@ -174,7 +174,7 @@ def find_schemas(self, *, include_deleted: bool, latest_only: bool) -> dict[Subj for subject, subject_data in self.subjects.items(): selected_schemas: list[SchemaVersion] = [] schemas = list(subject_data.schemas.values()) - if latest_only: + if latest_only and len(schemas) > 0: # TODO don't include the deleted here? selected_schemas = [schemas[-1]] else: diff --git a/tests/unit/test_in_memory_database.py b/tests/unit/test_in_memory_database.py new file mode 100644 index 000000000..04ac6102d --- /dev/null +++ b/tests/unit/test_in_memory_database.py @@ -0,0 +1,14 @@ +""" +Copyright (c) 2024 Aiven Ltd +See LICENSE for details +""" +from karapace.in_memory_database import InMemoryDatabase, Subject + + +class TestFindSchemas: + def test_returns_empty_list_when_no_schemas(self) -> None: + database = InMemoryDatabase() + subject = Subject("hello_world") + database.insert_subject(subject=subject) + expected = {subject: []} + assert database.find_schemas(include_deleted=True, latest_only=True) == expected