From 8a141995aabdee289d2b096bd77a10b52b08a1bf Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 31 Mar 2020 01:11:26 +0700 Subject: 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. --- api_client_test.go | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 api_client_test.go (limited to 'api_client_test.go') 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 . 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) + } +} -- cgit v1.3