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

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Feb 28, 2023
1 parent f36b77a commit bf5c80b
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 70 deletions.
34 changes: 4 additions & 30 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
docs/_build/
docs/_autosummary/

.tokens

__pycache__/
venv/

Expand Down
6 changes: 6 additions & 0 deletions nose2.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[unittest]
start-dir = tests
code-directories = ..

[log-capture]
always-on = True
28 changes: 10 additions & 18 deletions pytransifex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
10 changes: 5 additions & 5 deletions pytransifex/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/data/.qgis-plugin-ci-test-changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project_slug: test_project_pytransifex_public
transifex_organization: test_pytransifex
project_slug: pytransifex_test_public
transifex_organization: pytransifex
transifex_coordinator: john_doe
translation_languages:
- fr
Expand Down
3 changes: 1 addition & 2 deletions tests/data/test_config.toml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 1 addition & 2 deletions tests/data/test_config_public.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
14 changes: 4 additions & 10 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bf5c80b

Please sign in to comment.