2017-11-02 19:37:07 +00:00
|
|
|
package observers_test
|
2017-10-25 15:57:59 +00:00
|
|
|
|
|
|
|
import (
|
2017-11-02 22:37:27 +00:00
|
|
|
"runtime"
|
|
|
|
|
2017-11-01 15:17:01 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/config"
|
2017-10-25 15:57:59 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/core"
|
2017-11-02 19:37:07 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/observers"
|
2017-11-03 13:01:35 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/repositories"
|
2017-10-25 15:57:59 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2017-11-02 22:37:27 +00:00
|
|
|
var (
|
|
|
|
_, filename, _, _ = runtime.Caller(0)
|
|
|
|
)
|
|
|
|
|
2017-10-25 15:57:59 +00:00
|
|
|
var _ = Describe("Saving blocks to the database", func() {
|
|
|
|
|
|
|
|
var db *sqlx.DB
|
|
|
|
var err error
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2017-11-02 22:37:27 +00:00
|
|
|
cfg := config.NewConfig("private")
|
|
|
|
pgConfig := config.DbConnectionString(cfg.Database)
|
2017-10-25 15:57:59 +00:00
|
|
|
db, err = sqlx.Connect("postgres", pgConfig)
|
2017-10-31 13:58:04 +00:00
|
|
|
db.MustExec("DELETE FROM transactions")
|
2017-10-31 15:36:37 +00:00
|
|
|
db.MustExec("DELETE FROM blocks")
|
2017-10-25 15:57:59 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 15:17:01 +00:00
|
|
|
AfterEach(func() {
|
|
|
|
db.Close()
|
|
|
|
})
|
|
|
|
|
2017-10-25 15:57:59 +00:00
|
|
|
It("implements the observer interface", func() {
|
2017-11-02 19:37:07 +00:00
|
|
|
var observer core.BlockchainObserver = observers.BlockchainDBObserver{Db: db}
|
2017-10-25 15:57:59 +00:00
|
|
|
Expect(observer).NotTo(BeNil())
|
|
|
|
})
|
|
|
|
|
2017-11-03 13:01:35 +00:00
|
|
|
It("saves a block with one transaction", func() {
|
2017-11-02 14:36:53 +00:00
|
|
|
block := core.Block{
|
2017-11-03 13:01:35 +00:00
|
|
|
Number: 123,
|
|
|
|
Transactions: []core.Transaction{{}},
|
2017-11-02 14:36:53 +00:00
|
|
|
}
|
2017-10-25 15:57:59 +00:00
|
|
|
|
2017-11-02 19:37:07 +00:00
|
|
|
observer := observers.BlockchainDBObserver{Db: db}
|
2017-10-25 15:57:59 +00:00
|
|
|
observer.NotifyBlockAdded(block)
|
|
|
|
|
2017-11-03 13:01:35 +00:00
|
|
|
repository := repositories.NewPostgres(db)
|
|
|
|
savedBlock := repository.FindBlockByNumber(123)
|
|
|
|
Expect(savedBlock).NotTo(BeNil())
|
|
|
|
Expect(len(savedBlock.Transactions)).To(Equal(1))
|
2017-10-31 13:58:04 +00:00
|
|
|
})
|
|
|
|
|
2017-10-25 15:57:59 +00:00
|
|
|
})
|