File: search_test.go

package info (click to toggle)
golang-github-apptainer-container-library-client 1.4.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 384 kB
  • sloc: makefile: 5
file content (101 lines) | stat: -rw-r--r-- 2,485 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package client

import (
	"context"
	"net/http"
	"reflect"
	"testing"

	jsonresp "github.com/sylabs/json-resp"
)

func Test_Search(t *testing.T) {
	tests := []struct {
		description   string
		code          int
		body          interface{}
		reqCallback   func(*http.Request, *testing.T)
		searchArgs    map[string]string
		expectResults *SearchResults
		expectError   bool
	}{
		{
			description: "ValidRequest",
			searchArgs: map[string]string{
				"value": "test",
			},
			code:          http.StatusOK,
			body:          jsonresp.Response{Data: testSearch},
			expectResults: &testSearch,
			expectError:   false,
		},
		{
			description: "ValidRequestMultiArg",
			searchArgs: map[string]string{
				"value":  "test",
				"arch":   "x86_64",
				"signed": "true",
			},
			code:          http.StatusOK,
			body:          jsonresp.Response{Data: testSearch},
			expectResults: &testSearch,
			expectError:   false,
		},
		{
			description: "InternalServerError",
			searchArgs:  map[string]string{"value": "test"},
			code:        http.StatusInternalServerError,
			expectError: true,
		},
		{
			description: "BadRequest",
			searchArgs:  map[string]string{},
			code:        http.StatusBadRequest,
			expectError: true,
		},
		{
			description: "InvalidValue",
			searchArgs:  map[string]string{"value": "aa"},
			code:        http.StatusBadRequest,
			expectError: true,
		},
	}

	// Loop over test cases
	for _, tt := range tests {
		t.Run(tt.description, func(t *testing.T) {
			m := mockService{
				t:           t,
				code:        tt.code,
				body:        tt.body,
				reqCallback: tt.reqCallback,
				httpPath:    "/v1/search",
			}

			m.Run()
			defer m.Stop()

			c, err := NewClient(&Config{AuthToken: testToken, BaseURL: m.baseURI})
			if err != nil {
				t.Errorf("Error initializing client: %v", err)
			}

			results, err := c.Search(context.Background(), tt.searchArgs)

			if err != nil && !tt.expectError {
				t.Errorf("Unexpected error: %v", err)
			}
			if err == nil && tt.expectError {
				t.Errorf("Unexpected success. Expected error.")
			}
			if !reflect.DeepEqual(results, tt.expectResults) {
				t.Errorf("Got created collection %v - expected %v", results, tt.expectResults)
			}
		})
	}
}