Skip to content

Commit

Permalink
Merge pull request #6 from manodyaSenevirathne/testing
Browse files Browse the repository at this point in the history
Add Test Cases and Mock server
  • Loading branch information
NipunaRanasinghe authored Aug 14, 2024
2 parents a593109 + 3ffa8c8 commit 2e923c3
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.error"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.int"
Expand Down Expand Up @@ -205,6 +214,9 @@ dependencies = [
{org = "ballerina", name = "lang.value"},
{org = "ballerina", name = "observe"}
]
modules = [
{org = "ballerina", packageName = "log", moduleName = "log"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -245,6 +257,9 @@ dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "os", moduleName = "os"}
]

[[package]]
org = "ballerina"
Expand All @@ -255,6 +270,20 @@ dependencies = [
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "test"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.array"},
{org = "ballerina", name = "lang.error"}
]
modules = [
{org = "ballerina", packageName = "test", moduleName = "test"}
]

[[package]]
org = "ballerina"
name = "time"
Expand Down Expand Up @@ -293,6 +322,9 @@ version = "2.0.1"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "url"},
{org = "ballerinai", name = "observe"}
]
Expand Down
89 changes: 89 additions & 0 deletions ballerina/tests/README.md
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
```
60 changes: 60 additions & 0 deletions ballerina/tests/mock_service.bal
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();
}
44 changes: 44 additions & 0 deletions ballerina/tests/tests.bal
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");
}

0 comments on commit 2e923c3

Please sign in to comment.