405 lines
10 KiB
Go
405 lines
10 KiB
Go
|
package storage
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"github.com/google/uuid"
|
||
|
"github.com/spf13/viper"
|
||
|
"reflect"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
var cfg *viper.Viper
|
||
|
|
||
|
func init() {
|
||
|
cfg = viper.New()
|
||
|
cfg.SetDefault("storage.db.psql.host", "localhost")
|
||
|
cfg.SetDefault("storage.db.psql.port", "5432")
|
||
|
cfg.SetDefault("storage.db.psql.user", "postgres")
|
||
|
cfg.SetDefault("storage.db.psql.password", "postgres")
|
||
|
cfg.SetDefault("storage.db.psql.dbname", "coopgo_platform")
|
||
|
cfg.SetDefault("storage.db.psql.sslmode", "disable")
|
||
|
cfg.SetDefault("storage.db.psql.schema", "groups_management")
|
||
|
cfg.SetDefault("storage.db.psql.tables.groups", "groups")
|
||
|
cfg.SetDefault("storage.db.psql.tables.group_members", "group_members")
|
||
|
cfg.SetConfigName("config") // Override default values in a config.yaml file within this directory
|
||
|
cfg.AddConfigPath(".")
|
||
|
cfg.ReadInConfig()
|
||
|
}
|
||
|
|
||
|
var group = Group{
|
||
|
ID: uuid.New().String(),
|
||
|
Namespace: "test_namespace",
|
||
|
Members: []string{uuid.NewString(), uuid.NewString()},
|
||
|
Data: map[string]any{
|
||
|
"test": "test",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var group1 = Group{
|
||
|
ID: uuid.New().String(),
|
||
|
Namespace: "namespace",
|
||
|
Members: []string{uuid.NewString(), uuid.NewString()},
|
||
|
Data: map[string]any{
|
||
|
"test": "test",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var groupmember = GroupMember{
|
||
|
ID: uuid.New().String(),
|
||
|
Memberid: uuid.NewString(),
|
||
|
Groupid: group.ID,
|
||
|
Data: map[string]any{
|
||
|
"test": "test",
|
||
|
},
|
||
|
}
|
||
|
var groupmember1 = GroupMember{
|
||
|
ID: uuid.New().String(),
|
||
|
Memberid: uuid.NewString(),
|
||
|
Groupid: group1.ID,
|
||
|
Data: map[string]any{
|
||
|
"test": "test",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_Initialize(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
defer storage.DbConnection.Close()
|
||
|
|
||
|
err = storage.Migrate()
|
||
|
if err != nil {
|
||
|
t.Errorf("database migration issue: %v", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
tx, err := storage.DbConnection.BeginTx(context.Background(), nil)
|
||
|
if err != nil {
|
||
|
t.Errorf("transaction issue: %v", err)
|
||
|
return
|
||
|
}
|
||
|
defer tx.Rollback()
|
||
|
_, err = tx.Exec(fmt.Sprintf("DELETE FROM %s;", storage.Tables["group_members"]))
|
||
|
if err != nil {
|
||
|
t.Errorf("delete accounts group_members issue: %v", err)
|
||
|
return
|
||
|
}
|
||
|
_, err = tx.Exec(fmt.Sprintf("DELETE FROM %s;", storage.Tables["groups"]))
|
||
|
if err != nil {
|
||
|
t.Errorf("delete accounts groups issue: %v", err)
|
||
|
return
|
||
|
}
|
||
|
if err = tx.Commit(); err != nil {
|
||
|
t.Errorf("commit transaction issue: %v", err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_CreateAndGetGroup(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
err = storage.CreateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group : %s", err)
|
||
|
return
|
||
|
}
|
||
|
retrieved, err := storage.GetGroup(group.ID)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to get group: %s", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if !reflect.DeepEqual(&group, retrieved) {
|
||
|
t.Errorf("The received group is not the same as expected\nSaved Group : %v\nRetrieved Group : %v",
|
||
|
&group, retrieved)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_Migrate(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
err = storage.Migrate()
|
||
|
if err != nil {
|
||
|
t.Error(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_GetGroups(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
group.Namespace = "x"
|
||
|
group1.Namespace = "y"
|
||
|
err = storage.CreateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group: %s", err)
|
||
|
return
|
||
|
}
|
||
|
err = storage.CreateGroup(group1)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group1 : %s", err)
|
||
|
return
|
||
|
}
|
||
|
groups, err := storage.GetGroups([]string{group.Namespace, group1.Namespace})
|
||
|
if err != nil {
|
||
|
t.Error(err)
|
||
|
}
|
||
|
expectedGroups := []Group{
|
||
|
{
|
||
|
ID: group.ID,
|
||
|
Namespace: "x",
|
||
|
Members: group.Members,
|
||
|
Data: group.Data,
|
||
|
},
|
||
|
{
|
||
|
ID: group1.ID,
|
||
|
Namespace: "y",
|
||
|
Members: group1.Members,
|
||
|
Data: group1.Data,
|
||
|
},
|
||
|
}
|
||
|
if !reflect.DeepEqual(expectedGroups, groups) {
|
||
|
t.Errorf("The received groups is not the same as expected")
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_GetGroupsByIds(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
err = storage.CreateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group : %s", err)
|
||
|
return
|
||
|
}
|
||
|
err = storage.CreateGroup(group1)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group1 : %s", err)
|
||
|
return
|
||
|
}
|
||
|
retrievedGroups, err := storage.GetGroupsByIds([]string{group.ID, group1.ID})
|
||
|
if err != nil {
|
||
|
t.Errorf("error retrieving groups by IDS: %v", err)
|
||
|
}
|
||
|
if len(retrievedGroups) != 2 {
|
||
|
t.Error("Error in retrieving groups by IDs")
|
||
|
}
|
||
|
expectedGroups := []Group{
|
||
|
{
|
||
|
ID: group.ID,
|
||
|
Namespace: group.Namespace,
|
||
|
Members: group.Members,
|
||
|
Data: group.Data,
|
||
|
},
|
||
|
{
|
||
|
ID: group1.ID,
|
||
|
Namespace: group1.Namespace,
|
||
|
Members: group1.Members,
|
||
|
Data: group1.Data,
|
||
|
},
|
||
|
}
|
||
|
if !reflect.DeepEqual(expectedGroups, retrievedGroups) {
|
||
|
t.Errorf("The received groups is not the same as expected")
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_UpdateGroup(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
err = storage.CreateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group : %s", err)
|
||
|
return
|
||
|
}
|
||
|
group.Namespace = "updated_namespace"
|
||
|
group.Members = []string{"updated"}
|
||
|
group.Data = map[string]any{
|
||
|
"updated": "updated",
|
||
|
}
|
||
|
err = storage.UpdateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Error(err)
|
||
|
}
|
||
|
group, err := storage.GetGroup(group.ID)
|
||
|
if err != nil {
|
||
|
t.Error(err)
|
||
|
}
|
||
|
expectedGroup := &Group{
|
||
|
ID: group.ID,
|
||
|
Namespace: group.Namespace,
|
||
|
Members: group.Members,
|
||
|
Data: group.Data,
|
||
|
}
|
||
|
if !reflect.DeepEqual(group, expectedGroup) {
|
||
|
t.Errorf("The received groups is not the same as expected")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_CreateAndGetGroupMember(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
err = storage.CreateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group : %s", err)
|
||
|
return
|
||
|
}
|
||
|
groupmember.Groupid = group.ID
|
||
|
err = storage.CreateGroupMember(groupmember)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new group member: %v", err)
|
||
|
}
|
||
|
received, err := storage.GetGroupMember(groupmember.ID)
|
||
|
if err != nil {
|
||
|
t.Errorf("error getting a group member: %v", err)
|
||
|
}
|
||
|
if !reflect.DeepEqual(received, &groupmember) {
|
||
|
t.Errorf("The received groups is not the same as expected")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_GetGroupsMember(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
group.Namespace = "xyxy"
|
||
|
err = storage.CreateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group : %s", err)
|
||
|
return
|
||
|
}
|
||
|
groupmember.Groupid = group.ID
|
||
|
err = storage.CreateGroupMember(groupmember)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new group member: %v", err)
|
||
|
}
|
||
|
groupMembers, err := storage.GetGroupsMember([]string{"xyxy"})
|
||
|
if err != nil {
|
||
|
t.Errorf("error getting group members: %v", err)
|
||
|
}
|
||
|
expectedGroupMembers := []GroupMember{
|
||
|
{
|
||
|
ID: groupmember.ID,
|
||
|
Memberid: groupmember.Memberid,
|
||
|
Groupid: groupmember.Groupid,
|
||
|
Data: groupmember.Data,
|
||
|
},
|
||
|
}
|
||
|
if !reflect.DeepEqual(expectedGroupMembers, groupMembers) {
|
||
|
t.Errorf("The received group members slice is not the same as expected")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_GetGroupsMemberByIds(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
err = storage.CreateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group : %s", err)
|
||
|
return
|
||
|
}
|
||
|
err = storage.CreateGroupMember(groupmember)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new group member: %v", err)
|
||
|
}
|
||
|
if err != nil {
|
||
|
t.Errorf("error getting group members: %v", err)
|
||
|
}
|
||
|
err = storage.CreateGroup(group1)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group : %s", err)
|
||
|
return
|
||
|
}
|
||
|
err = storage.CreateGroupMember(groupmember1)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new group member: %v", err)
|
||
|
}
|
||
|
groupmembers, err := storage.GetGroupsMemberByIds([]string{groupmember.Groupid, groupmember1.Groupid})
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating in getting group members by IDS: %v", err)
|
||
|
}
|
||
|
expectedGroupMembers := []GroupMember{
|
||
|
groupmember,
|
||
|
groupmember1,
|
||
|
}
|
||
|
if !reflect.DeepEqual(expectedGroupMembers, groupmembers) {
|
||
|
t.Errorf("The received group members slice is not the same as expected")
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_UpdateGroupMember(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
err = storage.CreateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group : %s", err)
|
||
|
return
|
||
|
}
|
||
|
groupmember.Groupid = group.ID
|
||
|
err = storage.CreateGroupMember(groupmember)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new group member: %v", err)
|
||
|
}
|
||
|
groupmember.Data = map[string]any{
|
||
|
"updated": "updated",
|
||
|
}
|
||
|
err = storage.UpdateGroupMember(groupmember)
|
||
|
if err != nil {
|
||
|
t.Error("failed to update group member")
|
||
|
}
|
||
|
updatedGroupMember, err := storage.GetGroupMember(groupmember.ID)
|
||
|
if err != nil {
|
||
|
t.Error("failed to get group member")
|
||
|
}
|
||
|
if !reflect.DeepEqual(updatedGroupMember, &groupmember) {
|
||
|
t.Errorf("The received group members slice is not the same as expected")
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestPostgresqlStorage_DeleteGroupMember(t *testing.T) {
|
||
|
storage, err := NewPostgresqlStorage(cfg)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||
|
}
|
||
|
err = storage.CreateGroup(group)
|
||
|
if err != nil {
|
||
|
t.Errorf("Failed to create group : %s", err)
|
||
|
return
|
||
|
}
|
||
|
groupmember.Groupid = group.ID
|
||
|
err = storage.CreateGroupMember(groupmember)
|
||
|
if err != nil {
|
||
|
t.Errorf("error creating new group member: %v", err)
|
||
|
}
|
||
|
err = storage.DeleteGroupMember(groupmember.ID)
|
||
|
if err != nil {
|
||
|
t.Error("error in deleting a group member")
|
||
|
}
|
||
|
_, err = storage.GetGroupMember(groupmember.ID)
|
||
|
if strings.Contains(err.Error(), "no rows in result set") == false {
|
||
|
t.Error("Delete operation failed", err)
|
||
|
}
|
||
|
|
||
|
}
|