Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
Merge pull request #27 from gbowne1/tests/improvements
  • Loading branch information
shoshta73 authored Oct 13, 2024
2 parents 557c521 + 595d141 commit a890169
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 83 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: test

on:
push:
branches: [ "master" ]
branches: ["master"]
pull_request:
branches: [ "master" ]
branches: ["master"]

jobs:
test:
Expand All @@ -15,9 +15,17 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
- name: Setup Venv
run: |
python -m pip install --upgrade pip
python -m venv ./venv
- name: Install packages
run: |
source ./venv/bin/activate
pip install -r requirements.txt
pip install .
- name: Run tests
run: pytest
run: |
source ./venv/bin/activate
pytest
2 changes: 1 addition & 1 deletion tests/test_format_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from unittest.mock import mock_open
from jsonmaestro import is_json, get_format
from jsonmaestro.helpers import is_json, get_format

# Mock Data
valid_json_content = '{"name": "John", "age": 30}' # Valid JSON content
Expand Down
77 changes: 0 additions & 77 deletions tests/test_json_cleaner.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import pytest
import json
import os
import tempfile
from pathlib import Path
from jsonmaestro import add_schema_keys, sort_json_keys


def create_temp_json(tmp_path, content):
file_path = tmp_path / "test_file.json"
try:
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(content, f)
return file_path
except IOError as e:
raise AssertionError(f"Failed to create temporary file: {e}")


def test_add_schema_keys():
input_data = {}
result = add_schema_keys(input_data)
assert "json.schemas" in result
assert isinstance(result["json.schemas"], list)
assert len(result["json.schemas"]) > 0


def test_sort_json_keys():
Expand All @@ -31,63 +14,3 @@ def test_sort_json_keys():

result_reverse = sort_json_keys(input_data, reverse=True)
assert list(result_reverse.keys()) == ["c", "b", "a"]


@pytest.fixture(scope='function')
def cleanup_temp_files(tmp_path_factory):
yield
for file in tmp_path_factory.mktemp("cleanup").iterdir():
os.remove(file)


def test_cleanup_temp_files(cleanup_temp_files):
content = '''
{
"test_key": "test_value"
}
'''
file_path = create_temp_json(tmp_path_factory.mktemp("test_dir"), content)
assert file_path.exists()
cleanup_temp_files
assert not file_path.exists()


def test_create_multiple_files(tmp_path_factory):
content1 = '{"key1": "value1"}'
content2 = '{"key2": "value2"}'

file1 = create_temp_json(tmp_path_factory.mktemp("test_dir_1"), content1)
file2 = create_temp_json(tmp_path_factory.mktemp("test_dir_2"), content2)

assert file1.exists()
assert file2.exists()
assert file1 != file2

cleanup_temp_files()
assert not file1.exists()
assert not file2.exists()


def test_parse_json_content(tmp_path):
content = '''
{
"key1": "value1",
"key2": 42
}
'''
file_path = create_temp_json(tmp_path, content)
with open(file_path, 'r') as f:
parsed_content = json.load(f)
assert parsed_content == {"key1": "value1", "key2": 42}


def test_tempfile_usage():
with tempfile.TemporaryDirectory() as temp_dir:
file_path = Path(temp_dir) / "test_file.txt"
with file_path.open(mode="w") as f:
f.write("Hello, pytest!")

assert file_path.exists()
assert file_path.read_text() == "Hello, pytest!"

# Cleanup happens automatically when exiting the context manager

0 comments on commit a890169

Please sign in to comment.