- Timestamp:
- Apr 22, 2011, 5:18:04 PM (14 years ago)
- Location:
- branches/features/purchaseOrders
- Files:
-
- 3 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/features/purchaseOrders/grails-app/services/CreateDataService.groovy
r893 r898 17 17 def appConfigService 18 18 def searchableService 19 def purchaseOrderService 19 20 def inventoryItemService 20 21 def assignedGroupService … … 1740 1741 1741 1742 def createDemoPurchaseOrderNumbers() { 1742 for (int i=10000; i<10100; i++) { 1743 saveAndTest(new PurchaseOrderNumber(value:"P${i}")) 1744 } 1743 def r = purchaseOrderService.savePurchaseOrderNumberRange("P", 10000, 10100) 1745 1744 } 1746 1745 -
branches/features/purchaseOrders/grails-app/services/PurchaseOrderService.groovy
r897 r898 1 /** 2 * Provides a service class for the PurchaseOrder and PurchaseOrderNumber domain classes. 3 */ 1 4 class PurchaseOrderService { 2 5 3 6 boolean transactional = false 7 8 def savePurchaseOrderNumberRange(prefix, startOfRange, endOfRange, suffix="") { 9 PurchaseOrderNumber.withTransaction { status -> 10 def result = [:] 11 12 def fail = { Map m -> 13 status.setRollbackOnly() 14 if(result.purchaseOrderNumber && m.field) 15 result.purchaseOrderNumber.errors.rejectValue(m.field, m.code) 16 result.error = [ code: m.code, args: ["PurchaseOrderNumber"] ] 17 return result 18 } 19 20 // Sanity checks. 21 if(startOfRange < 0) 22 return fail(code:"default.create.failure") 23 if(endOfRange < 0) 24 return fail(code:"default.create.failure") 25 // Auto swap range. 26 if(endOfRange < startOfRange) 27 (startOfRange, endOfRange) = [endOfRange, startOfRange] 28 29 def r 30 def p = [:] 31 for(i in startOfRange..endOfRange) { 32 p.value= "${prefix}${i}${suffix}" 33 r = savePurchaseOrderNumber(p) 34 if(r.error) 35 return fail(code: r.code) 36 37 } 38 39 // Success. 40 return result 41 } 42 } 4 43 5 44 PurchaseOrderNumber findNextUnusedPurchaseOrderNumber() { … … 37 76 return drafts 38 77 } 78 79 def savePurchaseOrderNumber(params) { 80 PurchaseOrderNumber.withTransaction { status -> 81 def result = [:] 82 83 def fail = { Map m -> 84 status.setRollbackOnly() 85 if(result.purchaseOrderNumber && m.field) 86 result.purchaseOrderNumber.errors.rejectValue(m.field, m.code) 87 result.error = [ code: m.code, args: ["PurchaseOrderNumber", params.id] ] 88 return result 89 } 90 91 result.purchaseOrderNumber = new PurchaseOrderNumber(params) 92 93 if(result.purchaseOrderNumber.hasErrors() || !result.purchaseOrderNumber.save()) 94 return fail(code:"default.create.failure") 95 96 // Success. 97 return result 98 } 99 } 100 39 101 } -
branches/features/purchaseOrders/test/unit/PurchaseOrderServiceTests.groovy
r896 r898 1 1 import grails.test.* 2 import com.henyo.BaseUnitTestCase 2 3 import static org.junit.Assert.assertThat 3 4 import static org.hamcrest.CoreMatchers.equalTo … … 6 7 * Unit tests for PurchaseOrderService class. 7 8 */ 8 class PurchaseOrderServiceTests extends GrailsUnitTestCase {9 class PurchaseOrderServiceTests extends BaseUnitTestCase { 9 10 def pos = new PurchaseOrderService() 10 11 … … 21 22 } 22 23 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 23 62 void testFindsFirstUnusedPurchaseOrderWhenAllUnused() { 24 createTenPurchaseOrderNumbers()25 assertThat PurchaseOrderNumber.list().size(), equalTo(10)63 mockForTransaction(PurchaseOrderNumber) 64 pos.savePurchaseOrderNumberRange("P", 1000, 1009) 26 65 27 66 def next = pos.findNextUnusedPurchaseOrderNumber() … … 31 70 32 71 void testFindsFirstUnusuedPurchaseOrderWhenSomeUsed() { 33 createTenPurchaseOrderNumbers() 72 mockForTransaction(PurchaseOrderNumber) 73 pos.savePurchaseOrderNumberRange("P", 1000, 1009) 34 74 createPurchaseOrders(4) 35 75 … … 40 80 41 81 void testFindsNullIfNoUnusedPurchaseOrderNumbers() { 42 createTenPurchaseOrderNumbers() 82 mockForTransaction(PurchaseOrderNumber) 83 pos.savePurchaseOrderNumberRange("P", 1000, 1009) 43 84 createPurchaseOrders(10) 44 85 … … 49 90 50 91 void testGetOrCreatePurchaseOrderWithExistingOrder() { 51 createTenPurchaseOrderNumbers() 92 mockForTransaction(PurchaseOrderNumber) 93 pos.savePurchaseOrderNumberRange("P", 1000, 1009) 52 94 createPurchaseOrders(3) 53 95 def params=[purchaseOrderNumber:[id:2]] … … 60 102 61 103 void testGetOrCreatePurchaseOrderWithNoExistingOrder() { 62 createTenPurchaseOrderNumbers() 104 mockForTransaction(PurchaseOrderNumber) 105 pos.savePurchaseOrderNumberRange("P", 1000, 1009) 63 106 createPurchaseOrders(3) 64 107 createSuppliers(1) … … 72 115 73 116 void testFindsDraftPurchaseOrderNumbers() { 74 createTenPurchaseOrderNumbers() 117 mockForTransaction(PurchaseOrderNumber) 118 pos.savePurchaseOrderNumberRange("P", 1000, 1009) 75 119 createPurchaseOrders(3) 76 120 releaseOrder(PurchaseOrder.get(2)) … … 103 147 } 104 148 105 private createTenPurchaseOrderNumbers() {106 for (int i = 1000; i < 1010; i++) {107 new PurchaseOrderNumber(value: "P${i}").save(failOnError: true)108 }109 }110 111 149 def releaseOrder(PurchaseOrder po) { 112 150 po.ordered = new Date()
Note: See TracChangeset
for help on using the changeset viewer.