Add test cases

This commit is contained in:
2020-03-02 23:46:44 +11:00
parent 4478aa2fbe
commit 9dbd04eed8
4 changed files with 65 additions and 6 deletions
+1
View File
@@ -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`
+5 -2
View File
@@ -10,9 +10,12 @@ 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())
}
+4 -3
View File
@@ -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
+54
View File
@@ -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")
}
})
}
}