[891] | 1 | import grails.test.* |
---|
[898] | 2 | import com.henyo.BaseUnitTestCase |
---|
[891] | 3 | import static org.junit.Assert.assertThat |
---|
| 4 | import static org.hamcrest.CoreMatchers.equalTo |
---|
| 5 | |
---|
[894] | 6 | /** |
---|
| 7 | * Unit tests for PurchaseOrderService class. |
---|
| 8 | */ |
---|
[898] | 9 | class PurchaseOrderServiceTests extends BaseUnitTestCase { |
---|
[891] | 10 | def pos = new PurchaseOrderService() |
---|
| 11 | |
---|
| 12 | protected void setUp() { |
---|
| 13 | super.setUp() |
---|
| 14 | mockDomain(PurchaseOrder) |
---|
| 15 | mockDomain(PurchaseOrderNumber) |
---|
| 16 | mockDomain(Supplier) |
---|
| 17 | mockDomain(SupplierType) |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | protected void tearDown() { |
---|
| 21 | super.tearDown() |
---|
| 22 | } |
---|
| 23 | |
---|
[898] | 24 | void testSavePurchaseOrderNumberExpectCommit() { |
---|
| 25 | mockForTransaction(PurchaseOrderNumber) |
---|
| 26 | |
---|
| 27 | def p = [value: "P100"] |
---|
| 28 | def r = pos.savePurchaseOrderNumber(p) |
---|
| 29 | |
---|
| 30 | assert ! r.error |
---|
| 31 | assert PurchaseOrderNumber.count() == 1 |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | void testSavePurchaseOrderNumberExpectRollback() { |
---|
| 35 | mockForTransaction(PurchaseOrderNumber, true) |
---|
| 36 | |
---|
| 37 | def p = [value: ""] |
---|
| 38 | def r = pos.savePurchaseOrderNumber(p) |
---|
| 39 | |
---|
| 40 | assert r.error |
---|
| 41 | assert PurchaseOrderNumber.count() == 0 |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void testSavePurchaseOrderNumberRangeExpectCommit() { |
---|
| 45 | mockForTransaction(PurchaseOrderNumber) |
---|
| 46 | |
---|
| 47 | def r = pos.savePurchaseOrderNumberRange("P", 1000, 1009) |
---|
| 48 | |
---|
| 49 | assert ! r.error |
---|
| 50 | assert PurchaseOrderNumber.count() == 10 |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void testSavePurchaseOrderNumberRangeExpectRollback() { |
---|
| 54 | mockForTransaction(PurchaseOrderNumber, true) |
---|
| 55 | |
---|
| 56 | def r = pos.savePurchaseOrderNumberRange("P", -1000, 1009) |
---|
| 57 | |
---|
| 58 | assert r.error |
---|
| 59 | assert PurchaseOrderNumber.count() == 0 |
---|
| 60 | } |
---|
| 61 | |
---|
[891] | 62 | void testFindsFirstUnusedPurchaseOrderWhenAllUnused() { |
---|
[898] | 63 | mockForTransaction(PurchaseOrderNumber) |
---|
| 64 | pos.savePurchaseOrderNumberRange("P", 1000, 1009) |
---|
[891] | 65 | |
---|
| 66 | def next = pos.findNextUnusedPurchaseOrderNumber() |
---|
| 67 | |
---|
| 68 | assertThat next.value, equalTo("P1000") |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void testFindsFirstUnusuedPurchaseOrderWhenSomeUsed() { |
---|
[898] | 72 | mockForTransaction(PurchaseOrderNumber) |
---|
| 73 | pos.savePurchaseOrderNumberRange("P", 1000, 1009) |
---|
[891] | 74 | createPurchaseOrders(4) |
---|
| 75 | |
---|
| 76 | def next = pos.findNextUnusedPurchaseOrderNumber() |
---|
| 77 | |
---|
| 78 | assertThat next.value, equalTo("P1004") |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void testFindsNullIfNoUnusedPurchaseOrderNumbers() { |
---|
[898] | 82 | mockForTransaction(PurchaseOrderNumber) |
---|
| 83 | pos.savePurchaseOrderNumberRange("P", 1000, 1009) |
---|
[891] | 84 | createPurchaseOrders(10) |
---|
| 85 | |
---|
| 86 | def next = pos.findNextUnusedPurchaseOrderNumber() |
---|
| 87 | |
---|
| 88 | assertThat next, equalTo(null) |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void testGetOrCreatePurchaseOrderWithExistingOrder() { |
---|
[898] | 92 | mockForTransaction(PurchaseOrderNumber) |
---|
| 93 | pos.savePurchaseOrderNumberRange("P", 1000, 1009) |
---|
[891] | 94 | createPurchaseOrders(3) |
---|
| 95 | def params=[purchaseOrderNumber:[id:2]] |
---|
| 96 | |
---|
| 97 | def po = pos.getOrCreatePurchaseOrder(params) |
---|
| 98 | |
---|
[896] | 99 | assertThat po.comment, equalTo("Created by test") |
---|
[891] | 100 | assertThat po.purchaseOrderNumber.value, equalTo("P1001") |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void testGetOrCreatePurchaseOrderWithNoExistingOrder() { |
---|
[898] | 104 | mockForTransaction(PurchaseOrderNumber) |
---|
| 105 | pos.savePurchaseOrderNumberRange("P", 1000, 1009) |
---|
[891] | 106 | createPurchaseOrders(3) |
---|
| 107 | createSuppliers(1) |
---|
| 108 | def params=[purchaseOrderNumber:[id:4],supplier:[id:1]] |
---|
| 109 | |
---|
| 110 | def po = pos.getOrCreatePurchaseOrder(params) |
---|
| 111 | |
---|
[896] | 112 | assertThat po.comment, equalTo("") |
---|
[891] | 113 | assertThat po.purchaseOrderNumber.value, equalTo("P1003") |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void testFindsDraftPurchaseOrderNumbers() { |
---|
[898] | 117 | mockForTransaction(PurchaseOrderNumber) |
---|
| 118 | pos.savePurchaseOrderNumberRange("P", 1000, 1009) |
---|
[891] | 119 | createPurchaseOrders(3) |
---|
| 120 | releaseOrder(PurchaseOrder.get(2)) |
---|
| 121 | |
---|
| 122 | def drafts = pos.findDraftPurchaseOrderNumbers() |
---|
| 123 | |
---|
| 124 | assertThat drafts.size(), equalTo(2) |
---|
| 125 | assertThat drafts[0].value, equalTo("P1000") |
---|
| 126 | assertThat drafts[1].value, equalTo("P1002") |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | def createPurchaseOrders(int howMany) { |
---|
| 132 | for (int i=0; i<howMany; i++) { |
---|
[896] | 133 | def po = new PurchaseOrder(comment:"Created by test", supplier:new Supplier()) |
---|
[891] | 134 | def pon = PurchaseOrderNumber.list()[i] |
---|
| 135 | pon.purchaseOrder = po; |
---|
| 136 | po.purchaseOrderNumber = pon; |
---|
| 137 | po.save(failOnError:true) |
---|
| 138 | pon.save(failOnError:true) |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | def createSuppliers(int howMany) { |
---|
| 143 | for (int i=0; i<howMany; i++) { |
---|
| 144 | def supplier = new Supplier(name:"Supplier ${i}", supplierType:new SupplierType()) |
---|
| 145 | supplier.save(failOnError:true) |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | def releaseOrder(PurchaseOrder po) { |
---|
| 150 | po.ordered = new Date() |
---|
| 151 | po.save(failOnError:true) |
---|
| 152 | } |
---|
| 153 | } |
---|