Changeset 898 for branches


Ignore:
Timestamp:
Apr 22, 2011, 5:18:04 PM (14 years ago)
Author:
gav
Message:

Add savePurchaseOrderNumber() and savePurchaseOrderNumberRange() to PurchaseOrderService.

Location:
branches/features/purchaseOrders
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/features/purchaseOrders/grails-app/services/CreateDataService.groovy

    r893 r898  
    1717    def appConfigService
    1818    def searchableService
     19    def purchaseOrderService
    1920    def inventoryItemService
    2021    def assignedGroupService
     
    17401741
    17411742    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)
    17451744    }
    17461745
  • branches/features/purchaseOrders/grails-app/services/PurchaseOrderService.groovy

    r897 r898  
     1/**
     2 * Provides a service class for the PurchaseOrder and PurchaseOrderNumber domain classes.
     3 */
    14class PurchaseOrderService {
    25
    36    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    }
    443
    544    PurchaseOrderNumber findNextUnusedPurchaseOrderNumber() {
     
    3776        return drafts
    3877    }
     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
    39101}
  • branches/features/purchaseOrders/test/unit/PurchaseOrderServiceTests.groovy

    r896 r898  
    11import grails.test.*
     2import com.henyo.BaseUnitTestCase
    23import static org.junit.Assert.assertThat
    34import static org.hamcrest.CoreMatchers.equalTo
     
    67 * Unit tests for PurchaseOrderService class.
    78 */
    8 class PurchaseOrderServiceTests extends GrailsUnitTestCase {
     9class PurchaseOrderServiceTests extends BaseUnitTestCase {
    910    def pos = new PurchaseOrderService()
    1011
     
    2122    }
    2223
     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
    2362    void testFindsFirstUnusedPurchaseOrderWhenAllUnused() {
    24         createTenPurchaseOrderNumbers()
    25         assertThat PurchaseOrderNumber.list().size(), equalTo(10)
     63        mockForTransaction(PurchaseOrderNumber)
     64        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
    2665
    2766        def next = pos.findNextUnusedPurchaseOrderNumber()
     
    3170
    3271    void testFindsFirstUnusuedPurchaseOrderWhenSomeUsed() {
    33         createTenPurchaseOrderNumbers()
     72        mockForTransaction(PurchaseOrderNumber)
     73        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
    3474        createPurchaseOrders(4)
    3575
     
    4080
    4181    void testFindsNullIfNoUnusedPurchaseOrderNumbers() {
    42         createTenPurchaseOrderNumbers()
     82        mockForTransaction(PurchaseOrderNumber)
     83        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
    4384        createPurchaseOrders(10)
    4485
     
    4990
    5091    void testGetOrCreatePurchaseOrderWithExistingOrder() {
    51         createTenPurchaseOrderNumbers()
     92        mockForTransaction(PurchaseOrderNumber)
     93        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
    5294        createPurchaseOrders(3)
    5395        def params=[purchaseOrderNumber:[id:2]]
     
    60102
    61103    void testGetOrCreatePurchaseOrderWithNoExistingOrder() {
    62         createTenPurchaseOrderNumbers()
     104        mockForTransaction(PurchaseOrderNumber)
     105        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
    63106        createPurchaseOrders(3)
    64107        createSuppliers(1)
     
    72115
    73116    void testFindsDraftPurchaseOrderNumbers() {
    74         createTenPurchaseOrderNumbers()
     117        mockForTransaction(PurchaseOrderNumber)
     118        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
    75119        createPurchaseOrders(3)
    76120        releaseOrder(PurchaseOrder.get(2))
     
    103147    }
    104148
    105     private createTenPurchaseOrderNumbers() {
    106         for (int i = 1000; i < 1010; i++) {
    107             new PurchaseOrderNumber(value: "P${i}").save(failOnError: true)
    108         }
    109     }
    110 
    111149    def releaseOrder(PurchaseOrder po) {
    112150        po.ordered = new Date()
Note: See TracChangeset for help on using the changeset viewer.