Skip to content

Commit

Permalink
Merge pull request #3 from NipunaRanasinghe/hussain-main
Browse files Browse the repository at this point in the history
Fix formatting, variable names and anti-patterns in tests sources
  • Loading branch information
HussainLatiff authored Jul 5, 2024
2 parents a88c1e6 + 14ab31a commit d51f163
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
27 changes: 9 additions & 18 deletions ballerina/tests/mock_server.bal
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
import ballerina/http;
import ballerina/log;

listener http:Listener httpServer = new(9090);
listener http:Listener httpServer = new (9090);

http:Service mockService = service object {

http:Service mockService = service object {

resource function get users/[string user_id]() returns UserResponse|http:Response {
UserResponse response = {
id: "688069266636800112",
resource function get users/[string userId]() returns UserResponse|http:Response => {
id: userId,
username: "exampleUser",
discriminator: "1234",
public_flags: 0,
Expand All @@ -36,26 +34,19 @@ resource function get users/[string user_id]() returns UserResponse|http:Respons
accent_color: 16777215,
global_name: "globalExampleUser"
};
return response;
}
resource function get channels/[string channel_id]/invites() returns anydata {
anydata response = [1, "string", true];
return response;
}

resource function get channels/[string channel_id]/webhooks() returns anydata {
anydata response = [1, "string", true];
return response;
}
resource function get channels/[string channelId]/invites() returns anydata => ["Invite1", "Invite2"];

resource function get channels/[string channelId]/webhooks() returns anydata => ["Webhook1", "Webhook2"];
};

function init() returns error? {
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 httpServer.attach(mockService, "/");
check httpServer.'start();
}
}
50 changes: 31 additions & 19 deletions ballerina/tests/tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,47 @@
// specific language governing permissions and limitations
// under the License.

import ballerina/test;
import ballerina/os;
import ballerina/test;

configurable boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true";
configurable string CliId = isLiveServer ? os:getEnv("Client_ID") : "test";
configurable string CliScre = isLiveServer ? os:getEnv("Client_Secret") : "test";
configurable string serviceUrl = isLiveServer ? os:getEnv("DISCORD_URL") : "http://localhost:9090/";
ConnectionConfig config = {auth:{
token: "BearerToken"
}};
configurable string token = isLiveServer ? os:getEnv("TOKEN") : "token";
configurable string clientId = isLiveServer ? os:getEnv("CLIENT_ID") : "test";
configurable string clientSecret = isLiveServer ? os:getEnv("CLIENT_SECRET") : "test";
configurable string serviceUrl = isLiveServer ? os:getEnv("DISCORD_URL") : "http://localhost:9090";

string userId = "User Id";
string voiceChannelId = "Voice Channel ID";
string channelId = "Channel ID";

ConnectionConfig config = {
auth: {
token: "BearerToken"
}
};

final Client discord = check new Client(config, serviceUrl);

@test:Config{}
@test:Config {
groups: ["live_tests", "mock_tests"]
}
function testGetUser() returns error? {
string user_id = "User Id";
UserResponse getUserInfo = check discord->/users/[user_id]();
test:assertTrue((getUserInfo.length()> 0));
UserResponse user = check discord->/users/[userId]();
test:assertEquals(user.id, userId);
}

@test:Config{}
@test:Config {
groups: ["live_tests", "mock_tests"]
}
function testGetVoiceChannel() returns error? {
string channel_id = "Voice Channel ID";
anydata getVoiceChannelInfo = check discord->/channels/[channel_id]/invites();
test:assertTrue((getVoiceChannelInfo.count()> 0));
anydata[] voiceChannels = check discord->/channels/[voiceChannelId]/invites();
test:assertTrue(voiceChannels.length() > 0);
}

@test:Config{}
@test:Config {
groups: ["live_tests", "mock_tests"]
}
function testGetWebhook() returns error? {
string channel_id = "Channel ID";
anydata getWebhookInfo = check discord->/channels/[channel_id]/webhooks();
test:assertTrue((getWebhookInfo.count()> 0));
anydata[] channelWebhooks = check discord->/channels/[channelId]/webhooks();
test:assertTrue(channelWebhooks.length() > 0);
}

0 comments on commit d51f163

Please sign in to comment.