forked from rc/aircox
!112 Co-authored-by: bkfox <thomas bkfox net> Reviewed-on: rc/aircox#113
This commit is contained in:
62
aircox/tests/admin/test_filters.py
Normal file
62
aircox/tests/admin/test_filters.py
Normal file
@ -0,0 +1,62 @@
|
||||
from datetime import date, timedelta
|
||||
|
||||
from django.contrib.admin import filters as d_filters
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
import pytest
|
||||
|
||||
from ..conftest import req_factory
|
||||
from aircox import models
|
||||
from aircox.admin import filters
|
||||
|
||||
|
||||
class FakeFilter(d_filters.FieldListFilter):
|
||||
def __init__(self, field, request, params, model, model_admin, field_path):
|
||||
self.field = field
|
||||
self.request = request
|
||||
self.params = params
|
||||
self.model = model
|
||||
self.model_admin = model_admin
|
||||
self.field_path = field_path
|
||||
|
||||
|
||||
class DateFieldFilter(filters.DateFieldFilter, FakeFilter):
|
||||
pass
|
||||
|
||||
|
||||
today = date.today()
|
||||
tomorrow = date.today() + timedelta(days=1)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def req():
|
||||
return req_factory.get("/test", {"pub_date__gte": today})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def date_filter(req):
|
||||
return DateFieldFilter(
|
||||
models.Page._meta.get_field("pub_date"),
|
||||
req,
|
||||
{"pub_date__lte": tomorrow, "other_param": 13},
|
||||
models.Page,
|
||||
None,
|
||||
"pub_date",
|
||||
)
|
||||
|
||||
|
||||
class TestDateFieldFilter:
|
||||
def test___init__(self, date_filter):
|
||||
assert date_filter.date_params == {"pub_date__lte": tomorrow}
|
||||
|
||||
date_filter.links = [
|
||||
(str(link[0]), *list(link[1:])) for link in date_filter.links
|
||||
]
|
||||
assert date_filter.links == [
|
||||
(str(_("None")), "pub_date__isnull", None, "1"),
|
||||
(str(_("Exact")), "pub_date__date", date_filter.input_type),
|
||||
(str(_("Since")), "pub_date__gte", date_filter.input_type),
|
||||
(str(_("Until")), "pub_date__lte", date_filter.input_type),
|
||||
]
|
||||
assert date_filter.query_attrs == {
|
||||
"pub_date__gte": today.strftime("%Y-%m-%d")
|
||||
}
|
@ -2,6 +2,10 @@ from datetime import time, timedelta
|
||||
import itertools
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import RequestFactory
|
||||
|
||||
import pytest
|
||||
from model_bakery import baker
|
||||
|
||||
@ -9,6 +13,21 @@ from aircox import models
|
||||
from aircox.test import Interface
|
||||
|
||||
|
||||
req_factory = RequestFactory()
|
||||
"""Request Factory used among different tests."""
|
||||
|
||||
|
||||
settings.ALLOWED_HOSTS = list(settings.ALLOWED_HOSTS) + [
|
||||
"sub.server.org",
|
||||
"server.org",
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def staff_user():
|
||||
return baker.make(User, is_active=True, is_staff=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def logger():
|
||||
logger = Interface(
|
||||
@ -18,8 +37,24 @@ def logger():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def stations():
|
||||
return baker.make(models.Station, _quantity=2)
|
||||
def station():
|
||||
return baker.make(
|
||||
models.Station,
|
||||
hosts="server.org",
|
||||
audio_streams="stream 1\nstream 2",
|
||||
default=True,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sub_station():
|
||||
"""Non-default station."""
|
||||
return baker.make(models.Station, hosts="sub.server.org", default=False)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def stations(station, sub_station):
|
||||
return [station, sub_station]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -28,7 +63,11 @@ def programs(stations):
|
||||
itertools.chain(
|
||||
*(
|
||||
baker.make(
|
||||
models.Program, station=station, cover=None, _quantity=2
|
||||
models.Program,
|
||||
station=station,
|
||||
cover=None,
|
||||
status=models.Program.STATUS_PUBLISHED,
|
||||
_quantity=2,
|
||||
)
|
||||
for station in stations
|
||||
)
|
||||
|
@ -24,7 +24,7 @@ class TestSchedule:
|
||||
@pytest.mark.django_db
|
||||
def test_tz(self, schedules):
|
||||
for schedule in schedules:
|
||||
assert schedule.timezone == schedule.tz.zone
|
||||
assert schedule.timezone == schedule.tz.key
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_start(self, schedules):
|
||||
@ -45,7 +45,7 @@ class TestSchedule:
|
||||
def test_normalize(self, schedules):
|
||||
for schedule in schedules:
|
||||
dt = datetime.combine(schedule.date, schedule.time)
|
||||
assert schedule.normalize(dt).tzinfo.zone == schedule.timezone
|
||||
assert schedule.normalize(dt).tzinfo.key == schedule.timezone
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_dates_of_month_ponctual(self):
|
||||
@ -117,7 +117,7 @@ class TestSchedule:
|
||||
assert dt.month == at.month
|
||||
assert dt.weekday() == schedule.date.weekday()
|
||||
assert dt.time() == schedule.time
|
||||
assert dt.tzinfo.zone == schedule.timezone
|
||||
assert dt.tzinfo.key == schedule.timezone
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_diffusions_of_month(self, sched_initials):
|
||||
|
45
aircox/tests/test_admin_site.py
Normal file
45
aircox/tests/test_admin_site.py
Normal file
@ -0,0 +1,45 @@
|
||||
from django.urls import path, reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
import pytest
|
||||
|
||||
from aircox import admin_site, urls as _urls
|
||||
from .conftest import req_factory
|
||||
|
||||
|
||||
# Just for code quality: urls module is required because we need some
|
||||
# url resolvers to be registered in order to run tests.
|
||||
_urls
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def site():
|
||||
return admin_site.AdminSite()
|
||||
|
||||
|
||||
class TestAdminSite:
|
||||
@pytest.mark.django_db
|
||||
def test_each_context(self, site, staff_user):
|
||||
req = req_factory.get("admin/test")
|
||||
req.user = staff_user
|
||||
context = site.each_context(req)
|
||||
assert "programs" in context
|
||||
assert "diffusions" in context
|
||||
assert "comments" in context
|
||||
|
||||
def test_get_urls(self, site):
|
||||
extra_url = path("test/path", lambda *_, **kw: _)
|
||||
site.extra_urls.append(extra_url)
|
||||
urls = site.get_urls()
|
||||
assert extra_url in urls
|
||||
|
||||
def test_get_tools(self, site):
|
||||
tools = site.get_tools()
|
||||
tools = dict(tools)
|
||||
assert tools == {
|
||||
_("Statistics"): reverse("admin:tools-stats"),
|
||||
}
|
||||
|
||||
def test_route_view(self, site):
|
||||
# TODO
|
||||
pass
|
54
aircox/tests/test_converters.py
Normal file
54
aircox/tests/test_converters.py
Normal file
@ -0,0 +1,54 @@
|
||||
from datetime import date
|
||||
from django.utils.safestring import SafeString
|
||||
|
||||
import pytest
|
||||
|
||||
from aircox import converters
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def page_path_conv():
|
||||
return converters.PagePathConverter()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def week_conv():
|
||||
return converters.WeekConverter()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def date_conv():
|
||||
return converters.DateConverter()
|
||||
|
||||
|
||||
class TestPagePathConverter:
|
||||
def test_to_python(self, page_path_conv):
|
||||
val = "path_value"
|
||||
result = page_path_conv.to_python(val)
|
||||
assert result == "/" + val + "/"
|
||||
|
||||
def test_to_url(self, page_path_conv):
|
||||
val = "/val"
|
||||
result = page_path_conv.to_url(val)
|
||||
assert isinstance(result, SafeString)
|
||||
assert result == val[1:] + "/"
|
||||
|
||||
|
||||
class TestWeekConverter:
|
||||
def test_to_python(self, week_conv):
|
||||
val = "2023/02"
|
||||
assert week_conv.to_python(val) == date(2023, 1, 9)
|
||||
|
||||
def test_to_url(self, week_conv):
|
||||
val = date(2023, 1, 10)
|
||||
assert week_conv.to_url(val) == "2023/02"
|
||||
|
||||
|
||||
class TestDateConverter:
|
||||
def test_to_python(self, date_conv):
|
||||
val = "2023/02/05"
|
||||
assert date_conv.to_python(val) == date(2023, 2, 5)
|
||||
|
||||
def test_to_url(self, date_conv):
|
||||
val = date(2023, 5, 10)
|
||||
assert date_conv.to_url(val) == "2023/05/10"
|
Reference in New Issue
Block a user