Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
usefulalgorithm committed Oct 9, 2024
1 parent 6968575 commit 9a1ac4e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
11 changes: 9 additions & 2 deletions aesop/commands/common/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any

import typer
from rich.box import SIMPLE
from rich.markdown import Markdown
from rich.panel import Panel

Expand All @@ -13,16 +14,20 @@ def _validate_input_file(
input_model: type[InputModel], input_file: typer.FileText
) -> typer.FileText:
if input_file.name == "<stdin>" and input_file.isatty():
console.print(Markdown("---"))
# Got nothing, print example and exit
example_contents = ["```json"]
example_contents.extend(input_model.example_json(indent=2).splitlines())
example_contents.append("```")
example_panel = Panel(
Markdown("\n".join(example_contents)),
title="[green][bold]Example input",
title="[green][bold][Example input]",
title_align="left",
padding=1,
box=SIMPLE,
)
console.print(example_panel)
console.print(Markdown("---"))
commands = " ".join(sys.argv[1:])
usage_contents = [
"Pipe the JSON body into the command:",
Expand All @@ -39,8 +44,10 @@ def _validate_input_file(
]
usage_panel = Panel(
Markdown("\n".join(usage_contents)),
title="[green][bold]Usage",
title="[green][bold][Usage]",
title_align="left",
padding=1,
box=SIMPLE,
)
console.print(usage_panel)

Expand Down
6 changes: 6 additions & 0 deletions tests/base_test_suite.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import random
import string
from typing import Optional, Sequence

import loguru
Expand All @@ -10,6 +12,10 @@


class BaseTestSuite:
@staticmethod
def gen_rand_str(length: int) -> str:
return "".join(random.choices(string.ascii_uppercase + string.digits, k=length))

@pytest.fixture(autouse=True)
def _setup(self, config_file: str) -> None:
self._config_file = config_file
Expand Down
89 changes: 89 additions & 0 deletions tests/commands/aspects/test_custom_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import json
from tempfile import NamedTemporaryFile
from typing import List

from pydantic import TypeAdapter

from aesop.commands.aspects.custom_metadata import _Item
from tests.base_test_suite import BaseTestSuite


class TestCustomMetadata(BaseTestSuite):
def test_add_get_then_remove_custom_metadata(self) -> None:
# Create bogus name and description
items = {
self.gen_rand_str(10): "0.12345",
self.gen_rand_str(10): '"this is a string"',
self.gen_rand_str(10): '{"name": "john doe"}',
}
entity_id = "DATASET~8CDF81FE6B8050607E78C53E8B704B05"
with NamedTemporaryFile("w+") as f:
f.write(
json.dumps(
{
"entity_id": entity_id,
"set": [
{"key": key, "value": value} for key, value in items.items()
],
}
)
)
f.seek(0)
res = self.run_app(
[
"custom-metadata",
"update",
f.name,
]
)
assert res.exit_code == 0

res = self.run_app(
[
"custom-metadata",
"get",
entity_id,
]
)
assert res.exit_code == 0
metadata = {
item.key: item.value
for item in TypeAdapter(List[_Item]).validate_json(res.stdout)
}
for key, value in items.items():
assert key in metadata
assert metadata[key] == value

with NamedTemporaryFile("w+") as f:
f.write(
json.dumps(
{
"entity_id": entity_id,
"unset": list(items.keys()),
}
)
)
f.seek(0)
res = self.run_app(
[
"custom-metadata",
"update",
f.name,
]
)
assert res.exit_code == 0

res = self.run_app(
[
"custom-metadata",
"get",
entity_id,
]
)
assert res.exit_code == 0
metadata = {
item.key: item.value
for item in TypeAdapter(List[_Item]).validate_json(res.stdout)
}
for key, value in items.items():
assert key not in metadata
8 changes: 2 additions & 6 deletions tests/commands/aspects/user_defined_resources/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import json
import random
import string

from aesop.commands.aspects.user_defined_resources.tags.models import (
AddTagsOutput,
Expand All @@ -15,10 +13,8 @@ class TestTags(BaseTestSuite):
def test_add_get_then_remove_tag(self) -> None:

# Create bogus name and description
name = "".join(random.choices(string.ascii_uppercase + string.digits, k=10))
description = "".join(
random.choices(string.ascii_uppercase + string.digits, k=40)
)
name = self.gen_rand_str(10)
description = self.gen_rand_str(40)

# Add a tag
res = self.run_app(
Expand Down

0 comments on commit 9a1ac4e

Please sign in to comment.