/
JpaInvoiceSpec.scala
85 lines (66 loc) · 2.6 KB
/
JpaInvoiceSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.zilverline.examples.immutabledomain.jpa
import org.specs.Specification
import JpaSupport._
import javax.persistence.PersistenceException
import java.math.BigDecimal
import org.joda.time.{DateTimeUtils, LocalDate}
import org.specs.runner.JUnit4
class JpaInvoiceTest extends JUnit4(JpaInvoiceSpec)
object JpaInvoiceSpec extends Specification {
implicit val emf = JpaSupport.entityManagerFactory
implicit def string2BigDecimal(amount: String) = new BigDecimal(amount)
doBeforeSpec {
DateTimeUtils.setCurrentMillisFixed(new LocalDate(2011, 1, 29).toDateTimeAtCurrentTime.getMillis)
}
doAfterSpec {
DateTimeUtils.setCurrentMillisSystem
}
"draft invoice" should {
val invoice = new Invoice
"be persisted" in {
invoice.recipient must beEqualTo(None)
invoice.recipient_? must beFalse
invoice.items_? must beFalse
withEntityManager(_.persist(invoice)) mustNot throwA[PersistenceException]
}
"become ready to send when recipient and at least one item are specified" in {
invoice.recipient = Some("Erik")
invoice.addItem("Programming Scala", "42.95")
invoice.recipient_? must beTrue
invoice.items_? must beTrue
invoice.readyToSend_? must beTrue
invoice.sentDate must beNull
invoice.dueDate must beNull
invoice.totalAmount must beEqualTo("42.95": BigDecimal)
withEntityManager(_.persist(invoice)) mustNot throwA[PersistenceException]
invoice.removeItem(0)
invoice.readyToSend_? must beFalse
withEntityManager(_.merge(invoice)) mustNot throwA[PersistenceException]
}
}
"sent invoice" should {
val invoice = new Invoice
invoice.recipient = Some("Erik")
invoice.addItem("Programming Scala", "42.95")
invoice.send
"have sent and due date" in {
invoice.sentDate must beEqualTo(new LocalDate(2011, 1, 29))
invoice.dueDate must beEqualTo(new LocalDate(2011, 2, 12))
invoice.readyToSend_? must beFalse
}
"be marked as paid when paid" in {
invoice.paymentReceived
invoice.paid_? must beTrue
invoice.paymentDate must beEqualTo(new LocalDate(2011, 1, 29))
withEntityManager(_.persist(invoice)) mustNot throwA[PersistenceException]
}
"be late when dueDate is in the past" in {
invoice.late_? must beFalse
DateTimeUtils.setCurrentMillisFixed(new LocalDate(2011, 2, 20).toDateTimeAtStartOfDay.getMillis)
invoice.late_? must beTrue
invoice.remind
invoice.reminderDate must beEqualTo(new LocalDate(2011, 2, 20))
withEntityManager(_.persist(invoice)) mustNot throwA[PersistenceException]
}
}
}