Update tests and convert to stock unittest

For these simple tests Python's built-in unittest framework is more than enough.
No additional dependencies are required.

Added some more test cases with "special" characters to test the escaping code
better.
This commit is contained in:
Dennis Camera 2025-02-18 10:13:27 +01:00
parent 0bd3fa63b8
commit 17b826a6d3
5 changed files with 120 additions and 35 deletions

65
code/tests.py Normal file → Executable file
View file

@ -1,27 +1,58 @@
"""These tests can be run with pytest.
This requires pytest: pip install pytest
cd to the `code` directory and run `pytest`
"""
#!/usr/bin/env python3
"""To run these tests just execute this script."""
import json
from pathlib import Path
import unittest
from robots import json_to_txt, json_to_table, json_to_htaccess
class RobotsUnittestExtensions:
def loadJson(self, pathname):
with open(pathname, "rt") as f:
return json.load(f)
def test_robots_txt_creation():
robots_json = json.loads(Path("test_files/robots.json").read_text())
robots_txt = json_to_txt(robots_json)
assert Path("test_files/robots.txt").read_text() == robots_txt
def assertEqualsFile(self, f, s):
with open(f, "rt") as f:
f_contents = f.read()
return self.assertMultiLineEqual(f_contents, s)
def test_table_of_bot_metrices_md():
robots_json = json.loads(Path("test_files/robots.json").read_text())
robots_table = json_to_table(robots_json)
assert Path("test_files/table-of-bot-metrics.md").read_text() == robots_table
class TestRobotsTXTGeneration(unittest.TestCase, RobotsUnittestExtensions):
maxDiff = 8192
def setUp(self):
self.robots_dict = self.loadJson("test_files/robots.json")
def test_robots_txt_generation(self):
robots_txt = json_to_txt(self.robots_dict)
self.assertEqualsFile("test_files/robots.txt", robots_txt)
def test_htaccess_creation():
robots_json = json.loads(Path("test_files/robots.json").read_text())
robots_htaccess = json_to_htaccess(robots_json)
assert Path("test_files/.htaccess").read_text() == robots_htaccess
class TestTableMetricsGeneration(unittest.TestCase, RobotsUnittestExtensions):
maxDiff = 32768
def setUp(self):
self.robots_dict = self.loadJson("test_files/robots.json")
def test_table_generation(self):
robots_table = json_to_table(self.robots_dict)
self.assertEqualsFile("test_files/table-of-bot-metrics.md", robots_table)
class TestHtaccessGeneration(unittest.TestCase, RobotsUnittestExtensions):
maxDiff = 8192
def setUp(self):
self.robots_dict = self.loadJson("test_files/robots.json")
def test_htaccess_generation(self):
robots_htaccess = json_to_htaccess(self.robots_dict)
self.assertEqualsFile("test_files/.htaccess", robots_htaccess)
if __name__ == "__main__":
import os
os.chdir(os.path.dirname(__file__))
unittest.main(verbosity=2)