diff --git a/api/src/model/types.go b/api/src/model/types.go index dbb9c62..c94675f 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:"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"` +} \ No newline at end of file diff --git a/api/tests/db_test.go b/api/tests/db_test.go index 11b0903..b36b14b 100644 --- a/api/tests/db_test.go +++ b/api/tests/db_test.go @@ -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: "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) +} + +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) +} +