Files
axolotl/tests/test_convert.py
Aarush defee62d99 fix: fix CONTRIBUTING.md placeholders, bare except clauses, and add convert.py tests (#3485) [skip ci]
* docs: fix codestyle placeholders in CONTRIBUTING.md

Replace unresolved {codestyle} and {URLofCodestyle} template
variables with Ruff, the project's actual linter/formatter
as configured in .pre-commit-config.yaml.

* fix: replace bare except clauses with specific exception types

- quantization.py: use except ImportError for optional torchao imports
  (consistent with line 48 which already uses ImportError correctly)
- cli/config.py: use except (RuntimeError, AssertionError) for CUDA
  device property query

Prevents masking unrelated errors like KeyboardInterrupt or SystemExit.

* test: add unit tests for convert.py JSON/JSONL utilities

Cover FileReader, FileWriter, StdoutWriter, JsonParser,
JsonlSerializer, and JsonToJsonlConverter with 8 test cases
including roundtrip and edge case (empty list) scenarios.

Previously this module had zero test coverage.

* fix: address CodeRabbit review feedback

- quantization.py: catch (ImportError, RuntimeError) for optional
  torchao imports; CUDA wheel/GPU mismatches raise RuntimeError,
  not ImportError
- convert.py: remove unused output_file_path parameter from
  JsonToJsonlConverter.convert() — FileWriter already holds the
  output path from construction
- tests/test_convert.py: update call site to match new signature
2026-03-16 00:12:40 -04:00

92 lines
2.6 KiB
Python

"""Unit tests for src/axolotl/convert.py"""
import json
import pytest
from axolotl.convert import (
FileReader,
FileWriter,
JsonlSerializer,
JsonParser,
JsonToJsonlConverter,
StdoutWriter,
)
class TestJsonParser:
def test_parse_valid_json_array(self):
parser = JsonParser()
result = parser.parse('[{"key": "value"}]')
assert result == [{"key": "value"}]
def test_parse_valid_json_object(self):
parser = JsonParser()
result = parser.parse('{"key": "value"}')
assert result == {"key": "value"}
def test_parse_invalid_json_raises(self):
parser = JsonParser()
with pytest.raises(json.JSONDecodeError):
parser.parse("not valid json")
class TestJsonlSerializer:
def test_serialize_single_item(self):
serializer = JsonlSerializer()
result = serializer.serialize([{"a": 1}])
assert result == '{"a": 1}'
def test_serialize_multiple_items(self):
serializer = JsonlSerializer()
result = serializer.serialize([{"a": 1}, {"b": 2}])
lines = result.split("\n")
assert len(lines) == 2
assert json.loads(lines[0]) == {"a": 1}
assert json.loads(lines[1]) == {"b": 2}
def test_serialize_empty_list(self):
serializer = JsonlSerializer()
result = serializer.serialize([])
assert result == ""
class TestFileReaderWriter:
def test_read_write_roundtrip(self, tmp_path):
test_file = tmp_path / "test.txt"
content = '{"hello": "world"}'
writer = FileWriter(str(test_file))
writer.write(content)
reader = FileReader()
result = reader.read(str(test_file))
assert result == content
class TestStdoutWriter:
def test_write_to_stdout(self, capsys):
writer = StdoutWriter()
writer.write("hello")
captured = capsys.readouterr()
assert captured.out == "hello\n"
class TestJsonToJsonlConverter:
def test_convert_json_to_jsonl(self, tmp_path):
input_data = [{"name": "Alice"}, {"name": "Bob"}]
input_file = tmp_path / "input.json"
output_file = tmp_path / "output.jsonl"
input_file.write_text(json.dumps(input_data), encoding="utf-8")
converter = JsonToJsonlConverter(
FileReader(), FileWriter(str(output_file)), JsonParser(), JsonlSerializer()
)
converter.convert(str(input_file))
result = output_file.read_text(encoding="utf-8")
lines = result.split("\n")
assert len(lines) == 2
assert json.loads(lines[0]) == {"name": "Alice"}
assert json.loads(lines[1]) == {"name": "Bob"}