From 439d9c8da4ab3e77f7c3b968da4c26943078cf1c Mon Sep 17 00:00:00 2001 From: joey-tsai Date: Sun, 24 Sep 2023 12:52:25 -0400 Subject: [PATCH 1/3] User Models --- api/src/model/types.go | 37 +++++++++++++++++++++++++++ api/tests/db_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/api/src/model/types.go b/api/src/model/types.go index dbb9c62..ad33efa 100644 --- a/api/src/model/types.go +++ b/api/src/model/types.go @@ -12,3 +12,40 @@ type ExampleGiftInput struct { Name string `binding:"required"` Price int `binding:"required"` } + +type User struct { + gorm.Model + Email string `binding:"required, email"` + FirstName string `binding:"required"` + LastName string `binding:"required"` + Password string `binding:"required" 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 `binding:"required"` + //GiftCollections []ExampleGiftCollection + //GiftRequests []ExampleGiftRequests +} + +type CustomerInput struct { + UserID uint `binding:"required"` + //GiftCollections []ExampleGiftCollection + //GiftRequests []ExampleGiftRequests +} + +type Admin struct { + gorm.Model + UserID uint `binding:"required"` +} + +type AdminInput struct { + UserID uint `binding:"required"` +} \ No newline at end of file diff --git a/api/tests/db_test.go b/api/tests/db_test.go index 11b0903..35fe48d 100644 --- a/api/tests/db_test.go +++ b/api/tests/db_test.go @@ -87,3 +87,61 @@ 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: "tsai.me@northeastern.edu", 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) + +} From c9eee10a5f3d0a1ebe8763b4561c833f53154c2c Mon Sep 17 00:00:00 2001 From: hmcclew <122323895+hmcclew@users.noreply.github.com> Date: Sun, 24 Sep 2023 13:11:03 -0400 Subject: [PATCH 2/3] testing customer and admin --- api/tests/db_test.go | 106 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/api/tests/db_test.go b/api/tests/db_test.go index 35fe48d..b36b14b 100644 --- a/api/tests/db_test.go +++ b/api/tests/db_test.go @@ -143,5 +143,111 @@ func TestUserModel(t *testing.T) { 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) } + From b348f5cce68b103bcbd5971817fbda4d26cc995b Mon Sep 17 00:00:00 2001 From: joey-tsai Date: Mon, 25 Sep 2023 17:19:49 -0400 Subject: [PATCH 3/3] Updated Changes --- api/src/model/types.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/api/src/model/types.go b/api/src/model/types.go index ad33efa..c94675f 100644 --- a/api/src/model/types.go +++ b/api/src/model/types.go @@ -15,10 +15,10 @@ type ExampleGiftInput struct { type User struct { gorm.Model - Email string `binding:"required, email"` - FirstName string `binding:"required"` - LastName string `binding:"required"` - Password string `binding:"required" valid:"length(6|20)"` + Email string `binding:"email"` + FirstName string + LastName string + Password string `valid:"length(6|20)"` } type UserInput struct { @@ -30,7 +30,7 @@ type UserInput struct { type Customer struct { gorm.Model - UserID uint `binding:"required"` + UserID uint //GiftCollections []ExampleGiftCollection //GiftRequests []ExampleGiftRequests } @@ -43,7 +43,7 @@ type CustomerInput struct { type Admin struct { gorm.Model - UserID uint `binding:"required"` + UserID uint } type AdminInput struct {