Example #1
0
func main() {
	es := store.Empty()
	as := account.NewService(&es)
	ts := transfer.NewService(&es)

	as.StartListener()
	ts.StartListener()

	firstAccount := <-as.Act(account.OpenAccount{InitialBalance: 100})
	secondAccount := <-as.Act(account.OpenAccount{InitialBalance: 0})

	as.Act(account.Credit{Uuid: firstAccount, Amount: 300})
	transfer := ts.Act(transfer.CreateTransfer{firstAccount, secondAccount, 125})

	time.Sleep(1000 * time.Millisecond)

	fmt.Println("Accounts:")
	fmt.Println("---------")
	fmt.Println(<-as.Find(firstAccount))
	fmt.Println(<-as.Find(secondAccount))
	fmt.Println("Transfers:")
	fmt.Println("----------")
	fmt.Println(<-ts.Find(<-transfer))
	fmt.Println("Events:")
	fmt.Println("-------")
	page := <-es.Events(0, 100)
	for _, event := range page.Events {
		fmt.Println(event)
	}
}
func (s *MySuite) TestOpenAccount(c *C) {
	es := store.Empty()
	as := Service{&es}
	uuid := <-as.Act(OpenAccount{InitialBalance: 100})
	page := <-es.Events(0, 10)
	c.Assert(page.Events, HasLen, 1)
	history := <-es.Find(uuid)
	c.Assert(history.Events, HasLen, 1)
	c.Assert(history.Events[0], Equals, AccountOpened{Uuid: uuid, InitialBalance: 100})
	c.Assert(history.Version, Equals, 1)
}
func (s *MySuite) TestCreateTransfer(c *C) {
	es := store.Empty()
	ts := Service{&es}
	from, _ := NewV4()
	to, _ := NewV4()
	uuid := <-ts.Act(CreateTransfer{from, to, 100})
	page := <-es.Events(0, 10)
	c.Assert(page.Events, HasLen, 1)
	history := <-es.Find(uuid)
	c.Assert(history.Events, HasLen, 1)
	c.Assert(history.Events[0], Equals, TransferCreated{uuid, from, to, 100})
	c.Assert(history.Version, Equals, 1)
}
func (s *MySuite) TestDebitAccountOnTransfer(c *C) {
	es := store.Empty()
	as := Service{&es}
	uuid := <-as.Act(OpenAccount{InitialBalance: 100})
	transaction, _ := NewV4()
	otherAccount, _ := NewV4()
	<-as.Act(DebitOnTransfer{transaction, 50, uuid, otherAccount})
	page := <-es.Events(0, 10)
	c.Assert(page.Events, HasLen, 2)
	history := <-es.Find(uuid)
	c.Assert(history.Events, HasLen, 2)
	c.Assert(history.Events[0], Equals, AccountOpened{Uuid: uuid, InitialBalance: 100})
	c.Assert(history.Events[1], Equals, AccountDebitedOnTransfer{uuid, transaction, 50, uuid, otherAccount})
	c.Assert(history.Version, Equals, 2)
}
func (s *MySuite) TestHandleTransferCreatedEvent(c *C) {
	es := store.Empty()
	as := Service{&es}
	thisAccount := <-as.Act(OpenAccount{InitialBalance: 100})
	otherAccount, _ := NewV4()
	transaction, _ := NewV4()

	<-as.handleEvent(TransferCreated{transaction, thisAccount, otherAccount, 50})
	page := <-es.Events(0, 10)
	c.Assert(page.Events, HasLen, 2)

	history := <-es.Find(thisAccount)
	c.Assert(history.Events, HasLen, 2)
	c.Assert(history.Events[0], Equals, AccountOpened{Uuid: thisAccount, InitialBalance: 100})
	c.Assert(history.Events[1], Equals, AccountDebitedOnTransfer{thisAccount, transaction, 50, thisAccount, otherAccount})
}
func (s *MySuite) TestHandleAccountDebitedOnTransferEvent(c *C) {
	es := store.Empty()
	ts := Service{&es}
	from, _ := NewV4()
	to, _ := NewV4()

	uuid := <-ts.Act(CreateTransfer{from, to, 100})
	<-ts.handleEvent(AccountDebitedOnTransfer{from, uuid, 100, from, to})

	page := <-es.Events(0, 10)
	c.Assert(page.Events, HasLen, 2)
	history := <-es.Find(uuid)
	c.Assert(history.Events, HasLen, 2)
	c.Assert(history.Events[0], Equals, TransferCreated{uuid, from, to, 100})
	c.Assert(history.Events[1], Equals, TransferDebited{uuid, from, to, 100})
	c.Assert(history.Version, Equals, 2)
}