Skip to content

Commit

Permalink
test(python): adapt tests to run against eppo_client-3
Browse files Browse the repository at this point in the history
To run tests against old SDK:
1. Create and activate a virtual for the old SDK.
2. Install old SDK with `pip install -e .`
3. Install pytest
4. Run pytest in this directory with `-k 'not rust_only'`
  • Loading branch information
rasendubi committed Aug 21, 2024
1 parent 107b037 commit b0c4a8c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion python-sdk/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[pytest]
markers =
rust_only: mark a test as only passing on Rust-based SDK.
rust_only: mark a test as only passing on Python-on-Rust SDK.
2 changes: 1 addition & 1 deletion python-sdk/python/tests/test_assignment_logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from eppo_client import AssignmentLogger
from eppo_client.assignment_logger import AssignmentLogger


def test_can_inherit_assignment_logger():
Expand Down
24 changes: 20 additions & 4 deletions python-sdk/python/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
import pytest

from eppo_client import Config, AssignmentLogger
from eppo_client.config import Config
from eppo_client.assignment_logger import AssignmentLogger


class TestConfig:
def test_can_create_with_api_key_and_assignment_logger(self):
Config(api_key="test-key", assignment_logger=AssignmentLogger())

@pytest.mark.rust_only
def test_requires_non_empty_key(self):
with pytest.raises(ValueError):
Config(api_key="", assignment_logger=AssignmentLogger())

def test_requires_api_key(self):
with pytest.raises(TypeError):
# Python SDK raises Pydantic's ValidationError.
# Python-on-rust raises TypeError.
with pytest.raises(Exception):
Config(assignment_logger=AssignmentLogger())

def test_requires_assignment_logger(self):
with pytest.raises(TypeError):
# Python SDK raises Pydantic's ValidationError.
# Python-on-rust raises TypeError.
with pytest.raises(Exception):
Config(api_key="test-key")

def test_assignment_logger_must_be_a_subclass_of_logger(self):
class MyLogger:
pass

with pytest.raises(TypeError):
# Python SDK raises Pydantic's ValidationError.
# Python-on-rust raises TypeError.
with pytest.raises(Exception):
Config(api_key="test-key", assignment_logger=MyLogger())

def test_assignment_accepts_a_subclass_of_logger(self):
Expand All @@ -32,7 +40,15 @@ class MyLogger(AssignmentLogger):

Config(api_key="test-key", assignment_logger=MyLogger())

# This one is failing on native python sdk as we don't have
# `validate_assignment` enabled.
@pytest.mark.rust_only
def test_cant_reset_assignment_logger(self):
config = Config(api_key="test-key", assignment_logger=AssignmentLogger())
with pytest.raises(TypeError):
config.assignment_logger = None
assert config.assignment_logger is not None

def test_can_set_assignment_logger_to_another_logger(self):
config = Config(api_key="test-key", assignment_logger=AssignmentLogger())
config.assignment_logger = AssignmentLogger()
9 changes: 6 additions & 3 deletions python-sdk/python/tests/test_sdk_test_data_eval_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
from time import sleep

import eppo_client
from eppo_client.assignment_logger import AssignmentLogger

TEST_DIR = "../sdk-test-data/ufc/tests"
TEST_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "../../../sdk-test-data/ufc/tests"
)
test_data = []
for file_name in os.listdir(TEST_DIR):
with open("{}/{}".format(TEST_DIR, file_name)) as test_case_json:
Expand All @@ -19,10 +22,10 @@
@pytest.fixture(scope="session", autouse=True)
def init_fixture():
eppo_client.init(
eppo_client.Config(
eppo_client.config.Config(
base_url=MOCK_BASE_URL + "ufc/api",
api_key="dummy",
assignment_logger=eppo_client.AssignmentLogger(),
assignment_logger=AssignmentLogger(),
)
)
sleep(0.1) # wait for initialization
Expand Down

0 comments on commit b0c4a8c

Please sign in to comment.