示例#1
0
func suggestions(w rest.ResponseWriter, r *rest.Request) {
	var req data.SuggestionReq
	err := r.DecodeJsonPayload(&req)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if req.FallbackLocale == "" {
		req.FallbackLocale = data.Locale("en")
	}
	if req.Locales == nil {
		rest.Error(w, "locales required", 400)
		return
	}
	if len(req.Query) > maxQueryLen {
		rest.Error(w, fmt.Sprintf("Max query length is %d", maxQueryLen), 400)
		return
	}

	resp, err := glosbe.Suggest(req)
	if err != nil {
		rest.Error((w), err.Error(), 500)
		return
	}

	w.WriteJson(resp)
}
示例#2
0
func TestMaxQueryLen(t *testing.T) {
	payload := data.MultiTranslationReq{
		Query:   "hello long long long long long long long long long long query",
		Targets: []data.Locale{"ru", "de"},
		Source:  data.Locale("en"),
	}
	makeRequest(t, "/translations", payload).CodeIs(400)
}
示例#3
0
func TestSuggestions(t *testing.T) {
	payload := data.SuggestionReq{
		Query:          "irgend",
		Locales:        []data.Locale{"en", "de"},
		FallbackLocale: data.Locale("en"),
	}
	rec := makeRequest(t, "/suggestions", payload)
	rec.CodeIs(200)

	var ss []data.Suggestion
	unmarshal(t, rec, &ss)

	if ss == nil {
		t.Error("Expected to return suggestions")
	}

	// Make the same request to hit cache
	makeRequest(t, "/suggestions", payload).CodeIs(200)
}
示例#4
0
func TestTranslations(t *testing.T) {
	payload := data.MultiTranslationReq{
		Query:   "hello",
		Targets: []data.Locale{"ru", "de"},
		Source:  data.Locale("en"),
	}
	rec := makeRequest(t, "/translations", payload)
	rec.CodeIs(200)

	var mt data.MultiTranslation
	unmarshal(t, rec, &mt)

	if mt.Translations == nil {
		t.Error("Expected to return translations")
	}

	// Make the same request to hit cache
	makeRequest(t, "/translations", payload).CodeIs(200)
}
func TestTranslateText(t *testing.T) {
	req := data.TranslationReq{Source: "en", Target: "de", Query: "hello"}
	expectedRes := data.Translation{
		Target: data.Locale("de"),
		WebURL: "https://translate.google.com/#en/de/hello",
		Meanings: []data.Meaning{data.Meaning{
			TranslatedText: "Hallo",
			OriginName:     "google",
			WebURL:         "https://translate.google.com/#de/en/Hallo",
		}},
	}
	res, err := Translate(req)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(res, expectedRes) {
		t.Errorf("Wrong translation for '%s'.\nExpected: %+v\nGot: %+v",
			req.Query, expectedRes, res)
	}
}
package glosbe

import (
	"testing"

	"github.com/eicca/translate-server/data"
)

var suggestTests = []struct {
	in  data.SuggestionReq
	out []data.Suggestion
}{
	{
		data.SuggestionReq{
			Locales:        []data.Locale{"en", "de", "ru"},
			FallbackLocale: data.Locale("ru"),
			Query:          "irgend",
		},
		[]data.Suggestion{
			data.Suggestion{Text: "irgendein", Locale: "de"},
			data.Suggestion{Text: "irgend etwas", Locale: "de"},
		},
	},
	{
		data.SuggestionReq{
			Locales:        []data.Locale{"en", "ru"},
			FallbackLocale: data.Locale("ru"),
			Query:          "irgend",
		},
		[]data.Suggestion{
			data.Suggestion{Text: "Iris", Locale: "ru"},
import (
	"testing"

	"github.com/eicca/translate-server/data"
)

var detectTests = []struct {
	in  data.SuggestionReq
	out data.Locale
}{
	{
		data.SuggestionReq{
			Locales:        []data.Locale{"en", "de", "ru"},
			FallbackLocale: "de",
			Query:          "hallo",
		}, data.Locale("de"),
	},
	{
		data.SuggestionReq{
			Locales:        []data.Locale{"en", "ru"},
			FallbackLocale: "ru",
			Query:          "etwas",
		}, data.Locale("ru"),
	},
	{
		data.SuggestionReq{
			Locales:        []data.Locale{"en", "de", "ru"},
			FallbackLocale: "de",
		}, data.Locale("de"),
	},
}