Skip to content

Commit

Permalink
Skeleton for testing filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
terjekv committed Aug 7, 2024
1 parent 0c442c1 commit 9b65eb1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
67 changes: 67 additions & 0 deletions mreg/api/v1/tests/test_filtering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

from typing import List

from mreg.models.host import Host
from mreg.models.resource_records import Cname

from .tests import MregAPITestCase

from unittest_parametrize import param
from unittest_parametrize import parametrize
from unittest_parametrize import ParametrizedTestCase


class FilterTestCase(ParametrizedTestCase, MregAPITestCase):
"""Test filtering."""

# endpoint, query_key, target, expected_hits
#
# NOTE: The generated hostnames are UNIQUE across every test case!
# The format is: f"{endpoint}{query_key}{i}.example.com".replace("_", "")
# where i is the index of the hostname (and we make three for each test).
@parametrize(
("endpoint", "query_key", "target", "expected_hits"),
[
param("hosts", "name", "hostsname0.example.com", 1, id="hosts_name"),
param("cnames", "host__name", "cnameshostname1.example.com", 1, id="cnames_host__name"),
param("cnames", "host__name__icontains", "cnameshostnameicontains", 3, id="cnames_host__icontains"),
],
)
def test_filtering_for_host(self, endpoint: str, query_key: str, target: str, expected_hits: str) -> None:
"""Test filtering on host."""

generate_count = 3
msg_prefix = f"{endpoint} : {query_key} -> {target} => "

hosts: List[Host] = []
cnames: List[Cname] = []
for i in range(generate_count):
hostname = f"{endpoint}{query_key}{i}.example.com".replace("_", "")
hosts.append(Host.objects.create(
name=hostname,
contact="[email protected]",
ttl=3600,
comment="Test host",
))

for i in range(generate_count):
cname = f"cname.{endpoint}{query_key}{i}.example.com".replace("_", "")
cnames.append(Cname.objects.create(
host=hosts[i],
name=cname,
ttl=3600
))

hostname = hosts[0].name
response = self.client.get(f"/api/v1/{endpoint}/?{query_key}={target}")
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(data["count"], expected_hits, msg=f"{msg_prefix} {data}")

for host in hosts:
host.delete()

for cname in cnames:
cname.delete()

1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tzdata==2024.1
tox
pytest
pytest-django
unittest_parametrize

# These are currently not used, but should be...
pylint
Expand Down

0 comments on commit 9b65eb1

Please sign in to comment.