Skip to content

Commit

Permalink
test: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobi-De committed Dec 24, 2023
1 parent a79c591 commit 2d7d2a1
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Tips

To run a subset of tests::

$ pytest tests.test_falco
$ pytest tests/commands/test_htmx.py


Deploying
Expand Down
8 changes: 5 additions & 3 deletions docs/guides/deployment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ https://github.com/lincolnloop/django-production
Deployment Platforms
--------------------

Deployment is not a solved solution for me, it is still a pain, no matter how many time I do it, it never goes smoothly. If you can afford it I'll recommand
a managed solution (the cloud), if you any reason you decide to go the self-hosting route, I'll recommand you use a P.A.A.S (Platform as a Service) solution
to ease your burden or a least docker as a bare minimum. Deployment is not worth your blood and energy my friend.
Deployment is not a solved solution for me, it is still a pain, no matter how many time I do it, it never goes smoothly. If you can afford it I'll recommend
a managed solution (the cloud), if for any reason you decide to go the self-hosting route, I'll recommend you use a P.A.A.S (Platform as a Service) solution
to ease your burden or a least docker as a bare minimum.

.. Deployment is not worth your blood and energy my friend.
Paas and rent a VPS

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/interactive_user_interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Interactive user interfaces
===========================


In case you are not aware of it, htmx the new old hot stuff in web development, and pair really well with django.
.. In case you are not aware of it, htmx the new old hot stuff in web development, and pair really well with django.
Ressources and patterns
-----------------------
Expand Down
4 changes: 1 addition & 3 deletions docs/guides/optimizing_database_access.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Caching with Redis
-------------------

https://github.com/Suor/django-cacheops
https://github.com/noripyt/django-cachalot


Using indexes
Expand All @@ -40,6 +41,3 @@ Denormalization

Cursor based pagination
-----------------------



2 changes: 1 addition & 1 deletion docs/the_cli/migrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Remove / Reset your migrations
==============================

.. important::
Unless you are certain of what you're doing, both of these commands should not be used once your project has gone live.
Unless you know what you're doing, both of these commands should not be used once your project has gone live.
In other words, they become essentially useless once real users start interacting with your application.

rm-migrations
Expand Down
1 change: 1 addition & 0 deletions src/falco/commands/htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __call__(self):
raise cappa.Exit(f"Could not find htmx version {version}.", code=1)

filepath = self.output if str(self.output).endswith(".js") else self.output / "htmx.min.js"
filepath.parent.mkdir(parents=True, exist_ok=True)
filepath.write_text(content)

