generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 5
Adding Profile Tests #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
277ab1e
Adding Profile Tests
prateeks99 3bd1b5c
Addressed check issues
prateeks99 00cee0e
Addressed review comments
prateeks99 78fd12f
Fixed minor import issue
prateeks99 baee3ff
Added Logging
prateeks99 06679b8
Removed print creds
prateeks99 a1b8912
Addressed logging and assert comments
prateeks99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| my_examples | ||
| .idea | ||
| .env | ||
| .venv | ||
| .DS_Store | ||
| select_ai.egg-info | ||
|
|
||
This file contains hidden or 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 hidden or 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,57 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright (c) 2025, Oracle and/or its affiliates. | ||
| # | ||
| # Licensed under the Universal Permissive License v 1.0 as shown at | ||
| # http://oss.oracle.com/licenses/upl. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| import select_ai | ||
|
|
||
| LOG_FORMAT = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s" | ||
| DATE_FORMAT = "%Y-%m-%d %H:%M:%S" | ||
|
|
||
|
|
||
| def _configure_logger(logger: logging.Logger, module_file: str) -> None: | ||
| logger.setLevel(logging.DEBUG) | ||
| log_dir = Path(__file__).resolve().parents[2] / "logs" | ||
| log_dir.mkdir(parents=True, exist_ok=True) | ||
| log_file = log_dir / f"tkex_{Path(module_file).stem}.log" | ||
|
|
||
| formatter = logging.Formatter(fmt=LOG_FORMAT, datefmt=DATE_FORMAT) | ||
|
|
||
| file_handler = logging.FileHandler(log_file, mode="w", encoding="utf-8") | ||
| file_handler.setLevel(logging.INFO) | ||
| file_handler.setFormatter(formatter) | ||
|
|
||
| console_handler = logging.StreamHandler() | ||
| console_handler.setLevel(logging.WARNING) | ||
| console_handler.setFormatter(formatter) | ||
|
|
||
| logger.handlers.clear() | ||
| logger.propagate = False | ||
| logger.addHandler(file_handler) | ||
| logger.addHandler(console_handler) | ||
| logger.info("Configured logging for module") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module", autouse=True) | ||
| def configure_module_logging(request): | ||
| module = request.module | ||
| logger = logging.getLogger(module.__name__) | ||
| _configure_logger(logger, module.__file__) | ||
| yield | ||
| for handler in logger.handlers: | ||
| handler.close() | ||
| logger.handlers.clear() | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def log_test_case(request, configure_module_logging): | ||
| logger = logging.getLogger(request.module.__name__) | ||
| logger.info("Starting test %s", request.node.name) | ||
| yield | ||
| logger.info("Finished test %s", request.node.name) |
This file contains hidden or 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,191 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright (c) 2025, Oracle and/or its affiliates. | ||
| # | ||
| # Licensed under the Universal Permissive License v 1.0 as shown at | ||
| # http://oss.oracle.com/licenses/upl. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| """ | ||
| 2000 - Synthetic data generation tests | ||
| """ | ||
|
|
||
| import logging | ||
| import uuid | ||
|
|
||
| import pytest | ||
| import select_ai | ||
| from oracledb import DatabaseError | ||
| from select_ai import ( | ||
| Profile, | ||
| ProfileAttributes, | ||
| SyntheticDataAttributes, | ||
| SyntheticDataParams, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| PROFILE_PREFIX = f"PYSAI_2000_{uuid.uuid4().hex.upper()}" | ||
|
|
||
|
|
||
| def _build_attributes(record_count=1, **kwargs): | ||
| logger.debug( | ||
| "Building synthetic data attributes with record_count=%s and extras=%s", | ||
| record_count, | ||
| kwargs, | ||
| ) | ||
| return SyntheticDataAttributes( | ||
| object_name="people", | ||
| record_count=record_count, | ||
| **kwargs, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def synthetic_provider(oci_compartment_id): | ||
| return select_ai.OCIGenAIProvider( | ||
| oci_compartment_id=oci_compartment_id, | ||
| oci_apiformat="GENERIC", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def synthetic_profile_attributes(oci_credential, synthetic_provider): | ||
| return ProfileAttributes( | ||
| credential_name=oci_credential["credential_name"], | ||
| object_list=[ | ||
| {"owner": "ADMIN", "name": "people"}, | ||
| {"owner": "ADMIN", "name": "gymnast"}, | ||
| ], | ||
| provider=synthetic_provider, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def synthetic_profile(synthetic_profile_attributes): | ||
| logger.info("Creating synthetic profile %s", f"{PROFILE_PREFIX}_SYNC") | ||
| profile = Profile( | ||
| profile_name=f"{PROFILE_PREFIX}_SYNC", | ||
| attributes=synthetic_profile_attributes, | ||
| description="Synthetic data test profile", | ||
| replace=True, | ||
| ) | ||
| yield profile | ||
| try: | ||
| logger.info("Deleting synthetic profile %s", profile.profile_name) | ||
| profile.delete(force=True) | ||
| except Exception: | ||
| logger.warning("Failed to delete profile %s", profile.profile_name) | ||
| pass | ||
|
|
||
|
|
||
| def test_2000_generate_with_full_params(synthetic_profile): | ||
| """Generate synthetic data with full parameter set""" | ||
| logger.info( | ||
| "Generating synthetic data with full params for profile %s", | ||
| synthetic_profile.profile_name, | ||
| ) | ||
| params = SyntheticDataParams(sample_rows=10, priority="HIGH") | ||
| attributes = _build_attributes( | ||
| record_count=5, | ||
| params=params, | ||
| user_prompt="age must be greater than 20", | ||
| ) | ||
prateeks99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.info("Attributes = %s", attributes) | ||
| assert attributes.record_count is 5 | ||
| result = synthetic_profile.generate_synthetic_data(attributes) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_2001_generate_minimum_fields(synthetic_profile): | ||
| """Generate synthetic data with minimum fields""" | ||
| logger.info("Generating synthetic data with minimum fields") | ||
| attributes = _build_attributes() | ||
| logger.info("Attributes = %s", attributes) | ||
| result = synthetic_profile.generate_synthetic_data(attributes) | ||
| logger.info("Result = %s", result) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_2002_generate_zero_sample_rows(synthetic_profile): | ||
| """Generate synthetic data with zero sample rows""" | ||
| logger.info("Generating synthetic data with zero sample rows") | ||
| params = SyntheticDataParams(sample_rows=0, priority="HIGH") | ||
| attributes = _build_attributes(params=params) | ||
| logger.info("Attributes = %s", attributes) | ||
| assert attributes.params.sample_rows is 0 | ||
| result = synthetic_profile.generate_synthetic_data(attributes) | ||
prateeks99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.info("Result = %s", result) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_2003_generate_single_sample_row(synthetic_profile): | ||
| """Generate synthetic data with single sample row""" | ||
| logger.info("Generating synthetic data with single sample row") | ||
| params = SyntheticDataParams(sample_rows=1, priority="HIGH") | ||
| attributes = _build_attributes(params=params) | ||
| logger.info("Attributes = %s", attributes) | ||
| assert attributes.params.sample_rows is 1 | ||
| result = synthetic_profile.generate_synthetic_data(attributes) | ||
prateeks99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.info("Result = %s", result) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_2004_generate_low_priority(synthetic_profile): | ||
| """Generate synthetic data with low priority""" | ||
| logger.info("Generating synthetic data with low priority") | ||
| params = SyntheticDataParams(sample_rows=1, priority="LOW") | ||
| attributes = _build_attributes(params=params) | ||
| logger.info("Attributes = %s", attributes) | ||
| assert attributes.params.sample_rows is 1 | ||
| assert attributes.params.priority is "LOW" | ||
| result = synthetic_profile.generate_synthetic_data(attributes) | ||
| logger.info("Result = %s", result) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_2005_generate_missing_object_name(synthetic_profile): | ||
| """Missing object_name raises error""" | ||
| logger.info("Validating missing object_name raises ValueError") | ||
| attributes = SyntheticDataAttributes(record_count=1) | ||
| with pytest.raises( | ||
| ValueError, match="One of object_name and object_list should be set" | ||
| ): | ||
| synthetic_profile.generate_synthetic_data(attributes) | ||
prateeks99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_2006_generate_invalid_priority(synthetic_profile): | ||
| """Invalid priority raises error""" | ||
| logger.info("Validating invalid priority raises error") | ||
| params = SyntheticDataParams(sample_rows=1, priority="CRITICAL") | ||
| attributes = _build_attributes(params=params) | ||
| logger.info("Attributes = %s", attributes) | ||
| with pytest.raises(DatabaseError) as exc_info: | ||
| synthetic_profile.generate_synthetic_data(attributes) | ||
prateeks99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (error,) = exc_info.value.args | ||
| logger.debug("Error code: %s", error.code) | ||
| logger.debug("Error message:\n%s", error.message) | ||
| assert error.code == 20000 | ||
| assert "Invalid value for priority" in error.message | ||
|
|
||
|
|
||
| def test_2007_generate_negative_record_count(synthetic_profile): | ||
| """Negative record count raises error""" | ||
| logger.info("Validating negative record count raises error") | ||
| attributes = _build_attributes(record_count=-5) | ||
| logger.info("Attributes = %s", attributes) | ||
| with pytest.raises(DatabaseError) as exc_info: | ||
| synthetic_profile.generate_synthetic_data(attributes) | ||
prateeks99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (error,) = exc_info.value.args | ||
| logger.debug("Error code: %s", error.code) | ||
| logger.debug("Error message:\n%s", error.message) | ||
| assert error.code == 20000 | ||
| assert "record_count cannot be smaller than 0" in error.message | ||
|
|
||
|
|
||
| def test_2008_generate_with_none_attributes(synthetic_profile): | ||
| """Passing None as attributes raises error""" | ||
| logger.info("Validating None attributes raise ValueError") | ||
| with pytest.raises( | ||
| ValueError, match="'synthetic_data_attributes' cannot be None" | ||
| ): | ||
| synthetic_profile.generate_synthetic_data(None) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.