79 lines
3.1 KiB
Python
79 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
import os
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
import datetime
|
|
from zoneinfo import ZoneInfo
|
|
|
|
from cap_check import get_tunis_date, count_today, TUNIS_TZ
|
|
from main import update_journal_task, is_validation_approved, record_pending_validation
|
|
|
|
class TestJournalTunisAndValidation(unittest.TestCase):
|
|
def test_tunis_date_utc_midnight_crossover(self):
|
|
# 23h30 UTC le 1er novembre correspond à 00h30 le 2 novembre à Tunis (UTC+1)
|
|
utc_dt = "2026-11-01T23:30:00+00:00"
|
|
tunis_date = get_tunis_date(utc_dt)
|
|
self.assertEqual(tunis_date, "2026-11-02")
|
|
|
|
# 02h00 UTC le 2 novembre reste le 2 novembre à Tunis
|
|
utc_dt2 = "2026-11-02T02:00:00+00:00"
|
|
tunis_date2 = get_tunis_date(utc_dt2)
|
|
self.assertEqual(tunis_date2, "2026-11-02")
|
|
|
|
def test_count_today_tunis(self):
|
|
with tempfile.NamedTemporaryFile("w+", delete=False, suffix=".jsonl") as f:
|
|
tpath = f.name
|
|
now_tunis = datetime.datetime.now(TUNIS_TZ)
|
|
today_tunis = now_tunis.strftime("%Y-%m-%d")
|
|
|
|
# Entrée datée d'aujourd'hui en heure de Tunis
|
|
f.write(json.dumps({"date": f"{today_tunis}T10:00:00+01:00", "sphere": "nyora", "statut": "SUCCESS"}) + "\n")
|
|
# Entrée d'hier
|
|
f.write(json.dumps({"date": "2026-10-30T10:00:00+01:00", "sphere": "nyora", "statut": "SUCCESS"}) + "\n")
|
|
# Entrée d'une autre sphère
|
|
f.write(json.dumps({"date": f"{today_tunis}T11:00:00+01:00", "sphere": "perso", "statut": "SUCCESS"}) + "\n")
|
|
|
|
try:
|
|
cnt_nyora = count_today("nyora", tpath)
|
|
cnt_perso = count_today("perso", tpath)
|
|
self.assertEqual(cnt_nyora, 1)
|
|
self.assertEqual(cnt_perso, 1)
|
|
finally:
|
|
if os.path.exists(tpath):
|
|
os.remove(tpath)
|
|
|
|
def test_update_journal_credits_apres(self):
|
|
with tempfile.NamedTemporaryFile("w+", delete=False, suffix=".jsonl") as f:
|
|
tpath = f.name
|
|
f.write(json.dumps({
|
|
"date": "2026-11-02T10:00:00+01:00",
|
|
"sphere": "nyora",
|
|
"task_id": "task-test-123",
|
|
"statut": "PENDING",
|
|
"credits_avant": 9400,
|
|
"credits_apres": 9400
|
|
}) + "\n")
|
|
|
|
try:
|
|
# Remplacement temporaire du chemin de journal
|
|
import main
|
|
old_func = main.get_journal_path
|
|
main.get_journal_path = lambda: tpath
|
|
try:
|
|
update_journal_task("task-test-123", "SUCCESS", credits_apres=9300)
|
|
with open(tpath, "r", encoding="utf-8") as f_read:
|
|
data = json.loads(f_read.readline())
|
|
self.assertEqual(data["statut"], "SUCCESS")
|
|
self.assertEqual(data["credits_apres"], 9300)
|
|
self.assertEqual(data["credits_avant"], 9400)
|
|
finally:
|
|
main.get_journal_path = old_func
|
|
finally:
|
|
if os.path.exists(tpath):
|
|
os.remove(tpath)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|