From f92e727c2f21b8bfae671f1b7f89b91c60c0ccb6 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Tue, 28 Feb 2023 14:45:14 +0100 Subject: [PATCH] small fixes --- .github/workflows/test.yml | 34 ++++-------------------------- .gitignore | 2 ++ nose2.cfg | 6 ++++++ pytransifex/api.py | 28 +++++++++--------------- pytransifex/config.py | 10 ++++----- tests/data/test_config.toml | 3 +-- tests/data/test_config_public.toml | 3 +-- tests/test_api.py | 14 ++++-------- tests/test_cli.py | 2 +- 9 files changed, 34 insertions(+), 68 deletions(-) create mode 100644 nose2.cfg diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 37b84e0..59b2e25 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,34 +35,8 @@ jobs: python -m pip install -U -r requirements/base.txt python -m pip install -U -r requirements/dev.txt - - name: Test API + - name: Tests 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 + TX_ORGANIZATION: ${{ secrets.ORGANIZATION }} + TX_TOKEN: ${{ secrets.TX_TOKEN }} + run: nose2 -v diff --git a/.gitignore b/.gitignore index a0eb5e8..0ca33cc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ docs/_build/ docs/_autosummary/ +.tokens + __pycache__/ venv/ diff --git a/nose2.cfg b/nose2.cfg new file mode 100644 index 0000000..f9218c1 --- /dev/null +++ b/nose2.cfg @@ -0,0 +1,6 @@ +[unittest] +start-dir = tests +code-directories = .. + +[log-capture] +always-on = True diff --git a/pytransifex/api.py b/pytransifex/api.py index 5f8f232..e6e950b 100644 --- a/pytransifex/api.py +++ b/pytransifex/api.py @@ -42,15 +42,13 @@ def login(self): self.logged_in = True # Saving organization and projects to avoid round-trips - organization = tx_api.Organization.get(slug=self.organization_name) - self.projects = organization.fetch("projects") - self.organization = organization + self.organization = tx_api.Organization.get(slug=self.organization_name) + self.projects = self.organization.fetch("projects") logger.info(f"Logged in as organization: {self.organization_name}") @ensure_login def create_project( self, - *, project_slug: str, project_name: str | None = None, source_language_code: str = "en", @@ -370,18 +368,12 @@ class Transifex: def __new__(cls, *, defer_login: bool = False, **kwargs) -> Optional["Client"]: if not cls.client: - try: - if kwargs: - config = ApiConfig(**kwargs) - else: - logger.info( - f"As you called 'Transifex' without argument, we'll try defining your project from environment variables." - ) - config = ApiConfig.from_env() - - return Client(config, defer_login) + if kwargs: + config = ApiConfig(**kwargs) + else: + logger.info( + f"As you called 'Transifex' without argument, we'll try defining your project from environment variables." + ) + config = ApiConfig.from_env() - except ValueError as error: - available = list(ApiConfig._fields) - msg = f"Unable to define a proper config. API initialization uses the following fields, with only 'project_slug' optional: {available}" - logger.error(f"{msg}:\n{error}") + return Client(config, defer_login) diff --git a/pytransifex/config.py b/pytransifex/config.py index 18131ec..7a808c2 100644 --- a/pytransifex/config.py +++ b/pytransifex/config.py @@ -10,7 +10,7 @@ class ApiConfig(NamedTuple): api_token: str organization_name: str - i18n_type: str + i18n_type: str = "PO" host_name = "https://rest.api.transifex.com" project_slug: str | None = None @@ -19,21 +19,21 @@ def from_env(cls) -> "ApiConfig": load_dotenv() token = environ.get("TX_TOKEN") - organization = environ.get("ORGANIZATION") + organization = environ.get("TX_ORGANIZATION") i18n_type = environ.get("I18N_TYPE", "PO") if faulty := next( filter( lambda v: not v[1], zip( - ["token", "organization", "i18_ntype"], - [token, organization, i18n_type], + ["token", "organization"], + [token, organization], ), ), None, ): raise ValueError( - f"Envars 'TX_TOKEN', 'ORGANIZATION' and 'I18N_TYPE must be set to non-empty values, yet this one was found missing ('None' or empty string): {faulty[0]}" + f"Envars 'TX_TOKEN' and 'TX_ORGANIZATION', yet this one was found missing ('None' or empty string): {faulty[0]}" ) return cls(token, organization, i18n_type) # type: ignore diff --git a/tests/data/test_config.toml b/tests/data/test_config.toml index 56f6796..7929a75 100644 --- a/tests/data/test_config.toml +++ b/tests/data/test_config.toml @@ -1,5 +1,4 @@ -organization_slug = "test_pytransifex" -project_slug = "test_project_pytransifex" +project_slug = "pytransifex_test" project_name = "Python Transifex API testing" resource_slug = "test_resource_fr" resource_name = "Test Resource FR" diff --git a/tests/data/test_config_public.toml b/tests/data/test_config_public.toml index b3d5fd7..8ad806d 100644 --- a/tests/data/test_config_public.toml +++ b/tests/data/test_config_public.toml @@ -1,5 +1,4 @@ -organization_slug = "test_pytransifex" -project_slug = "test_project_pytransifex_public" +project_slug = "pytransifex_test_public" project_name = "Python Transifex API testing (public)" repository_url = "https://github.com/opengisch/pytransifex" resource_slug = "test_resource_fr" diff --git a/tests/test_api.py b/tests/test_api.py index b96f5bd..5e5fc6e 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -9,18 +9,15 @@ logger = logging.getLogger(__name__) -class TestNewApi(unittest.TestCase): +class TestApi(unittest.TestCase): @classmethod def setUpClass(cls): - client = Transifex(defer_login=True) - assert client - cls.path_to_input_dir = Path.cwd().joinpath("tests", "data", "resources") cls.path_to_file = cls.path_to_input_dir.joinpath("test_resource_fr.po") cls.output_dir = Path.cwd().joinpath("tests", "output") cls.output_file = cls.output_dir.joinpath("test_resource_fr_DOWNLOADED.po") - cls.tx = client + cls.tx = Transifex(defer_login=True) cls.project_slug = test_config["project_slug"] cls.project_name = test_config["project_name"] cls.resource_slug = test_config["resource_slug"] @@ -34,6 +31,8 @@ def setUpClass(cls): logger.info("Deleting test project if it already exists") cls.tx.delete_project(project_slug=cls.project_slug) + assert cls.tx.project_exists(cls.project_slug) is False + logger.info("Creating a brand new project") cls.tx.create_project( project_name=cls.project_name, project_slug=cls.project_slug, private=True @@ -58,9 +57,7 @@ def test3_create_resource(self): resource_slug=self.resource_slug, path_to_file=str(self.path_to_file), ) - assert True - def test4_list_resources(self): resources = self.tx.list_resources(project_slug=self.project_slug) logger.info(f"Resources found: {resources}") assert resources @@ -71,12 +68,9 @@ def test5_update_source_translation(self): path_to_file=str(self.path_to_file), resource_slug=self.resource_slug, ) - assert True def test6_create_language(self): self.tx.create_language(project_slug=self.project_slug, language_code="fr_CH") - - def test7_list_languages(self): languages = self.tx.list_languages(project_slug=self.project_slug) logger.info(f"Languages found: {languages}") assert languages diff --git a/tests/test_cli.py b/tests/test_cli.py index 99ddcaa..790c6fa 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -54,7 +54,7 @@ def tearDownClass(cls): def test1_init(self): result = self.runner.invoke( cli, - ["init", "-p", self.project_slug, "-org", "test_pytransifex"], + ["init", "-p", self.project_slug, "-org", "pytransifex"], ) passed = result.exit_code == 0 assert passed