Skip to content

Commit

Permalink
test: Add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Estrada Irribarra, Rodrigo Andres committed Oct 23, 2024
1 parent 8bf8145 commit f6fa3fb
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 601 deletions.
163 changes: 0 additions & 163 deletions tests/test_chapters.py

This file was deleted.

87 changes: 87 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import sys
import pytest
import requests
from unittest import mock
from pathlib import Path
from click import ClickException
from storycraftr.cli import (
load_openai_api_key,
download_file,
verify_book_path,
is_initialized,
project_not_initialized_error,
init_structure,
)


# Mock the console object to avoid printing to the console during tests
@pytest.fixture
def mock_console():
with mock.patch("storycraftr.cli.console") as mock_console:
yield mock_console


# Test download_file function
@mock.patch("requests.get")
def test_download_file_success(mock_get, mock_console):
mock_response = mock.Mock()
mock_response.text = "file content"
mock_response.raise_for_status = mock.Mock()
mock_get.return_value = mock_response

save_dir = "test_dir"
filename = "test_file.txt"

with mock.patch("pathlib.Path.mkdir") as mock_mkdir, mock.patch(
"pathlib.Path.write_text"
) as mock_write:
download_file("http://example.com", save_dir, filename)

mock_mkdir.assert_called_once_with(parents=True, exist_ok=True)
mock_write.assert_called_once_with("file content", encoding="utf-8")
mock_console.print.assert_called_with(
"[green]File downloaded successfully from http://example.com[/green]"
)


@mock.patch("requests.get", side_effect=requests.exceptions.RequestException("Error"))
def test_download_file_failure(mock_get, mock_console):
with pytest.raises(SystemExit):
download_file("http://example.com", "test_dir", "test_file.txt")

mock_console.print.assert_called_with(
"[red]Error downloading the file from http://example.com: Error[/red]"
)


# Test verify_book_path function
@mock.patch("pathlib.Path.exists", return_value=True)
def test_verify_book_path_success(mock_console): # Se agrega el parámetro mock_console
assert verify_book_path("test_path") == "test_path"


@mock.patch("pathlib.Path.exists", return_value=False)
def test_verify_book_path_failure(mock_console): # Se agrega el parámetro mock_console
with pytest.raises(ClickException):
verify_book_path("invalid_path")


# Test is_initialized function
@mock.patch("pathlib.Path.exists", return_value=True)
def test_is_initialized_true(mock_console): # Se agrega el parámetro mock_console
assert is_initialized("test_path")


@mock.patch("pathlib.Path.exists", return_value=False)
def test_is_initialized_false(mock_console): # Se agrega el parámetro mock_console
assert not is_initialized("test_path")


# Test project_not_initialized_error function
def test_project_not_initialized_error(mock_console):
project_not_initialized_error("test_path")

mock_console.print.assert_called_with(
"[red]✖ Project 'test_path' is not initialized. Run 'storycraftr init {book_path}' first.[/red]"
)
110 changes: 110 additions & 0 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import os
import pytest
import shutil
from unittest import mock
from storycraftr.utils.markdown import (
save_to_markdown,
append_to_markdown,
read_from_markdown,
consolidate_book_md,
)


# Mocks comunes para todos los tests
@pytest.fixture
def mock_console():
with mock.patch("storycraftr.cli.console") as mock_console:
yield mock_console


# Test para save_to_markdown
@mock.patch("shutil.copyfile")
@mock.patch("os.path.exists", return_value=True)
@mock.patch("builtins.open", new_callable=mock.mock_open)
def test_save_to_markdown_backup(mock_open, mock_exists, mock_copy, mock_console):
book_path = "test_book"
file_name = "test.md"
header = "Test Header"
content = "Test content"

save_to_markdown(book_path, file_name, header, content)

# Verificar que se realizó una copia de seguridad
mock_copy.assert_called_with(
os.path.join(book_path, file_name), os.path.join(book_path, file_name) + ".back"
)
mock_open.assert_called_with(
os.path.join(book_path, file_name), "w", encoding="utf-8"
)
mock_open().write.assert_called_with(f"# {header}\n\n{content}")


@mock.patch("os.path.exists", return_value=False)
@mock.patch("builtins.open", new_callable=mock.mock_open)
def test_save_to_markdown_no_backup(mock_open, mock_exists, mock_console):
book_path = "test_book"
file_name = "test.md"
header = "Test Header"
content = "Test content"

save_to_markdown(book_path, file_name, header, content)

# Verificar que no se hizo copia de seguridad
mock_open.assert_called_with(
os.path.join(book_path, file_name), "w", encoding="utf-8"
)
mock_open().write.assert_called_with(f"# {header}\n\n{content}")


# Test para append_to_markdown
@mock.patch("os.path.exists", return_value=True)
@mock.patch("builtins.open", new_callable=mock.mock_open)
def test_append_to_markdown_success(mock_open, mock_exists, mock_console):
book_path = "test_book"
folder_name = "test_folder"
file_name = "test.md"
content = "Appended content"

append_to_markdown(book_path, folder_name, file_name, content)

mock_open.assert_called_with(
os.path.join(book_path, folder_name, file_name), "a", encoding="utf-8"
)
mock_open().write.assert_called_with(f"\n\n{content}")


@mock.patch("os.path.exists", return_value=False)
def test_append_to_markdown_file_not_found(mock_console):
book_path = "test_book"
folder_name = "test_folder"
file_name = "test.md"
content = "Appended content"

with pytest.raises(FileNotFoundError):
append_to_markdown(book_path, folder_name, file_name, content)


# Test para read_from_markdown
@mock.patch("os.path.exists", return_value=True)
@mock.patch("builtins.open", new_callable=mock.mock_open, read_data="File content")
def test_read_from_markdown_success(mock_open, mock_exists, mock_console):
book_path = "test_book"
folder_name = "test_folder"
file_name = "test.md"

content = read_from_markdown(book_path, folder_name, file_name)

mock_open.assert_called_with(
os.path.join(book_path, folder_name, file_name), "r", encoding="utf-8"
)
assert content == "File content"


@mock.patch("os.path.exists", return_value=False)
def test_read_from_markdown_file_not_found(mock_console):
book_path = "test_book"
folder_name = "test_folder"
file_name = "test.md"

with pytest.raises(FileNotFoundError):
read_from_markdown(book_path, folder_name, file_name)
Loading

0 comments on commit f6fa3fb

Please sign in to comment.