Compare commits
7 Commits
0271432b23
...
add-ci-tes
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
987ce19aec | ||
|
|
b5025bf7ec | ||
|
|
d3d861bd2d | ||
|
|
b100533e03 | ||
|
|
d0196fefd0 | ||
|
|
1a09921fcc | ||
|
|
99445a12e2 |
@@ -21,6 +21,8 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
|
- name: Run tests
|
||||||
|
run: python3 test_export.py
|
||||||
- name: Build
|
- name: Build
|
||||||
run: pyinstaller --onefile --name tamigo-cli tamigo.py
|
run: pyinstaller --onefile --name tamigo-cli tamigo.py
|
||||||
- name: Upload Linux Artifact
|
- name: Upload Linux Artifact
|
||||||
@@ -53,6 +55,7 @@ jobs:
|
|||||||
export TEMP=C:\\\\temp && \
|
export TEMP=C:\\\\temp && \
|
||||||
mkdir -p /src/build /src/dist_win && \
|
mkdir -p /src/build /src/dist_win && \
|
||||||
pip install -r requirements.txt && \
|
pip install -r requirements.txt && \
|
||||||
|
python3 test_export.py && \
|
||||||
pyinstaller --onefile --name tamigo-cli \
|
pyinstaller --onefile --name tamigo-cli \
|
||||||
--workpath /src/build \
|
--workpath /src/build \
|
||||||
--distpath /src/dist_win \
|
--distpath /src/dist_win \
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ jobs:
|
|||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
|
- name: Run tests
|
||||||
|
run: python3 test_export.py
|
||||||
- name: Build
|
- name: Build
|
||||||
run: pyinstaller --onefile --name tamigo-cli tamigo.py
|
run: pyinstaller --onefile --name tamigo-cli tamigo.py
|
||||||
- name: Rename for release
|
- name: Rename for release
|
||||||
@@ -51,6 +53,7 @@ jobs:
|
|||||||
export TEMP=C:\\\\temp && \
|
export TEMP=C:\\\\temp && \
|
||||||
mkdir -p /src/build /src/dist_win && \
|
mkdir -p /src/build /src/dist_win && \
|
||||||
pip install -r requirements.txt && \
|
pip install -r requirements.txt && \
|
||||||
|
python3 test_export.py && \
|
||||||
pyinstaller --onefile --name tamigo-cli \
|
pyinstaller --onefile --name tamigo-cli \
|
||||||
--workpath /src/build \
|
--workpath /src/build \
|
||||||
--distpath /src/dist_win \
|
--distpath /src/dist_win \
|
||||||
|
|||||||
10
.gitignore
vendored
10
.gitignore
vendored
@@ -35,3 +35,13 @@ env/
|
|||||||
# OS specific
|
# OS specific
|
||||||
.DS_Store
|
.DS_Store
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
|
|
||||||
|
# Local config and exports
|
||||||
|
config.json
|
||||||
|
tamigo_export_*.csv
|
||||||
|
tamigo_export_*.json
|
||||||
|
tamigo_export_*.xlsx
|
||||||
|
|
||||||
|
# PDFs and metadata
|
||||||
|
*.pdf
|
||||||
|
*.pdf:Zone.Identifier
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -3,3 +3,4 @@ questionary
|
|||||||
rich
|
rich
|
||||||
python-dotenv
|
python-dotenv
|
||||||
pyinstaller
|
pyinstaller
|
||||||
|
openpyxl
|
||||||
|
|||||||
123
tamigo.py
123
tamigo.py
@@ -20,10 +20,13 @@ def load_config():
|
|||||||
if os.path.exists(CONFIG_FILE):
|
if os.path.exists(CONFIG_FILE):
|
||||||
try:
|
try:
|
||||||
with open(CONFIG_FILE, "r") as f:
|
with open(CONFIG_FILE, "r") as f:
|
||||||
return json.load(f)
|
config = json.load(f)
|
||||||
|
if "show_zero_hours" not in config:
|
||||||
|
config["show_zero_hours"] = False
|
||||||
|
return config
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
return {"theme": "fallout"} # Default to fallout because it's cooler
|
return {"theme": "fallout", "show_zero_hours": False} # Default to fallout and hide zero hours
|
||||||
|
|
||||||
def save_config(config):
|
def save_config(config):
|
||||||
with open(CONFIG_FILE, "w") as f:
|
with open(CONFIG_FILE, "w") as f:
|
||||||
@@ -163,7 +166,7 @@ class TamigoClient:
|
|||||||
except: pass
|
except: pass
|
||||||
return all_shifts
|
return all_shifts
|
||||||
|
|
||||||
def calculate_checkins(client):
|
def select_date_range():
|
||||||
range_choice = questionary.select(
|
range_choice = questionary.select(
|
||||||
"SELECT TEMPORAL RANGE:" if current_theme_name == "fallout" else "Select period:",
|
"SELECT TEMPORAL RANGE:" if current_theme_name == "fallout" else "Select period:",
|
||||||
choices=["Last 365 days", "Last 30 days", "This Month", "This Year", "Custom range..."],
|
choices=["Last 365 days", "Last 30 days", "This Month", "This Year", "Custom range..."],
|
||||||
@@ -184,10 +187,14 @@ def calculate_checkins(client):
|
|||||||
if start_date_dt <= end_date_dt: break
|
if start_date_dt <= end_date_dt: break
|
||||||
console.print("[error]ERROR: INVALID CHRONOLOGY.[/error]")
|
console.print("[error]ERROR: INVALID CHRONOLOGY.[/error]")
|
||||||
except: console.print("[error]ERROR: INVALID DATA FORMAT.[/error]")
|
except: console.print("[error]ERROR: INVALID DATA FORMAT.[/error]")
|
||||||
|
return start_date_dt, end_date_dt
|
||||||
|
|
||||||
|
def get_worked_days(client, start_date_dt, end_date_dt):
|
||||||
with console.status("[highlight]INITIALIZING DATA RETRIEVAL...[/highlight]" if current_theme_name == "fallout" else "Fetching data..."):
|
with console.status("[highlight]INITIALIZING DATA RETRIEVAL...[/highlight]" if current_theme_name == "fallout" else "Fetching data..."):
|
||||||
data = client.get_employee_actual_shifts(start_date_dt, end_date_dt)
|
data = client.get_employee_actual_shifts(start_date_dt, end_date_dt)
|
||||||
|
|
||||||
|
show_zero = current_config.get("show_zero_hours", False)
|
||||||
|
|
||||||
if data:
|
if data:
|
||||||
work_days = {}
|
work_days = {}
|
||||||
for item in data:
|
for item in data:
|
||||||
@@ -198,10 +205,22 @@ def calculate_checkins(client):
|
|||||||
if hours == 0 and item.get("StartTime") and item.get("EndTime"):
|
if hours == 0 and item.get("StartTime") and item.get("EndTime"):
|
||||||
st, et = parse_tamigo_date(item.get("StartTime")), parse_tamigo_date(item.get("EndTime"))
|
st, et = parse_tamigo_date(item.get("StartTime")), parse_tamigo_date(item.get("EndTime"))
|
||||||
if st and et: hours = (et - st).total_seconds() / 3600.0
|
if st and et: hours = (et - st).total_seconds() / 3600.0
|
||||||
|
|
||||||
|
# Filter zero hours if requested
|
||||||
|
if hours == 0 and not show_zero:
|
||||||
|
continue
|
||||||
|
|
||||||
if (hours > 0 or item.get("CheckInTime") or item.get("ActualStartTime") or item.get("StartTime")) and not item.get("IsAbsent", False):
|
if (hours > 0 or item.get("CheckInTime") or item.get("ActualStartTime") or item.get("StartTime")) and not item.get("IsAbsent", False):
|
||||||
if date_str not in work_days: work_days[date_str] = {"hours": hours, "text": item.get("ActualShiftText") or item.get("ActivityName") or "WORKED"}
|
if date_str not in work_days: work_days[date_str] = {"hours": hours, "text": item.get("ActualShiftText") or item.get("ActivityName") or "WORKED"}
|
||||||
else: work_days[date_str]["hours"] += hours
|
else: work_days[date_str]["hours"] += hours
|
||||||
|
return work_days
|
||||||
|
return None
|
||||||
|
|
||||||
|
def calculate_checkins(client):
|
||||||
|
start_date_dt, end_date_dt = select_date_range()
|
||||||
|
work_days = get_worked_days(client, start_date_dt, end_date_dt)
|
||||||
|
|
||||||
|
if work_days is not None:
|
||||||
if not work_days:
|
if not work_days:
|
||||||
console.print("[warning]WARNING: NO LOG ENTRIES DETECTED.[/warning]")
|
console.print("[warning]WARNING: NO LOG ENTRIES DETECTED.[/warning]")
|
||||||
return
|
return
|
||||||
@@ -220,6 +239,67 @@ def calculate_checkins(client):
|
|||||||
console.print(Panel(summary_text, title="SUMMARY STATISTICS" if current_theme_name == "fallout" else "Work Statistics", border_style="highlight", expand=False))
|
console.print(Panel(summary_text, title="SUMMARY STATISTICS" if current_theme_name == "fallout" else "Work Statistics", border_style="highlight", expand=False))
|
||||||
else: console.print("[error]FATAL ERROR: DATA RETRIEVAL FAILURE.[/error]")
|
else: console.print("[error]FATAL ERROR: DATA RETRIEVAL FAILURE.[/error]")
|
||||||
|
|
||||||
|
def export_worked_hours(client):
|
||||||
|
start_date_dt, end_date_dt = select_date_range()
|
||||||
|
work_days = get_worked_days(client, start_date_dt, end_date_dt)
|
||||||
|
|
||||||
|
if work_days is not None:
|
||||||
|
if not work_days:
|
||||||
|
console.print("[warning]WARNING: NO LOG ENTRIES DETECTED.[/warning]")
|
||||||
|
return
|
||||||
|
|
||||||
|
format_choice = questionary.select(
|
||||||
|
"SELECT EXPORT FORMAT:" if current_theme_name == "fallout" else "Select export format:",
|
||||||
|
choices=["CSV", "JSON", "XLSX"],
|
||||||
|
style=theme_data["questionary"]
|
||||||
|
).ask()
|
||||||
|
|
||||||
|
default_filename = f"tamigo_export_{start_date_dt.strftime('%Y%m%d')}_{end_date_dt.strftime('%Y%m%d')}"
|
||||||
|
filename = questionary.text(
|
||||||
|
"ENTER FILENAME:" if current_theme_name == "fallout" else "Enter filename:",
|
||||||
|
default=f"{default_filename}.{format_choice.lower()}",
|
||||||
|
style=theme_data["questionary"]
|
||||||
|
).ask()
|
||||||
|
|
||||||
|
if not filename: return
|
||||||
|
|
||||||
|
try:
|
||||||
|
if format_choice == "CSV":
|
||||||
|
import csv
|
||||||
|
with open(filename, 'w', newline='') as f:
|
||||||
|
writer = csv.writer(f)
|
||||||
|
writer.writerow(["Date", "Hours", "Details"])
|
||||||
|
for day in sorted(work_days.keys()):
|
||||||
|
info = work_days[day]
|
||||||
|
writer.writerow([day, f"{info['hours']:.2f}", info['text']])
|
||||||
|
elif format_choice == "XLSX":
|
||||||
|
from openpyxl import Workbook
|
||||||
|
from openpyxl.styles import Font
|
||||||
|
wb = Workbook()
|
||||||
|
ws = wb.active
|
||||||
|
ws.title = "Worked Hours"
|
||||||
|
|
||||||
|
# Header
|
||||||
|
headers = ["Date", "Hours", "Details"]
|
||||||
|
ws.append(headers)
|
||||||
|
for cell in ws[1]:
|
||||||
|
cell.font = Font(bold=True)
|
||||||
|
|
||||||
|
# Data
|
||||||
|
for day in sorted(work_days.keys()):
|
||||||
|
info = work_days[day]
|
||||||
|
ws.append([day, info['hours'], info['text']])
|
||||||
|
|
||||||
|
wb.save(filename)
|
||||||
|
else:
|
||||||
|
with open(filename, 'w') as f:
|
||||||
|
json.dump(work_days, f, indent=4)
|
||||||
|
|
||||||
|
console.print(f"[success]DATA EXPORTED SUCCESSFULLY TO: {filename}[/success]")
|
||||||
|
except Exception as e:
|
||||||
|
console.print(f"[error]ERROR SAVING FILE: {str(e)}[/error]")
|
||||||
|
else: console.print("[error]FATAL ERROR: DATA RETRIEVAL FAILURE.[/error]")
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print_header()
|
print_header()
|
||||||
client = TamigoClient()
|
client = TamigoClient()
|
||||||
@@ -235,23 +315,48 @@ def main():
|
|||||||
|
|
||||||
def menu(client):
|
def menu(client):
|
||||||
while True:
|
while True:
|
||||||
|
settings_text = "Settings"
|
||||||
choice = questionary.select(
|
choice = questionary.select(
|
||||||
"SELECT ACTION:" if current_theme_name == "fallout" else "What would you like to do?",
|
"SELECT ACTION:" if current_theme_name == "fallout" else "What would you like to do?",
|
||||||
choices=["Calculate actual work days", "Show profile info", "Switch UI Theme", "Logout and Exit"],
|
choices=["Calculate actual work days", "Export hours worked", "Show profile info", settings_text, "Logout and Exit"],
|
||||||
style=theme_data["questionary"]
|
style=theme_data["questionary"]
|
||||||
).ask()
|
).ask()
|
||||||
|
|
||||||
if choice == "Calculate actual work days": calculate_checkins(client)
|
if choice == "Calculate actual work days": calculate_checkins(client)
|
||||||
|
elif choice == "Export hours worked": export_worked_hours(client)
|
||||||
elif choice == "Show profile info":
|
elif choice == "Show profile info":
|
||||||
if client.user_info: console.print_json(data=client.user_info)
|
if client.user_info: console.print_json(data=client.user_info)
|
||||||
else: console.print("[error]ERROR: NO PROFILE INFO.[/error]")
|
else: console.print("[error]ERROR: NO PROFILE INFO.[/error]")
|
||||||
elif choice == "Switch UI Theme":
|
elif choice == settings_text:
|
||||||
new_theme = "normal" if current_theme_name == "fallout" else "fallout"
|
settings_menu()
|
||||||
apply_theme(new_theme)
|
|
||||||
console.print(f"[success]UI THEME SET TO: {new_theme.upper()}[/success]")
|
|
||||||
else:
|
else:
|
||||||
console.print("[highlight]SYSTEM SHUTDOWN...[/highlight]" if current_theme_name == "fallout" else "Exiting...")
|
console.print("[highlight]SYSTEM SHUTDOWN...[/highlight]" if current_theme_name == "fallout" else "Exiting...")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def settings_menu():
|
||||||
|
while True:
|
||||||
|
show_zero = current_config.get("show_zero_hours", False)
|
||||||
|
toggle_text = "Hide zero-hour days" if show_zero else "Show zero-hour days"
|
||||||
|
|
||||||
|
back_text = "Back"
|
||||||
|
|
||||||
|
choice = questionary.select(
|
||||||
|
"SELECT SETTING:" if current_theme_name == "fallout" else "Settings:",
|
||||||
|
choices=["Switch UI Theme", toggle_text, back_text],
|
||||||
|
style=theme_data["questionary"]
|
||||||
|
).ask()
|
||||||
|
|
||||||
|
if choice == "Switch UI Theme":
|
||||||
|
new_theme = "normal" if current_theme_name == "fallout" else "fallout"
|
||||||
|
apply_theme(new_theme)
|
||||||
|
console.print(f"[success]UI THEME SET TO: {new_theme.upper()}[/success]")
|
||||||
|
elif choice == toggle_text:
|
||||||
|
current_config["show_zero_hours"] = not show_zero
|
||||||
|
save_config(current_config)
|
||||||
|
state = "VISIBLE" if not show_zero else "HIDDEN"
|
||||||
|
console.print(f"[success]ZERO-HOUR DAYS ARE NOW {state}.[/success]" if current_theme_name == "fallout" else f"[success]Zero-hour days are now {state.lower()}.[/success]")
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
201
test_export.py
Normal file
201
test_export.py
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
import unittest
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import csv
|
||||||
|
from tamigo import get_worked_days, TamigoClient
|
||||||
|
|
||||||
|
class TestTamigoExport(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.client = MagicMock(spec=TamigoClient)
|
||||||
|
self.client.session_token = "fake-token"
|
||||||
|
|
||||||
|
def test_get_worked_days_processing(self):
|
||||||
|
# Mock API response
|
||||||
|
mock_shifts = [
|
||||||
|
{
|
||||||
|
"Date": "/Date(1741651200000)/", # 2025-03-11
|
||||||
|
"ActualShiftHours": 8.5,
|
||||||
|
"ActualShiftText": "Work",
|
||||||
|
"IsAbsent": False
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"StartTime": "2025-03-10T09:00:00Z",
|
||||||
|
"EndTime": "2025-03-10T17:00:00Z",
|
||||||
|
"ActualShiftHours": 0,
|
||||||
|
"IsAbsent": False
|
||||||
|
}
|
||||||
|
]
|
||||||
|
self.client.get_employee_actual_shifts.return_value = mock_shifts
|
||||||
|
|
||||||
|
start_date = datetime(2025, 3, 1)
|
||||||
|
end_date = datetime(2025, 3, 15)
|
||||||
|
|
||||||
|
with patch('tamigo.console'):
|
||||||
|
work_days = get_worked_days(self.client, start_date, end_date)
|
||||||
|
|
||||||
|
self.assertIsNotNone(work_days)
|
||||||
|
self.assertIn("2025-03-11", work_days)
|
||||||
|
self.assertEqual(work_days["2025-03-11"]["hours"], 8.5)
|
||||||
|
self.assertIn("2025-03-10", work_days)
|
||||||
|
self.assertEqual(work_days["2025-03-10"]["hours"], 8.0)
|
||||||
|
|
||||||
|
def test_get_worked_days_zero_hour_filtering(self):
|
||||||
|
# Mock API response with a 0-hour shift (missing EndTime so it stays 0)
|
||||||
|
mock_shifts = [
|
||||||
|
{
|
||||||
|
"Date": "/Date(1741564800000)/", # 2025-03-10
|
||||||
|
"StartTime": "2025-03-10T09:00:00Z",
|
||||||
|
"ActualShiftHours": 0,
|
||||||
|
"ActualShiftText": "Sick Day",
|
||||||
|
"IsAbsent": False
|
||||||
|
}
|
||||||
|
]
|
||||||
|
self.client.get_employee_actual_shifts.return_value = mock_shifts
|
||||||
|
start_date = datetime(2025, 3, 1)
|
||||||
|
end_date = datetime(2025, 3, 15)
|
||||||
|
|
||||||
|
# 1. Test with show_zero_hours = False (Default)
|
||||||
|
with patch('tamigo.console'), patch('tamigo.current_config', {'show_zero_hours': False}):
|
||||||
|
work_days = get_worked_days(self.client, start_date, end_date)
|
||||||
|
self.assertEqual(len(work_days), 0)
|
||||||
|
|
||||||
|
# 2. Test with show_zero_hours = True
|
||||||
|
with patch('tamigo.console'), patch('tamigo.current_config', {'show_zero_hours': True}):
|
||||||
|
work_days = get_worked_days(self.client, start_date, end_date)
|
||||||
|
self.assertEqual(len(work_days), 1)
|
||||||
|
self.assertIn("2025-03-10", work_days)
|
||||||
|
self.assertEqual(work_days["2025-03-10"]["hours"], 0)
|
||||||
|
|
||||||
|
@patch('tamigo.select_date_range')
|
||||||
|
@patch('questionary.select')
|
||||||
|
@patch('questionary.text')
|
||||||
|
@patch('tamigo.console')
|
||||||
|
def test_export_worked_hours_respects_zero_hour_filtering(self, mock_console, mock_text, mock_select, mock_date_range):
|
||||||
|
from tamigo import export_worked_hours
|
||||||
|
|
||||||
|
# Mock API response with a 0-hour shift
|
||||||
|
mock_shifts = [
|
||||||
|
{
|
||||||
|
"Date": "/Date(1741564800000)/", # 2025-03-10
|
||||||
|
"StartTime": "2025-03-10T09:00:00Z",
|
||||||
|
"ActualShiftHours": 0,
|
||||||
|
"ActualShiftText": "Sick Day",
|
||||||
|
"IsAbsent": False
|
||||||
|
}
|
||||||
|
]
|
||||||
|
self.client.get_employee_actual_shifts.return_value = mock_shifts
|
||||||
|
mock_date_range.return_value = (datetime(2025, 3, 1), datetime(2025, 3, 15))
|
||||||
|
|
||||||
|
# 1. Test with show_zero_hours = False -> Should NOT export anything
|
||||||
|
with patch('tamigo.current_config', {'show_zero_hours': False}):
|
||||||
|
export_worked_hours(self.client)
|
||||||
|
# Verify the warning was printed
|
||||||
|
mock_console.print.assert_any_call("[warning]WARNING: NO LOG ENTRIES DETECTED.[/warning]")
|
||||||
|
self.assertFalse(os.path.exists("tamigo_export_20250301_20250315.csv"))
|
||||||
|
|
||||||
|
# 2. Test with show_zero_hours = True -> Should export the 0-hour day
|
||||||
|
mock_select.return_value.ask.return_value = "CSV"
|
||||||
|
mock_text.return_value.ask.return_value = "test_zero_export.csv"
|
||||||
|
|
||||||
|
with patch('tamigo.current_config', {'show_zero_hours': True}):
|
||||||
|
export_worked_hours(self.client)
|
||||||
|
|
||||||
|
self.assertTrue(os.path.exists("test_zero_export.csv"))
|
||||||
|
with open("test_zero_export.csv", "r") as f:
|
||||||
|
rows = list(csv.reader(f))
|
||||||
|
self.assertEqual(len(rows), 2) # Header + 1 data row
|
||||||
|
self.assertEqual(rows[1][0], "2025-03-10")
|
||||||
|
self.assertEqual(rows[1][1], "0.00")
|
||||||
|
|
||||||
|
os.remove("test_zero_export.csv")
|
||||||
|
|
||||||
|
@patch('tamigo.select_date_range')
|
||||||
|
@patch('tamigo.get_worked_days')
|
||||||
|
@patch('questionary.select')
|
||||||
|
@patch('questionary.text')
|
||||||
|
@patch('tamigo.console')
|
||||||
|
def test_export_worked_hours_csv(self, mock_console, mock_text, mock_select, mock_get_worked, mock_date_range):
|
||||||
|
from tamigo import export_worked_hours
|
||||||
|
|
||||||
|
mock_date_range.return_value = (datetime(2025, 3, 1), datetime(2025, 3, 15))
|
||||||
|
mock_get_worked.return_value = {
|
||||||
|
"2025-03-11": {"hours": 8.5, "text": "Work"},
|
||||||
|
"2025-03-10": {"hours": 8.0, "text": "Work"}
|
||||||
|
}
|
||||||
|
|
||||||
|
mock_select.return_value.ask.return_value = "CSV"
|
||||||
|
mock_text.return_value.ask.return_value = "test_export.csv"
|
||||||
|
|
||||||
|
export_worked_hours(self.client)
|
||||||
|
|
||||||
|
self.assertTrue(os.path.exists("test_export.csv"))
|
||||||
|
with open("test_export.csv", "r") as f:
|
||||||
|
reader = csv.reader(f)
|
||||||
|
rows = list(reader)
|
||||||
|
self.assertEqual(rows[0], ["Date", "Hours", "Details"])
|
||||||
|
# CSV rows might be in different order if dict keys are not sorted, but sorted() was used in code
|
||||||
|
self.assertEqual(rows[1], ["2025-03-10", "8.00", "Work"])
|
||||||
|
self.assertEqual(rows[2], ["2025-03-11", "8.50", "Work"])
|
||||||
|
|
||||||
|
os.remove("test_export.csv")
|
||||||
|
|
||||||
|
@patch('tamigo.select_date_range')
|
||||||
|
@patch('tamigo.get_worked_days')
|
||||||
|
@patch('questionary.select')
|
||||||
|
@patch('questionary.text')
|
||||||
|
@patch('tamigo.console')
|
||||||
|
def test_export_worked_hours_json(self, mock_console, mock_text, mock_select, mock_get_worked, mock_date_range):
|
||||||
|
from tamigo import export_worked_hours
|
||||||
|
|
||||||
|
mock_date_range.return_value = (datetime(2025, 3, 1), datetime(2025, 3, 15))
|
||||||
|
mock_get_worked.return_value = {
|
||||||
|
"2025-03-11": {"hours": 8.5, "text": "Work"},
|
||||||
|
"2025-03-10": {"hours": 8.0, "text": "Work"}
|
||||||
|
}
|
||||||
|
|
||||||
|
mock_select.return_value.ask.return_value = "JSON"
|
||||||
|
mock_text.return_value.ask.return_value = "test_export.json"
|
||||||
|
|
||||||
|
export_worked_hours(self.client)
|
||||||
|
|
||||||
|
self.assertTrue(os.path.exists("test_export.json"))
|
||||||
|
with open("test_export.json", "r") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
self.assertEqual(data["2025-03-11"]["hours"], 8.5)
|
||||||
|
|
||||||
|
os.remove("test_export.json")
|
||||||
|
|
||||||
|
@patch('tamigo.select_date_range')
|
||||||
|
@patch('tamigo.get_worked_days')
|
||||||
|
@patch('questionary.select')
|
||||||
|
@patch('questionary.text')
|
||||||
|
@patch('tamigo.console')
|
||||||
|
def test_export_worked_hours_xlsx(self, mock_console, mock_text, mock_select, mock_get_worked, mock_date_range):
|
||||||
|
from tamigo import export_worked_hours
|
||||||
|
|
||||||
|
mock_date_range.return_value = (datetime(2025, 3, 1), datetime(2025, 3, 15))
|
||||||
|
mock_get_worked.return_value = {
|
||||||
|
"2025-03-11": {"hours": 8.5, "text": "Work"},
|
||||||
|
"2025-03-10": {"hours": 8.0, "text": "Work"}
|
||||||
|
}
|
||||||
|
|
||||||
|
mock_select.return_value.ask.return_value = "XLSX"
|
||||||
|
mock_text.return_value.ask.return_value = "test_export.xlsx"
|
||||||
|
|
||||||
|
export_worked_hours(self.client)
|
||||||
|
|
||||||
|
self.assertTrue(os.path.exists("test_export.xlsx"))
|
||||||
|
# We could use openpyxl to verify content if we really wanted to
|
||||||
|
import openpyxl
|
||||||
|
wb = openpyxl.load_workbook("test_export.xlsx")
|
||||||
|
ws = wb.active
|
||||||
|
self.assertEqual(ws.cell(row=1, column=1).value, "Date")
|
||||||
|
self.assertEqual(ws.cell(row=2, column=1).value, "2025-03-10")
|
||||||
|
self.assertEqual(ws.cell(row=2, column=2).value, 8.0)
|
||||||
|
|
||||||
|
os.remove("test_export.xlsx")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user