Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Sped up tests by clearing the tables instead of deleting, tests from …
Browse files Browse the repository at this point in the history
…45 seconds to ~1.2 seconds
  • Loading branch information
nylar committed Jan 12, 2015
1 parent f1ad925 commit eadd589
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions crawl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestCrawl_grabURL(t *testing.T) {
}

func TestCrawl_Crawler(t *testing.T) {
DatabaseRebuild(session)
defer tearDbDown(session)

data := []byte("really cool stuff")
ts := Handler(200, data)
Expand All @@ -38,7 +38,7 @@ func TestCrawl_Crawler(t *testing.T) {
}

func TestCrawl_CrawlerNoURL(t *testing.T) {
DatabaseRebuild(session)
defer tearDbDown(session)

err := Crawler("", session)
assert.Error(t, err)
Expand Down
23 changes: 18 additions & 5 deletions indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,21 @@ func init() {
}

// Reset database
rdb.DbDrop(Conf.Database.Name).Exec(session)
rdb.DbCreate(Conf.Database.Name).Exec(session)
rdb.Db(Conf.Database.Name).TableCreate(Conf.Tables.DocumentTable).Exec(session)
rdb.Db(Conf.Database.Name).TableCreate(Conf.Tables.IndexTable).Exec(session)
rdb.Db(Conf.Database.Name).Table(Conf.Tables.IndexTable).IndexCreate("word").Exec(session)
}

func tearDbDown(session *rdb.Session) {
rdb.Db(Conf.Database.Name).Table(Conf.Tables.DocumentTable).Delete(rdb.DeleteOpts{
Durability: "soft",
ReturnChanges: false,
}).Exec(session)
rdb.Db(Conf.Database.Name).Table(Conf.Tables.IndexTable).Delete(rdb.DeleteOpts{
Durability: "soft",
ReturnChanges: false,
}).Exec(session)
}

func TestIndexer_Stopper(t *testing.T) {
Expand Down Expand Up @@ -164,7 +177,7 @@ func TestIndexer_IndexString(t *testing.T) {
}

func TestIndexer_IndexPut(t *testing.T) {
DatabaseRebuild(session)
defer tearDbDown(session)

index := Index{Word: "hello", Count: 5, DocumentID: "12345-67890-ABCDE"}

Expand All @@ -185,7 +198,7 @@ func TestIndexer_IndexPut(t *testing.T) {
}

func TestIndexer_IndexPutInvalid(t *testing.T) {
DatabaseRebuild(session)
defer tearDbDown(session)

i := Index{ID: "1"}
i2 := Index{ID: "1"}
Expand Down Expand Up @@ -221,7 +234,7 @@ func TestIndexer_DocumentString(t *testing.T) {
}

func TestIndexer_DocumentPut(t *testing.T) {
DatabaseRebuild(session)
defer tearDbDown(session)

doc := Document{
Source: "www.google.com",
Expand All @@ -244,7 +257,7 @@ func TestIndexer_DocumentPut(t *testing.T) {
}

func TestIndexer_DocumentPutDupeDocs(t *testing.T) {
DatabaseRebuild(session)
defer tearDbDown(session)

doc1 := Document{ID: "1"}
doc2 := Document{ID: "1"}
Expand Down
9 changes: 6 additions & 3 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ func tearDown() {
}

func SearchSetup() error {
DatabaseRebuild(session)

d1 := Document{
ID: "1",
Source: "http://example.com",
Expand Down Expand Up @@ -59,6 +57,8 @@ func SearchSetup() error {
}

func TestSearch_Search(t *testing.T) {
defer tearDbDown(session)

setUp(1)
if err := SearchSetup(); err != nil {
t.Errorf(err.Error())
Expand All @@ -74,6 +74,8 @@ func TestSearch_Search(t *testing.T) {
}

func TestSearch_SearchNumberOfResults(t *testing.T) {
defer tearDbDown(session)

if err := SearchSetup(); err != nil {
t.Errorf(err.Error())
}
Expand All @@ -99,7 +101,8 @@ func TestSearch_SearchNumberOfResultsNoIndex(t *testing.T) {
}

func TestSearch_SearchWithNoIndex(t *testing.T) {
DatabaseRebuild(session)
defer tearDbDown(session)

rdb.Db(Conf.Database.Name).Table(Conf.Tables.IndexTable).IndexDrop("word").Exec(session)

_, err := Search("hello", session, 1)
Expand Down
10 changes: 8 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ var (
// DatabaseRebuild resets the database to an empty state, it also sets the
// secondary index for the index table.
func DatabaseRebuild(session *rdb.Session) {
rdb.Db(Conf.Database.Name).TableDrop(Conf.Tables.DocumentTable).Exec(session)
rdb.Db(Conf.Database.Name).TableDrop(Conf.Tables.IndexTable).Exec(session)
rdb.Db(Conf.Database.Name).TableCreate(Conf.Tables.DocumentTable).Exec(session)
rdb.Db(Conf.Database.Name).TableCreate(Conf.Tables.IndexTable).Exec(session)
rdb.Db(Conf.Database.Name).Table(Conf.Tables.IndexTable).IndexCreate("word").Exec(session)
rdb.Db(Conf.Database.Name).Table(Conf.Tables.DocumentTable).Delete(rdb.DeleteOpts{
Durability: "soft",
ReturnChanges: false,
}).Exec(session)
rdb.Db(Conf.Database.Name).Table(Conf.Tables.IndexTable).Delete(rdb.DeleteOpts{
Durability: "soft",
ReturnChanges: false,
}).Exec(session)
}

// ToString converts an interface{} to a string, a string, byte slice or integer
Expand Down

0 comments on commit eadd589

Please sign in to comment.