Skip to content

Commit

Permalink
add preliminary response generation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TuhinNair committed Dec 7, 2020
1 parent 31f5169 commit 7a4ae21
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
62 changes: 61 additions & 1 deletion cmd/web/bot_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import "testing"
import (
"os"
"testing"

"github.com/TuhinNair/durcov"
)

func TestBotMatchRequest(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -109,3 +114,58 @@ func TestBotMatchRequest(t *testing.T) {
}
}
}

func TestBotResponseGeneration(t *testing.T) {
dbURL := os.Getenv("TEST_DATABASE_URL")
pool, err := durcov.GetPgxPool(dbURL)
if err != nil {
t.Fatal(err)
}
defer pool.Close()

dataStore := &durcov.CovidDataStore{}
dataStore.SetDBConnection(pool)

exampleData, err := durcov.ExampleTestData()
if err != nil {
t.Fatal(err)
}
err = dataStore.StoreData(exampleData)
if err != nil {
t.Fatal(err)
}

dataView := &durcov.CovidBotView{}
dataView.SetDBConnection(pool)

testBot := &Bot{dataView}

tests := []struct {
input string
expected string
}{
{
"CASES TOTAL",
"Total Active Cases: 9000000",
},
{
"CASES AF",
"AF Active Cases: 8132",
},
{
"DEATHS TOTAL",
"Total Deaths: 500000",
},
{
"DEATHS SG",
"SG Deaths: 1822",
},
}

for _, test := range tests {
response := testBot.respond(test.input)
if response != test.expected {
t.Errorf("Response mismatch. Expected=%s Got=%s", test.expected, response)
}
}
}
2 changes: 1 addition & 1 deletion dataStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestDataStore(t *testing.T) {
dataStore := CovidDataStore{}
dataStore.SetDBConnection(pool)

exampleData, err := exampleTestData()
exampleData, err := ExampleTestData()
if err != nil {
t.Error(err)
}
Expand Down
9 changes: 6 additions & 3 deletions dataView_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ func TestDataView(t *testing.T) {
dataStore := CovidDataStore{}
dataStore.SetDBConnection(pool)

exampleData, err := exampleTestData()
exampleData, err := ExampleTestData()
if err != nil {
t.Error(err)
t.Fatal(err)
}
err = dataStore.StoreData(exampleData)
if err != nil {
t.Fatal(err)
}
dataStore.StoreData(exampleData)

dataView := CovidBotView{}
dataView.SetDBConnection(pool)
Expand Down
4 changes: 3 additions & 1 deletion testUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package durcov

import "time"

func exampleTestData() (*Data, error) {
// ExampleTestData returns a mock Data struct with global data and data for 2 countries.
// Inteneded as a testing utility.
func ExampleTestData() (*Data, error) {
exampleTime, err := time.Parse(time.RFC3339, "2020-12-04T03:49:29Z")
if err != nil {
return nil, err
Expand Down

0 comments on commit 7a4ae21

Please sign in to comment.