Commit 5032dd4aff
5032dd4aff82f1deceae11269f8973e89945b893
parent: 532e20db6e
Verified · cmc
cmc <hello@cleberg.net> · 2025-08-03 00:49 UTC
feat: add tests
Layout: unified · split
.github/workflows/tests.yml
added
+26
| @@ -0,0 +1,26 @@ |
| 1 | name: Run Tests |
| 2 | |
| 3 | on: |
| 4 | push: |
| 5 | branches: |
| 6 | - main |
| 7 | pull_request: |
| 8 | branches: |
| 9 | - main |
| 10 | |
| 11 | jobs: |
| 12 | test: |
| 13 | runs-on: ubuntu-latest |
| 14 | steps: |
| 15 | - uses: actions/checkout@v3 |
| 16 | - name: Set up Python |
| 17 | uses: actions/setup-python@v4 |
| 18 | with: |
| 19 | python-version: "3.11" |
| 20 | - name: Install Dependencies |
| 21 | run: | |
| 22 | python -m pip install --upgrade pip |
| 23 | python -m pip install --upgrade pytest |
| 24 | python -m pip install -r requirements.txt |
| 25 | - name: Run Tests |
| 26 | run: pytest |
tests/test_search.py
added
+34
| @@ -0,0 +1,34 @@ |
| 1 | import unittest |
| 2 | from unittest.mock import patch |
| 3 | from search import search_log |
| 4 | |
| 5 | class TestSearchLog(unittest.TestCase): |
| 6 | |
| 7 | @patch('search.LOG_PATH', 'test_log_path') |
| 8 | def test_search_log_with_match(self, mock_log_path): |
| 9 | query = 'crumb' |
| 10 | # Create a dummy log file (for testing) |
| 11 | with open('test_log_path', 'w') as f: |
| 12 | f.write("* Example Entry\n") |
| 13 | f.write(":PROPERTIES:\n") |
| 14 | f.write(":URL: http://example.com\n") |
| 15 | f.write(":TIMESTAMP: 2023-10-27 10:00:00\n") |
| 16 | |
| 17 | search_log(query) |
| 18 | # Assert that the function prints the expected output |
| 19 | with open('test_log_path', 'r') as f: |
| 20 | output = f.read() |
| 21 | self.assertIn("* Example Entry\n", output) |
| 22 | |
| 23 | @patch('search.LOG_PATH', 'test_log_path') |
| 24 | def test_search_log_no_match(self, mock_log_path): |
| 25 | query = 'nonexistent' |
| 26 | with open('test_log_path', 'w') as f: |
| 27 | f.write("* Example Entry\n") |
| 28 | f.write(":PROPERTIES:\n") |
| 29 | f.write(":URL: http://example.com\n") |
| 30 | f.write(":TIMESTAMP: 2023-10-27 10:00:00\n") |
| 31 | search_log(query) |
| 32 | with open('test_log_path', 'r') as f: |
| 33 | output = f.read() |
| 34 | self.assertNotIn("* Example Entry\n", output) |
tests/test_server.py
added
+35
| @@ -0,0 +1,35 @@ |
| 1 | import unittest |
| 2 | from flask import Flask |
| 3 | from server import app |
| 4 | |
| 5 | class TestServer(unittest.TestCase): |
| 6 | |
| 7 | def test_log_visit_post(self): |
| 8 | # Create a test client |
| 9 | client = app.test_client() |
| 10 | |
| 11 | # Sample JSON data |
| 12 | data = { |
| 13 | 'title': 'Test Visit', |
| 14 | 'url': 'https://test.com', |
| 15 | 'hostname': 'test.com', |
| 16 | 'path': '/', |
| 17 | 'query': 'test', |
| 18 | 'tabId': 123, |
| 19 | 'windowId': 456, |
| 20 | 'favIconUrl': 'https://example.com/favicon.ico' |
| 21 | } |
| 22 | |
| 23 | # Send a POST request |
| 24 | response = client.post('/', data=data, content_type='application/json') |
| 25 | |
| 26 | # Assertions |
| 27 | self.assertEqual(response.status_code, 204) |
| 28 | # Optionally check the log file contents (for verification) |
| 29 | # This part would require additional logging setup. |
| 30 | # self.assertTrue('Test Visit' in self.get_log_content()) |
| 31 | |
| 32 | def test_log_visit_options(self): |
| 33 | # Send an OPTIONS request |
| 34 | response = client.options( '/', content_type='application/json') |
| 35 | self.assertEqual(response.status_code, 204) #Verify 204 No Content for OPTIONS |