Implement saving and loading for account data, currently a basic json file
This commit is contained in:
parent
f1e6311366
commit
179dd3f984
11 changed files with 306 additions and 130 deletions
pkg/accounts
86
pkg/accounts/accounts.go
Normal file
86
pkg/accounts/accounts.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package accounts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const kDefaultSavePath = "/tmp/accounts.json"
|
||||
|
||||
// Account represents a registered user
|
||||
type Account struct {
|
||||
// Name simply describes the account and must be unique
|
||||
Name string `json:"name"`
|
||||
|
||||
// id represents a unique ID per account and is set one registered
|
||||
Id uuid.UUID `json:"id"`
|
||||
}
|
||||
|
||||
// Represents the accountant data to store
|
||||
type accountantData struct {
|
||||
Accounts []Account `json:"accounts"`
|
||||
}
|
||||
|
||||
// Accountant manages a set of accounts
|
||||
type Accountant struct {
|
||||
data accountantData
|
||||
}
|
||||
|
||||
// NewAccountant creates a new accountant
|
||||
func NewAccountant() *Accountant {
|
||||
return &Accountant{}
|
||||
}
|
||||
|
||||
// RegisterAccount adds an account to the set of internal accounts
|
||||
func (a *Accountant) RegisterAccount(acc Account) (Account, error) {
|
||||
|
||||
// Set the account ID to a new UUID
|
||||
acc.Id = uuid.New()
|
||||
|
||||
// Verify this acount isn't already registered
|
||||
for _, a := range a.data.Accounts {
|
||||
if a.Name == acc.Name {
|
||||
return Account{}, fmt.Errorf("Account name already registered")
|
||||
} else if a.Id == acc.Id {
|
||||
return Account{}, fmt.Errorf("Account ID already registered")
|
||||
}
|
||||
}
|
||||
|
||||
// Simply add the account to the list
|
||||
a.data.Accounts = append(a.data.Accounts, acc)
|
||||
|
||||
return acc, nil
|
||||
}
|
||||
|
||||
// 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(kDefaultSavePath)
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Printf("File %s didn't exist, loading with fresh accounts data\n", kDefaultSavePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
if b, err := ioutil.ReadFile(kDefaultSavePath); err != nil {
|
||||
return err
|
||||
} else if err := json.Unmarshal(b, &a.data); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Save will save the accountant data out
|
||||
func (a *Accountant) Save() error {
|
||||
if b, err := json.Marshal(a.data); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if err := ioutil.WriteFile(kDefaultSavePath, b, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
92
pkg/accounts/accounts_test.go
Normal file
92
pkg/accounts/accounts_test.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package accounts
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewAccountant(t *testing.T) {
|
||||
// Very basic verify here for now
|
||||
accountant := NewAccountant()
|
||||
if accountant == nil {
|
||||
t.Error("Failed to create accountant")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountant_RegisterAccount(t *testing.T) {
|
||||
|
||||
accountant := NewAccountant()
|
||||
|
||||
// Start by making two accounts
|
||||
|
||||
namea := "one"
|
||||
a := Account{Name: namea}
|
||||
acca, err := accountant.RegisterAccount(a)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if acca.Name != namea {
|
||||
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", namea, acca.Name)
|
||||
}
|
||||
|
||||
nameb := "two"
|
||||
b := Account{Name: nameb}
|
||||
accb, err := accountant.RegisterAccount(b)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if accb.Name != nameb {
|
||||
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
|
||||
}
|
||||
|
||||
// Verify our accounts have differing IDs
|
||||
if acca.Id == accb.Id {
|
||||
t.Error("Duplicate account IDs fo separate accounts")
|
||||
}
|
||||
|
||||
// Verify another request gets rejected
|
||||
_, err = accountant.RegisterAccount(a)
|
||||
if err == nil {
|
||||
t.Error("Duplicate account name did not produce error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountant_LoadSave(t *testing.T) {
|
||||
accountant := NewAccountant()
|
||||
if len(accountant.data.Accounts) != 0 {
|
||||
t.Error("New accountant created with non-zero account number")
|
||||
}
|
||||
|
||||
name := "one"
|
||||
a := Account{Name: name}
|
||||
a, err := accountant.RegisterAccount(a)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(accountant.data.Accounts) != 1 {
|
||||
t.Error("No new account made")
|
||||
} else if accountant.data.Accounts[0].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()
|
||||
if len(accountant.data.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.data.Accounts) != 1 {
|
||||
t.Error("No account after load")
|
||||
} else if accountant.data.Accounts[0].Name != name {
|
||||
t.Error("New account created with wrong name")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue