|
| 1 | +@IsTest |
| 2 | +private class IntegrationTest { |
| 3 | + @IsTest |
| 4 | + static void usingChildRelationships() { |
| 5 | + Account anyAccount = new Account(Name = 'Sample Account'); |
| 6 | + insert anyAccount; |
| 7 | + Contact firstContact = new Contact(FirstName = 'First', LastName = 'Contact', AccountId = anyAccount.Id); |
| 8 | + Contact secondContact = new Contact(FirstName = 'Second', LastName = 'Contact', AccountId = anyAccount.Id); |
| 9 | + insert new List<Contact> { firstContact, secondContact }; |
| 10 | + |
| 11 | + String expressionPiped = '@Context.Contacts -> WHERE(FirstName = "First") -> SIZE() = 1'; |
| 12 | + Boolean pipedResult = (Boolean)Evaluator.run(expressionPiped, anyAccount.Id); |
| 13 | + Assert.isTrue(pipedResult, 'There should be exactly one contact with the first name "First".'); |
| 14 | + |
| 15 | + String expressionNested = 'SIZE(WHERE(@Context.Contacts, FirstName = "First")) = 1'; |
| 16 | + Boolean nestedResult = (Boolean)Evaluator.run(expressionNested, anyAccount.Id); |
| 17 | + Assert.isTrue(nestedResult, 'There should be exactly one contact with the first name "First".'); |
| 18 | + } |
| 19 | + |
| 20 | + @IsTest |
| 21 | + static void hasPurchasedSomethingInThePast() { |
| 22 | + List<CustomRecordContext> contexts = getContexts(); |
| 23 | + |
| 24 | + String pipedExpression = '@Customer.Assets -> WHERE(Product2Id = @Product.Id && Status = "Purchased") -> SIZE() > 0'; |
| 25 | + Boolean pipedResult = (Boolean)Evaluator.run(pipedExpression, contexts, new Configuration()); |
| 26 | + Assert.isTrue(pipedResult, 'The customer should have purchased the product in the past.'); |
| 27 | + |
| 28 | + String nestedExpression = 'SIZE(WHERE(@Customer.Assets, Product2Id = @Product.Id && Status = "Purchased")) > 0'; |
| 29 | + Boolean nestedResult = (Boolean)Evaluator.run(nestedExpression, contexts, new Configuration()); |
| 30 | + Assert.isTrue(nestedResult, 'The customer should have purchased the product in the past.'); |
| 31 | + } |
| 32 | + |
| 33 | + private static List<CustomRecordContext> getContexts() { |
| 34 | + Account someAccount = new Account(Name = 'Test Account'); |
| 35 | + insert someAccount; |
| 36 | + Contact someone = new Contact(FirstName = 'Test', LastName = 'User', AccountId = someAccount.Id); |
| 37 | + insert someone; |
| 38 | + Product2 sampleProduct = new Product2(Name = 'Test Product', IsActive = true); |
| 39 | + insert sampleProduct; |
| 40 | + |
| 41 | + Asset sampleAsset = new Asset( |
| 42 | + Name = 'Test Asset', |
| 43 | + ContactId = someone.Id, Status = 'Purchased', |
| 44 | + Product2Id = sampleProduct.Id); |
| 45 | + insert sampleAsset; |
| 46 | + |
| 47 | + CustomRecordContext customer = new CustomRecordContext('Customer', someone.Id); |
| 48 | + CustomRecordContext product = new CustomRecordContext('Product', sampleProduct.Id); |
| 49 | + List<CustomRecordContext> contexts = new List<CustomRecordContext> { customer, product }; |
| 50 | + return contexts; |
| 51 | + } |
| 52 | +} |
0 commit comments