From 7a4ae21b806d043a458311b1b9f0d5376baa8e83 Mon Sep 17 00:00:00 2001 From: Tuhin Nair Date: Mon, 7 Dec 2020 11:23:25 +0530 Subject: [PATCH] add preliminary response generation tests --- cmd/web/bot_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++- dataStore_test.go | 2 +- dataView_test.go | 9 ++++--- testUtil.go | 4 ++- 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/cmd/web/bot_test.go b/cmd/web/bot_test.go index e697b70..6cf6e48 100644 --- a/cmd/web/bot_test.go +++ b/cmd/web/bot_test.go @@ -1,6 +1,11 @@ package main -import "testing" +import ( + "os" + "testing" + + "github.com/TuhinNair/durcov" +) func TestBotMatchRequest(t *testing.T) { tests := []struct { @@ -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) + } + } +} diff --git a/dataStore_test.go b/dataStore_test.go index 014c81e..411d394 100644 --- a/dataStore_test.go +++ b/dataStore_test.go @@ -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) } diff --git a/dataView_test.go b/dataView_test.go index d5d9c8f..e934de6 100644 --- a/dataView_test.go +++ b/dataView_test.go @@ -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) diff --git a/testUtil.go b/testUtil.go index 9deaa58..91dde95 100644 --- a/testUtil.go +++ b/testUtil.go @@ -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