-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23d7401
commit 330da01
Showing
10 changed files
with
261 additions
and
67 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |