Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cc 02 user models #5

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions api/src/model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,40 @@ type ExampleGiftInput struct {
Name string `binding:"required"`
Price int `binding:"required"`
}

type User struct {
gorm.Model
Email string `binding:"email"`
FirstName string
LastName string
Password string `valid:"length(6|20)"`
}

type UserInput struct {
Email string `binding:"required, email"`
FirstName string `binding:"required"`
LastName string `binding:"required"`
Password string `binding:"required" valid:"length(6|20)"`
}

type Customer struct {
gorm.Model
UserID uint
//GiftCollections []ExampleGiftCollection
//GiftRequests []ExampleGiftRequests
}

type CustomerInput struct {
UserID uint `binding:"required"`
//GiftCollections []ExampleGiftCollection
//GiftRequests []ExampleGiftRequests
}

type Admin struct {
gorm.Model
UserID uint
}

type AdminInput struct {
UserID uint `binding:"required"`
}
164 changes: 164 additions & 0 deletions api/tests/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,167 @@ func TestExampleGiftModel(t *testing.T) {
assert.Equal(t, int64(0), count)

}


func TestUserModel(t *testing.T) {
// This code should be the same for each test
dsn := "host=test-db user=testuser password=testpwd dbname=testdb port=5433 sslmode=disable"
if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists {
dsn = dbURL
}
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
t.Fatalf("Unable to connect to database: %v", err)
}
// Put auto migrations here
err = db.AutoMigrate(&model.User{})
if err != nil {
panic("failed to migrate test database schema")
}
// Setup db rollback to revert db changes
tx := db.Begin()
defer tx.Rollback()

// Create User
user := model.User{Email: "[email protected]", FirstName: "Joey", LastName: "Tsai", Password: "dgeeg32"}
err = db.Create(&user).Error
assert.NoError(t, err)

// Check if user exists
var fetchedUser model.User
err = db.First(&fetchedUser, user.ID).Error
assert.NoError(t, err)
assert.Equal(t, user.ID, fetchedUser.ID)
assert.Equal(t, user.FirstName, fetchedUser.FirstName)
assert.Equal(t, user.LastName, fetchedUser.LastName)
assert.Equal(t, user.Email, fetchedUser.Email)
assert.Equal(t, user.Password, fetchedUser.Password)
assert.Equal(t, user.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedUser.CreatedAt.In(time.UTC).Round(time.Millisecond))

// Update User
err = db.Model(&fetchedUser).Update("FirstName", "Dessy").Error
assert.NoError(t, err)

// Check if it's updated
var updatedUser model.User
err = db.First(&updatedUser, fetchedUser.ID).Error
assert.NoError(t, err)
assert.Equal(t, "Dessy", updatedUser.FirstName)

// Delete user
err = db.Delete(&updatedUser).Error
assert.NoError(t, err)

// Check if it's user
var count int64
db.Model(&model.User{}).Where("id = ?", updatedUser.ID).Count(&count)
assert.Equal(t, int64(0), count)
}

func TestAdminModel(t *testing.T) {
// This code should be the same for each test
dsn := "host=test-db user=testuser password=testpwd dbname=testdb port=5433 sslmode=disable"
if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists {
dsn = dbURL
}
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
t.Fatalf("Unable to connect to database: %v", err)
}
// Put auto migrations here
err = db.AutoMigrate(&model.Admin{})
if err != nil {
panic("failed to migrate test database schema")
}
// Setup db rollback to revert db changes
tx := db.Begin()
defer tx.Rollback()

// Create Admin
admin := model.Admin{UserID: uint(1)}
err = db.Create(&admin).Error
assert.NoError(t, err)

// Check if Admin exists
var fetchedAdmin model.Admin
err = db.First(&fetchedAdmin, admin.ID).Error
assert.NoError(t, err)
assert.Equal(t, admin.ID, fetchedAdmin.ID)
assert.Equal(t, admin.UserID, fetchedAdmin.UserID)
assert.Equal(t, admin.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedAdmin.CreatedAt.In(time.UTC).Round(time.Millisecond))

// Update Admin
err = db.Model(&fetchedAdmin).Update("UserID", uint(2)).Error
assert.NoError(t, err)

// Check if it's updated
var updatedAdmin model.Admin
err = db.First(&updatedAdmin, fetchedAdmin.ID).Error
assert.NoError(t, err)
assert.Equal(t, uint(2), updatedAdmin.UserID)

// Delete Admin
err = db.Delete(&updatedAdmin).Error
assert.NoError(t, err)

// Check if it's user
var count int64
db.Model(&model.Admin{}).Where("id = ?", updatedAdmin.ID).Count(&count)
assert.Equal(t, int64(0), count)
}

func TestCustomerModel(t *testing.T) {
// This code should be the same for each test
dsn := "host=test-db user=testuser password=testpwd dbname=testdb port=5433 sslmode=disable"
if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists {
dsn = dbURL
}
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
t.Fatalf("Unable to connect to database: %v", err)
}
// Put auto migrations here
err = db.AutoMigrate(&model.Customer{})
if err != nil {
panic("failed to migrate test database schema")
}
// Setup db rollback to revert db changes
tx := db.Begin()
defer tx.Rollback()

// Create customer
customer := model.Customer{UserID: uint(3)}
err = db.Create(&customer).Error
assert.NoError(t, err)

// Check if customer exists
var fetchedCustomer model.Customer
err = db.First(&fetchedCustomer, customer.ID).Error
assert.NoError(t, err)
assert.Equal(t, customer.ID, fetchedCustomer.ID)
assert.Equal(t, customer.UserID, fetchedCustomer.UserID)
assert.Equal(t, customer.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedCustomer.CreatedAt.In(time.UTC).Round(time.Millisecond))

// Update customer
err = db.Model(&fetchedCustomer).Update("UserID", uint(4)).Error
assert.NoError(t, err)

// Check if it's updated
var updatedCustomer model.Customer
err = db.First(&updatedCustomer, fetchedCustomer.ID).Error
assert.NoError(t, err)
assert.Equal(t, uint(4), updatedCustomer.UserID)

// Delete customer
err = db.Delete(&updatedCustomer).Error
assert.NoError(t, err)

// Check if it's user
var count int64
db.Model(&model.Customer{}).Where("id = ?", updatedCustomer.ID).Count(&count)
assert.Equal(t, int64(0), count)
}

Loading