-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from lgrosjean/feat/add-tests
Feat/add tests
- Loading branch information
Showing
9 changed files
with
560 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
name: Check | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: [main] | ||
workflow_dispatch: | ||
|
||
env: | ||
PACKAGE_DIR: powerbi_ext | ||
TESTS_DIR: tests | ||
|
||
jobs: | ||
tests: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.9", "3.10", "3.11"] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install Poetry | ||
run: | | ||
pipx install poetry | ||
poetry --version | ||
- name: Install dependencies | ||
run: | | ||
poetry install | ||
- name: Test with pytest | ||
run: poetry run pytest --cov=$PACKAGE_DIR $TESTS_DIR | ||
|
||
lint: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.9", "3.10", "3.11"] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install Poetry | ||
run: | | ||
pipx install poetry | ||
poetry --version | ||
- name: Install dependencies | ||
run: | | ||
poetry install | ||
- name: Lint with pylint | ||
run: poetry run pylint --jobs 0 $PACKAGE_DIR --fail-under 9 | ||
|
||
format: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.9", "3.10", "3.11"] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install black | ||
run: pip install black | ||
|
||
- name: Check format with black | ||
run: black --check $PACKAGE_DIR |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import os | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from azure.identity import ClientSecretCredential | ||
|
||
from powerbi_ext.auth import SCOPE, get_credential, get_token | ||
|
||
TOKEN = "token" | ||
TENANT_ID, CLIENT_ID, CLIENT_SECRET = "tenant_id", "client_id", "client_secret" | ||
|
||
|
||
def test_get_credential_with_args(): | ||
with patch.object(ClientSecretCredential, "__new__") as mock_ClientSecretCredential: | ||
get_credential( | ||
tenant_id=TENANT_ID, | ||
client_id=CLIENT_ID, | ||
client_secret=CLIENT_SECRET, | ||
) | ||
|
||
mock_ClientSecretCredential.assert_called_once_with( | ||
ClientSecretCredential, | ||
tenant_id=TENANT_ID, | ||
client_id=CLIENT_ID, | ||
client_secret=CLIENT_SECRET, | ||
) | ||
|
||
|
||
def test_get_credential_without_args_missing_envvar_tenant_id(): | ||
if os.getenv("POWERBI_EXT_TENANT_ID"): | ||
del os.environ["POWERBI_EXT_TENANT_ID"] | ||
os.environ["POWERBI_EXT_CLIENT_ID"] = CLIENT_ID | ||
os.environ["POWERBI_EXT_CLIENT_SECRET"] = CLIENT_SECRET | ||
|
||
with pytest.raises(KeyError, match="POWERBI_EXT_TENANT_ID"): | ||
get_credential() | ||
|
||
|
||
def test_get_credential_without_args_missing_envvar_client_id(): | ||
os.environ["POWERBI_EXT_TENANT_ID"] = TENANT_ID | ||
if os.getenv("POWERBI_EXT_CLIENT_ID"): | ||
del os.environ["POWERBI_EXT_CLIENT_ID"] | ||
os.environ["POWERBI_EXT_CLIENT_SECRET"] = CLIENT_SECRET | ||
|
||
with pytest.raises(KeyError, match="POWERBI_EXT_CLIENT_ID"): | ||
get_credential() | ||
|
||
|
||
def test_get_credential_without_args_missing_envvar_client_secret(): | ||
os.environ["POWERBI_EXT_TENANT_ID"] = TENANT_ID | ||
os.environ["POWERBI_EXT_CLIENT_ID"] = CLIENT_ID | ||
if os.getenv("POWERBI_EXT_CLIENT_SECRET"): | ||
del os.environ["POWERBI_EXT_CLIENT_SECRET"] | ||
|
||
with pytest.raises(KeyError, match="POWERBI_EXT_CLIENT_SECRET"): | ||
get_credential() | ||
|
||
|
||
def test_get_token(): | ||
mock_access_token = MagicMock(token=TOKEN) | ||
mock_get_token = MagicMock(return_value=mock_access_token) | ||
mock_credential = MagicMock(get_token=mock_get_token) | ||
with patch( | ||
"powerbi_ext.auth.get_credential", return_value=mock_credential | ||
) as mock_get_credential: | ||
result = get_token( | ||
tenant_id=TENANT_ID, | ||
client_id=CLIENT_ID, | ||
client_secret=CLIENT_SECRET, | ||
) | ||
|
||
mock_get_credential.assert_called_once_with( | ||
tenant_id=TENANT_ID, | ||
client_id=CLIENT_ID, | ||
client_secret=CLIENT_SECRET, | ||
) | ||
mock_get_token.assert_called_once_with(SCOPE) | ||
|
||
assert result == TOKEN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from meltano.edk.models import Describe, ExtensionCommand | ||
from requests import RequestException | ||
|
||
from powerbi_ext.extension import BASE_URL, TIMEOUT, PowerBIExtension | ||
|
||
TOKEN = "token" | ||
WORKSPACE_ID = "workspace_id" | ||
DATASET_ID = "dataset_id" | ||
|
||
|
||
@patch("powerbi_ext.extension.get_token", return_value=TOKEN) | ||
def test_init_not_token(mock_get_token: MagicMock): | ||
ext = PowerBIExtension() | ||
mock_get_token.assert_called_once() | ||
assert ext.log | ||
assert ext.headers == {"Authorization": f"Bearer {TOKEN}"} | ||
|
||
|
||
class TestExtension: | ||
ext = PowerBIExtension(token=TOKEN) | ||
|
||
def test_invoke(self): | ||
with pytest.raises(NotImplementedError): | ||
self.ext.invoke() | ||
|
||
@patch("requests.post") | ||
def test_refresh_ok(self, mock_post: MagicMock): | ||
mock_res = MagicMock(status_code=200, headers={"RequestId": "RequestId"}) | ||
url = BASE_URL + f"/groups/{WORKSPACE_ID}/datasets/{DATASET_ID}" + "/refreshes" | ||
body = { | ||
"notifyOption": "MailOnCompletion", | ||
} | ||
mock_post.return_value = mock_res | ||
res = self.ext.refresh(workspace_id=WORKSPACE_ID, dataset_id=DATASET_ID) | ||
mock_post.assert_called_once_with( | ||
url, json=body, headers=self.ext.headers, timeout=TIMEOUT | ||
) | ||
assert res == "RequestId" | ||
|
||
@patch("requests.post") | ||
def test_refresh_not_ok(self, mock_post: MagicMock): | ||
mock_res = MagicMock(status_code=202) | ||
url = BASE_URL + f"/groups/{WORKSPACE_ID}/datasets/{DATASET_ID}" + "/refreshes" | ||
body = { | ||
"notifyOption": "MailOnCompletion", | ||
} | ||
mock_post.return_value = mock_res | ||
with pytest.raises(RequestException): | ||
self.ext.refresh(workspace_id=WORKSPACE_ID, dataset_id=DATASET_ID) | ||
|
||
mock_post.assert_called_once_with( | ||
url, json=body, headers=self.ext.headers, timeout=TIMEOUT | ||
) | ||
|
||
@patch.object(ExtensionCommand, "__new__") | ||
@patch.object(Describe, "__new__") | ||
def test_describe( | ||
self, | ||
mock_describe_class: MagicMock, | ||
mock_command_class: MagicMock, | ||
): | ||
name, description = "powerbi_extension", "extension commands" | ||
mock_command = MagicMock(name=name, description=description) | ||
mock_command_class.return_value = mock_command | ||
|
||
self.ext.describe() | ||
|
||
mock_command_class.assert_called_once_with( | ||
ExtensionCommand, name=name, description=description | ||
) | ||
|
||
mock_describe_class.assert_called_once_with(Describe, commands=[mock_command]) |
Oops, something went wrong.