-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add popTxs test #61
add popTxs test #61
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package tests_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/BOPR/common" | ||
"github.com/BOPR/config" | ||
"github.com/BOPR/core" | ||
"github.com/BOPR/migrations" | ||
"github.com/jinzhu/gorm" | ||
_ "github.com/jinzhu/gorm/dialects/sqlite" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func setupDB() (db core.DB, cleanup func(), err error) { | ||
tmpfile, err := ioutil.TempFile("", "test.*.db") | ||
if err != nil { | ||
return | ||
} | ||
|
||
sqliteDb, err := gorm.Open("sqlite3", tmpfile.Name()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I eventually use SQLite because the setup is much easier than installing another docker instance in CI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very nice! |
||
if err != nil { | ||
return | ||
} | ||
logger := common.Logger.With("module", "tests") | ||
db = core.DB{Instance: sqliteDb, Logger: logger} | ||
|
||
allMigrations := migrations.GetMigrations() | ||
m := migrations.NewGormigrate(db.Instance, migrations.DefaultOptions, allMigrations) | ||
m.Migrate() | ||
cleanup = func() { | ||
db.Close() | ||
os.Remove(tmpfile.Name()) | ||
} | ||
|
||
return db, cleanup, nil | ||
|
||
} | ||
|
||
func TestPopTx(t *testing.T) { | ||
db, cleanup, err := setupDB() | ||
if err != nil { | ||
t.Errorf("setupDB error %s", err) | ||
} | ||
defer cleanup() | ||
|
||
var txType uint64 = 1 | ||
config.GlobalCfg.TxsPerBatch = 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have to override this global variable to work. |
||
|
||
tx1 := core.NewTx(1, 2, txType, []byte{00}, []byte{00}) | ||
tx2 := core.NewPendingTx(1, 2, txType, []byte{00}, []byte{01}) | ||
tx3 := core.NewPendingTx(1, 2, txType, []byte{00}, []byte{02}) | ||
|
||
if err = db.InsertTx(&tx1); err != nil { | ||
t.Errorf("PopTxs error %s", err) | ||
} | ||
if err = db.InsertTx(&tx2); err != nil { | ||
t.Errorf("PopTxs error %s", err) | ||
} | ||
if err = db.InsertTx(&tx3); err != nil { | ||
t.Errorf("PopTxs error %s", err) | ||
} | ||
|
||
fetchedTxType, err := db.FetchTxType() | ||
assert.Equal(t, txType, fetchedTxType) | ||
|
||
txs, err := db.PopTxs() | ||
if err != nil { | ||
t.Errorf("PopTxs error %s", err) | ||
} | ||
for i, tx := range []core.Tx{tx2, tx3} { | ||
assert.Equal(t, tx.TxHash, txs[i].TxHash) | ||
} | ||
Comment on lines
+73
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really want to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, I think we should have something like TxList that has a What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have something like TxList means we are doing pending Txs in memory, not in disk/database? Let's move the discussion here #63 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to a
tests/
package, because Golang complains a circular dependency betweenmigration/
andcore
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, these are anyways exported functions.