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

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

Domain change, rename PurchaseOrder.comment to singular, set maxSize constraint and init with blank string.

File size: 3.3 KB
Line 
1import grails.test.*
2import static org.junit.Assert.assertThat
3import static org.hamcrest.CoreMatchers.equalTo
4
5/**
6 * Unit tests for PurchaseOrderService class.
7 */
8class PurchaseOrderServiceTests extends GrailsUnitTestCase {
9    def pos = new PurchaseOrderService()
10
11    protected void setUp() {
12        super.setUp()
13        mockDomain(PurchaseOrder)
14        mockDomain(PurchaseOrderNumber)
15        mockDomain(Supplier)
16        mockDomain(SupplierType)
17    }
18
19    protected void tearDown() {
20        super.tearDown()
21    }
22
23    void testFindsFirstUnusedPurchaseOrderWhenAllUnused() {
24        createTenPurchaseOrderNumbers()
25        assertThat PurchaseOrderNumber.list().size(), equalTo(10)
26
27        def next = pos.findNextUnusedPurchaseOrderNumber()
28
29        assertThat next.value, equalTo("P1000")
30    }
31
32    void testFindsFirstUnusuedPurchaseOrderWhenSomeUsed() {
33        createTenPurchaseOrderNumbers()
34        createPurchaseOrders(4)
35
36        def next = pos.findNextUnusedPurchaseOrderNumber()
37
38        assertThat next.value, equalTo("P1004")
39    }
40
41    void testFindsNullIfNoUnusedPurchaseOrderNumbers() {
42        createTenPurchaseOrderNumbers()
43        createPurchaseOrders(10)
44
45        def next = pos.findNextUnusedPurchaseOrderNumber()
46
47        assertThat next, equalTo(null)
48    }
49
50    void testGetOrCreatePurchaseOrderWithExistingOrder() {
51        createTenPurchaseOrderNumbers()
52        createPurchaseOrders(3)
53        def params=[purchaseOrderNumber:[id:2]]
54
55        def po = pos.getOrCreatePurchaseOrder(params)
56
57        assertThat po.comment, equalTo("Created by test")
58        assertThat po.purchaseOrderNumber.value, equalTo("P1001")
59    }
60
61    void testGetOrCreatePurchaseOrderWithNoExistingOrder() {
62        createTenPurchaseOrderNumbers()
63        createPurchaseOrders(3)
64        createSuppliers(1)
65        def params=[purchaseOrderNumber:[id:4],supplier:[id:1]]
66
67        def po = pos.getOrCreatePurchaseOrder(params)
68
69        assertThat po.comment, equalTo("")
70        assertThat po.purchaseOrderNumber.value, equalTo("P1003")
71    }
72
73    void testFindsDraftPurchaseOrderNumbers() {
74        createTenPurchaseOrderNumbers()
75        createPurchaseOrders(3)
76        releaseOrder(PurchaseOrder.get(2))
77
78        def drafts = pos.findDraftPurchaseOrderNumbers()
79
80        assertThat drafts.size(), equalTo(2)
81        assertThat drafts[0].value, equalTo("P1000")
82        assertThat drafts[1].value, equalTo("P1002")
83    }
84
85
86
87    def createPurchaseOrders(int howMany) {
88        for (int i=0; i<howMany; i++) {
89            def po = new PurchaseOrder(comment:"Created by test", supplier:new Supplier())
90            def pon = PurchaseOrderNumber.list()[i]
91            pon.purchaseOrder = po;
92            po.purchaseOrderNumber = pon;
93            po.save(failOnError:true)
94            pon.save(failOnError:true)
95        }
96    }
97
98    def createSuppliers(int howMany) {
99        for (int i=0; i<howMany; i++) {
100            def supplier = new Supplier(name:"Supplier ${i}", supplierType:new SupplierType())
101            supplier.save(failOnError:true)
102        }
103    }
104
105    private createTenPurchaseOrderNumbers() {
106        for (int i = 1000; i < 1010; i++) {
107            new PurchaseOrderNumber(value: "P${i}").save(failOnError: true)
108        }
109    }
110
111    def releaseOrder(PurchaseOrder po) {
112        po.ordered = new Date()
113        po.save(failOnError:true)
114    }
115}
Note: See TracBrowser for help on using the repository browser.