chore: initialize containerized Go learning workbook (2026 DevOps/SRE)
This commit is contained in:
commit
84be3e5d77
64 files changed
+1382
No files matched your search
@@ -0,0 +1,10 @@
|
||||
# 04 — Dobre Praktyki Testowania (`t.TempDir()`, In-Memory Fakes)
|
||||
|
||||
## Zadania (`testing_ex.go`)
|
||||
1. `WriteAndReadLines(path string, lines []string) ([]string, error)` — zapisz linie do pliku i odczytaj je z powrotem.
|
||||
2. `FeatureFlags.IsEnabled(name string) bool` — korzysta z małego interfejsu `Store` (łatwego do testowania z In-Memory Fake bez mocków).
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/02-engineering-2026/04-testing-best-practices
|
||||
```
|
||||
@@ -0,0 +1,17 @@
|
||||
package testing_ex
|
||||
|
||||
type Store interface {
|
||||
Get(key string) (string, bool)
|
||||
}
|
||||
|
||||
type FeatureFlags struct {
|
||||
Store Store
|
||||
}
|
||||
|
||||
func (f FeatureFlags) IsEnabled(name string) bool {
|
||||
panic("TODO: pobierz z f.Store.Get(name); zwróć true dla 'true' lub '1' (case-insensitive)")
|
||||
}
|
||||
|
||||
func WriteAndReadLines(path string, lines []string) ([]string, error) {
|
||||
panic("TODO: zapisz lines (os.WriteFile) i odczytaj z powrotem (os.ReadFile)")
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package testing_ex
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeStore map[string]string
|
||||
|
||||
func (f fakeStore) Get(key string) (string, bool) { v, ok := f[key]; return v, ok }
|
||||
|
||||
func TestFeatureFlagsTableDriven(t *testing.T) {
|
||||
flags := FeatureFlags{Store: fakeStore{"a": "true", "b": "1", "c": "false"}}
|
||||
cases := []struct {
|
||||
name string
|
||||
want bool
|
||||
}{{"a", true}, {"b", true}, {"c", false}, {"missing", false}}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := flags.IsEnabled(tc.name); got != tc.want {
|
||||
t.Errorf("IsEnabled(%q) = %v, want %v", tc.name, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteAndReadLines(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "lines.txt")
|
||||
want := []string{"alpha", "beta", "gamma"}
|
||||
got, err := WriteAndReadLines(path, want)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user