Skip to content

Commit

Permalink
Add support for TIME_ZONE setting. Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamghill committed Feb 26, 2024
1 parent f71f611 commit 224fc61
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
2 changes: 2 additions & 0 deletions coltrane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ def _merge_settings(base_dir: Path, django_settings: Dict[str, Any]) -> Dict[str
is_build_management_command = len(sys.argv) >= 2 and sys.argv[1] == "build" # noqa: PLR2004

debug = django_settings.get("DEBUG", getenv("DEBUG", "True") == "True")
time_zone = django_settings.get("TIME_ZONE", getenv("TIME_ZONE", "UTC"))

staticfiles_dirs = [
base_dir / "static",
Expand Down Expand Up @@ -356,6 +357,7 @@ def _merge_settings(base_dir: Path, django_settings: Dict[str, Any]) -> Dict[str
"STATIC_ROOT": base_dir / "output" / "static",
"STATIC_URL": "static/",
"STATICFILES_DIRS": staticfiles_dirs,
"TIME_ZONE": time_zone,
"LOGGING": {
"version": 1,
"disable_existing_loggers": False,
Expand Down
2 changes: 1 addition & 1 deletion coltrane/feeds.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import django.utils.timezone as timezone
from django.contrib.syndication.views import Feed
from django.utils import timezone

from coltrane.config.settings import get_description, get_site_url, get_title
from coltrane.retriever import ContentItem, get_content_items
Expand Down
4 changes: 4 additions & 0 deletions docs/source/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ The type of cache to use for `coltrane`. Acceptable options are: [`dummy`](https
### CACHE_LOCATION

The location of the cache. Required for `filesystem`, `memcache`, and `redis` cache options. The `filesystem` cache requires an absolute path. The `memcache` and `redis` cache options include multiple cache servers in a commma-delimited list.

### TIME_ZONE

The timezone of the server. Defaults to "UTC".
47 changes: 47 additions & 0 deletions tests/feeds/test_content_feed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from datetime import datetime
from pathlib import Path

from zoneinfo import ZoneInfo

from coltrane.feeds import ContentFeed


Expand Down Expand Up @@ -80,3 +83,47 @@ def test_item_description(settings, tmp_path: Path):
actual = ContentFeed().item_description(items[0])

assert actual == "a description"


def test_item_pubdate(settings, tmp_path: Path):
settings.BASE_DIR = tmp_path
(tmp_path / "content").mkdir()
(tmp_path / "content/test.md").write_text(
"""---
title: test title
description: a description
publish_date: 2024-02-25 22:36:00
---
test data
"""
)

settings.TIME_ZONE = "UTC"

items = ContentFeed().items()
actual = ContentFeed().item_pubdate(items[0])

assert actual == datetime(2024, 2, 25, 22, 36, tzinfo=ZoneInfo(key="UTC"))


def test_item_pubdate_with_time_zone(settings, tmp_path: Path):
settings.BASE_DIR = tmp_path
(tmp_path / "content").mkdir()
(tmp_path / "content/test.md").write_text(
"""---
title: test title
description: a description
publish_date: 2024-02-25 22:36:00
---
test data
"""
)

settings.TIME_ZONE = "America/Chicago"

items = ContentFeed().items()
actual = ContentFeed().item_pubdate(items[0])

assert actual == datetime(2024, 2, 25, 22, 36, tzinfo=ZoneInfo(key="America/Chicago"))
74 changes: 72 additions & 2 deletions tests/init/test_initialize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from copy import deepcopy
from os import environ
from pathlib import Path
from unittest.mock import ANY, patch

Expand Down Expand Up @@ -29,6 +30,7 @@
"STATIC_ROOT": ANY,
"STATIC_URL": ANY,
"STATICFILES_DIRS": ANY,
"TIME_ZONE": ANY,
"LOGGING": ANY,
"COLTRANE": deepcopy(DEFAULT_COLTRANE_SETTINGS),
"SETTINGS_MODULE": "coltrane",
Expand Down Expand Up @@ -56,8 +58,27 @@ def _get_settings_with_whitenoise():
return settings


def _get_settings_with_debug_false():
settings = deepcopy(DEFAULT_SETTINGS)
settings.update(
{
"WHITENOISE_MANIFEST_STRICT": False,
"STATICFILES_STORAGE": ANY,
}
)

settings["INSTALLED_APPS"] = deepcopy(DEFAULT_INSTALLED_APPS)
settings["INSTALLED_APPS"].insert(0, "whitenoise.runserver_nostatic")

settings["MIDDLEWARE"] = deepcopy(DEFAULT_MIDDLEWARE)
settings["MIDDLEWARE"].insert(1, "whitenoise.middleware.WhiteNoiseMiddleware")

settings["DEBUG"] = False

return settings


@patch("coltrane._configure_settings")
# @patch("coltrane._is_whitenoise_installed", return_value=False)
def test_initialize_no_base_dir(_configure_settings):
initialize()

Expand All @@ -67,7 +88,6 @@ def test_initialize_no_base_dir(_configure_settings):


@patch("coltrane._configure_settings")
# @patch("coltrane._is_whitenoise_installed", return_value=False)
def test_initialize_with_base_dir(_configure_settings):
initialize(BASE_DIR=Path("test"))

Expand Down Expand Up @@ -140,3 +160,53 @@ def test_initialize_with_template_tags_in_directory_with_py_extension(
expected["TEMPLATES"][0]["OPTIONS"]["builtins"].append("fake.templatetag")

_configure_settings.assert_called_once_with(expected)


@patch("coltrane._configure_settings")
def test_initialize_debug_setting(_configure_settings):
initialize(DEBUG=False)

expected = _get_settings_with_debug_false()

_configure_settings.assert_called_once_with(expected)


@patch.dict(environ, {"DEBUG": "False"})
@patch("coltrane._configure_settings")
def test_initialize_debug_env(_configure_settings):
initialize()

expected = _get_settings_with_debug_false()

_configure_settings.assert_called_once_with(expected)


@patch("coltrane._configure_settings")
def test_initialize_time_zone_default(_configure_settings):
initialize()

expected = _get_settings_with_whitenoise()
expected["TIME_ZONE"] = "UTC"

_configure_settings.assert_called_once_with(expected)


@patch("coltrane._configure_settings")
def test_initialize_time_zone_setting(_configure_settings):
initialize(TIME_ZONE="America/New_York")

expected = _get_settings_with_whitenoise()
expected["TIME_ZONE"] = "America/New_York"

_configure_settings.assert_called_once_with(expected)


@patch.dict(environ, {"TIME_ZONE": "America/Chicago"})
@patch("coltrane._configure_settings")
def test_initialize_time_zone_env(_configure_settings):
initialize()

expected = _get_settings_with_whitenoise()
expected["TIME_ZONE"] = "America/Chicago"

_configure_settings.assert_called_once_with(expected)

0 comments on commit 224fc61

Please sign in to comment.