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 03 relations user gifts #20

Merged
merged 4 commits into from
Oct 6, 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
1 change: 0 additions & 1 deletion .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions api/src/model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,47 @@ type ExampleGift struct {
}

type ExampleGiftInput struct {
Name string `binding:"required"`
Price int `binding:"required"`
Name string
Price int
}

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

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

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

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

type Admin struct {
gorm.Model
UserID uint
User User
}

type AdminInput struct {
UserID uint `binding:"required"`
UserID uint
User User
}
94 changes: 79 additions & 15 deletions api/tests/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestUserModel(t *testing.T) {
defer tx.Rollback()

// Create User
user := model.User{Email: "tsai.me@northeastern.edu", FirstName: "Joey", LastName: "Tsai", Password: "dgeeg32"}
user := model.User{Email: "example@northeastern.edu", FirstName: "PersonFirstName", LastName: "PersonLastName", Password: "dgeeg32"}
err = db.Create(&user).Error
assert.NoError(t, err)

Expand All @@ -125,14 +125,14 @@ func TestUserModel(t *testing.T) {
fetchedUser.CreatedAt.In(time.UTC).Round(time.Millisecond))

// Update User
err = db.Model(&fetchedUser).Update("FirstName", "Dessy").Error
err = db.Model(&fetchedUser).Update("FirstName", "ChangeFirstName").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)
assert.Equal(t, "ChangeFirstName", updatedUser.FirstName)

// Delete user
err = db.Delete(&updatedUser).Error
Expand All @@ -157,17 +157,49 @@ func TestAdminModel(t *testing.T) {
// Put auto migrations here
err = db.AutoMigrate(&model.Admin{})
if err != nil {
panic("failed to migrate test database schema")
panic("failed to migrate test admin database schema")
}

// Put auto migrations here
err = db.AutoMigrate(&model.User{})
if err != nil {
panic("failed to migrate test user database schema")
}

// Setup db rollback to revert db changes
tx := db.Begin()
defer tx.Rollback()

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

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

// Check Relationship between Admin and User
var admins []model.Admin
err = db.Model(&model.Admin{}).Preload("User").Find(&admins).Error
if err != nil {
panic("relationship failed")
}

// Check User information
adminUser := admins[0].User
var fetchedUser model.User
err = db.First(&fetchedUser, user.ID).Error
assert.NoError(t, err)
assert.Equal(t, adminUser.ID, fetchedUser.ID)
assert.Equal(t, adminUser.FirstName, fetchedUser.FirstName)
assert.Equal(t, adminUser.LastName, fetchedUser.LastName)
assert.Equal(t, adminUser.Email, fetchedUser.Email)
assert.Equal(t, adminUser.Password, fetchedUser.Password)
assert.Equal(t, adminUser.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedUser.CreatedAt.In(time.UTC).Round(time.Millisecond))

// Check if Admin exists
var fetchedAdmin model.Admin
err = db.First(&fetchedAdmin, admin.ID).Error
Expand All @@ -191,7 +223,7 @@ func TestAdminModel(t *testing.T) {
err = db.Delete(&updatedAdmin).Error
assert.NoError(t, err)

// Check if it's user
// Check if it's admin
var count int64
db.Model(&model.Admin{}).Where("id = ?", updatedAdmin.ID).Count(&count)
assert.Equal(t, int64(0), count)
Expand All @@ -210,18 +242,50 @@ func TestCustomerModel(t *testing.T) {
// Put auto migrations here
err = db.AutoMigrate(&model.Customer{})
if err != nil {
panic("failed to migrate test database schema")
panic("failed to migrate test customer database schema")
}

// Put auto migrations here
err = db.AutoMigrate(&model.User{})
if err != nil {
panic("failed to migrate test user database schema")
}

// Setup db rollback to revert db changes
tx := db.Begin()
defer tx.Rollback()

// Create customer
customer := model.Customer{UserID: uint(3)}
// Create User
user := model.User{Email: "[email protected]", FirstName: "PersonFirstName", LastName: "PersonLastName", Password: "dgeeg32"}
err = db.Create(&user).Error
assert.NoError(t, err)

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

// Check if customer exists
// Check Relationship between Customer and User
var customers []model.Customer
err = db.Model(&model.Customer{}).Preload("User").Find(&customers).Error
if err != nil {
panic("relationship failed")
}

// Check User information
customerUser := customers[0].User
var fetchedUser model.User
err = db.First(&fetchedUser, user.ID).Error
assert.NoError(t, err)
assert.Equal(t, customerUser.ID, fetchedUser.ID)
assert.Equal(t, customerUser.FirstName, fetchedUser.FirstName)
assert.Equal(t, customerUser.LastName, fetchedUser.LastName)
assert.Equal(t, customerUser.Email, fetchedUser.Email)
assert.Equal(t, customerUser.Password, fetchedUser.Password)
assert.Equal(t, customerUser.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedUser.CreatedAt.In(time.UTC).Round(time.Millisecond))

// Check if Customer exists
var fetchedCustomer model.Customer
err = db.First(&fetchedCustomer, customer.ID).Error
assert.NoError(t, err)
Expand All @@ -230,22 +294,22 @@ func TestCustomerModel(t *testing.T) {
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
// Update Custoer
err = db.Model(&fetchedCustomer).Update("UserID", uint(2)).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)
assert.Equal(t, uint(2), updatedCustomer.UserID)

// Delete customer
// 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
Loading