diff --git a/.github/workflows/github.yml b/.github/workflows/github.yml new file mode 100644 index 00000000..99943e20 --- /dev/null +++ b/.github/workflows/github.yml @@ -0,0 +1,37 @@ +name: Trgoman API CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.11" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run lint + run: pip install flake8 && flake8 --ignore=E501 api.py + + - name: Run tests + run: pytest -v + + - name: Build Docker image + run: docker build -t translate-app . + + - name: Run Docker container + run: docker run --rm translate-app diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..7bbad788 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.11-slim + +WORKDIR /app + +# Install dependencies +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Copy project files +COPY . . + +# Default command: run tests +CMD ["pytest", "-v"] diff --git a/README.md b/README.md new file mode 100644 index 00000000..cb704552 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# 🐍 Python Translator with CI/CD 🚀 + +This project is a simple **Python translator client** for the [Targoman API](https://targoman.ir), fully containerized with Docker and tested automatically using **GitHub Actions (CI/CD)**. + +--- + +## 📂 Project Structure +- `api.py` → Main code for translation +- `test_translate.py` → Unit tests (with pytest & mock) +- `Dockerfile` → Container setup +- `.github/workflows/ci.yml` → CI/CD pipeline + +--- + +## ⚡ Features +- ✅ Translation (fa → en / en → fa) +- ✅ Unit tests with `pytest` +- ✅ Linting with `flake8` +- ✅ Dockerized environment +- ✅ GitHub Actions CI/CD + +--- + +## 🔧 Run Locally + +```bash +# Create venv +python -m venv .venv +source .venv/bin/activate + +# Install dependencies +pip install -r requirements.txt + +# Run tests +pytest -v diff --git a/__pycache__/api.cpython-312.pyc b/__pycache__/api.cpython-312.pyc new file mode 100644 index 00000000..16b40501 Binary files /dev/null and b/__pycache__/api.cpython-312.pyc differ diff --git a/__pycache__/test_translate.cpython-312-pytest-8.4.1.pyc b/__pycache__/test_translate.cpython-312-pytest-8.4.1.pyc new file mode 100644 index 00000000..538c312e Binary files /dev/null and b/__pycache__/test_translate.cpython-312-pytest-8.4.1.pyc differ diff --git a/api.py b/api.py new file mode 100644 index 00000000..34e441fe --- /dev/null +++ b/api.py @@ -0,0 +1,39 @@ +import requests as req +import json + +URL = "Https://targoman.ir/API" + + +def translate(word: str = "سلام", fromLang: str = "fa", toLang: str = "en") -> dict: + payload = { + "jsonrpc": "2.0", + "method": "Targoman::translate", + "id": 1, + "params": [ + "sSTargomanWUI", + word, + "%s2%s" % (fromLang.lower().strip(), toLang.lower().strip()), + "127.0.0.10", + "NMT", + True, + True, + True, + None, + "formal", + ], + } + + data = req.post(URL, json=payload) + data = json.loads(data.text) + return data + + +def Translate(word: str, fromLang: str, toLang: str) -> str: + if fromLang == "fa" and toLang == "en": + return translate(word, toLang=toLang, fromLang=fromLang)["result"]["tr"][ + "base" + ][0][1] + if fromLang == "en" and toLang == "fa": + return translate(word, toLang=toLang, fromLang=fromLang)["result"]["tr"][ + "base" + ][0][1] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..2001f3e2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +requests +pytest diff --git a/test_translate.py b/test_translate.py new file mode 100644 index 00000000..f223717c --- /dev/null +++ b/test_translate.py @@ -0,0 +1,27 @@ +from unittest.mock import patch +import json + +from api import Translate + + +# Mock response data +mock_response_fa_en = {"result": {"tr": {"base": [[0, "Hello"]]}}} + +mock_response_en_fa = {"result": {"tr": {"base": [[0, "سلام"]]}}} + + +@patch("requests.post") +def test_translate_fa_to_en(mock_post): + # Mock the requests.post response + mock_post.return_value.text = json.dumps(mock_response_fa_en) + + result = Translate("سلام", "fa", "en") + assert result == "Hello" + + +@patch("requests.post") +def test_translate_en_to_fa(mock_post): + mock_post.return_value.text = json.dumps(mock_response_en_fa) + + result = Translate("Hello", "en", "fa") + assert result == "سلام"