-
Notifications
You must be signed in to change notification settings - Fork 3
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 #6 from manodyaSenevirathne/testing
Add Test Cases and Mock server
- Loading branch information
Showing
4 changed files
with
225 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
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,89 @@ | ||
# Running Tests | ||
|
||
## Prerequisites | ||
|
||
You need an API token from OpenAI. | ||
|
||
To obtain this, refer to the [Ballerina OpenAI Chat Connector](https://github.com/ballerina-platform/module-ballerinax-openai.chat/blob/main/ballerina/Module.md). | ||
|
||
## Test Environments | ||
|
||
There are two test environments for running the `openai.chat` connector tests. The default environment is a mock server for the OpenAI API. The other environment is the actual OpenAI API. | ||
|
||
You can run the tests in either of these environments, and each has its own compatible set of tests. | ||
|
||
| Test Groups | Environment | | ||
|-------------|---------------------------------------------------| | ||
| mock_tests | Mock server for OpenAI API (Default Environment) | | ||
| live_tests | OpenAI API | | ||
|
||
## Running Tests in the Mock Server | ||
|
||
To execute the tests on the mock server, ensure that the `isLiveServer` environment variable is either set to `false` or left unset before initiating the tests. | ||
|
||
This environment variable can be configured within the `Config.toml` file located in the `tests` directory or specified as an environment variable. | ||
|
||
### Using a `Config.toml` File | ||
|
||
Create a `Config.toml` file in the `tests` directory with the following content: | ||
|
||
```toml | ||
isLiveServer = false | ||
``` | ||
|
||
### Using Environment Variables | ||
|
||
Alternatively, you can set the environment variable directly. | ||
|
||
For Linux or macOS: | ||
|
||
```bash | ||
export isLiveServer=false | ||
``` | ||
|
||
For Windows: | ||
|
||
```bash | ||
setx isLiveServer false | ||
``` | ||
|
||
Then, run the following command to execute the tests: | ||
|
||
```bash | ||
./gradlew clean test | ||
``` | ||
|
||
## Running Tests Against the OpenAI Live API | ||
|
||
### Using a `Config.toml` File | ||
|
||
Create a `Config.toml` file in the `tests` directory and add your authentication credentials: | ||
|
||
```toml | ||
isLiveServer = true | ||
token = "<your-openAI-api-token>" | ||
``` | ||
|
||
### Using Environment Variables | ||
|
||
Alternatively, you can set your authentication credentials as environment variables. | ||
|
||
For Linux or macOS: | ||
|
||
```bash | ||
export isLiveServer=true | ||
export token="<your-openAI-api-token>" | ||
``` | ||
|
||
For Windows: | ||
|
||
```bash | ||
setx isLiveServer true | ||
setx token <your-openAI-api-token> | ||
``` | ||
|
||
Then, run the following command to execute the tests: | ||
|
||
```bash | ||
./gradlew clean test | ||
``` |
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 (c) 2024, WSO2 LLC. (http://www.wso2.com). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/http; | ||
import ballerina/log; | ||
|
||
listener http:Listener httpListener = new (9090); | ||
|
||
http:Service mockService = service object { | ||
resource function post chat/completions(@http:Payload CreateChatCompletionRequest payload) returns CreateChatCompletionResponse|http:BadRequest { | ||
|
||
// Validate the request payload | ||
if payload.messages[0]["content"].toString() is "" || payload.model.toString() is "" { | ||
return http:BAD_REQUEST; | ||
} | ||
|
||
// Mock response | ||
CreateChatCompletionResponse response = { | ||
id: "chatcmpl-00000", | ||
choices: [ | ||
{ | ||
finish_reason: "stop", | ||
index: 0, | ||
message: {"content": "Test message received! How can I assist you today?", "role": "assistant", "refusal": null}, | ||
logprobs: null | ||
} | ||
], | ||
created: 1723091495, | ||
model: "gpt-4o-mini-2024-07-18", | ||
system_fingerprint: "fp_48196bc67a", | ||
"object": "chat.completion", | ||
"usage": {"completion_tokens": 11, "prompt_tokens": 13, "total_tokens": 24} | ||
}; | ||
return response; | ||
} | ||
}; | ||
|
||
function init() returns error? { | ||
if isLiveServer { | ||
log:printInfo("Skiping mock server initialization as the tests are running on live server"); | ||
return; | ||
} | ||
|
||
log:printInfo("Initiating mock server..."); | ||
check httpListener.attach(mockService, "/"); | ||
check httpListener.'start(); | ||
} |
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,44 @@ | ||
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/os; | ||
import ballerina/test; | ||
|
||
configurable boolean isLiveServer = os:getEnv("isLiveServer") == "true"; | ||
configurable string token = isLiveServer ? os:getEnv("token") : "test"; | ||
final string mockServiceUrl = "http://localhost:9090"; | ||
final Client openAIChat = check initClient(); | ||
|
||
function initClient() returns Client|error { | ||
if isLiveServer { | ||
return new ({auth: {token}}); | ||
} | ||
return new ({auth: {token}}, mockServiceUrl); | ||
} | ||
|
||
@test:Config { | ||
groups: ["live_tests", "mock_tests"] | ||
} | ||
isolated function testChatCompletion() returns error? { | ||
CreateChatCompletionRequest request = { | ||
model: "gpt-4o-mini", | ||
messages: [{"role": "user", "content": "This is a test message"}] | ||
}; | ||
CreateChatCompletionResponse response = check openAIChat->/chat/completions.post(request); | ||
test:assertTrue(response.choices.length() > 0, msg = "Expected at least one completion choice"); | ||
string? content = response.choices[0].message.content; | ||
test:assertTrue(content !is (), msg = "Expected content in the completion response"); | ||
} |