search_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "testing"
  10. )
  11. func TestSegmentCleanup(t *testing.T) {
  12. sourceSegment := "<seg>This is test</seg>"
  13. targetSegment := "<seg>This is test for target</seg>"
  14. cleanedSourceSegment := "This is test"
  15. cleanedTargetSegment := "This is test for target"
  16. segment := Segment{sourceSegment, targetSegment}
  17. segment.clean()
  18. if segment.Source != cleanedSourceSegment || segment.Target != cleanedTargetSegment {
  19. t.Fatalf("Segments still have tags!\nSource: %s\nTarget: %s", segment.Source, segment.Target)
  20. }
  21. }
  22. func serveSearchResults() *httptest.Server {
  23. searchResults, err := ioutil.ReadFile("./testFiles/searchResults.json")
  24. if err != nil {
  25. log.Fatalf("Error reading file: %s", err)
  26. }
  27. f := func(w http.ResponseWriter, r *http.Request) {
  28. w.Header().Set("Content-Type", "application/json")
  29. fmt.Fprintln(w, string(searchResults))
  30. }
  31. return httptest.NewServer(http.HandlerFunc(f))
  32. }
  33. func TestSearch(t *testing.T) {
  34. server := serveSearchResults()
  35. defer server.Close()
  36. var app Application
  37. app.setBaseURL(server.URL)
  38. tmsJSON, err := os.Open("./testFiles/tms.json")
  39. if err != nil {
  40. t.Fatalf("Error reading tms: %s", err)
  41. }
  42. defer tmsJSON.Close()
  43. var tms []TM
  44. err = jsonDecoder(tmsJSON, &tms)
  45. if err != nil {
  46. t.Fatalf("Error decoding tms: %s", err)
  47. }
  48. searchResults := app.search(tms, "something")
  49. if searchResults.TotalResults != 4 {
  50. t.Fatalf("Not all results returned! (%d)", searchResults.TotalResults)
  51. }
  52. testSourceSegments := []string{"<bpt i='1' type='bold'>{}</bpt>Something Test/ Whatever<ept i='1'>{}</ept>",
  53. "<bpt i='1' type='bold'>{}</bpt>Another Test/ Anything<ept i='1'>{}</ept>"}
  54. for index, segment := range searchResults.Results[0].Segments {
  55. if segment.Source != testSourceSegments[index] {
  56. t.Fatalf("Something is wrong with returned segment!\nShould be:\n%s\nBut is:\n%s",
  57. testSourceSegments[index], segment)
  58. }
  59. }
  60. }