aboutsummaryrefslogtreecommitdiff
path: root/api_client_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'api_client_test.go')
-rw-r--r--api_client_test.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/api_client_test.go b/api_client_test.go
new file mode 100644
index 0000000..23e1e81
--- /dev/null
+++ b/api_client_test.go
@@ -0,0 +1,103 @@
+// Copyright 2020, Shulhan <m.shulhan@gmail.com>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package kbbi
+
+import (
+ "testing"
+
+ "github.com/shuLhan/share/lib/test"
+)
+
+func TestApiClient_CariDefinisi_offline(t *testing.T) {
+ testServer.offline = true
+
+ client := newAPIClient(testServerAPI)
+
+ cases := []struct {
+ desc string
+ words []string
+ exp DefinisiResponse
+ expError string
+ }{{
+ desc: "With empty input",
+ }, {
+ desc: "With words not found",
+ words: []string{"a"},
+ }, {
+ desc: "With valid word in cache",
+ words: []string{"mengeja"},
+ exp: DefinisiResponse{
+ "mengeja": testKataMengeja,
+ },
+ }, {
+ desc: "With duplicate words",
+ words: []string{"mengeja", "mengeja"},
+ exp: DefinisiResponse{
+ "mengeja": testKataMengeja,
+ },
+ }}
+
+ for _, c := range cases {
+ t.Logf(c.desc)
+
+ got, err := client.CariDefinisi(c.words)
+ if err != nil {
+ test.Assert(t, "error", c.expError, err.Error(), true)
+ continue
+ }
+
+ test.Assert(t, "DefinisiResponse", c.exp, got, true)
+ }
+}
+
+func TestApiClient_CariDefinisi_online(t *testing.T) {
+ testServer.offline = false
+
+ client := newAPIClient(testServerAPI)
+
+ cases := []struct {
+ desc string
+ words []string
+ exp DefinisiResponse
+ expError string
+ }{{
+ desc: "With empty input",
+ }, {
+ desc: "With valid word in cache",
+ words: []string{"mengeja"},
+ exp: DefinisiResponse{
+ "mengeja": testKataMengeja,
+ },
+ }, {
+ desc: "With duplicate words",
+ words: []string{"mengeja", "mengeja"},
+ exp: DefinisiResponse{
+ "mengeja": testKataMengeja,
+ },
+ }, {
+ desc: "With one of the word not in cache",
+ words: []string{"mengeja", "eja"},
+ exp: DefinisiResponse{
+ "mengeja": testKataMengeja,
+ "eja": testKataEja,
+ },
+ }}
+
+ for _, c := range cases {
+ t.Logf(c.desc)
+
+ got, err := client.CariDefinisi(c.words)
+ if err != nil {
+ test.Assert(t, "error", c.expError, err.Error(), true)
+ continue
+ }
+
+ for k, v := range got {
+ t.Logf("got: %s = %+v", k, v)
+ }
+
+ test.Assert(t, "DefinisiResponse", c.exp, got, true)
+ }
+}