278 lines
7.0 KiB
Go
278 lines
7.0 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/google/uuid"
|
|
"github.com/spf13/viper"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var cfg *viper.Viper
|
|
|
|
var date, _ = time.Parse(time.RFC3339Nano, "2023-05-04T16:00:50.165419+02:00")
|
|
var event = Event{
|
|
ID: uuid.New().String(),
|
|
Namespace: "test_namespace",
|
|
Owners: []string{"owners", "owners"},
|
|
RestrictedTo: []string{"restricted_to", "restricted_to"},
|
|
Type: "type",
|
|
Name: "test",
|
|
Description: "description",
|
|
Startdate: date,
|
|
Enddate: date,
|
|
Starttime: "starttime",
|
|
Endtime: "endtime",
|
|
Allday: false,
|
|
MaxSubscribers: 23,
|
|
Subscriptions: []Subscription{
|
|
{
|
|
ID: uuid.NewString(),
|
|
Subscriber: "subscriber1",
|
|
Tags: []string{"tag1", "tag2"},
|
|
CreatedAt: date,
|
|
Data: map[string]any{
|
|
"test": "test",
|
|
},
|
|
},
|
|
},
|
|
DeletedSubscription: []Subscription{
|
|
{
|
|
ID: uuid.NewString(),
|
|
Subscriber: "subscriber_deleted",
|
|
Tags: []string{"tag1", "tag2"},
|
|
CreatedAt: date,
|
|
Data: map[string]any{
|
|
"deleted": "deleted",
|
|
},
|
|
},
|
|
},
|
|
Data: map[string]any{
|
|
"data": "data",
|
|
},
|
|
Deleted: false,
|
|
}
|
|
var event1 = Event{
|
|
ID: uuid.New().String(),
|
|
Namespace: "test_namespace",
|
|
Owners: []string{"owners", "owners"},
|
|
RestrictedTo: []string{"restricted_to", "restricted_to"},
|
|
Type: "type",
|
|
Name: "test",
|
|
Description: "description",
|
|
Startdate: date,
|
|
Enddate: date,
|
|
Starttime: "starttime",
|
|
Endtime: "endtime",
|
|
Allday: false,
|
|
MaxSubscribers: 23,
|
|
Subscriptions: []Subscription{
|
|
{
|
|
ID: uuid.NewString(),
|
|
Subscriber: "subscriber1",
|
|
Tags: []string{"tag1", "tag2"},
|
|
CreatedAt: date,
|
|
Data: map[string]any{
|
|
"test": "test",
|
|
},
|
|
},
|
|
},
|
|
DeletedSubscription: []Subscription{
|
|
{
|
|
ID: uuid.NewString(),
|
|
Subscriber: "subscriber_deleted",
|
|
Tags: []string{"tag1", "tag2"},
|
|
CreatedAt: date,
|
|
Data: map[string]any{
|
|
"deleted": "deleted",
|
|
},
|
|
},
|
|
},
|
|
Data: map[string]any{
|
|
"data": "data",
|
|
},
|
|
Deleted: false,
|
|
}
|
|
|
|
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", "agenda")
|
|
cfg.SetDefault("storage.db.psql.tables.event", "event")
|
|
cfg.SetDefault("storage.db.psql.tables.subscription", "subscription")
|
|
cfg.SetConfigName("config") // Override default values in a config.yaml file within this directory
|
|
cfg.AddConfigPath(".")
|
|
cfg.ReadInConfig()
|
|
}
|
|
|
|
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["subscription"]))
|
|
if err != nil {
|
|
t.Errorf("delete accounts subscription issue: %v", err)
|
|
return
|
|
}
|
|
_, err = tx.Exec(fmt.Sprintf("DELETE FROM %s;", storage.Tables["event"]))
|
|
if err != nil {
|
|
t.Errorf("delete accounts event issue: %v", err)
|
|
return
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
t.Errorf("commit transaction issue: %v", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlStorage_CreateAndGetEvent(t *testing.T) {
|
|
storage, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
|
}
|
|
err = storage.CreateEvent(event)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
retrieved_event, err := storage.GetEvent(event.ID)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
diff := cmp.Diff(&event, retrieved_event)
|
|
if diff != "" {
|
|
fmt.Printf("The retrieved event differs from the original event:\n%s", diff)
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlStorage_GetEvents(t *testing.T) {
|
|
storage, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
|
}
|
|
event.Namespace = "test"
|
|
err = storage.CreateEvent(event)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
event1.Namespace = "test1"
|
|
err = storage.CreateEvent(event1)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
events, err := storage.GetEvents([]string{"test", "test1"})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
diff := cmp.Diff(events, []Event{event, event1})
|
|
if diff != "" {
|
|
fmt.Printf("The retrieved event differs from the original event:\n%s", diff)
|
|
t.Fail()
|
|
}
|
|
|
|
}
|
|
|
|
func TestPostgresqlStorage_AddSubscription(t *testing.T) {
|
|
storage, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
|
}
|
|
err = storage.CreateEvent(event)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
subscription := Subscription{
|
|
ID: uuid.NewString(),
|
|
Subscriber: "salim",
|
|
Tags: []string{"tag"},
|
|
CreatedAt: date,
|
|
Data: map[string]any{"data": "data"},
|
|
}
|
|
err = storage.AddSubscription(event.ID, subscription)
|
|
stored_event, err := storage.GetEvent(event.ID)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
expected_event := Event{
|
|
ID: event.ID,
|
|
Namespace: event.Namespace,
|
|
Owners: event.Owners,
|
|
RestrictedTo: event.RestrictedTo,
|
|
Type: event.Type,
|
|
Name: event.Name,
|
|
Description: event.Description,
|
|
Startdate: event.Startdate,
|
|
Enddate: event.Enddate,
|
|
Starttime: event.Starttime,
|
|
Endtime: event.Endtime,
|
|
Allday: event.Allday,
|
|
MaxSubscribers: event.MaxSubscribers,
|
|
Subscriptions: []Subscription{event.Subscriptions[0], subscription},
|
|
DeletedSubscription: event.DeletedSubscription,
|
|
Data: event.Data,
|
|
Deleted: event.Deleted,
|
|
}
|
|
diff := cmp.Diff(stored_event, &expected_event)
|
|
if diff != "" {
|
|
fmt.Printf("The retrieved event differs from the original event:\n%s", diff)
|
|
t.Fail()
|
|
}
|
|
|
|
}
|
|
|
|
func TestPostgresqlStorage_UpdateEvent(t *testing.T) {
|
|
storage, err := NewPostgresqlStorage(cfg)
|
|
if err != nil {
|
|
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
|
}
|
|
err = storage.CreateEvent(event)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
subscription := Subscription{
|
|
ID: event.Subscriptions[0].ID,
|
|
Subscriber: "updated",
|
|
Tags: []string{"tag"},
|
|
CreatedAt: date,
|
|
Data: map[string]any{"data": "data"},
|
|
}
|
|
event.Subscriptions[0] = subscription
|
|
|
|
err = storage.UpdateEvent(event)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
retrieved_event, err := storage.GetEvent(event.ID)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
diff := cmp.Diff(retrieved_event, &event)
|
|
if diff != "" {
|
|
fmt.Printf("The retrieved event differs from the original event:\n%s", diff)
|
|
t.Fail()
|
|
}
|
|
|
|
}
|