package main import "testing" func TestNewContactsStore(t *testing.T) { t.Run("normal build", func(t *testing.T) { cs, err := NewContactsStore() if err != nil { t.Fatal(err) } if cs == nil { t.Fatal("nil store created") } if len(cs.data) == 0 { t.Fatal("empty store") } }) t.Run("building with missing file", func(t *testing.T) { scf := ContactsFile t.Cleanup(func() { ContactsFile = scf }) ContactsFile = "badfile.json" cs, err := NewContactsStore() if err == nil || cs != nil { t.Fatalf("unexpected success creation from missing file") } }) } func TestContactsStore_Load(t *testing.T) { var cs *ContactsStore t.Run("nil store", func(t *testing.T) { if err := cs.Load(); err == nil { t.Fatalf("unexpected success on loading nil store") } }) cs = &ContactsStore{ data: make(map[int]Contact), } t.Run("valid load", func(t *testing.T) { if err := cs.Load(); err != nil { t.Fatalf("unexpected error on loading valid store: err") } }) t.Run("missing file", func(t *testing.T) { scf := ContactsFile t.Cleanup(func() { ContactsFile = scf }) ContactsFile = "badfile.json" if err := cs.Load(); err == nil { t.Fatalf("unexpected success on loading missing file") } }) t.Run("non-json file", func(t *testing.T) { scf := ContactsFile t.Cleanup(func() { ContactsFile = scf }) ContactsFile = "contacts_model_test.go" if err := cs.Load(); err == nil { t.Fatalf("unexpected success on loading non-json file") } }) }