Skip to content

Commit

Permalink
adding Apex tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoenraets committed Dec 12, 2018
1 parent 368391d commit 9bb31b1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
39 changes: 39 additions & 0 deletions force-app/main/default/classes/TestOrderController.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@isTest
public class TestOrderController {

public static Order__c createOrder() {
Account acc = new Account(
Name='Sample Account'
);
insert acc;

Order__c newOrder = new Order__c(
Account__c=acc.Id
);
insert newOrder;
return newOrder;
}

public static void createOrderItem(Id orderId) {
Product__c p = new Product__c(
Name='Sample Product'
);
insert p;
Order_Item__c orderItem = new Order_Item__c(
Order__c=orderId,
Product__c=p.Id
);
insert orderItem;
}

static testMethod void testGetOrderItems() {
Order__c newOrder = TestOrderController.createOrder();
Id orderId = newOrder.Id;
TestOrderController.createOrderItem(orderId);
Test.startTest();
List<Order_Item__c> orderItems = OrderController.getOrderItems(orderId);
Test.stopTest();
System.assertEquals(orderItems.size(), 1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="TestOrderController">
<apiVersion>45.0</apiVersion>
<status>Active</status>
</ApexClass>
42 changes: 42 additions & 0 deletions force-app/main/default/classes/TestProductController.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@isTest
public class TestProductController {

public static void createProducts() {
List<Product__c> products = new List<Product__c>();

products.add(new Product__c(
Name='Sample Bike 1',
MSRP__c=1000,
Category__c='Mountain',
Level__c='Beginner',
Material__c='Carbon'));

products.add(new Product__c(
Name='Sample Bike 2',
MSRP__c=1200,
Category__c='Mountain',
Level__c='Beginner',
Material__c='Carbon'));

insert products;
}

static testMethod void testGetProducts() {
TestProductController.createProducts();
String filters = '{"searchKey":"Sample", "maxPrice":2000, "categories": ["Mountain"], "levels": ["Beginner"], "materials":["Carbon"]}, 1';
Test.startTest();
PagedResult result = ProductController.getProducts(filters, 1);
Test.stopTest();
System.assertEquals(result.records.size(), 2);
}
static testMethod void testGetSimilarProducts() {
TestProductController.createProducts();
Test.startTest();
String filters = '{"searchKey":"Sample", "maxPrice":2000, "categories": ["Mountain"], "levels": ["Beginner"], "materials":["Carbon"]}, 1';
PagedResult result = ProductController.getProducts(filters, 1);
Product__c productToCompare = (Product__c)result.records[0];
Product__c[] products = ProductController.getSimilarProducts(productToCompare.Id, null);
System.assertEquals(products.size(), 1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="TestProductController">
<apiVersion>45.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 9bb31b1

Please sign in to comment.