From 9dbd04eed85e6fae4c54b0d71515e120df1c6351 Mon Sep 17 00:00:00 2001 From: Junwei Zhao Date: Mon, 2 Mar 2020 23:46:44 +1100 Subject: [PATCH] Add test cases --- README.md | 1 + main.go | 9 +++++--- search/app.go | 7 +++--- search/app_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 search/app_test.go diff --git a/README.md b/README.md index 8493f09..4d93865 100644 --- a/README.md +++ b/README.md @@ -91,3 +91,4 @@ Last but not least, the `Search()` method will delegate the call the one `Search - Reuse more shard logic especially in `search/index.go`, but due to Go missing generics concept it's different when compare to `Ruby`, `Java` and some other popular languages. - Add more tests, due to this code test costs me a lot of time to complete, so I didn't cover all the code currently, but I am keen to add more tests if possible. - Better user experience in CLI application, current the cli app is very basic and go can't go back to previous menu. +- Better error handling instead of just `panic` diff --git a/main.go b/main.go index 9075bd1..37229b5 100644 --- a/main.go +++ b/main.go @@ -10,16 +10,19 @@ import ( func main() { - app := search.InitAPP("data/organizations.json", "data/users.json", "data/tickets.json") + app, err := search.InitAPP("data/organizations.json", "data/users.json", "data/tickets.json") + if err != nil { + panic("cannot init application " + err.Error()) + } - if err := app.Parse(); err != nil { + if err = app.Parse(); err != nil { panic("cannot load data: " + err.Error()) } if err := app.BuildIndexes(); err != nil { panic("failed to build indexes: " + err.Error()) } - + for { prompt := promptui.Select{ diff --git a/search/app.go b/search/app.go index d8809ca..83ad94b 100644 --- a/search/app.go +++ b/search/app.go @@ -1,6 +1,7 @@ package search import ( + "fmt" "io/ioutil" "github.com/wahyd4/zendesk/index" @@ -25,7 +26,7 @@ type APP struct { } // InitAPP takes data file paths and then initialise the application -func InitAPP(organisationsFile, usersFile, ticketsFile string) *APP { +func InitAPP(organisationsFile, usersFile, ticketsFile string) (*APP, error) { files := map[string]string{ OrganisationsKey: organisationsFile, UsersKey: usersFile, @@ -35,7 +36,7 @@ func InitAPP(organisationsFile, usersFile, ticketsFile string) *APP { for fileType, file := range files { bytes, err := ioutil.ReadFile(file) if err != nil { - panic("cannot init application due to unable to load:" + file) + return nil, fmt.Errorf("cannot init application due to unable to load: %s with error %v", file, err.Error()) } jsonContents[fileType] = bytes } @@ -44,7 +45,7 @@ func InitAPP(organisationsFile, usersFile, ticketsFile string) *APP { jsonContents: jsonContents, indexes: make(map[string]index.SearchIndex), searchContext: &SearchContext{}, - } + }, nil } // FindOrganisation find a organisation by organisation ID diff --git a/search/app_test.go b/search/app_test.go new file mode 100644 index 0000000..511a86f --- /dev/null +++ b/search/app_test.go @@ -0,0 +1,54 @@ +package search + +import ( + "testing" +) + +func TestInitAPP(t *testing.T) { + type args struct { + organisationsFile string + usersFile string + ticketsFile string + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "can init the application", + args: args{ + organisationsFile: "../data/organizations.json", + usersFile: "../data/users.json", + ticketsFile: "../data/tickets.json", + }, + wantErr: false, + }, + { + name: "cannot init the application due to users json is not found", + args: args{ + organisationsFile: "../data/organizations.json", + usersFile: "not_exist.json", + ticketsFile: "../data/tickets.json", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := InitAPP(tt.args.organisationsFile, tt.args.usersFile, tt.args.ticketsFile) + if (err != nil) != tt.wantErr { + t.Errorf("InitAPP() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if !tt.wantErr && got == nil { + t.Error("expect to init a app but didn't ") + } + + if tt.wantErr && got != nil { + t.Error("expect to receive some error but didn't") + } + }) + } +}