Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Tests split into distinct jobs.
Browse files Browse the repository at this point in the history
Removed user-specific tests not immediately relevant to the library or cli.
  • Loading branch information
why-not-try-calmer committed Feb 23, 2023
1 parent 4028422 commit feca8c0
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 74 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: test pytransifex

concurrency: testing_environment

on:
pull_request:
branches:
- master
push:
branches:
- master
workflow_call:

jobs:
tests:
runs-on: ubuntu-latest
steps:

# Not using strategy.matrix to create multiple jobs
# as we do NOT want to test with any form of concurrency
# to avoid 'race conditions' against Transifex

- name: Check out repository code
uses: actions/checkout@v3

- uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "pip"
cache-dependency-path: "requirements/*.txt"

- name: Install project requirements
run: |
python -m pip install -U -r requirements/base.txt
python -m pip install -U -r requirements/dev.txt
- name: Test API
env:
organization: ${{ secrets.ORGANIZATION }}
tx_token: ${{ secrets.TX_TOKEN }}
run: |
TX_TOKEN=$tx_token ORGANIZATION=$organization \
python -m unittest ./tests/test_api.py
- name: Test CLI
env:
organization: ${{ secrets.ORGANIZATION }}
tx_token: ${{ secrets.TX_TOKEN }}
run: |
TX_TOKEN=$tx_token ORGANIZATION=$organization \
python -m unittest ./tests/test_cli.py
- name: Test with a public project
env:
organization: ${{ secrets.ORGANIZATION }}
tx_token: ${{ secrets.TX_TOKEN }}
run: |
TX_TOKEN=$tx_token ORGANIZATION=$organization \
python -m unittest ./tests/test_public_project.py
- name: Test with Qgisplugin's 'test_translation'
env:
organization: ${{ secrets.ORGANIZATION }}
tx_token: ${{ secrets.TX_TOKEN }}
run: |
TX_TOKEN=$tx_token ORGANIZATION=$organization \
python -m unittest ./tests/test_translation.py
38 changes: 0 additions & 38 deletions .github/workflows/test_pytx.yml

This file was deleted.

7 changes: 7 additions & 0 deletions pytransifex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,19 @@ def delete_project(self, project_slug: str):
def get_project(self, project_slug: str) -> None | Resource:
"""Fetches the project matching the given slug"""
if self.projects:
logger.info(
f"Attempting to get 'o:{self.organization_name}:p:{project_slug}'"
)
try:
res = self.projects.get(slug=project_slug)
logger.info("Got the project!")
return res
except DoesNotExist:
return None
"""
except MultipleObjectsReturned:
pass
"""

@ensure_login
def list_resources(self, project_slug: str) -> list[Any]:
Expand Down
22 changes: 12 additions & 10 deletions tests/_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(
lang = self.parameters.translation_source_language
self.ts_file = f"{plugin_path}/i18n/{tx}_{lang}.ts"

if self.tx_client.project_exists(parameters.transifex_project):
if self.tx_client.project_exists(parameters.project_slug):
logger.debug(
f"Project {self.parameters.transifex_organization}/"
f"{self.parameters.transifex_project} exists on Transifex"
Expand All @@ -79,7 +79,7 @@ def __init__(
f"{self.parameters.transifex_project}"
)
self.tx_client.create_project(
project_slug=self.parameters.transifex_project,
project_slug=self.parameters.project_slug,
private=False,
repository_url=self.parameters.repository_url,
source_language_code=parameters.translation_source_language,
Expand All @@ -92,7 +92,7 @@ def __init__(
f"{self.parameters.transifex_resource} with {self.ts_file}"
)
self.tx_client.create_resource(
project_slug=self.parameters.transifex_project,
project_slug=self.parameters.project_slug,
path_to_file=self.ts_file,
resource_slug=self.parameters.transifex_resource,
)
Expand Down Expand Up @@ -177,7 +177,7 @@ def pull(self):
"""
resource = self.__get_resource()
existing_langs = self.tx_client.list_languages(
project_slug=self.parameters.transifex_project
project_slug=self.parameters.project_slug
)
lang = self.parameters.translation_source_language
if lang in existing_langs:
Expand All @@ -190,7 +190,7 @@ def pull(self):
if lang not in existing_langs:
logger.debug(f"Creating missing language: {lang}")
self.tx_client.create_language(
project_slug=self.parameters.transifex_project,
project_slug=self.parameters.project_slug,
language_code=lang,
coordinators=[self.parameters.transifex_coordinator],
)
Expand All @@ -199,7 +199,7 @@ def pull(self):
ts_file = f"{self.parameters.plugin_path}/i18n/{self.parameters.transifex_resource}_{lang}.ts"
logger.debug(f"Downloading translation file: {ts_file}")
self.tx_client.get_translation(
project_slug=self.parameters.transifex_project,
project_slug=self.parameters.project_slug,
resource_slug=resource.slug,
language_code=lang,
path_to_output_file=ts_file,
Expand All @@ -212,17 +212,19 @@ def push(self):
f"with file {self.ts_file}"
)
result = self.tx_client.update_source_translation(
project_slug=self.parameters.transifex_project,
resource_slug=resource["slug"],
project_slug=self.parameters.project_slug,
resource_slug=resource.slug,
path_to_file=self.ts_file,
)
logger.info(f"Translation resource updated: {result}")

def __get_resource(self) -> dict:
resources = self.tx_client.list_resources(self.parameters.transifex_project)
resources = self.tx_client.list_resources(
project_slug=self.parameters.project_slug
)
if len(resources) == 0:
logger.error(
f"Project '{self.parameters.transifex_project}' has no resource on Transifex"
f"Project '{self.parameters.project_slug}' has no resource on Transifex"
)
sys.exit(1)
if len(resources) > 1:
Expand Down
37 changes: 11 additions & 26 deletions tests/test_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import yaml

from pytransifex.exceptions import PyTransifexException
from tests._translation import Parameters, Translation

logger = logging.getLogger(__name__)
Expand All @@ -15,42 +14,28 @@ class TestTranslation(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Initialize the test case"""
transifex_token = os.getenv("TX_TOKEN")
cls.transifex_token = transifex_token
assert transifex_token

config_yaml = Path.cwd().joinpath(
"tests", "data", ".qgis-plugin-ci-test-changelog.yaml"
)
print(config_yaml)
with open(config_yaml) as f:
arg_dict = yaml.safe_load(f)
transifex_token = os.getenv("TX_TOKEN")
assert transifex_token
cls.transifex_token = transifex_token

cls.parameters = Parameters(**arg_dict)
cls.t = Translation(cls.parameters, transifex_token=transifex_token)
logger.info(f"Set up classed with {cls.parameters}")

def tearDown(self):
try:
self.t.tx_client.delete_project(self.parameters.project_slug)
except PyTransifexException as error:
logger.debug(error)
"""
try:
self.t.tx_client.delete_team(f"{self.parameters.project_slug}-team")
except PyTransifexException as error:
logger.debug(error)
"""
@classmethod
def tearDownClass(cls):
assert cls.parameters.project_slug
cls.t.tx_client.delete_project(cls.parameters.project_slug)

def test1_creation(self):
self.tearDown()
self.t = Translation(self.parameters, transifex_token=self.transifex_token) # type: ignore

def test2_push(self):
self.t.update_strings()
self.t.push()

def test3_pull(self):
self.t.pull()
self.t.compile_strings()
assert self.transifex_token
self.t = Translation(self.parameters, transifex_token=self.transifex_token)


if __name__ == "__main__":
Expand Down

0 comments on commit feca8c0

Please sign in to comment.