search_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. }
  61. func TestSearchWrongStatus(t *testing.T) {
  62. server := fakeServer(http.StatusBadRequest, "")
  63. defer server.Close()
  64. var app Application
  65. app.setBaseURL(server.URL)
  66. tmsJSON, err := os.Open("./testFiles/tms.json")
  67. if err != nil {
  68. t.Fatalf("Error reading tms: %s", err)
  69. }
  70. defer tmsJSON.Close()
  71. var tms []TM
  72. err = jsonDecoder(tmsJSON, &tms)
  73. if err != nil {
  74. t.Fatalf("Error decoding tms: %s", err)
  75. }
  76. searchResults := app.search(tms, "something")
  77. if searchResults.TotalResults != 0 {
  78. t.Fatal("There should be no results!")
  79. }
  80. }