Skip to content

Commit

Permalink
feat(config): add mock config and tests (#5)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Semagin <[email protected]>
  • Loading branch information
pixel365 authored Feb 15, 2025
1 parent 1f46d83 commit 581ca67
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: go mod download

- name: Run tests
run: go test -coverprofile=coverage.txt
run: go test ./... -coverprofile=coverage.txt

- name: Upload results to Codecov
uses: codecov/codecov-action@v5
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: fa fmt lint
.PHONY: fa fmt lint test

fa:
@fieldalignment -fix ./...
Expand All @@ -9,4 +9,7 @@ fmt:
@golines -w .

lint:
@golangci-lint run
@golangci-lint run

test:
@go $@ ./...
2 changes: 1 addition & 1 deletion internal/config/config_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (o *Config) AddModules(modules ...model.Module) {
o.Modules = append(o.Modules, modules...)
}

func Load() (*Config, error) {
func NewConfig() (*Config, error) {
var err error

filePath, err := path()
Expand Down
75 changes: 75 additions & 0 deletions internal/config/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package config

import (
"fmt"
"time"

"github.com/pixel365/bx/internal/model"
)

type MockConfig struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Accounts []model.Account `json:"accounts,omitempty"`
Modules []model.Module `json:"modules,omitempty"`
}

func (o *MockConfig) PrintSummary(verbose bool) {
if verbose {
fmt.Printf("Created At: %s\n", o.CreatedAt)
fmt.Printf("Updated At: %s\n", o.UpdatedAt)
fmt.Printf("Accounts: %d\n", len(o.Accounts))
fmt.Printf("Modules: %d\n", len(o.Modules))
} else {
fmt.Printf("Created At: %s\n", o.CreatedAt)
fmt.Printf("Updated At: %s\n", o.UpdatedAt)
}
}

func (o *MockConfig) Save() error {
return nil
}

func (o *MockConfig) Reset() error {
now := time.Now().UTC()
o.CreatedAt = now
o.UpdatedAt = now
o.Accounts = nil
o.Modules = nil

return o.Save()
}

func (o *MockConfig) GetAccounts() []model.Account {
return o.Accounts
}

func (o *MockConfig) GetModules() []model.Module {
return o.Modules
}

func (o *MockConfig) SetAccounts(accounts ...model.Account) {
o.Accounts = accounts
}

func (o *MockConfig) SetModules(modules ...model.Module) {
o.Modules = modules
}

func (o *MockConfig) AddAccounts(accounts ...model.Account) {
o.Accounts = append(o.Accounts, accounts...)
}

func (o *MockConfig) AddModules(modules ...model.Module) {
o.Modules = append(o.Modules, modules...)
}

func NewMockConfig() (*MockConfig, error) {
now := time.Now().UTC()
cfg := &MockConfig{
CreatedAt: now,
UpdatedAt: now,
}

return cfg, nil
}
234 changes: 234 additions & 0 deletions internal/config/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
package config

import (
"testing"
"time"

"github.com/pixel365/bx/internal/model"
)

func TestNewMockConfig(t *testing.T) {
cfg, _ := NewMockConfig()
tests := []struct {
want *MockConfig
name string
wantErr bool
}{
{cfg, "success", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewMockConfig()
if (err != nil) != tt.wantErr {
t.Errorf("NewMockConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}

if tt.want.CreatedAt.Format(time.RFC3339) != got.CreatedAt.Format(time.RFC3339) {
t.Errorf("NewMockConfig() = %v, want %v", got, tt.want)
}

if tt.want.UpdatedAt.Format(time.RFC3339) != got.UpdatedAt.Format(time.RFC3339) {
t.Errorf("NewMockConfig() = %v, want %v", got, tt.want)
}

if len(tt.want.Modules) != len(got.Modules) {
t.Errorf("NewMockConfig() = %v, want %v", got, tt.want)
}

if len(tt.want.Accounts) != len(got.Accounts) {
t.Errorf("NewMockConfig() = %v, want %v", got, tt.want)
}
})
}
}

func TestMockConfig_GetAccounts(t *testing.T) {
now := time.Now().UTC()
cfg, _ := NewMockConfig()

t.Run("success", func(t *testing.T) {
cfg.SetAccounts(model.Account{
CreatedAt: now,
UpdatedAt: now,
Login: "test",
Cookies: nil,
})

if len(cfg.GetAccounts()) != 1 {
t.Errorf("GetAccounts() = %v, want %v", len(cfg.GetAccounts()), 1)
}
})
}

func TestMockConfig_GetModules(t *testing.T) {
now := time.Now().UTC()
cfg, _ := NewMockConfig()

t.Run("success", func(t *testing.T) {
cfg.SetModules(model.Module{
CreatedAt: now,
UpdatedAt: now,
Name: "test",
Path: "test",
Description: "test",
Login: "test",
})

if len(cfg.GetModules()) != 1 {
t.Errorf("GetModules() = %v, want %v", len(cfg.GetModules()), 1)
}
})
}

func TestMockConfig_SetAccounts(t *testing.T) {
now := time.Now().UTC()
cfg, _ := NewMockConfig()

t.Run("success", func(t *testing.T) {
cfg.SetAccounts(model.Account{
CreatedAt: now,
UpdatedAt: now,
Login: "test",
Cookies: nil,
})

if len(cfg.GetAccounts()) != 1 {
t.Errorf("GetAccounts() = %v, want %v", len(cfg.GetAccounts()), 1)
}
})
}

func TestMockConfig_SetModules(t *testing.T) {
now := time.Now().UTC()
cfg, _ := NewMockConfig()

t.Run("success", func(t *testing.T) {
cfg.SetModules(model.Module{
CreatedAt: now,
UpdatedAt: now,
Name: "test",
Path: "test",
Description: "test",
Login: "test",
})

if len(cfg.GetModules()) != 1 {
t.Errorf("GetModules() = %v, want %v", len(cfg.GetModules()), 1)
}
})
}

func TestMockConfig_AddAccounts(t *testing.T) {
now := time.Now().UTC()
cfg, _ := NewMockConfig()

t.Run("success", func(t *testing.T) {
cfg.AddAccounts(model.Account{
CreatedAt: now,
UpdatedAt: now,
Login: "test",
Cookies: nil,
})

if len(cfg.GetAccounts()) != 1 {
t.Errorf("GetAccounts() = %v, want %v", len(cfg.GetAccounts()), 1)
}

cfg.AddAccounts(model.Account{
CreatedAt: now,
UpdatedAt: now,
Login: "test",
Cookies: nil,
})

if len(cfg.GetAccounts()) != 2 {
t.Errorf("GetAccounts() = %v, want %v", len(cfg.GetAccounts()), 2)
}
})
}

func TestMockConfig_AddModules(t *testing.T) {
now := time.Now().UTC()
cfg, _ := NewMockConfig()

t.Run("success", func(t *testing.T) {
cfg.AddModules(model.Module{
CreatedAt: now,
UpdatedAt: now,
Name: "test",
Path: "test",
Description: "test",
Login: "test",
})

if len(cfg.GetModules()) != 1 {
t.Errorf("GetModules() = %v, want %v", len(cfg.GetModules()), 1)
}

cfg.AddModules(model.Module{
CreatedAt: now,
UpdatedAt: now,
Name: "test",
Path: "test",
Description: "test",
Login: "test",
})

if len(cfg.GetModules()) != 2 {
t.Errorf("GetModules() = %v, want %v", len(cfg.GetModules()), 2)
}
})
}

func TestMockConfig_Reset(t *testing.T) {
now := time.Now().UTC()
cfg, _ := NewMockConfig()

t.Run("success", func(t *testing.T) {
cfg.AddAccounts(model.Account{
CreatedAt: now,
UpdatedAt: now,
Login: "test",
Cookies: nil,
})

cfg.AddModules(model.Module{
CreatedAt: now,
UpdatedAt: now,
Name: "test",
Path: "test",
Description: "test",
Login: "test",
})

if len(cfg.GetAccounts()) != 1 {
t.Errorf("GetAccounts() = %v, want %v", len(cfg.GetAccounts()), 1)
}

if len(cfg.GetModules()) != 1 {
t.Errorf("GetModules() = %v, want %v", len(cfg.GetModules()), 1)
}

err := cfg.Reset()
if err != nil {
t.Errorf("Reset() = %v, want nil", err)
}

if cfg.GetAccounts() != nil {
t.Errorf("GetAccounts() = %v, want %v", cfg.GetAccounts(), nil)
}

if len(cfg.GetAccounts()) != 0 {
t.Errorf("GetAccounts() = %v, want %v", len(cfg.GetAccounts()), 0)
}

if cfg.GetModules() != nil {
t.Errorf("GetModules() = %v, want %v", cfg.GetModules(), 0)
}

if len(cfg.GetModules()) != 0 {
t.Errorf("GetModules() = %v, want %v", len(cfg.GetModules()), 0)
}
})
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

conf, err := cfg.Load()
conf, err := cfg.NewConfig()
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 581ca67

Please sign in to comment.