Skip to content

Commit

Permalink
Merge pull request #20 from vil02/add_basic_tests_of_create_puzzle
Browse files Browse the repository at this point in the history
tests: test `create_puzzle`
  • Loading branch information
vil02 authored Jul 22, 2024
2 parents 3c356cc + 72231a9 commit 45f4f38
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 3 additions & 1 deletion puzzle_generator/create_puzzle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pathlib
import inspect
import sys

from .puzzle_data_encryption import decrypt_data, encrypt_data
from . import simple_encryption_utils as seu
Expand All @@ -16,6 +17,7 @@ def _run_puzzle(in_puzzle):
)
if new_puzzle is None:
print("This is a wrong answer. Try again!")
sys.exit(1)
else:
_run_puzzle(new_puzzle)

Expand All @@ -32,7 +34,7 @@ def _create_str(in_modules, in_functions, in_encrypted_puzzle):
def create(in_puzzle, output_path: pathlib.Path) -> None:
encrypted_puzzle = encrypt_data(in_puzzle, seu.encrypt_str)

needed_modules = ["hashlib", "itertools", "base64", "json"]
needed_modules = ["hashlib", "itertools", "base64", "json", "sys"]

needed_functions = [
seu.hash_bytes,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "puzzle-generator"
version = "0.1.2"
version = "0.1.3"
description = "Generates python code representing a puzzle"
authors = ["piotr.idzik <[email protected]>"]
readme = "./puzzle_generator/README.md"
Expand Down
66 changes: 66 additions & 0 deletions tests/test_create_puzzle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pathlib
import subprocess
import pytest

import puzzle_generator.create_puzzle as cp


@pytest.fixture(name="puzzle")
def fixture_puzzle():
return {
"str": "Question 1?",
"pass": "Answer 1",
"rest": {
"str": "Question 2?",
"pass": "Is this the final answer?",
"rest": {"str": "Congratulations!"},
},
}


@pytest.fixture(name="puzzle_path")
def fixture_puzzle_path(tmp_path) -> pathlib.Path:
return tmp_path / "puzzle.py"


def _run_puzzle_file(
in_puzzle_path: pathlib.Path, answers: list[str]
) -> subprocess.CompletedProcess[str]:
assert in_puzzle_path.is_file()
puzzle_dir = in_puzzle_path.parent
puzzle_name = in_puzzle_path.name
return subprocess.run(
["python3", puzzle_name],
cwd=puzzle_dir,
input="\n".join(answers),
text=True,
capture_output=True,
check=False,
)


def test_all_good_answers(puzzle, puzzle_path: pathlib.Path) -> None:
cp.create(puzzle, puzzle_path)
res = _run_puzzle_file(puzzle_path, ["Answer 1", "Is this the final answer?"])

assert res.returncode == 0
assert res.stdout == "Question 1?\nQuestion 2?\nCongratulations!\n"
assert not res.stderr


def test_second_answer_wrong(puzzle, puzzle_path: pathlib.Path) -> None:
cp.create(puzzle, puzzle_path)
res = _run_puzzle_file(puzzle_path, ["Answer 1", "This is a wrong answer"])
assert res.returncode == 1
assert (
res.stdout == "Question 1?\nQuestion 2?\nThis is a wrong answer. Try again!\n"
)
assert not res.stderr


def test_first_answer_wrong(puzzle, puzzle_path: pathlib.Path) -> None:
cp.create(puzzle, puzzle_path)
res = _run_puzzle_file(puzzle_path, ["This is a wrong answer."])
assert res.returncode == 1
assert res.stdout == "Question 1?\nThis is a wrong answer. Try again!\n"
assert not res.stderr

0 comments on commit 45f4f38

Please sign in to comment.