Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions tests/test_dar_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import unittest
import json
import os

from tools.validators.dar_validator import DARValidator


class TestDARValidator(unittest.TestCase):
def setUp(self):
self.valid_file = 'valid_test.dar'
self.invalid_file = 'invalid_test.dar'

valid_dar = {
"log": {
"version": "1.0",
"creator": {"name": "tester", "version": "1"},
"renders": [
{
"url": "https://example.com",
"status": "200",
"content": "<html></html>",
"time": "2024-01-01T00:00:00Z"
}
],
"result": {"summary": "ok"},
"entries": [
{"request": {}, "response": {}, "time": 0}
]
}
}

invalid_dar = {
"log": {
"version": 1.0,
"creator": {"name": "tester"},
"renders": "notalist",
"result": {"summary": 123},
"entries": "notalist"
}
}

with open(self.valid_file, 'w', encoding='utf-8') as f:
json.dump(valid_dar, f)

with open(self.invalid_file, 'w', encoding='utf-8') as f:
json.dump(invalid_dar, f)

def tearDown(self):
os.remove(self.valid_file)
os.remove(self.invalid_file)

def test_validate_correct_dar(self):
validator = DARValidator(self.valid_file)
errors = validator.validate()
self.assertEqual(errors, [])

def test_validate_malformed_dar(self):
validator = DARValidator(self.invalid_file)
Comment on lines +42 to +58
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding filenames in the working directory can lead to collisions or leftover files if a test crashes. Consider using Python's tempfile module (e.g., tempfile.NamedTemporaryFile or pytest's tmp_path) to create isolated temporary files.

Suggested change
with open(self.valid_file, 'w', encoding='utf-8') as f:
json.dump(valid_dar, f)
with open(self.invalid_file, 'w', encoding='utf-8') as f:
json.dump(invalid_dar, f)
def tearDown(self):
os.remove(self.valid_file)
os.remove(self.invalid_file)
def test_validate_correct_dar(self):
validator = DARValidator(self.valid_file)
errors = validator.validate()
self.assertEqual(errors, [])
def test_validate_malformed_dar(self):
validator = DARValidator(self.invalid_file)
self.valid_file = tempfile.NamedTemporaryFile(delete=False, suffix=".dar", mode='w', encoding='utf-8')
json.dump(valid_dar, self.valid_file)
self.valid_file.close()
self.invalid_file = tempfile.NamedTemporaryFile(delete=False, suffix=".dar", mode='w', encoding='utf-8')
json.dump(invalid_dar, self.invalid_file)
self.invalid_file.close()
def tearDown(self):
os.remove(self.valid_file.name)
os.remove(self.invalid_file.name)
def test_validate_correct_dar(self):
validator = DARValidator(self.valid_file.name)
errors = validator.validate()
self.assertEqual(errors, [])
def test_validate_malformed_dar(self):
validator = DARValidator(self.invalid_file.name)

Copilot uses AI. Check for mistakes.

errors = validator.validate()
self.assertGreater(len(errors), 0)


if __name__ == '__main__':
unittest.main()