Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made secret store iterable #51

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
]