Skip to content

Commit e7c7b23

Browse files
committed
end
1 parent d78e6e1 commit e7c7b23

File tree

11 files changed

+500
-16
lines changed

11 files changed

+500
-16
lines changed

session_6/unter/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type API struct {
1414

1515
func (a *API) Health(w http.ResponseWriter, r *http.Request) {
1616
// TODO: Health check
17+
a.log.Info("Health", "remote", r.RemoteAddr)
1718
fmt.Fprintln(w, "OK")
1819
}
1920

session_6/unter/api_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"log/slog"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestAPI_Health(t *testing.T) {
13+
w := httptest.NewRecorder()
14+
r := httptest.NewRequest(http.MethodGet, "/health", nil)
15+
16+
api := API{
17+
log: slog.New(slog.DiscardHandler),
18+
}
19+
mux := buildMux(&api)
20+
mux.ServeHTTP(w, r)
21+
/* Bypass mux
22+
api.Health(w, mux)
23+
api.Health(w, r)
24+
*/
25+
26+
resp := w.Result()
27+
require.Equal(t, http.StatusOK, resp.StatusCode)
28+
}

session_6/unter/backoffice_test.go

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package main
22

33
import (
4+
"math"
45
"os"
56
"testing"
67

78
"github.com/goccy/go-yaml"
9+
"github.com/stretchr/testify/require"
810
)
911

1012
type priceTest struct {
@@ -14,23 +16,62 @@ type priceTest struct {
1416
Want int `yaml:"want"`
1517
}
1618

17-
func TestRidePrice(t *testing.T) {
19+
func loadPriceCases(t *testing.T) []priceTest {
1820
data, err := os.ReadFile("testdata/price_tests.yml")
19-
if err != nil {
20-
t.Fatalf("failed to read test file: %v", err)
21-
}
21+
require.NoError(t, err, "failed to read test file")
2222

2323
var priceTests []priceTest
24-
if err := yaml.Unmarshal(data, &priceTests); err != nil {
25-
t.Fatalf("failed to parse test file: %v", err)
26-
}
24+
err = yaml.Unmarshal(data, &priceTests)
25+
require.NoError(t, err, "failed to parse test file")
26+
27+
return priceTests
28+
}
2729

28-
for _, tt := range priceTests {
30+
func TestRidePrice(t *testing.T) {
31+
for _, tt := range loadPriceCases(t) {
2932
t.Run(tt.Name, func(t *testing.T) {
3033
got := RidePrice(tt.Distance, tt.Shared)
31-
if got != tt.Want {
32-
t.Errorf("RidePrice(%v, %v) = %v, want %v", tt.Distance, tt.Shared, got, tt.Want)
33-
}
34+
require.Equal(t, tt.Want, got, "RidePrice(%v, %v)", tt.Distance, tt.Shared)
3435
})
3536
}
3637
}
38+
39+
var isCI = os.Getenv("CI") != ""
40+
41+
// In Jenkins use BUILD_NUMBER
42+
43+
func TestInCI(t *testing.T) {
44+
if !isCI {
45+
t.Skip("not in CI")
46+
}
47+
48+
t.Log("testing")
49+
}
50+
51+
/* Option to run subset of tests
52+
go test -run Example
53+
go test -short # Use testing.Short() + t.Skip in your code
54+
Environment variable
55+
Build tags (//go:build web)
56+
*/
57+
58+
func FuzzRidePrice(f *testing.F) {
59+
/*
60+
file, err := os.Create("/tmp/fuzz.txt")
61+
require.NoError(f, err)
62+
defer file.Close()
63+
*/
64+
65+
f.Add(0.0, true)
66+
f.Fuzz(func(t *testing.T, distance float64, shared bool) {
67+
68+
// Sometimes you'll need to adjust the fuzzed data
69+
distance = math.Abs(distance)
70+
71+
//fmt.Fprintf(file, "%v %v\n", distance, shared)
72+
price := RidePrice(distance, shared)
73+
require.Greater(t, price, 0)
74+
})
75+
}
76+
77+
// go test -fuzz . -fuzztime 30s

session_6/unter/go.mod

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,64 @@ go 1.25
44

55
require (
66
github.com/goccy/go-yaml v1.19.2
7+
github.com/stretchr/testify v1.11.1
8+
github.com/testcontainers/testcontainers-go v0.40.0
79
go.etcd.io/bbolt v1.4.3
810
)
911

1012
require (
11-
github.com/stretchr/testify v1.11.1 // indirect
13+
dario.cat/mergo v1.0.2 // indirect
14+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
15+
github.com/Microsoft/go-winio v0.6.2 // indirect
16+
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
17+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
18+
github.com/containerd/errdefs v1.0.0 // indirect
19+
github.com/containerd/errdefs/pkg v0.3.0 // indirect
20+
github.com/containerd/log v0.1.0 // indirect
21+
github.com/containerd/platforms v0.2.1 // indirect
22+
github.com/cpuguy83/dockercfg v0.3.2 // indirect
23+
github.com/davecgh/go-spew v1.1.1 // indirect
24+
github.com/distribution/reference v0.6.0 // indirect
25+
github.com/docker/docker v28.5.1+incompatible // indirect
26+
github.com/docker/go-connections v0.6.0 // indirect
27+
github.com/docker/go-units v0.5.0 // indirect
28+
github.com/ebitengine/purego v0.8.4 // indirect
29+
github.com/felixge/httpsnoop v1.0.4 // indirect
30+
github.com/go-logr/logr v1.4.3 // indirect
31+
github.com/go-logr/stdr v1.2.2 // indirect
32+
github.com/go-ole/go-ole v1.2.6 // indirect
33+
github.com/google/uuid v1.6.0 // indirect
34+
github.com/klauspost/compress v1.18.0 // indirect
35+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
36+
github.com/magiconair/properties v1.8.10 // indirect
37+
github.com/moby/docker-image-spec v1.3.1 // indirect
38+
github.com/moby/go-archive v0.1.0 // indirect
39+
github.com/moby/patternmatcher v0.6.0 // indirect
40+
github.com/moby/sys/sequential v0.6.0 // indirect
41+
github.com/moby/sys/user v0.4.0 // indirect
42+
github.com/moby/sys/userns v0.1.0 // indirect
43+
github.com/moby/term v0.5.0 // indirect
44+
github.com/morikuni/aec v1.0.0 // indirect
45+
github.com/opencontainers/go-digest v1.0.0 // indirect
46+
github.com/opencontainers/image-spec v1.1.1 // indirect
47+
github.com/pkg/errors v0.9.1 // indirect
48+
github.com/pmezard/go-difflib v1.0.0 // indirect
49+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
50+
github.com/shirou/gopsutil/v4 v4.25.6 // indirect
51+
github.com/sirupsen/logrus v1.9.3 // indirect
52+
github.com/tklauser/go-sysconf v0.3.12 // indirect
53+
github.com/tklauser/numcpus v0.6.1 // indirect
54+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
55+
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
56+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
57+
go.opentelemetry.io/otel v1.39.0 // indirect
58+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.39.0 // indirect
59+
go.opentelemetry.io/otel/metric v1.39.0 // indirect
60+
go.opentelemetry.io/otel/sdk v1.39.0 // indirect
61+
go.opentelemetry.io/otel/trace v1.39.0 // indirect
62+
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
63+
golang.org/x/crypto v0.43.0 // indirect
1264
golang.org/x/sys v0.39.0 // indirect
65+
google.golang.org/protobuf v1.36.10 // indirect
66+
gopkg.in/yaml.v3 v3.0.1 // indirect
1367
)

0 commit comments

Comments
 (0)