Skip to content

Commit

Permalink
Made secret store iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsimb committed Jan 8, 2024
1 parent 589c8a7 commit 423e6b6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
24 changes: 23 additions & 1 deletion exasol/secret_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from inspect import cleandoc
from pathlib import Path
from typing import (
List,
Iterable,
Optional,
Tuple,
)

from sqlcipher3 import dbapi2 as sqlcipher # type: ignore
Expand Down Expand Up @@ -129,3 +130,24 @@ def __getattr__(self, key) -> str:
if val is None:
raise AttributeError(f'Unknown key "{key}"')
return val

def keys(self) -> Iterable[str]:
"""Iterator over keys akin to dict.keys()"""
with self._cursor() as cur:
res = cur.execute(f"SELECT key FROM {TABLE_NAME}")
for row in res:
yield row[0]

def values(self) -> Iterable[str]:
"""Iterator over values akin to dict.values()"""
with self._cursor() as cur:
res = cur.execute(f"SELECT value FROM {TABLE_NAME}")
for row in res:
yield row[0]

def items(self) -> Iterable[Tuple[str, str]]:
"""Iterator over keys and values akin to dict.items()"""
with self._cursor() as cur:
res = cur.execute(f"SELECT key, value FROM {TABLE_NAME}")
for row in res:
yield row[0], row[1]
27 changes: 27 additions & 0 deletions test/unit/test_secret_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,30 @@ def test_access_non_existing_key_as_attribute(secrets):
with pytest.raises(AttributeError) as ex:
secrets.non_existing_key
assert str(ex.value) == 'Unknown key "non_existing_key"'


@pytest.fixture
def secrets_with_names(secrets):
secrets.save("electrician", "John")
secrets.save("musician", "Linda")
secrets.save("prime_minister", "Rishi")
return secrets


def test_keys_iterator(secrets_with_names):
professions = list(secrets_with_names.keys())
assert professions == ["electrician", "musician", "prime_minister"]


def test_values_iterator(secrets_with_names):
people = list(secrets_with_names.values())
assert people == ["John", "Linda", "Rishi"]


def test_items_iterator(secrets_with_names):
people_and_prof = list(secrets_with_names.items())
assert people_and_prof == [
("electrician", "John"),
("musician", "Linda"),
("prime_minister", "Rishi"),
]

0 comments on commit 423e6b6

Please sign in to comment.