Skip to content

Commit

Permalink
Add translate test
Browse files Browse the repository at this point in the history
  • Loading branch information
candy12t committed Nov 15, 2021
1 parent 04315d5 commit eab4756
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 10 deletions.
16 changes: 6 additions & 10 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/candy12t/deepl-cli/test"
)

func TestParseConfig(t *testing.T) {
Expand All @@ -20,7 +21,7 @@ func TestParseConfig(t *testing.T) {
BaseURL: "https://api-free.deepl.com/v2",
}

configPath := filepath.Join(projectDirPath(), "testdata", "config", "free.yaml")
configPath := filepath.Join(test.ProjectDirPath(), "test", "testdata", "config", "free.yaml")
err := ParseConfig(configPath)
gotConfig := CachedConfig()

Expand All @@ -43,7 +44,7 @@ func TestParseConfig(t *testing.T) {
BaseURL: "https://api.deepl.com/v2",
}

configPath := filepath.Join(projectDirPath(), "testdata", "config", "pro.yaml")
configPath := filepath.Join(test.ProjectDirPath(), "test", "testdata", "config", "pro.yaml")
err := ParseConfig(configPath)
gotConfig := CachedConfig()

Expand All @@ -56,7 +57,7 @@ func TestParseConfig(t *testing.T) {
t.Run("can not read config file", func(t *testing.T) {
wantConfig := Config{}

configPath := filepath.Join(projectDirPath(), "testdata", "config", "not_read.yaml")
configPath := filepath.Join(test.ProjectDirPath(), "test", "testdata", "config", "not_read.yaml")
err := ParseConfig(configPath)
gotConfig := CachedConfig()

Expand All @@ -69,7 +70,7 @@ func TestParseConfig(t *testing.T) {
t.Run("can not unmarshal config file", func(t *testing.T) {
wantConfig := Config{}

configPath := filepath.Join(projectDirPath(), "testdata", "config", "not_unmarshal.yaml")
configPath := filepath.Join(test.ProjectDirPath(), "test", "testdata", "config", "not_unmarshal.yaml")
err := ParseConfig(configPath)
gotConfig := CachedConfig()

Expand Down Expand Up @@ -114,11 +115,6 @@ func assertUnmarshalError(t *testing.T, got, want error) {
}
}

func projectDirPath() string {
currentDirPath, _ := os.Getwd()
return filepath.Join(currentDirPath, "..", "..")
}

func cleanCachedConfig(t *testing.T) {
t.Cleanup(func() {
config = Config{}
Expand Down
76 changes: 76 additions & 0 deletions internal/deepl/translate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package deepl

import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strconv"
"strings"
"testing"

"github.com/candy12t/deepl-cli/test"
)

func TestTranslate(t *testing.T) {
t.Run("success translate", func(t *testing.T) {
client, server, err := deeplMockServer(t, "test-auth-key", "success-body", "success-header")
if err != nil {
t.Fatal(err)
}
defer server.Close()

ctx := context.Background()
got, err := client.Translate(ctx, "hello", "EN", "JA")
if err != nil {
t.Fatal(err)
}
want := "こんにちわ"

if got != want {
t.Fatalf("got %v, want %v", got, want)
}
})
}

func deeplMockServer(t *testing.T, mockAuthKey string, mockResponseBody, mockResponseHeader string) (*Client, *httptest.Server, error) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := filepath.Join(test.ProjectDirPath(), "test", "testdata", "deepl", mockResponseHeader)
headerBytes, err := ioutil.ReadFile(header)
h := strings.Split(string(headerBytes), "\n")
for _, line := range h {
if strings.Contains(line, "HTTP") {
statusCode, err := strconv.Atoi(strings.Split(line, " ")[1])
if err != nil {
t.Fatal(err)
}
w.WriteHeader(statusCode)
}

if strings.Contains(line, "content-type") {
contentType := strings.Split(line, " ")[1]
w.Header().Add("content-type", contentType)
}
}

body := filepath.Join(test.ProjectDirPath(), "test", "testdata", "deepl", mockResponseBody)
bodyBytes, err := ioutil.ReadFile(body)
if err != nil {
t.Fatal(err)
}
w.Write(bodyBytes)
}))

serverURL, err := url.ParseRequestURI(server.URL)
if err != nil {
return nil, nil, err
}

return &Client{
BaseURL: serverURL,
HTTPClient: server.Client(),
AuthKey: mockAuthKey,
}, server, nil
}
11 changes: 11 additions & 0 deletions test/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test

import (
"os"
"path/filepath"
)

func ProjectDirPath() string {
currentDirPath, _ := os.Getwd()
return filepath.Join(currentDirPath, "..", "..")
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/testdata/deepl/non_target_lang-body
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"\"Value for 'target_lang' not supported.\""}
7 changes: 7 additions & 0 deletions test/testdata/deepl/non_target_lang-header
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HTTP/2 400
server: nginx
date: Sun, 14 Nov 2021 13:34:43 GMT
content-type: application/json
content-length: 56
access-control-allow-origin: *

1 change: 1 addition & 0 deletions test/testdata/deepl/success-body
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"translations":[{"detected_source_language":"EN","text":"こんにちわ"}]}
7 changes: 7 additions & 0 deletions test/testdata/deepl/success-header
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HTTP/2 200
server: nginx
date: Sun, 14 Nov 2021 13:29:37 GMT
content-type: application/json
content-length: 77
access-control-allow-origin: *

0 comments on commit eab4756

Please sign in to comment.