diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-03-31 01:11:26 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-04-01 06:28:03 +0700 |
| commit | 8a141995aabdee289d2b096bd77a10b52b08a1bf (patch) | |
| tree | 98af569d4c3e2620809ce591c2733184cd77db8f /api_client_test.go | |
| parent | 84fdfdb6ae4175a125fc67a6aed377476d31ee0e (diff) | |
| download | kamusku-8a141995aabdee289d2b096bd77a10b52b08a1bf.tar.xz | |
all: implement server and client for dictionary API
Currently the server and client can onyl handle API for looking up
definitions of the words through "/api/definisi" URL.
Diffstat (limited to 'api_client_test.go')
| -rw-r--r-- | api_client_test.go | 103 |
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) + } +} |
