source: branches/features/purchaseOrders/test/unit/PurchaseOrderServiceTests.groovy @ 954

Last change on this file since 954 was 898, checked in by gav, 14 years ago

Add savePurchaseOrderNumber() and savePurchaseOrderNumberRange() to PurchaseOrderService.

File size: 4.5 KB
Line 
1import grails.test.*
2import com.henyo.BaseUnitTestCase
3import static org.junit.Assert.assertThat
4import static org.hamcrest.CoreMatchers.equalTo
5
6/**
7 * Unit tests for PurchaseOrderService class.
8 */
9class PurchaseOrderServiceTests extends BaseUnitTestCase {
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
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
62    void testFindsFirstUnusedPurchaseOrderWhenAllUnused() {
63        mockForTransaction(PurchaseOrderNumber)
64        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
65
66        def next = pos.findNextUnusedPurchaseOrderNumber()
67
68        assertThat next.value, equalTo("P1000")
69    }
70
71    void testFindsFirstUnusuedPurchaseOrderWhenSomeUsed() {
72        mockForTransaction(PurchaseOrderNumber)
73        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
74        createPurchaseOrders(4)
75
76        def next = pos.findNextUnusedPurchaseOrderNumber()
77
78        assertThat next.value, equalTo("P1004")
79    }
80
81    void testFindsNullIfNoUnusedPurchaseOrderNumbers() {
82        mockForTransaction(PurchaseOrderNumber)
83        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
84        createPurchaseOrders(10)
85
86        def next = pos.findNextUnusedPurchaseOrderNumber()
87
88        assertThat next, equalTo(null)
89    }
90
91    void testGetOrCreatePurchaseOrderWithExistingOrder() {
92        mockForTransaction(PurchaseOrderNumber)
93        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
94        createPurchaseOrders(3)
95        def params=[purchaseOrderNumber:[id:2]]
96
97        def po = pos.getOrCreatePurchaseOrder(params)
98
99        assertThat po.comment, equalTo("Created by test")
100        assertThat po.purchaseOrderNumber.value, equalTo("P1001")
101    }
102
103    void testGetOrCreatePurchaseOrderWithNoExistingOrder() {
104        mockForTransaction(PurchaseOrderNumber)
105        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
106        createPurchaseOrders(3)
107        createSuppliers(1)
108        def params=[purchaseOrderNumber:[id:4],supplier:[id:1]]
109
110        def po = pos.getOrCreatePurchaseOrder(params)
111
112        assertThat po.comment, equalTo("")
113        assertThat po.purchaseOrderNumber.value, equalTo("P1003")
114    }
115
116    void testFindsDraftPurchaseOrderNumbers() {
117        mockForTransaction(PurchaseOrderNumber)
118        pos.savePurchaseOrderNumberRange("P", 1000, 1009)
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++) {
133            def po = new PurchaseOrder(comment:"Created by test", supplier:new Supplier())
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}
Note: See TracBrowser for help on using the repository browser.