First real commit. Setting up for full CI/CD and testing pipeline
This commit is contained in:
176
internal/storage/storage_test.go
Normal file
176
internal/storage/storage_test.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testSettings struct {
|
||||
ReadingSpeed int `json:"readingSpeed"`
|
||||
Theme string `json:"theme"`
|
||||
}
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
input := testSettings{
|
||||
ReadingSpeed: 300,
|
||||
Theme: "dark",
|
||||
}
|
||||
|
||||
got, err := Encode(input)
|
||||
if err != nil {
|
||||
t.Fatalf("Encode() returned error: %v", err)
|
||||
}
|
||||
|
||||
var decoded testSettings
|
||||
|
||||
if err := json.Unmarshal(got, &decoded); err != nil {
|
||||
t.Fatalf("encoded JSON is invalid: %v", err)
|
||||
}
|
||||
|
||||
if decoded != input {
|
||||
t.Errorf("decoded value = %+v, want %+v", decoded, input)
|
||||
}
|
||||
|
||||
if !strings.Contains(string(got), "\n") {
|
||||
t.Error("Encode() should produce indented JSON")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeNil(t *testing.T) {
|
||||
got, err := Encode(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Encode(nil) returned error: %v", err)
|
||||
}
|
||||
|
||||
if string(got) != "null" {
|
||||
t.Errorf("Encode(nil) = %q, want %q", got, "null")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeUnsupportedValue(t *testing.T) {
|
||||
_, err := Encode(make(chan int))
|
||||
|
||||
if err == nil {
|
||||
t.Error("Encode() expected an error for unsupported value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
input := []byte(`{
|
||||
"readingSpeed": 400,
|
||||
"theme": "light"
|
||||
}`)
|
||||
|
||||
var got testSettings
|
||||
|
||||
if err := Decode(input, &got); err != nil {
|
||||
t.Fatalf("Decode() returned error: %v", err)
|
||||
}
|
||||
|
||||
expected := testSettings{
|
||||
ReadingSpeed: 400,
|
||||
Theme: "light",
|
||||
}
|
||||
|
||||
if got != expected {
|
||||
t.Errorf("Decode() = %+v, want %+v", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeInvalidJSON(t *testing.T) {
|
||||
input := []byte(`{"readingSpeed": }`)
|
||||
|
||||
var got testSettings
|
||||
|
||||
if err := Decode(input, &got); err == nil {
|
||||
t.Error("Decode() expected an error for invalid JSON")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeNilTarget(t *testing.T) {
|
||||
err := Decode([]byte(`{"readingSpeed":300}`), nil)
|
||||
|
||||
if err == nil {
|
||||
t.Error("Decode() expected an error for nil target")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeEmptyJSON(t *testing.T) {
|
||||
var got testSettings
|
||||
|
||||
if err := Decode([]byte(``), &got); err == nil {
|
||||
t.Error("Decode() expected an error for empty input")
|
||||
}
|
||||
}
|
||||
|
||||
// NEW
|
||||
func TestEncodeDecode(t *testing.T) {
|
||||
original := testSettings{
|
||||
ReadingSpeed: 400,
|
||||
Theme: "dark",
|
||||
}
|
||||
|
||||
data, err := Encode(original)
|
||||
if err != nil {
|
||||
t.Fatalf("Encode() error = %v", err)
|
||||
}
|
||||
|
||||
var decoded testSettings
|
||||
|
||||
if err := Decode(data, &decoded); err != nil {
|
||||
t.Fatalf("Decode() error = %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(decoded, original) {
|
||||
t.Errorf("decoded = %+v, want %+v", decoded, original)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveAndLoad(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "settings", "settings.json")
|
||||
|
||||
original := testSettings{
|
||||
ReadingSpeed: 500,
|
||||
Theme: "light",
|
||||
}
|
||||
|
||||
if err := Save(path, original); err != nil {
|
||||
t.Fatalf("Save() error = %v", err)
|
||||
}
|
||||
|
||||
var loaded testSettings
|
||||
|
||||
if err := Load(path, &loaded); err != nil {
|
||||
t.Fatalf("Load() error = %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(loaded, original) {
|
||||
t.Errorf("loaded = %+v, want %+v", loaded, original)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMissingFile(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "missing.json")
|
||||
|
||||
var target testSettings
|
||||
|
||||
err := Load(path, &target)
|
||||
|
||||
if !os.IsNotExist(err) {
|
||||
t.Errorf("Load() error = %v, want file-not-found error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveInvalidValue(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "settings.json")
|
||||
|
||||
err := Save(path, make(chan int))
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Save() expected an error for an unsupported JSON value")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user