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

small fixes #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 9 additions & 29 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ on:

jobs:
tests:
env:
TX_ORGANIZATION: ${{ secrets.TX_ORGANIZATION }}
TX_TOKEN: ${{ secrets.TX_TOKEN }}
runs-on: ubuntu-latest
steps:
# Not using strategy.matrix to create multiple jobs
Expand All @@ -35,34 +38,11 @@ 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.TX_ORGANIZATION }}
tx_token: ${{ secrets.TX_TOKEN }}
run: |
TX_TOKEN=$tx_token ORGANIZATION=$organization \
python -m unittest ./tests/test_api.py
TX_ORGANIZATION: ${{ secrets.TX_ORGANIZATION }}
TX_TOKEN: ${{ secrets.TX_TOKEN }}
run: nose2 -v

- name: Test CLI
env:
organization: ${{ secrets.TX_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.TX_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.TX_ORGANIZATION }}
tx_token: ${{ secrets.TX_TOKEN }}
run: |
TX_TOKEN=$tx_token ORGANIZATION=$organization \
python -m unittest ./tests/test_translation.py
- uses: mxschmitt/action-tmate@v3
if: failure()
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
4 changes: 4 additions & 0 deletions .pytx_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
organization_slug = "pytransifex"
project_slug = "pytransifex_test"
output_directory = "/Users/denis/Documents/pytransifex/output"
config_file = "/Users/denis/Documents/pytransifex/.pytx_config.yml"
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
1 change: 1 addition & 0 deletions tests/test_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def setUpClass(cls):
)
with open(config_yaml) as f:
arg_dict = yaml.safe_load(f)
arg_dict["transifex_organization"] = os.getenv("TX_ORGANIZATION")

cls.parameters = Parameters(**arg_dict)
cls.t = Translation(cls.parameters, transifex_token=transifex_token)
Expand Down
Loading