-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ci): introduce tests with Nox (#4537)
- Loading branch information
1 parent
8a754a2
commit 07ef4da
Showing
10 changed files
with
430 additions
and
233 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
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,58 @@ | ||
# Run nox tests | ||
# | ||
# usage: | ||
# poetry run nox --error-on-external-run --reuse-venv=yes --non-interactive | ||
# | ||
# If you want to target a specific Python version, add -p parameter | ||
|
||
from typing import List, Optional | ||
|
||
import nox | ||
|
||
PREFIX_TESTS_FUNCTIONAL = "tests/functional" | ||
PREFIX_TESTS_UNIT = "tests/unit" | ||
|
||
|
||
def build_and_run_test(session: nox.Session, folders: List, extras: Optional[str] = "") -> None: | ||
""" | ||
This function is responsible for setting up the testing environment and running the test suite for specific feature. | ||
The function performs the following tasks: | ||
1. Installs the required dependencies for executing any test | ||
2. If the `extras` parameter is provided, the function installs the additional dependencies | ||
3. the function runs the pytest command with the specified folders as arguments, executing the test suite. | ||
Parameters | ||
---------- | ||
session: nox.Session | ||
The current Nox session object, which is used to manage the virtual environment and execute commands. | ||
folders: List | ||
A list of folder paths that contain the test files to be executed. | ||
extras: Optional[str] | ||
A string representing additional dependencies that should be installed for the test environment. | ||
If not provided, the function will install the project with basic dependencies | ||
""" | ||
|
||
# Required install to execute any test | ||
session.install("poetry", "pytest", "pytest-mock", "pytest_socket") | ||
|
||
# Powertools project folder is in the root | ||
if extras: | ||
session.install(f"./[{extras}]") | ||
else: | ||
session.install("./") | ||
|
||
# Execute test in specific folders | ||
session.run("pytest", *folders) | ||
|
||
|
||
@nox.session() | ||
def test_with_only_required_packages(session: nox.Session): | ||
"""Tests that only depends for required libraries""" | ||
# Logger | ||
build_and_run_test( | ||
session, | ||
folders=[ | ||
f"{PREFIX_TESTS_FUNCTIONAL}/logger/", | ||
], | ||
) |
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
File renamed without changes.
File renamed without changes.
113 changes: 113 additions & 0 deletions
113
tests/functional/logger/test_logger_with_package_logger.py
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,113 @@ | ||
import io | ||
import json | ||
import logging | ||
import random | ||
import string | ||
import warnings | ||
from collections import namedtuple | ||
|
||
import pytest | ||
|
||
from aws_lambda_powertools import Metrics, set_package_logger_handler | ||
from aws_lambda_powertools.logging.logger import set_package_logger | ||
from aws_lambda_powertools.shared import constants | ||
|
||
|
||
@pytest.fixture | ||
def stdout(): | ||
return io.StringIO() | ||
|
||
|
||
@pytest.fixture | ||
def lambda_context(): | ||
lambda_context = { | ||
"function_name": "test", | ||
"memory_limit_in_mb": 128, | ||
"invoked_function_arn": "arn:aws:lambda:eu-west-1:809313241:function:test", | ||
"aws_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72", | ||
} | ||
|
||
return namedtuple("LambdaContext", lambda_context.keys())(*lambda_context.values()) | ||
|
||
|
||
@pytest.fixture | ||
def lambda_event(): | ||
return {"greeting": "hello"} | ||
|
||
|
||
@pytest.fixture | ||
def service_name(): | ||
chars = string.ascii_letters + string.digits | ||
return "".join(random.SystemRandom().choice(chars) for _ in range(15)) | ||
|
||
|
||
def capture_logging_output(stdout): | ||
return json.loads(stdout.getvalue().strip()) | ||
|
||
|
||
def capture_multiple_logging_statements_output(stdout): | ||
return [json.loads(line.strip()) for line in stdout.getvalue().split("\n") if line] | ||
|
||
|
||
def test_package_logger_stream(stdout): | ||
# GIVEN package logger "aws_lambda_powertools" is explicitly set with no params | ||
set_package_logger(stream=stdout) | ||
|
||
# WHEN we add a dimension in Metrics feature | ||
my_metrics = Metrics(namespace="powertools") | ||
my_metrics.add_dimension(name="dimension", value="test") | ||
|
||
# THEN Metrics debug log statement should be logged | ||
output = stdout.getvalue() | ||
logger = logging.getLogger("aws_lambda_powertools") | ||
assert "Adding dimension:" in output | ||
assert logger.level == logging.DEBUG | ||
|
||
|
||
def test_package_logger_format(capsys): | ||
# GIVEN package logger "aws_lambda_powertools" is explicitly | ||
# with a custom formatter | ||
formatter = logging.Formatter("message=%(message)s") | ||
set_package_logger(formatter=formatter) | ||
|
||
# WHEN we add a dimension in Metrics feature | ||
my_metrics = Metrics(namespace="powertools") | ||
my_metrics.add_dimension(name="dimension", value="test") | ||
|
||
# THEN Metrics debug log statement should be logged using `message=` format | ||
output = capsys.readouterr().out | ||
logger = logging.getLogger("aws_lambda_powertools") | ||
assert "message=" in output | ||
assert logger.level == logging.DEBUG | ||
|
||
|
||
def test_set_package_logger_handler_with_powertools_debug_env_var(stdout, monkeypatch: pytest.MonkeyPatch): | ||
# GIVEN POWERTOOLS_DEBUG is set | ||
monkeypatch.setenv(constants.POWERTOOLS_DEBUG_ENV, "1") | ||
logger = logging.getLogger("aws_lambda_powertools") | ||
|
||
# WHEN set_package_logger is used at initialization | ||
# and any Powertools for AWS Lambda (Python) operation is used (e.g., Metrics add_dimension) | ||
set_package_logger_handler(stream=stdout) | ||
|
||
my_metrics = Metrics(namespace="powertools") | ||
my_metrics.add_dimension(name="dimension", value="test") | ||
|
||
# THEN Metrics debug log statement should be logged | ||
output = stdout.getvalue() | ||
assert "Adding dimension:" in output | ||
assert logger.level == logging.DEBUG | ||
|
||
|
||
def test_powertools_debug_env_var_warning(monkeypatch: pytest.MonkeyPatch): | ||
# GIVEN POWERTOOLS_DEBUG is set | ||
monkeypatch.setenv(constants.POWERTOOLS_DEBUG_ENV, "1") | ||
warning_message = "POWERTOOLS_DEBUG environment variable is enabled. Setting logging level to DEBUG." | ||
|
||
# WHEN set_package_logger is used at initialization | ||
# THEN a warning should be emitted | ||
with warnings.catch_warnings(record=True) as w: | ||
warnings.simplefilter("default") | ||
set_package_logger_handler() | ||
assert len(w) == 1 | ||
assert str(w[0].message) == warning_message |