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