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

     1  package eventsearch_test
     2  
     3  import (
     4  	"net/http/httptest"
     5  	"net/url"
     6  	"testing"
     7  	"time"
     8  
     9  	"eintopf.info/service/eventsearch"
    10  	"github.com/google/go-cmp/cmp"
    11  )
    12  
    13  func TestOptionsFromRequest(t *testing.T) {
    14  	tests := []struct {
    15  		url     string
    16  		options eventsearch.Options
    17  	}{
    18  		{
    19  			url: "/?sort=start",
    20  			options: eventsearch.Options{
    21  				Sort:         "start",
    22  				Filters:      eventsearch.Filters{},
    23  				Aggregations: eventsearch.Aggregations{},
    24  			},
    25  		},
    26  		{
    27  			url: "/?sort=start&groupIDs=12c22b9b-173a-4488-94df-3a474bc310e9",
    28  			options: eventsearch.Options{
    29  				Sort: "start",
    30  				Filters: eventsearch.Filters{
    31  					eventsearch.GroupIDsFilter{GroupIDs: []string{"12c22b9b-173a-4488-94df-3a474bc310e9"}},
    32  				},
    33  				Aggregations: eventsearch.Aggregations{},
    34  			},
    35  		},
    36  		{
    37  			url: "/?sort=start&dateMin=2023-03-06T09:55:05Z",
    38  			options: eventsearch.Options{
    39  				Sort:         "start",
    40  				Filters:      eventsearch.Filters{eventsearch.DateRangeFilter{DateMin: time.Date(2023, 3, 6, 9, 55, 5, 0, time.UTC)}},
    41  				Aggregations: eventsearch.Aggregations{},
    42  			},
    43  		},
    44  		{
    45  			url: "/?sort=start&dateMin=" + url.QueryEscape("2023-03-06T09:55:05Z"),
    46  			options: eventsearch.Options{
    47  				Sort:         "start",
    48  				Filters:      eventsearch.Filters{eventsearch.DateRangeFilter{DateMin: time.Date(2023, 3, 6, 9, 55, 5, 0, time.UTC)}},
    49  				Aggregations: eventsearch.Aggregations{},
    50  			},
    51  		},
    52  	}
    53  
    54  	for _, test := range tests {
    55  		r := httptest.NewRequest("GET", test.url, nil)
    56  		got, err := eventsearch.OptionsFromRequest(r)
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  		if diff := cmp.Diff(test.options, got); diff != "" {
    61  			t.Errorf("options mismatch (-want +got):\n%s", diff)
    62  		}
    63  	}
    64  }