Passed
Push — main ( 816aa1...fec294 )
by Acho
01:22
created

checkouts_service_test.go   A

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 70
dl 0
loc 145
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A client.TestCheckoutService_CreateWithError 0 18 1
A client.TestCheckoutService_List 0 20 1
A client.TestCheckoutService_Create 0 28 1
A client.TestCheckoutService_ListWithError 0 18 1
A client.TestCheckoutService_GetWithError 0 18 1
A client.TestCheckoutService_Get 0 20 1
1
package client
2
3
import (
4
	"context"
5
	"net/http"
6
	"testing"
7
	"time"
8
9
	"github.com/NdoleStudio/lemonsqueezy-go/internal/helpers"
10
	"github.com/NdoleStudio/lemonsqueezy-go/internal/stubs"
11
	"github.com/stretchr/testify/assert"
12
)
13
14
func TestCheckoutService_Create(t *testing.T) {
15
	// Setup
16
	t.Parallel()
17
18
	// Arrange
19
	server := helpers.MakeTestServer(http.StatusCreated, stubs.CheckoutGetResponse())
20
	client := New(WithBaseURL(server.URL))
21
22
	// Act
23
	checkout, response, err := client.Checkouts.Create(context.Background(), &CheckoutCreateParams{
24
		CustomPrice:     5000,
25
		EnabledVariants: []int{1},
26
		ButtonColor:     "#2DD272",
27
		CustomData:      map[string]string{"user_id": "123"},
28
		ExpiresAt:       time.Now().UTC(),
29
		StoreID:         "1",
30
		VariantID:       "1",
31
	})
32
33
	// Assert
34
	assert.Nil(t, err)
35
36
	assert.Equal(t, http.StatusCreated, response.HTTPResponse.StatusCode)
37
	assert.Equal(t, stubs.CheckoutGetResponse(), *response.Body)
38
	assert.Equal(t, "5e8b546c-c561-4a2c-a586-40c18bb2a195", checkout.Data.ID)
39
40
	// Teardown
41
	server.Close()
42
}
43
44
func TestCheckoutService_CreateWithError(t *testing.T) {
45
	// Setup
46
	t.Parallel()
47
48
	// Arrange
49
	server := helpers.MakeTestServer(http.StatusInternalServerError, nil)
50
	client := New(WithBaseURL(server.URL))
51
52
	// Act
53
	_, response, err := client.Checkouts.Create(context.Background(), &CheckoutCreateParams{})
54
55
	// Assert
56
	assert.NotNil(t, err)
57
58
	assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode)
59
60
	// Teardown
61
	server.Close()
62
}
63
64
func TestCheckoutService_Get(t *testing.T) {
65
	// Setup
66
	t.Parallel()
67
68
	// Arrange
69
	server := helpers.MakeTestServer(http.StatusOK, stubs.CheckoutGetResponse())
70
	client := New(WithBaseURL(server.URL))
71
72
	// Act
73
	checkout, response, err := client.Checkouts.Get(context.Background(), "1")
74
75
	// Assert
76
	assert.Nil(t, err)
77
78
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
79
	assert.Equal(t, stubs.CheckoutGetResponse(), *response.Body)
80
	assert.Equal(t, "5e8b546c-c561-4a2c-a586-40c18bb2a195", checkout.Data.ID)
81
82
	// Teardown
83
	server.Close()
84
}
85
86
func TestCheckoutService_GetWithError(t *testing.T) {
87
	// Setup
88
	t.Parallel()
89
90
	// Arrange
91
	server := helpers.MakeTestServer(http.StatusInternalServerError, nil)
92
	client := New(WithBaseURL(server.URL))
93
94
	// Act
95
	_, response, err := client.Checkouts.Get(context.Background(), "1")
96
97
	// Assert
98
	assert.NotNil(t, err)
99
100
	assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode)
101
102
	// Teardown
103
	server.Close()
104
}
105
106
func TestCheckoutService_List(t *testing.T) {
107
	// Setup
108
	t.Parallel()
109
110
	// Arrange
111
	server := helpers.MakeTestServer(http.StatusOK, stubs.CheckoutListResponse())
112
	client := New(WithBaseURL(server.URL))
113
114
	// Act
115
	checkouts, response, err := client.Checkouts.List(context.Background())
116
117
	// Assert
118
	assert.Nil(t, err)
119
120
	assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
121
	assert.Equal(t, stubs.CheckoutListResponse(), *response.Body)
122
	assert.Equal(t, 1, len(checkouts.Data))
123
124
	// Teardown
125
	server.Close()
126
}
127
128
func TestCheckoutService_ListWithError(t *testing.T) {
129
	// Setup
130
	t.Parallel()
131
132
	// Arrange
133
	server := helpers.MakeTestServer(http.StatusInternalServerError, nil)
134
	client := New(WithBaseURL(server.URL))
135
136
	// Act
137
	_, response, err := client.Checkouts.List(context.Background())
138
139
	// Assert
140
	assert.NotNil(t, err)
141
142
	assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode)
143
144
	// Teardown
145
	server.Close()
146
}
147