PSQL CreateAccount

This commit is contained in:
sbouaram 2023-04-25 22:29:39 +02:00
parent d1e71e32f9
commit 57e5a73c2f
2 changed files with 84 additions and 10 deletions

View File

@ -100,8 +100,41 @@ func (PostgresqlStorage) GetAccountsByIds(accountids []string) ([]Account, error
return nil, fmt.Errorf("") return nil, fmt.Errorf("")
} }
func (PostgresqlStorage) CreateAccount(account Account) error { func (psql PostgresqlStorage) CreateAccount(account Account) error {
return fmt.Errorf("") insertAccountStmt, err := psql.DbConnection.Prepare("INSERT INTO accounts (id, namespace, data, metadata) VALUES ($1, $2, $3, $4)")
if err != nil {
return err
}
dataAccountJson, err := json.Marshal(account.Data)
if err != nil {
return err
}
metadataAccountJson, err := json.Marshal(account.Metadata)
if err != nil {
return err
}
_, err = insertAccountStmt.Exec(account.ID, account.Namespace, dataAccountJson, metadataAccountJson)
if err != nil {
return err
}
insertAccountAuthStmt, err := psql.DbConnection.Prepare("INSERT INTO account_auth (account_id, username, password, email, email_validation,phone_number,phone_number_validation) values($1, $2, $3, $4, $5, $6, $7)")
if err != nil {
return err
}
emailValidationJson, err := json.Marshal(account.Authentication.Local.EmailValidation)
if err != nil {
return err
}
phoneValidationJson, err := json.Marshal(account.Authentication.Local.PhoneNumberValidation)
if err != nil {
return err
}
_, err = insertAccountAuthStmt.Exec(account.ID, account.Authentication.Local.Username, account.Authentication.Local.Password,
account.Authentication.Local.Email, emailValidationJson, account.Authentication.Local.PhoneNumber, phoneValidationJson)
if err != nil {
return err
}
return nil
} }
func (PostgresqlStorage) UpdateAccount(account Account) error { func (PostgresqlStorage) UpdateAccount(account Account) error {

View File

@ -10,14 +10,18 @@ import (
"testing" "testing"
) )
func TestNewPostgresqlStorage(t *testing.T) { var cfg *viper.Viper
cfg := viper.New()
func init() {
cfg = viper.New()
cfg.Set("storage.db.psql.host", "localhost") cfg.Set("storage.db.psql.host", "localhost")
cfg.Set("storage.db.psql.port", "5432") cfg.Set("storage.db.psql.port", "5432")
cfg.Set("storage.db.psql.user", "postgres") cfg.Set("storage.db.psql.user", "postgres")
cfg.Set("storage.db.psql.password", "postgres") cfg.Set("storage.db.psql.password", "postgres")
cfg.Set("storage.db.psql.dbname", "mobilityaccounts") cfg.Set("storage.db.psql.dbname", "mobilityaccounts")
}
func TestNewPostgresqlStorage(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg) storage, err := NewPostgresqlStorage(cfg)
if err != nil { if err != nil {
t.Errorf("error creating new PostgreSQL storage: %v", err) t.Errorf("error creating new PostgreSQL storage: %v", err)
@ -35,14 +39,9 @@ func generateUUIDv4() string {
uuid[8] = (uuid[8] & 0xbf) | 0x80 uuid[8] = (uuid[8] & 0xbf) | 0x80
return hex.EncodeToString(uuid[:4]) + "-" + hex.EncodeToString(uuid[4:6]) + "-" + hex.EncodeToString(uuid[6:8]) + "-" + hex.EncodeToString(uuid[8:10]) + "-" + hex.EncodeToString(uuid[10:]) return hex.EncodeToString(uuid[:4]) + "-" + hex.EncodeToString(uuid[4:6]) + "-" + hex.EncodeToString(uuid[6:8]) + "-" + hex.EncodeToString(uuid[8:10]) + "-" + hex.EncodeToString(uuid[10:])
} }
func TestGetAccount(t *testing.T) { func TestGetAccount(t *testing.T) {
// Open a database connection // Open a database connection
cfg := viper.New()
cfg.Set("storage.db.psql.host", "localhost")
cfg.Set("storage.db.psql.port", "5432")
cfg.Set("storage.db.psql.user", "postgres")
cfg.Set("storage.db.psql.password", "postgres")
cfg.Set("storage.db.psql.dbname", "mobilityaccounts")
db, err := NewPostgresqlStorage(cfg) db, err := NewPostgresqlStorage(cfg)
Id := generateUUIDv4() Id := generateUUIDv4()
if err != nil { if err != nil {
@ -116,3 +115,45 @@ func TestGetAccount(t *testing.T) {
t.Errorf("The received account is not the same as expected") t.Errorf("The received account is not the same as expected")
} }
} }
func TestPostgresqlStorage_CreateAccount(t *testing.T) {
db, err := NewPostgresqlStorage(cfg)
Id := generateUUIDv4()
if err != nil {
t.Errorf("failed to create new psql connection")
}
emailValidation := Validation{
Validated: true,
ValidationCode: "code",
}
localAuth := LocalAuth{
Username: "salim",
Password: "testpassword",
Email: "test@test.com",
EmailValidation: emailValidation,
PhoneNumber: "1234567890",
PhoneNumberValidation: emailValidation,
}
accountData := map[string]any{
"key1": "value1",
"key2": "value2",
}
accountMetadata := map[string]any{
"key1": "value1",
"key2": "value2",
}
account := Account{
ID: Id,
Namespace: "test_namespace",
Authentication: AccountAuth{
Local: localAuth,
},
Data: accountData,
Metadata: accountMetadata,
}
err = db.CreateAccount(account)
if err != nil {
fmt.Println(err)
t.Errorf("Failed to create account")
}
}