Skip to content

Commit

Permalink
Merge pull request #5 from roshanemoraes/main
Browse files Browse the repository at this point in the history
Fix Mock Tests With Grouping
  • Loading branch information
MohamedSabthar authored Jan 20, 2025
2 parents 3b878e3 + 1e9ae42 commit 1579af6
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 81 deletions.
2 changes: 1 addition & 1 deletion ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version = "1.0.0"
license = ["Apache-2.0"]
authors = ["Ballerina"]
keywords = ["hubspot","crm", "commerce", "orders"]
icon = "icon.png"
icon = "icon.png"
repository = "https://github.com/ballerina-platform/module-ballerinax-hubspot.crm.commerce.orders"

[build-options]
Expand Down
7 changes: 6 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.3"
distribution-version = "2201.10.0"

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

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -318,10 +321,12 @@ version = "1.0.0"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "oauth2"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "url"},
{org = "ballerinai", name = "observe"}
]
modules = [
{org = "ballerinax", packageName = "hubspot.crm.commerce.orders", moduleName = "hubspot.crm.commerce.orders"}
]

11 changes: 7 additions & 4 deletions ballerina/tests/mock_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

import ballerina/test;

final Client orderClient = check new Client(config, serviceUrl = "http://localhost:9090/crm/v3/objects/orders");
final Client ordersClient = check new(config = {auth:{
token: "test-token"
}}, serviceUrl="http://localhost:9090/crm/v3/objects/orders"
);

