Extract persistence code into own class
This commit is contained in:
parent
4c76530832
commit
c5ebbc3c40
9 changed files with 163 additions and 149 deletions
|
@ -1,11 +1,7 @@
|
|||
package accounts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
@ -31,13 +27,11 @@ type accountantData struct {
|
|||
// Accountant manages a set of accounts
|
||||
type Accountant struct {
|
||||
Accounts map[uuid.UUID]Account `json:"accounts"`
|
||||
dataPath string
|
||||
}
|
||||
|
||||
// NewAccountant creates a new accountant
|
||||
func NewAccountant(dataPath string) *Accountant {
|
||||
func NewAccountant() *Accountant {
|
||||
return &Accountant{
|
||||
dataPath: dataPath,
|
||||
Accounts: make(map[uuid.UUID]Account),
|
||||
}
|
||||
}
|
||||
|
@ -64,40 +58,6 @@ func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// path returns the full path to the data file
|
||||
func (a Accountant) path() string {
|
||||
return path.Join(a.dataPath, kAccountsFileName)
|
||||
}
|
||||
|
||||
// Load will load the accountant from data
|
||||
func (a *Accountant) Load() error {
|
||||
// Don't load anything if the file doesn't exist
|
||||
_, err := os.Stat(a.path())
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Printf("File %s didn't exist, loading with fresh accounts data\n", a.path())
|
||||
return nil
|
||||
}
|
||||
|
||||
if b, err := ioutil.ReadFile(a.path()); err != nil {
|
||||
return err
|
||||
} else if err := json.Unmarshal(b, &a); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Save will save the accountant data out
|
||||
func (a *Accountant) Save() error {
|
||||
if b, err := json.MarshalIndent(a, "", "\t"); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if err := ioutil.WriteFile(a.path(), b, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AssignPrimary assigns primary ownership of an instance to an account
|
||||
func (a *Accountant) AssignPrimary(account uuid.UUID, instance uuid.UUID) error {
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package accounts
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
@ -9,7 +8,7 @@ import (
|
|||
|
||||
func TestNewAccountant(t *testing.T) {
|
||||
// Very basic verify here for now
|
||||
accountant := NewAccountant(os.TempDir())
|
||||
accountant := NewAccountant()
|
||||
if accountant == nil {
|
||||
t.Error("Failed to create accountant")
|
||||
}
|
||||
|
@ -17,7 +16,7 @@ func TestNewAccountant(t *testing.T) {
|
|||
|
||||
func TestAccountant_RegisterAccount(t *testing.T) {
|
||||
|
||||
accountant := NewAccountant(os.TempDir())
|
||||
accountant := NewAccountant()
|
||||
|
||||
// Start by making two accounts
|
||||
|
||||
|
@ -49,50 +48,8 @@ func TestAccountant_RegisterAccount(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAccountant_LoadSave(t *testing.T) {
|
||||
accountant := NewAccountant(os.TempDir())
|
||||
if len(accountant.Accounts) != 0 {
|
||||
t.Error("New accountant created with non-zero account number")
|
||||
}
|
||||
|
||||
name := uuid.New().String()
|
||||
a, err := accountant.RegisterAccount(name)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(accountant.Accounts) != 1 {
|
||||
t.Error("No new account made")
|
||||
} else if accountant.Accounts[a.Id].Name != name {
|
||||
t.Error("New account created with wrong name")
|
||||
}
|
||||
|
||||
// Save out the accountant
|
||||
if err := accountant.Save(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// Re-create the accountant
|
||||
accountant = NewAccountant(os.TempDir())
|
||||
if len(accountant.Accounts) != 0 {
|
||||
t.Error("New accountant created with non-zero account number")
|
||||
}
|
||||
|
||||
// Load the old accountant data
|
||||
if err := accountant.Load(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// Verify we have the same account again
|
||||
if len(accountant.Accounts) != 1 {
|
||||
t.Error("No account after load")
|
||||
} else if accountant.Accounts[a.Id].Name != name {
|
||||
t.Error("New account created with wrong name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountant_AssignPrimary(t *testing.T) {
|
||||
accountant := NewAccountant(os.TempDir())
|
||||
accountant := NewAccountant()
|
||||
if len(accountant.Accounts) != 0 {
|
||||
t.Error("New accountant created with non-zero account number")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue