Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak committed Sep 24, 2024
1 parent e12c771 commit cb9e8c0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
78 changes: 39 additions & 39 deletions care/facility/tests/test_pdf_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,47 @@ def compare_pngs(png_path1, png_path2):


def test_compile_typ(data):
sample_file_dir: Path = (
logo_path = (
Path(settings.BASE_DIR) / "staticfiles" / "images" / "logos" / "black-logo.svg"
)
data["logo_path"] = str(logo_path)
content = render_to_string(
"reports/patient_discharge_summary_pdf_template.typ", context=data
)

sample_files_dir: Path = (
settings.BASE_DIR / "care" / "facility" / "tests" / "sample_reports"
)
try:
logo_path = (
Path(settings.BASE_DIR)
/ "staticfiles"
/ "images"
/ "logos"
/ "black-logo.svg"
)
data["logo_path"] = str(logo_path)
content = render_to_string(
"reports/patient_discharge_summary_pdf_template.typ", context=data
)
subprocess.run( # noqa: S603
["typst", "compile", "-", sample_file_dir.as_posix(), "--format", "png"], # noqa: S607
input=content.encode("utf-8"),
capture_output=True,
check=True,
cwd="/",
)

number_of_pngs_generated = 2
# To be updated only if the number of sample png increase in future

for i in range(1, number_of_pngs_generated + 1):
current_sample_file_path = sample_file_dir / f"sample{i}.png"
current_test_output_file_path = sample_file_dir / f"test_output{i}.png"

if not compare_pngs(
current_sample_file_path, current_test_output_file_path
):
return False
return True
except Exception:
return False
finally:
# Clean up the generated files use path.unlink() instead of os.remove()
for file in sample_file_dir.glob("test_output*.png"):
file.unlink()

subprocess.run( # noqa: S603
[ # noqa: S607
"typst",
"compile",
"-",
sample_files_dir / "test_output{n}.png",
"--format",
"png",
],
input=content.encode("utf-8"),
capture_output=True,
check=True,
cwd="/",
)

sample_files = sorted(sample_files_dir.glob("sample*.png"))
test_generated_files = sorted(sample_files_dir.glob("test_output*.png"))

result = all(
compare_pngs(sample_image, test_output_image)
for sample_image, test_output_image in zip(
sample_files, test_generated_files, strict=True
)
)

for file in test_generated_files:
file.unlink()

return result


class TestTypstInstallation(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion care/users/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def setUpTestData(cls) -> None:
cls.user = cls.create_user("user", cls.district)

def create_reset_password_token(
self, user: User, *, expired: bool
self, user: User, expired: bool = False
) -> ResetPasswordToken:
token = ResetPasswordToken.objects.create(user=user)
if expired:
Expand Down
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from plug_config import manager

logger = logging.getLogger(__name__)

BASE_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
APPS_DIR = BASE_DIR / "care"
env = environ.Env()
Expand Down
2 changes: 1 addition & 1 deletion plug_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
hcx_plugin = Plug(
name="hcx",
package_name="git+https://github.com/ohcnetwork/care_hcx.git",
version="@main",
version="@sainak/fix-compatibility-with-care",
configs={},
)

Expand Down

0 comments on commit cb9e8c0

Please sign in to comment.