@test:Config {}
isolated function mockTestForCreatingABatchOfOrders() returns error? {
BatchResponseSimplePublicObject response = check orderClient->/batch/create.post(
function mockTestForCreatingABatchOfOrders() returns error? {
BatchResponseSimplePublicObject response = check ordersClient->/batch/create.post(
{
inputs: [
{
Expand Down Expand Up @@ -50,7 +53,7 @@ isolated function mockTestForCreatingABatchOfOrders() returns error? {
@test:Config {}
isolated function mockTestForCreatingBatchOfOrdersByUniqueProperty() returns error? {
BatchResponseSimplePublicUpsertObject|BatchResponseSimplePublicUpsertObjectWithErrors response =
check orderClient->/batch/upsert.post(
check ordersClient->/batch/upsert.post(
payload = {
inputs: [
{
Expand Down
87 changes: 63 additions & 24 deletions ballerina/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

import ballerina/http;
import ballerina/oauth2;
import ballerina/os;
import ballerina/test;

configurable string clientId = ?;
configurable string clientSecret = ?;
configurable string refreshToken = ?;
configurable boolean enableLiveServerTest = false;

final boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true";

OAuth2RefreshTokenGrantConfig auth = {
clientId,
Expand All @@ -29,10 +33,15 @@ OAuth2RefreshTokenGrantConfig auth = {
credentialBearer: oauth2:POST_BODY_BEARER
};

ConnectionConfig config = {auth: auth};
final Client baseClient = check new Client(config);
final Client orderClient = check new ({
auth: enableLiveServerTest ? auth
: {token: "test-token"}
});

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testPostOrdersSearch() returns error? {
PublicObjectSearchRequest payload = {
query: "1",
Expand All @@ -55,11 +64,14 @@ function testPostOrdersSearch() returns error? {
]
};
CollectionResponseWithTotalSimplePublicObjectForwardPaging response =
check baseClient->/search.post(payload = payload);
check orderClient->/search.post(payload = payload);
test:assertTrue(response.total >= 0);
}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testPostOrdersBatchRead() returns error? {

BatchReadInputSimplePublicObjectId payload = {
Expand All @@ -74,7 +86,7 @@ function testPostOrdersBatchRead() returns error? {
properties: ["hs_lastmodifieddate", "hs_createdate", "hs_object_id", "updatedAt"]
};
BatchResponseSimplePublicObject|BatchResponseSimplePublicObjectWithErrors response =
check baseClient->/batch/read.post(payload = payload);
check orderClient->/batch/read.post(payload = payload);
if response.status != "PENDING" && response.status != "PROCESSING"
&& response.status != "CANCELED" && response.status != "COMPLETE" {
test:assertFail("invalid status type");
Expand All @@ -83,15 +95,21 @@ function testPostOrdersBatchRead() returns error? {
test:assertFalse(response.startedAt is "", "startedAt should not be empty");
}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testDeleteObjectsOrdersByOrderId() returns error? {
string orderId = "10";

http:Response response = check baseClient->/[orderId].delete();
http:Response response = check orderClient->/[orderId].delete();
test:assertTrue(response.statusCode == 204);
}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testPatchObjectsOrdersByOrderId() returns error? {
string orderId = "395972319872";
SimplePublicObjectInput payload =
Expand All @@ -102,23 +120,29 @@ function testPatchObjectsOrdersByOrderId() returns error? {
"hs_shipping_tracking_number": "123098521091"
}
};
SimplePublicObject response = check baseClient->/[orderId].patch(payload = payload);
SimplePublicObject response = check orderClient->/[orderId].patch(payload = payload);
test:assertFalse(response?.id is "", "id should not be empty");
test:assertFalse(response?.createdAt is "", "creation time should not be empty");
test:assertFalse(response?.updatedAt is "", "updated time should not be empty");
}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testGetObjectsOrdersByOrderId() returns error? {
string orderId = "395972319872";

SimplePublicObjectWithAssociations response = check baseClient->/[orderId];
SimplePublicObjectWithAssociations response = check orderClient->/[orderId];
test:assertFalse(response?.createdAt is "", "creation time should not be empty");
test:assertFalse(response?.updatedAt is "", "updated time should not be empty");

}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testPostordersBatchUpsert() returns error? {
BatchInputSimplePublicObjectBatchInputUpsert payload = {
inputs: [
Expand All @@ -135,13 +159,16 @@ function testPostordersBatchUpsert() returns error? {
]
};
BatchResponseSimplePublicUpsertObject|BatchResponseSimplePublicUpsertObjectWithErrors response =
check baseClient->/batch/upsert.post(payload = payload);
check orderClient->/batch/upsert.post(payload = payload);
test:assertTrue(response.status == "COMPLETE");
test:assertFalse(response?.completedAt is "", "creation time should not be empty");
test:assertFalse(response?.startedAt is "", "start time should not be empty");
}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testPostOrdersBatchCreate() returns error? {

BatchInputSimplePublicObjectInputForCreate payload = {
Expand All @@ -166,12 +193,15 @@ function testPostOrdersBatchCreate() returns error? {
}
]
};
BatchResponseSimplePublicObject response = check baseClient->/batch/create.post(payload = payload);
BatchResponseSimplePublicObject response = check orderClient->/batch/create.post(payload = payload);
test:assertFalse(response.completedAt is "", "completedAt should not be empty");
test:assertFalse(response.startedAt is "", "startedAt should not be empty");
}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testPostObjectsOrdersBatchUpdate() returns error? {
BatchInputSimplePublicObjectBatchInput payload =
{
Expand All @@ -185,12 +215,15 @@ function testPostObjectsOrdersBatchUpdate() returns error? {
]
};
BatchResponseSimplePublicObject|BatchResponseSimplePublicObjectWithErrors response =
check baseClient->/batch/update.post(payload = payload);
check orderClient->/batch/update.post(payload = payload);
test:assertFalse(response.completedAt is "", "completedAt should not be empty");
test:assertFalse(response.startedAt is "", "startedAt should not be empty");
}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testPostObjectsOrders() returns error? {

SimplePublicObjectInputForCreate payload =
Expand Down Expand Up @@ -219,24 +252,30 @@ function testPostObjectsOrders() returns error? {
"hs_shipping_address_street": "123 Fake Street"
}
};
SimplePublicObject response = check baseClient->/.post(payload = payload);
SimplePublicObject response = check orderClient->/.post(payload = payload);

test:assertFalse(response.createdAt is "", "createdAt should not be empty");
test:assertFalse(response.updatedAt is "", "updateAt should not be empty");

}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testGetObjectsOrders() returns error? {
CollectionResponseSimplePublicObjectWithAssociationsForwardPaging response = check baseClient->/;
CollectionResponseSimplePublicObjectWithAssociationsForwardPaging response = check orderClient->/;

foreach SimplePublicObjectWithAssociations result in response.results {
test:assertFalse(result.createdAt is "", "createdAt should not be empty");
test:assertFalse(result.updatedAt is "", "updatedAt should not be empty");
}
}

@test:Config {}
@test:Config {
groups: ["live_tests"],
enable: enableLiveServerTest
}
function testPostOrdersBatchArchive() returns error? {
BatchInputSimplePublicObjectId payload = {
inputs: [
Expand All @@ -245,6 +284,6 @@ function testPostOrdersBatchArchive() returns error? {
}
]
};
http:Response response = check baseClient->/batch/archive.post(payload = payload);
http:Response response = check orderClient->/batch/archive.post(payload = payload);
test:assertTrue(response.statusCode == 204);
}
34 changes: 15 additions & 19 deletions examples/batch-operations/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import ballerina/io;
import ballerina/oauth2;
import ballerinax/hubspot.crm.commerce.orders as orders;
import ballerinax/hubspot.crm.commerce.orders as hsorders;

// Configuration for HubSpot API client credentials
configurable string clientId = ?;
Expand All @@ -25,38 +25,36 @@ configurable string refreshToken = ?;

public function main() returns error? {
// Initialize the HubSpot client with the given configuration
orders:ConnectionConfig config = {
hsorders:ConnectionConfig config = {
auth: {
clientId,
clientSecret,
refreshToken,
credentialBearer: oauth2:POST_BODY_BEARER
}
};
final orders:Client hubspotClient = check new orders:Client(
config, serviceUrl = "https://api.hubapi.com/crm/v3/objects");
final hsorders:Client hubspotClient = check new hsorders:Client(config);
io:println("HubSpot Client initialized successfully.");

// Handle batch operations
check handleBatchOperations(hubspotClient);
handleBatchOperations(hubspotClient);
}

// Function to handle all batch operations
function handleBatchOperations(orders:Client hubspotClient) returns error? {
function handleBatchOperations(hsorders:Client hubspotClient) {
io:println("Starting Batch Operations...");

// Perform batch create orders
check batchCreateOrders(hubspotClient);
batchCreateOrders(hubspotClient);

// Perform batch update orders
check batchUpdateOrders(hubspotClient);

batchUpdateOrders(hubspotClient);
io:println("Batch Operations Completed.");
}

// Function to create a batch of orders
function batchCreateOrders(orders:Client hubspotClient) returns error? {
orders:BatchInputSimplePublicObjectInputForCreate batchCreateRequest = {
function batchCreateOrders(hsorders:Client hubspotClient) {
hsorders:BatchInputSimplePublicObjectInputForCreate batchCreateRequest = {
inputs: [
{
associations: [
Expand All @@ -79,19 +77,18 @@ function batchCreateOrders(orders:Client hubspotClient) returns error? {
]
};

orders:BatchResponseSimplePublicObject|error response =
hsorders:BatchResponseSimplePublicObject|error response =
hubspotClient->/orders/batch/create.post(batchCreateRequest);
if response is orders:BatchResponseSimplePublicObject {
if response is hsorders:BatchResponseSimplePublicObject {
io:println("Batch of orders created successfully.");
} else {
io:println("Failed to create batch of orders.");
return error("Batch creation failed.");
}
}

// Function to update a batch of orders
function batchUpdateOrders(orders:Client hubspotClient) returns error? {
orders:BatchInputSimplePublicObjectBatchInput batchUpdateRequest = {
function batchUpdateOrders(hsorders:Client hubspotClient) {
hsorders:BatchInputSimplePublicObjectBatchInput batchUpdateRequest = {
inputs: [
{
id: "394961395351",
Expand All @@ -102,12 +99,11 @@ function batchUpdateOrders(orders:Client hubspotClient) returns error? {
]
};

orders:BatchResponseSimplePublicObject|error response =
hsorders:BatchResponseSimplePublicObject|error response =
hubspotClient->/orders/batch/update.post(batchUpdateRequest);
if response is orders:BatchResponseSimplePublicObject {
if response is hsorders:BatchResponseSimplePublicObject {
io:println("Batch of orders updated successfully.");
} else {
io:println("Failed to update batch of orders.");
return error("Batch update failed.");
}
}
Loading

0 comments on commit 1579af6

Please sign in to comment.