| 27 | ) |
| 28 | |
| 29 | func TestApiES(t *testing.T) { |
| 30 | t.Run("test es api", func(t *testing.T) { |
| 31 | t.Run("POST /es/_bulk", func(t *testing.T) { |
| 32 | t.Run("bulk documents", func(t *testing.T) { |
| 33 | body := bytes.NewBuffer(nil) |
| 34 | body.WriteString(bulkData) |
| 35 | resp := request("POST", "/es/_bulk", body) |
| 36 | assert.Equal(t, http.StatusOK, resp.Code) |
| 37 | }) |
| 38 | t.Run("bulk documents with delete", func(t *testing.T) { |
| 39 | body := bytes.NewBuffer(nil) |
| 40 | body.WriteString(bulkDataWithDelete) |
| 41 | resp := request("POST", "/es/_bulk", body) |
| 42 | assert.Equal(t, http.StatusOK, resp.Code) |
| 43 | }) |
| 44 | t.Run("bulk with error input", func(t *testing.T) { |
| 45 | body := bytes.NewBuffer(nil) |
| 46 | body.WriteString(`{"index":{}}`) |
| 47 | resp := request("POST", "/es/_bulk", body) |
| 48 | assert.Equal(t, http.StatusOK, resp.Code) |
| 49 | }) |
| 50 | }) |
| 51 | |
| 52 | t.Run("POST /es/:target/_bulk", func(t *testing.T) { |
| 53 | t.Run("bulk create documents with not exist indexName", func(t *testing.T) { |
| 54 | body := bytes.NewBuffer(nil) |
| 55 | data := strings.ReplaceAll(bulkData, `"_index": "games3"`, `"_index": ""`) |
| 56 | body.WriteString(data) |
| 57 | resp := request("POST", "/es/notExistIndex/_bulk", body) |
| 58 | assert.Equal(t, http.StatusOK, resp.Code) |
| 59 | }) |
| 60 | t.Run("bulk create documents with exist indexName", func(t *testing.T) { |
| 61 | // create index |
| 62 | body := bytes.NewBuffer(nil) |
| 63 | body.WriteString(`{"name": "` + indexName + `", "storage_type": "disk"}`) |
| 64 | resp := request("PUT", "/api/index", body) |
| 65 | assert.Equal(t, http.StatusBadRequest, resp.Code) |
| 66 | |
| 67 | respData := make(map[string]string) |
| 68 | err := json.Unmarshal(resp.Body.Bytes(), &respData) |
| 69 | assert.NoError(t, err) |
| 70 | assert.Equal(t, "index ["+indexName+"] already exists", respData["error"]) |
| 71 | |
| 72 | // check bulk |
| 73 | body.Reset() |
| 74 | data := strings.ReplaceAll(bulkData, `"_index": "games3"`, `"_index": ""`) |
| 75 | body.WriteString(data) |
| 76 | resp = request("POST", "/es/"+indexName+"/_bulk", body) |
| 77 | assert.Equal(t, http.StatusOK, resp.Code) |
| 78 | }) |
| 79 | t.Run("bulk with error input", func(t *testing.T) { |
| 80 | body := bytes.NewBuffer(nil) |
| 81 | body.WriteString(`{"index":{}}`) |
| 82 | resp := request("POST", "/es/"+indexName+"/_bulk", body) |
| 83 | assert.Equal(t, http.StatusOK, resp.Code) |
| 84 | }) |
| 85 | }) |
| 86 | |