new exporter version

This commit is contained in:
a.pivkin
2025-12-20 17:00:05 +03:00
parent bf8f562768
commit af4a1c46e1
171 changed files with 0 additions and 0 deletions
@@ -0,0 +1,5 @@
# How to Use Mock Testing in Go
This tutorial demonstrates several techniques for using mock objects in Go tests.
Find the tutorial [here]().
@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="Go" enabled="true" />
</module>
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="TestProcessUser_All" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="fetchuser" />
<working_directory value="$PROJECT_DIR$" />
<kind value="PACKAGE" />
<package value="github.com/JetBrains/go-code-samples/mock-testing/fetchuser" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<method v="2" />
</configuration>
</component>
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="TestProcessUser_HigherOrderFunctions" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="fetchuser" />
<working_directory value="$PROJECT_DIR$" />
<kind value="PACKAGE" />
<package value="github.com/JetBrains/go-code-samples/mock-testing/fetchuser" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<pattern value="^\QTestProcessUser_HigherOrderFunctions\E$" />
<method v="2" />
</configuration>
</component>
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="TestProcessUser_HttpTest" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="fetchuser" />
<working_directory value="$PROJECT_DIR$" />
<kind value="PACKAGE" />
<package value="github.com/JetBrains/go-code-samples/mock-testing/fetchuser" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<pattern value="^\QTestProcessUser_HttpTest\E$" />
<method v="2" />
</configuration>
</component>
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="TestProcessUser_InterfaceMock1" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="fetchuser" />
<working_directory value="$PROJECT_DIR$" />
<kind value="PACKAGE" />
<package value="github.com/JetBrains/go-code-samples/mock-testing/fetchuser" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<pattern value="^\QTestProcessUser_InterfaceMock1\E$" />
<method v="2" />
</configuration>
</component>
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="TestProcessUser_InterfaceMock2" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="fetchuser" />
<working_directory value="$PROJECT_DIR$" />
<kind value="PACKAGE" />
<package value="github.com/JetBrains/go-code-samples/mock-testing/fetchuser" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<pattern value="^\QTestProcessUser_InterfaceMock1\E$" />
<method v="2" />
</configuration>
</component>
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="TestProcessUser_Mockgen" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="fetchuser" />
<working_directory value="$PROJECT_DIR$" />
<kind value="PACKAGE" />
<package value="github.com/JetBrains/go-code-samples/mock-testing/fetchuser" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<pattern value="^\QTestProcessUser_Mockgen\E$" />
<method v="2" />
</configuration>
</component>
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="TestProcessUser_TestifyMock" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="fetchuser" />
<working_directory value="$PROJECT_DIR$" />
<kind value="PACKAGE" />
<package value="github.com/JetBrains/go-code-samples/mock-testing/fetchuser" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<pattern value="^\QTestProcessUser_TestifyMock\E$" />
<method v="2" />
</configuration>
</component>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>
@@ -0,0 +1,47 @@
package fetchuser
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
type APIFetcher interface {
FetchData(id int) (User, error)
}
type RealAPIFetcher struct {
ApiURL string
}
func (ra *RealAPIFetcher) FetchData(id int) (User, error) {
resp, err := http.Get(fmt.Sprintf("%s/users/%d", ra.ApiURL, id))
if err != nil {
return User{}, err
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return User{}, err
}
var user User
err = json.Unmarshal(bodyBytes, &user)
return user, err
}
func ProcessUser(fetcher APIFetcher, id int) (User, error) {
user, err := fetcher.FetchData(id)
if err != nil {
return User{}, err
}
// Process the user data.
return user, nil
}
@@ -0,0 +1,36 @@
package fetchuser
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type FetchDataFunc func(url string, id int) (User, error)
func RealFetchData(url string, id int) (User, error) {
resp, err := http.Get(fmt.Sprintf("%s/users/%d", url, id))
if err != nil {
return User{}, err
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return User{}, err
}
var user User
err = json.Unmarshal(bodyBytes, &user)
return user, err
}
func ProcessUserHOF(fetchData FetchDataFunc, url string, id int) (User, error) {
user, err := fetchData(url, id)
if err != nil {
return User{}, err
}
// Process the user data.
return user, nil
}
@@ -0,0 +1,22 @@
package fetchuser
import (
"testing"
)
func TestProcessUser_HigherOrderFunctions(t *testing.T) {
user := User{ID: 1, Name: "Alice"}
var mockFetcher FetchDataFunc = func(url string, id int) (User, error) {
return user, nil
}
result, err := ProcessUserHOF(mockFetcher, "noURL", 1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if result != user {
t.Errorf("Expected user: %v, got: %v", user, result)
}
}
@@ -0,0 +1,35 @@
package fetchuser
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestProcessUser_HttpTest(t *testing.T) {
user := User{ID: 1, Name: "Alice"}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userJSON, _ := json.Marshal(user)
n, err := w.Write(userJSON)
if err != nil {
t.Errorf("test server: unexpected error after writing %d bytes: %v", n, err)
}
}))
defer ts.Close()
fetcher := &RealAPIFetcher{
ApiURL: ts.URL, // mock URL provided by httptest
}
http.DefaultClient = ts.Client() // client provided by httptest
result, err := ProcessUser(fetcher, 1)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if result != user {
t.Errorf("expected user: %v, got: %v", user, result)
}
}
@@ -0,0 +1,25 @@
package fetchuser
import (
"testing"
)
type MockInterfaceFetcher struct {
u User
}
func (m *MockInterfaceFetcher) FetchData(_ int) (User, error) {
return m.u, nil
}
func TestProcessUser_InterfaceMock(t *testing.T) {
user := User{ID: 1, Name: "Alice"}
result, err := ProcessUser(&MockInterfaceFetcher{user}, 1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if result != user {
t.Errorf("Expected user: %v, got: %v", user, result)
}
}
@@ -0,0 +1,49 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: fetchuser.go
// Package fetchuser is a generated GoMock package.
package fetchuser
import (
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockAPIFetcher is a mock of APIFetcher interface.
type MockAPIFetcher struct {
ctrl *gomock.Controller
recorder *MockAPIFetcherMockRecorder
}
// MockAPIFetcherMockRecorder is the mock recorder for MockAPIFetcher.
type MockAPIFetcherMockRecorder struct {
mock *MockAPIFetcher
}
// NewMockAPIFetcher creates a new mock instance.
func NewMockAPIFetcher(ctrl *gomock.Controller) *MockAPIFetcher {
mock := &MockAPIFetcher{ctrl: ctrl}
mock.recorder = &MockAPIFetcherMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockAPIFetcher) EXPECT() *MockAPIFetcherMockRecorder {
return m.recorder
}
// FetchData mocks base method.
func (m *MockAPIFetcher) FetchData(id int) (User, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FetchData", id)
ret0, _ := ret[0].(User)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FetchData indicates an expected call of FetchData.
func (mr *MockAPIFetcherMockRecorder) FetchData(id interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FetchData", reflect.TypeOf((*MockAPIFetcher)(nil).FetchData), id)
}
@@ -0,0 +1,25 @@
package fetchuser
import (
"testing"
"go.uber.org/mock/gomock"
)
func TestProcessUser_Mockgen(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
user := User{ID: 1, Name: "Alice"}
mockFetcher := NewMockAPIFetcher(ctrl)
mockFetcher.EXPECT().FetchData(1).Return(user, nil)
result, err := ProcessUser(mockFetcher, 1)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if result != user {
t.Errorf("expected user: %v, got: %v", user, result)
}
}
@@ -0,0 +1,31 @@
package fetchuser
import (
"testing"
"github.com/stretchr/testify/mock"
)
type MockTestifyFetcher struct {
mock.Mock
}
func (m *MockTestifyFetcher) FetchData(id int) (User, error) {
args := m.Called(id)
return args.Get(0).(User), args.Error(1)
}
func TestProcessUser_TestifyMock(t *testing.T) {
user := User{ID: 1, Name: "Alice"}
mockFetcher := new(MockTestifyFetcher)
mockFetcher.On("FetchData", 1).Return(user, nil)
result, err := ProcessUser(mockFetcher, 1)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if result != user {
t.Errorf("expected user: %v, got: %v", user, result)
}
}
@@ -0,0 +1,15 @@
module github.com/JetBrains/go-code-samples/mock-testing/fetchuser
go 1.20
require (
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.2.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
@@ -0,0 +1,20 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/mock v0.2.0 h1:TaP3xedm7JaAgScZO7tlvlKrqT0p7I6OsdGB5YNSMDU=
go.uber.org/mock v0.2.0/go.mod h1:J0y0rp9L3xiff1+ZBfKxlC1fz2+aO16tw0tsDOixfuM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=