Repository: moby/moby
File: client/container_list_test.go (Go)

Code before:
func TestContainerListLatest(t *testing.T) {
	client, err := New(
		WithMockClient(func(req *http.Request) (*http.Response, error) {
			if err := assertRequest(req, http.MethodGet, "/containers/json"); err != nil {
				return nil, err
			}
			query := req.URL.Query()
			limit := query.Get("limit")
			if limit != "1" {
				return nil, fmt.Errorf("limit not set properly for Latest option. Expected '1', got %s", limit)
			}
			return mockJSONResponse(http.StatusOK, nil, []container.Summary{
				{ID: "latest_container"},
			})(req)
		}),
	)
	assert.NilError(t, err)

	list, err := client.ContainerList(t.Context(), ContainerListOptions{
		Latest: true,
	})
	assert.NilError(t, err)
	assert.Check(t, is.Len(list.Items, 1))
}

func TestContainerList(t *testing.T) {
	const (
		expectedURL     = "/containers/json"
		expectedFilters = `{"before":{"container":true},"label":{"label1":true,"label2":true}}`
	)
	client, err := New(
		WithMockClient(func(req *http.Request) (*http.Response, error) {
			if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
				return nil, err
			}
			query := req.URL.Query()
			all := query.Get("all")
			if all != "1" {
				return nil, fmt.Errorf("all not set in URL query properly. Expected '1', got %s", all)
			}
			limit := query.Get("limit")
			if limit != "" {
				return nil, fmt.Errorf("limit should have not be present in query, got %s", limit)
			}
			since := query.Get("since")
			if since != "container" {
				return nil, fmt.Errorf("since not set in URL query properly. Expected 'container', got %s", since)
			}
			before := query.Get("before")
			if before != "" {
				return nil, fmt.Errorf("before should have not be present in query, got %s", before)

Review comment (suggestion):
My comment above was when I started to write a suggestion to instead use a table-test for the `TestContainerList`; I think we can still do so, and then in a follow-up look at deprecating (and later removing) the fields that are no longer used;

```go
func TestContainerList(t *testing.T) {
	const expectedURL = "/containers/json"

	tests := []struct {
		doc      string
		options  ContainerListOptions
		expected url.Values
	}{
		{
			doc:      "no options",
			expected: url.Values{},
		},
		{
			doc:      "size",
			options:  ContainerListOptions{Size: true},
			expected: url.Values{"size": []string{"1"}},
		},
		{
			doc:      "all",
			options:  ContainerListOptions{All: true},
			expected: url.Values{"all": []string{"1"}},
		},
		{
			doc:      "latest", // FIXME(thaJeztah): the "latest" option is a cli-side shortcut for "limit: 1". It has never been used https://github.com/moby/moby/commit/d05aa418b0466553a24d42896f99176cfa29765f
			options:  ContainerListOptions{Latest: true},
			expected: url.Values{},
		},
		{
			doc:      "since", // FIXME(thaJeztah): the "since" option was deprecated, and is no longer handled by the API server; https://github.com/moby/moby/pull/22138
			options:  ContainerListOptions{Since: "container"},
			expected: url.Values{"since": []string{"container"}},
		},
		{
			doc:      "before", // FIXME(thaJeztah): the "before" option was deprecated, and is no longer handled by the API server; https://github.com/moby/moby/pull/22138
			options:  ContainerListOptions{Before: "container"},
			expected: url.Values{"before": []string{"container"}},
		},
		{
			doc:      "limit",
			options:  ContainerListOptions{Limit: 1},
			expected: url.Values{"limit": []string{"1"}},
		},
		{
			doc: "filters",
			options: ContainerListOptions{
				Filters: make(Filters).
					Add("label", "label1").
					Add("label", "label2").
					Add("before", "container"),
			},
			expected: url.Values{"filters": []string{`{"before":{"container":true},"label":{"label1":true,"label2":true}}`}},
		},
	}

	for _, tc := range tests {
		t.Run(tc.doc, func(t *testing.T) {
			var query url.Values
			client, err := New(
				WithMockClient(func(req *http.Request) (*http.Response, error) {
					if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
						return nil, err
					}
					query = req.URL.Query()

					return mockJSONResponse(http.StatusOK, nil, []container.Summary{
						{ID: "container_id1"},
						{ID: "container_id2"},
					})(req)
				}),
			)
			assert.NilError(t, err)

			list, err := client.ContainerList(t.Context(), tc.options)
			assert.NilError(t, err)
			assert.Check(t, is.Len(list.Items, 2))
			assert.Check(t, is.DeepEqual(query, tc.expected))
		})
	}
}
```




Code after:
func TestContainerList(t *testing.T) {
	const expectedURL = "/containers/json"

	tests := []struct {
		doc      string
		options  ContainerListOptions
		expected url.Values
	}{
		{
			doc:      "no options",
			expected: url.Values{},
		},
		{
			doc:      "size",
			options:  ContainerListOptions{Size: true},
			expected: url.Values{"size": []string{"1"}},
		},
		{
			doc:      "all",
			options:  ContainerListOptions{All: true},
			expected: url.Values{"all": []string{"1"}},
		},
		{
			doc:      "latest", // FIXME(thaJeztah): the "latest" option is a cli-side shortcut for "limit: 1". It has never been used https://github.com/moby/moby/commit/d05aa418b0466553a24d42896f99176cfa29765f
			options:  ContainerListOptions{Latest: true},
			expected: url.Values{},
		},
		{
			doc:      "since", // FIXME(thaJeztah): the "since" option was deprecated, and is no longer handled by the API server; https://github.com/moby/moby/pull/22138
			options:  ContainerListOptions{Since: "container"},
			expected: url.Values{"since": []string{"container"}},
		},
		{
			doc:      "before", // FIXME(thaJeztah): the "before" option was deprecated, and is no longer handled by the API server; https://github.com/moby/moby/pull/22138
			options:  ContainerListOptions{Before: "container"},
			expected: url.Values{"before": []string{"container"}},
		},
		{
			doc:      "limit",
			options:  ContainerListOptions{Limit: 1},
			expected: url.Values{"limit": []string{"1"}},
		},
		{
			doc: "filters",
			options: ContainerListOptions{
				Filters: make(Filters).
					Add("label", "label1").
					Add("label", "label2").
					Add("before", "container"),
			},
			expected: url.Values{"filters": []string{`{"before":{"container":true},"label":{"label1":true,"label2":true}}`}},