eintopf.info@v0.13.16/service/search/transport_test.go (about)

     1  // Copyright (C) 2022 The Eintopf authors
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  
    16  package search_test
    17  
    18  // import (
    19  // 	"fmt"
    20  // 	"io"
    21  // 	"net/http"
    22  // 	"net/http/httptest"
    23  // 	"testing"
    24  
    25  // 	"github.com/go-chi/chi/v5"
    26  // 	"github.com/petergtz/pegomock"
    27  
    28  // 	"eintopf.info/internal/mock"
    29  // 	"eintopf.info/service/search"
    30  // )
    31  
    32  // func TestRouter(t *testing.T) {
    33  // 	tests := []struct {
    34  // 		name     string
    35  // 		url      string
    36  // 		wantBody string
    37  // 		wantCode int
    38  // 	}{
    39  // 		{
    40  // 			name:     "ResponseIsEmpty",
    41  // 			url:      "/",
    42  // 			wantBody: `{"hits":null,"total":0,"buckets":null}`,
    43  // 			wantCode: 200,
    44  // 		}, {
    45  // 			name:     "ResponseHasHits",
    46  // 			url:      "/?query=foo",
    47  // 			wantBody: `{"hits":[{"type":"event","raw":"{\"id\":\"1\"}"}],"total":0,"buckets":null}`,
    48  // 			wantCode: 200,
    49  // 			// }, {
    50  // 			// 	name:     "WithFilters",
    51  // 			// 	url:      "/",
    52  // 			// 	body:     `{"filters": [{"field": "is", "type": "terms", "terms":["foo"]}]}`,
    53  // 			// 	wantBody: `{"hits":[{"type":"event","raw":"{\"id\":\"1\"}"}],"buckets":null}`,
    54  // 			// 	wantCode: 200,
    55  // 			// }, {
    56  // 			// 	name:     "WithAggregations",
    57  // 			// 	url:      "/",
    58  // 			// 	body:     `{"aggregations": {"foo": {"field":"foo","type":"terms"}}}`,
    59  // 			// 	wantBody: `{"events":[],"buckets":[{"field":"foo","terms":["a"]}]}`,
    60  // 			// 	wantCode: 200,
    61  // 		}, {
    62  // 			name:     "search error",
    63  // 			url:      "/?query=error",
    64  // 			wantBody: `{"error":"internal error"}`,
    65  // 			wantCode: 500,
    66  // 		},
    67  // 	}
    68  
    69  // 	mockedSearchService := mock.NewSearchService()
    70  // 	pegomock.When(mockedSearchService.Search(emptyOptions())).ThenReturn(nil, nil)
    71  // 	pegomock.When(mockedSearchService.Search(emptyOptions())).ThenReturn(&search.Result{
    72  // 		Hits: []search.Hit{search.Hit{Type: "event", Raw: `{"id":"1"}`}},
    73  // 	}, nil)
    74  // 	pegomock.When(mockedSearchService.Search(&search.Options{
    75  // 		Query: "foo",
    76  // 		Filters: []search.Filter{
    77  // 			&search.TermsFilter{Field: "is", Terms: []string{"foo"}},
    78  // 		},
    79  // 		Aggregations: make(map[string]search.Aggregation, 0),
    80  // 	})).ThenReturn(&search.Result{
    81  // 		Hits: []search.Hit{search.Hit{Type: "event", Raw: `{"id":"1"}`}},
    82  // 	}, nil)
    83  // 	pegomock.When(mockedSearchService.Search(&search.Options{
    84  // 		Aggregations: map[string]search.Aggregation{
    85  // 			"foo": search.Aggregation{Field: "foo", Type: search.TermsAggregation},
    86  // 		},
    87  // 	})).ThenReturn(&search.Result{
    88  // 		Buckets: map[string]search.Bucket{
    89  // 			"foo": search.TermsBucket([]search.Term{search.Term{Term: "a", Count: 1}}),
    90  // 		},
    91  // 	}, nil)
    92  // 	pegomock.When(mockedSearchService.Search(&search.Options{
    93  // 		Query:        "error",
    94  // 		Sort:         "",
    95  // 		Filters:      make([]search.Filter, 0),
    96  // 		Aggregations: make(map[string]search.Aggregation, 0),
    97  // 	})).ThenReturn(nil, fmt.Errorf("error"))
    98  
    99  // 	r := chi.NewRouter()
   100  // 	r.Route("/", search.Router(mockedSearchService))
   101  // 	ts := httptest.NewServer(r)
   102  // 	defer ts.Close()
   103  
   104  // 	for _, tc := range tests {
   105  // 		t.Run(tc.name, func(tt *testing.T) {
   106  // 			res, err := http.Get(ts.URL + tc.url)
   107  // 			if err != nil {
   108  // 				tt.Errorf("http.Get(%s): %s", ts.URL+tc.url, err)
   109  // 			}
   110  // 			if res == nil {
   111  // 				tt.Error("no response")
   112  // 			}
   113  // 			if res.Body != nil {
   114  // 				defer res.Body.Close()
   115  // 			}
   116  
   117  // 			b, err := io.ReadAll(res.Body)
   118  // 			if err != nil {
   119  // 				tt.Errorf("io.ReadAll: %s", err)
   120  // 			}
   121  
   122  // 			if tc.wantCode != res.StatusCode {
   123  // 				tt.Errorf("status code: %d != %d", tc.wantCode, res.StatusCode)
   124  // 			}
   125  // 			if tc.wantBody != string(b) {
   126  // 				tt.Errorf("body doesn't match:\nwant: %s\ngot:  %s", tc.wantBody, string(b))
   127  // 			}
   128  // 		})
   129  // 	}
   130  // }
   131  
   132  // func emptyOptions() *search.Options {
   133  // 	return &search.Options{
   134  // 		Query:        "",
   135  // 		Sort:         "",
   136  // 		Filters:      make([]search.Filter, 0),
   137  // 		Aggregations: make(map[string]search.Aggregation, 0),
   138  // 	}
   139  // }