search_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. t.Log("Testing if <seg> tags will be removed from segments.")
  19. if segment.Source == cleanedSourceSegment && segment.Target == cleanedTargetSegment {
  20. t.Log("Segments have been cleaned!")
  21. } else {
  22. t.Fatalf("Segments still have tags!\nSource: %v\nTarget: %v", segment.Source, segment.Target)
  23. }
  24. }
  25. func ServeSearchResults() *httptest.Server {
  26. searchResults, err := ioutil.ReadFile("./testFiles/searchResults.json")
  27. if err != nil {
  28. log.Fatalf("Error reading file: %v\n", err)
  29. }
  30. f := func(w http.ResponseWriter, r *http.Request) {
  31. w.WriteHeader(200)
  32. w.Header().Set("Content-Type", "application/json")
  33. fmt.Fprintln(w, string(searchResults))
  34. }
  35. return httptest.NewServer(http.HandlerFunc(f))
  36. }
  37. func TestSearch(t *testing.T) {
  38. server := ServeSearchResults()
  39. defer server.Close()
  40. var app Application
  41. app.SetBaseURL(server.URL)
  42. tmsJSON, err := os.Open("./testFiles/tms.json")
  43. defer tmsJSON.Close()
  44. if err != nil {
  45. t.Fatalf("error reading tms: %v", err)
  46. return
  47. }
  48. var tms []TM
  49. JSONDecoder(tmsJSON, &tms)
  50. testSourceSegment1 := "<bpt i='1' type='bold'>{}</bpt>Something Test/ Whatever<ept i='1'>{}</ept>"
  51. testSourceSegment2 := "<bpt i='1' type='bold'>{}</bpt>Another Test/ Anything<ept i='1'>{}</ept>"
  52. t.Log("Testing search method.")
  53. searchResults := app.Search(tms, "something")
  54. if searchResults.TotalResults != 4 {
  55. t.Fatalf("Not all results returned! (%v)", searchResults.TotalResults)
  56. }
  57. segment := searchResults.Results[0].Segments[0]
  58. if segment.Source == testSourceSegment1 || segment.Source == testSourceSegment2 {
  59. t.Log("Search results fine!")
  60. } else {
  61. t.Fatalf("Something is wrong with returned segment!\n%v", segment)
  62. }
  63. }