Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions backend/tests/unit/test_translation_cost_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,76 @@

import asyncio
import hashlib
import importlib.util
import os
import sys
import time
from collections import OrderedDict
from types import ModuleType
from unittest.mock import MagicMock, patch, AsyncMock

import pytest

_BACKEND_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))


def _restore_package_path(name: str, path: str):
module = sys.modules.get(name)
if module is not None:
module.__path__ = [path]


class _DetectedLang:
def __init__(self, lang: str, prob: float):
self.lang = lang
self.prob = prob


def _guess_language(text: str) -> str:
lower = (text or '').lower()
if any(marker in lower for marker in ('bonjour', 'merci', 'revoir', 'allez', 'suis', 'content')):
return 'fr'
if any(marker in lower for marker in ('hola', 'gracias', 'adios', 'adi贸s')):
return 'es'
if 'danke' in lower:
return 'de'
return 'en'


def _install_langdetect_stub_if_missing():
if 'langdetect' not in sys.modules and importlib.util.find_spec('langdetect') is not None:
return

langdetect_mod = sys.modules.get('langdetect') or ModuleType('langdetect')
exception_mod = sys.modules.get('langdetect.lang_detect_exception') or ModuleType(
'langdetect.lang_detect_exception'
)

class LangDetectException(Exception):
pass

class DetectorFactory:
seed = None

def detect(text):
return _guess_language(text)

def detect_langs(text):
return [_DetectedLang(_guess_language(text), 0.99)]

exception_mod.LangDetectException = LangDetectException
langdetect_mod.detect = detect
langdetect_mod.detect_langs = detect_langs
langdetect_mod.DetectorFactory = DetectorFactory
langdetect_mod.lang_detect_exception = exception_mod
sys.modules['langdetect'] = langdetect_mod
sys.modules['langdetect.lang_detect_exception'] = exception_mod


_restore_package_path('utils', os.path.join(_BACKEND_DIR, 'utils'))
_restore_package_path('models', os.path.join(_BACKEND_DIR, 'models'))
_install_langdetect_stub_if_missing()

# --- Module-level mocks for heavy dependencies (must happen before any project imports) ---
_mock_redis = MagicMock()
_mock_redis.get.return_value = None
Expand Down
63 changes: 63 additions & 0 deletions backend/tests/unit/test_translation_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import sys
import json
import hashlib
import importlib.util
from types import ModuleType
from unittest.mock import MagicMock, patch, PropertyMock

os.environ.setdefault(
Expand Down Expand Up @@ -63,6 +65,67 @@ def _ensure_mock_module(name: str):
return sys.modules[name]


_BACKEND_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))


def _restore_package_path(name: str, path: str):
module = sys.modules.get(name)
if module is not None:
module.__path__ = [path]


class _DetectedLang:
def __init__(self, lang: str, prob: float):
self.lang = lang
self.prob = prob


def _guess_language(text: str) -> str:
lower = (text or '').lower()
if any(marker in lower for marker in ('bonjour', 'merci', 'revoir', 'allez', 'suis', 'content')):
return 'fr'
if any(marker in lower for marker in ('hola', 'gracias', 'adios', 'adi贸s')):
return 'es'
if 'danke' in lower:
return 'de'
return 'en'


def _install_langdetect_stub_if_missing():
if 'langdetect' not in sys.modules and importlib.util.find_spec('langdetect') is not None:
return

langdetect_mod = sys.modules.get('langdetect') or ModuleType('langdetect')
exception_mod = sys.modules.get('langdetect.lang_detect_exception') or ModuleType(
'langdetect.lang_detect_exception'
)

class LangDetectException(Exception):
pass

class DetectorFactory:
seed = None

def detect(text):
return _guess_language(text)

def detect_langs(text):
return [_DetectedLang(_guess_language(text), 0.99)]

exception_mod.LangDetectException = LangDetectException
langdetect_mod.detect = detect
langdetect_mod.detect_langs = detect_langs
langdetect_mod.DetectorFactory = DetectorFactory
langdetect_mod.lang_detect_exception = exception_mod
sys.modules['langdetect'] = langdetect_mod
sys.modules['langdetect.lang_detect_exception'] = exception_mod


_restore_package_path('utils', os.path.join(_BACKEND_DIR, 'utils'))
_restore_package_path('models', os.path.join(_BACKEND_DIR, 'models'))
_install_langdetect_stub_if_missing()


# Stub database module and redis
_ensure_mock_module("database")
sys.modules["database"].__path__ = getattr(sys.modules["database"], '__path__', [])
Expand Down
Loading