Skip to content

Commit

Permalink
with test & resructure dir
Browse files Browse the repository at this point in the history
  • Loading branch information
mergemaven11 committed May 18, 2024
1 parent 23d7401 commit 330da01
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 67 deletions.
Empty file added cli/__init__.py
Empty file.
45 changes: 45 additions & 0 deletions cli/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json
import uuid
import typing as t

import cli.utils as util # Import utility functions

def add_entry_to_file(entry_obj: t.Dict[str, t.Any], entries_file: str) -> None:
"""
Add a JSON object representing a journal entry to the entries file.
Args:
entry_obj (Dict[str, Any]): Dictionary representing the journal entry.
entries_file (str): Path to the entries file.
Returns:
None
"""
# Load existing entries from the file
with open(entries_file, 'r') as f:
entries_data: list = json.load(f)

# Generate a unique ID and timestamp for the new entry
entry_obj['id'] = str(uuid.uuid4())
entry_obj['timestamp'] = util.current_datetime()

# Append the new entry object to the existing list
entries_data.append(entry_obj)

# Write the updated entry data back to the file
with open(entries_file, 'w') as f:
json.dump(entries_data, f, indent=4) # indent for pretty printing

# Searching an entry

def search_entries(title: str):
""" Search Entires by title, dropdown selection for muliple finds.
View all vs view one
Args:
title (str): _description_
"""
pass

# viewing an entry
4 changes: 2 additions & 2 deletions journal.py → cli/journal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typer
import handlers as handler
import utils as util
import cli.utils as util

app = typer.Typer()

Expand All @@ -13,7 +13,7 @@ def add():
title = typer.prompt("Please enter a title")
entry = typer.prompt("Please enter your journal entry")
# Send data to add handler
handler.add_entry_to_file({"Title": title, "entry": entry})
handler.add_entry_to_file({"Title": title, "Entry": entry})


@app.command()
Expand Down
File renamed without changes.
64 changes: 0 additions & 64 deletions handlers.py

This file was deleted.

98 changes: 97 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
typer = "^0.12.3"
pytest = "^8.2.0"
freezegun = "^1.5.1"


[build-system]
Expand Down
Empty file added tests/__init__.py
Empty file.
66 changes: 66 additions & 0 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import json
import os
import pytest
from uuid import UUID
from datetime import datetime

import cli.handlers as handler

@pytest.fixture
def temp_entries_file(tmp_path):
"""
Fixture to create a temporary file for entries.
"""
entries_dir = tmp_path / "entries"
entries_dir.mkdir()

# Create 'entries.json' file with an empty list
entries_file = entries_dir / "entries.json"
with open(entries_file, 'w') as f:
json.dump([], f)

print("Temporary entries file created:", entries_file) # Add debug output
return entries_file



def test_add_entry_to_file(temp_entries_file):
"""Test add entry to file handler."""

entry_data = {
"title": "foo bar",
"content": "Lorem ipsum"
}

# Add the entry to the temporary entries file
handler.add_entry_to_file(entry_data, entries_file=str(temp_entries_file))

# Load the entries from the file
with open(temp_entries_file, 'r') as f:
entries = json.load(f)

# Check if one entry was added
assert len(entries) == 1

# Validate the entry fields
entry = entries[0]
assert "id" in entry
assert "title" in entry
assert "content" in entry
assert "timestamp" in entry

# Check if the ID is a valid UUID
try:
UUID(entry["id"], version=4)
except ValueError:
pytest.fail("Invalid UUID")

# Check the values
assert entry["title"] == "foo bar"
assert entry["content"] == "Lorem ipsum"

# Check if the timestamp is not none
try:
assert entry["timestamp"] is not None
except ValueError:
pytest.fail("Timestamp is empty")
49 changes: 49 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import json
import pytest

import cli.utils as util


@pytest.fixture
def temp_entries_env(tmp_path):
"""
Fixture to create a temporary directory for entries.
"""
original_dir = os.getcwd()
temp_dir = tmp_path / "entries_test"
temp_dir.mkdir(parents=True, exist_ok=True) # Create directory if it doesn't exist

# Change the current directory to the temp directory
os.chdir(temp_dir)

# Yield to the test
yield

# After the test, restore the original working directory
os.chdir(original_dir)

def test_check_entries_dir(temp_entries_env):
"""Test the check_entries_dir function."""

entries_dir = './entries'
entries_file = './entries/entries.json'

# Call the function to check and create the directory and file
result_file = util.check_entries_dir()

# Verify the directory was created
assert os.path.exists(entries_dir)
assert os.path.isdir(entries_dir)

# Verify the file was created
assert os.path.exists(entries_file)
assert os.path.isfile(entries_file)

# Verify the file content is an empty list
with open(entries_file, 'r') as f:
content = json.load(f)
assert content == []

# Verify the function returns the correct file path
assert result_file == entries_file

0 comments on commit 330da01

Please sign in to comment.