subtitle = (
Expand Down
6 changes: 4 additions & 2 deletions src/falco/commands/install_crud_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@


UTILS_FILE = get_falco_blueprints_path() / "crud" / "utils.py"
DEFAULT_INSTALL_PATH = "core/utils.py"


@cappa.command(help="Install utils necessary for CRUD views.", name="install-crud-utils")
class InstallCrudUtils:
output: Annotated[
Path,
cappa.Arg(
default=Path("core/utils.py"),
default=Path(DEFAULT_INSTALL_PATH),
help="The directory to write the utils file to.",
short="-o",
long="--output",
Expand All @@ -30,6 +31,7 @@ def __call__(self):
imports_template, code_template = extract_python_file_templates(UTILS_FILE.read_text())
output_file = self.output if self.output.name.endswith(".py") else self.output / "utils.py"
output_file.parent.mkdir(parents=True, exist_ok=True)
output_file.write_text(imports_template + self.output.read_text() + code_template)
output_file.touch(exist_ok=True)
output_file.write_text(imports_template + output_file.read_text() + code_template)
run_python_formatters(str(output_file))
rich_print("[green]CRUD Utils installed successfully.")
2 changes: 1 addition & 1 deletion src/falco/commands/sync_dotenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __call__(self, project_name: Annotated[str, cappa.Dep(get_current_dir_as_pro
# empty and write to .env.template file
original_values = dotenv_values(dotenv_template_file)
dotenv_template_file.write_text("")
for key, value in sorted_config.items():
for key in sorted_config:
set_key(
dotenv_template_file,
key,
Expand Down
30 changes: 30 additions & 0 deletions tests/commands/test_htmx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pathlib import Path

from cappa.testing import CommandRunner


def test_htmx_download(runner: CommandRunner):
runner.invoke("htmx")
assert Path("htmx.min.js").exists()


def test_htmx_download_with_version(runner: CommandRunner):
runner.invoke("htmx", "latest")
assert Path("htmx.min.js").exists()


def test_htmx_download_with_specific_version(runner: CommandRunner):
runner.invoke("htmx", "1.8.0")
assert Path("htmx.min.js").exists()


def test_htmx_download_to_output_dir(runner: CommandRunner):
output = Path("htmx/vendors")
runner.invoke("htmx", "-o", str(output.resolve()))
assert (output / "htmx.min.js").exists()


def test_htmx_download_to_output_file(runner: CommandRunner):
output = Path("htmx/vendors/htmx.js")
runner.invoke("htmx", "-o", str(output.resolve()))
assert output.exists()
26 changes: 26 additions & 0 deletions tests/commands/test_htmx_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pathlib import Path

from cappa.testing import CommandRunner


def test_htmx_ext_download(runner: CommandRunner):
runner.invoke("htmx-ext", "sse")
assert Path("sse.js").exists()


# def test_htmx_ext_list_extensions(runner: CommandRunner):
# result = runner.invoke("htmx-ext")
# assert result.strip() != ""
# assert "sse" in result


def test_htmx_ext_download_to_output_dir(runner: CommandRunner):
output = Path("htmx/vendors/extensions")
runner.invoke("htmx-ext", "sse", "-o", str(output.resolve()))
assert (output / "sse.js").exists()


def test_htmx_ext_download_to_output_file(runner: CommandRunner):
output = Path("htmx/vendors/extensions/sse.js")
runner.invoke("htmx-ext", "sse", "-o", str(output.resolve()))
assert output.exists()
21 changes: 21 additions & 0 deletions tests/commands/test_install_crud_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path

from cappa.testing import CommandRunner
from falco.commands.install_crud_utils import DEFAULT_INSTALL_PATH


def test_install_crud_utils(runner: CommandRunner):
runner.invoke("install-crud-utils")
assert Path(DEFAULT_INSTALL_PATH).exists()


def test_install_crud_utils_to_output_dir(runner: CommandRunner):
output = Path("core")
runner.invoke("install-crud-utils", "-o", str(output.resolve()))
assert (output / "utils.py").exists


def test_install_crud_utils_to_output_file(runner: CommandRunner):
output = Path("core/utils.py")
runner.invoke("install-crud-utils", "-o", str(output.resolve()))
assert output.exists()
82 changes: 82 additions & 0 deletions tests/commands/test_sync_dotenv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from pathlib import Path

from cappa.testing import CommandRunner


def test_sync_dotenv(runner: CommandRunner):
runner.invoke("sync-dotenv")
env_file = Path(".env")
env_template_file = Path(".env.template")
assert env_file.exists()
assert env_template_file.exists()
assert "DJANGO_DEBUG=True" in env_file.read_text()
assert "DJANGO_DEBUG=" in env_template_file.read_text()


def test_sync_dotenv_update_files(runner: CommandRunner):
env_file = Path(".env")
env_template_file = Path(".env.template")
env_file.write_text("ANOTHER_SPECIAL_ENV=True")
env_template_file.write_text("SPECIAL_ENV=")
runner.invoke("sync-dotenv")
assert "SPECIAL_ENV=" in env_file.read_text()
assert "ANOTHER_SPECIAL_ENV=" in env_template_file.read_text()


def test_sync_dotenv_priority(runner: CommandRunner):
env_file = Path(".env")
env_template_file = Path(".env.template")
env_file.write_text("SPECIAL_ENV=True")
env_template_file.write_text("SPECIAL_ENV=")
runner.invoke("sync-dotenv")
assert "SPECIAL_ENV=True" in env_file.read_text()


# TODO: test fill missing


#
# def test_write_env_with_template(tmp_path: Path):
# env_template = tmp_path / ".env.template"
# env_template.write_text("DJANGO_SPECIAL_KEY=")
# result = runner.invoke(cli, ["write-env"])
#
# env_file_content = dotenv_values(".env")
#
# assert "SUCCESS" in result.output
# assert "DJANGO_SPECIAL_KEY" in env_file_content
#
#
# def test_write_env_to_output(tmp_path: Path):
# result = runner.invoke(cli, ["write-env", "-o", "output.env"])
# output_env = tmp_path / "output.env"
#
# output_env_file_content = dotenv_values(output_env)
#
# assert "SUCCESS" in result.output
# assert output_env.exists()
# assert "DJANGO_SECRET_KEY" in output_env_file_content
#
#
# def test_write_env_priority_order(tmp_path: Path):
# original_env = tmp_path / ".env"
# original_env.write_text("DJANGO_SPECIAL_KEY=my_special_key")
#
# env_template = tmp_path / ".env.template"
# env_template.write_text("DJANGO_SPECIAL_KEY=")
#
# result = runner.invoke(cli, ["write-env"])
#
# env_file_content = dotenv_values(".env")
#
# assert "SUCCESS" in result.output
# assert "DJANGO_SECRET_KEY" in env_file_content
# assert env_file_content["DJANGO_SPECIAL_KEY"] == "my_special_key"
#
#
# def test_write_env_postgres_pass(tmp_path: Path):
# runner.invoke(cli, ["write-env", "-p"], input="password")
#
# env_file_content = dotenv_values(".env")
#
# assert "password" in env_file_content["DATABASE_URL"]
33 changes: 33 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
import os
import subprocess

import pytest
from cappa.testing import CommandRunner
from falco.__main__ import Falco


@pytest.fixture(autouse=True)
def change_test_dir(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)


@pytest.fixture
def runner():
return CommandRunner(Falco)


@pytest.fixture
def django_project(tmp_path):
project_dir = tmp_path / "myproject"
subprocess.run(["django-admin", "startproject", "myproject", str(project_dir)], check=True)
os.chdir(project_dir)

# Create a new Django app
subprocess.run(["python", "manage.py", "startapp", "myapp"], check=True)

# Create a basic model in the app
model_code = """
from django.db import models
class MyModel(models.Model):
field1 = models.CharField(max_length=200)
field2 = models.IntegerField()
"""
(project_dir / "myapp" / "models.py").write_text(model_code)

yield project_dir
os.chdir(tmp_path)

0 comments on commit 2d7d2a1

Please sign in to comment.