~rogpeppe/juju-core/254-txn-tester

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
package state

import (
	"labix.org/v2/mgo/txn"
	"launchpad.net/juju-core/testing/txntest"
)

// This file holds the code that adapts a State to be
// used as a txntest.DB.

type TxnState State

func (st *TxnState) Run(t txntest.Transaction) error {
	return st.runTxn(t.([]txn.Op))
}

func (st *TxnState) Clone(runTxn func(txntest.Transaction) error) txntest.DB {
	st1 := *st
	st1.runTxn = func(ops []txn.Op) error {
		return runTxn(ops)
	}
	return &st1
}

var _ txntest.DB = (*TxnState)(nil)

func TxnTest(reset func() *State, validate func() error, ops ...func(st *State) error) txntest.Result {
	reset1 := func() txntest.DB {
		st := reset()
		return (*TxnState)(st)
	}
	ops1 := make([]func(txntest.DB) error, len(ops))
	for i, op := range ops {
		op := op
		ops1[i] = func(db txntest.DB) error {
			st := (*State)(db.(*TxnState))
			return op(st)
		}
	}
	return txntest.Run(txntest.Params{
		Reset:    reset1,
		Validate: validate,
		Ops:      ops1,
	})
}