-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): add mock config and tests (#5)
Signed-off-by: Ruslan Semagin <[email protected]>
- Loading branch information
Showing
6 changed files
with
317 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters