-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Antoni Ivanov <aivanov@vmware.com>
- Loading branch information
1 parent
cff372a
commit fca3eb1
Showing
4 changed files
with
65 additions
and
0 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 |
---|---|---|
|
@@ -4,5 +4,7 @@ | |
openai | ||
|
||
pytest | ||
pytest-httpserver | ||
vdk-core | ||
vdk-sqlite | ||
vdk-test-utils |
2 changes: 2 additions & 0 deletions
2
projects/vdk-plugins/vdk-smarter/tests/jobs/sql-job/10_create_table.sql
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,2 @@ | ||
|
||
CREATE TABLE stocks (date text, symbol text, price real) |
1 change: 1 addition & 0 deletions
1
projects/vdk-plugins/vdk-smarter/tests/jobs/sql-job/20_populate_table.sql
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 @@ | ||
INSERT INTO stocks VALUES ('2020-01-01', 'GOOG', 123.0), ('2020-01-01', 'GOOG', 123.0) |
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,60 @@ | ||
# Copyright 2021-2023 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import os | ||
import pathlib | ||
import re | ||
from unittest import mock | ||
|
||
import openai | ||
from click.testing import Result | ||
from pytest_httpserver import HTTPServer | ||
from vdk.plugin.smarter import openai_plugin_entry | ||
from vdk.plugin.sqlite import sqlite_plugin | ||
from vdk.plugin.test_utils.util_funcs import cli_assert | ||
from vdk.plugin.test_utils.util_funcs import cli_assert_equal | ||
from vdk.plugin.test_utils.util_funcs import CliEntryBasedTestRunner | ||
from vdk.plugin.test_utils.util_funcs import jobs_path_from_caller_directory | ||
|
||
# uses the pytest tmpdir fixture - https://docs.pytest.org/en/6.2.x/tmpdir.html#the-tmpdir-fixture | ||
|
||
|
||
def test_openai_review_plugin(tmpdir, httpserver: HTTPServer): | ||
# Mock OpenAI response | ||
review_comment = "This is a well written SQL query." | ||
httpserver.expect_oneshot_request(re.compile(r".*")).respond_with_json( | ||
{ | ||
"id": "test", | ||
"model": "foo", | ||
"choices": [ | ||
{ | ||
"text": " Here is the review: {'score': 4, 'review': '" | ||
+ review_comment | ||
+ "'}", | ||
} | ||
], | ||
} | ||
) | ||
|
||
# Set the OpenAI endpoint to the httpserver's uri | ||
openai.api_base = httpserver.url_for("") | ||
|
||
with mock.patch.dict( | ||
os.environ, | ||
{ | ||
"VDK_DB_DEFAULT_TYPE": "SQLITE", | ||
"VDK_SQLITE_FILE": str(tmpdir) + "vdk-sqlite.db", | ||
"VDK_OPENAI_REVIEW_ENABLED": "true", | ||
"VDK_OPENAI_MODEL": "foo", | ||
}, | ||
): | ||
runner = CliEntryBasedTestRunner(openai_plugin_entry, sqlite_plugin) | ||
|
||
result: Result = runner.invoke( | ||
["run", jobs_path_from_caller_directory("sql-job")] | ||
) | ||
|
||
cli_assert_equal(0, result) | ||
cli_assert(review_comment in result.output, result) | ||
|
||
assert pathlib.Path("queries_reviews_report.md").exists() | ||
assert review_comment in pathlib.Path("queries_reviews_report.md").read_text() |