Skip to content

Commit

Permalink
[tests] rework tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MalinAhlberg committed Nov 29, 2024
1 parent 404191b commit 8dbe9b0
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions sda/internal/database/db_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,45 +932,51 @@ func (suite *DatabaseTests) TestListUserDatasets() {
assert.Equal(suite.T(), "test-user-dataset-01", datasets[0].DatasetID)
}

func (suite *DatabaseTests) TestInsertUserInfo() {
func (suite *DatabaseTests) TestUpdateUserInfo() {
db, err := NewSDAdb(suite.dbConf)
assert.NoError(suite.T(), err, "got (%v) when creating new connection", err)

// Verify that a user can be inserted
// Insert a userID
var groups []string
userID, name, email := "[email protected]", "Test User", "[email protected]"
err = db.UpdateUserInfo(userID, name, email, groups)
assert.NoError(suite.T(), err, "could not insert user info: %v", err)
var exists bool
err = db.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM sda.userinfo WHERE id=$1)", userID).Scan(&exists)
assert.NoError(suite.T(), err, "failed to verify user info existence")
assert.True(suite.T(), exists, "user info was not added to the database")

// Insert new information about the user
groups = append(groups, "appleGroup", "bananaGroup")
name = "newName"
err = db.UpdateUserInfo(userID, name, email, groups)
assert.NoError(suite.T(), err, "could not insert updated user info: %v", err)
// Verify that the userID is connected to the details
var numRows int
err = db.DB.QueryRow("SELECT COUNT(*) FROM sda.userinfo WHERE id=$1", userID).Scan(&numRows)
assert.NoError(suite.T(), err, "could select user info: %v", err)
assert.Equal(suite.T(), 1, numRows, "there should be exactly 1 row about %v in userinfo table", userID)
var name2 string
err = db.DB.QueryRow("SELECT name FROM sda.userinfo WHERE id=$1", userID).Scan(&name2)
assert.NoError(suite.T(), err, "could not select user info: %v", err)
assert.Equal(suite.T(), name, name2, "user info table did not update correctly")
}

func (suite *DatabaseTests) TestUpdateUserInfo() {
func (suite *DatabaseTests) TestUpdateUserInfo_newInfo() {
db, err := NewSDAdb(suite.dbConf)
assert.NoError(suite.T(), err, "got (%v) when creating new connection", err)

// Insert a userID
// Insert a user
var groups []string
userID, name, email := "[email protected]", "Test User", "[email protected]"
err = db.UpdateUserInfo(userID, name, email, groups)
assert.NoError(suite.T(), err, "could not insert user info: %v", err)
// Verify that the userID is connected to the new details
var exists bool
err = db.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM sda.userinfo WHERE id=$1)", userID).Scan(&exists)
assert.NoError(suite.T(), err, "failed to verify user info existence")
assert.True(suite.T(), exists, "user info was not added to the database")

// Insert new information about the user and verify that there is still only 1 row,
// and that this row is updated
var numRows int
err = db.DB.QueryRow("SELECT COUNT(*) FROM sda.userinfo WHERE id=$1", userID).Scan(&numRows)

Check failure on line 972 in sda/internal/database/db_functions_test.go

View workflow job for this annotation

GitHub Actions / Lint sda code

ineffectual assignment to err (ineffassign)
assert.NoError(suite.T(), err, "could select user info: %v", err)
assert.Equal(suite.T(), 1, numRows, "there should be exactly 1 row about %v in userinfo table", userID)
var name2 string
var groups2 []string
err = db.DB.QueryRow("SELECT name, groups FROM sda.userinfo WHERE id=$1", userID).Scan(&name2, pq.Array(&groups2))
assert.NoError(suite.T(), err, "could select user info: %v", err)
assert.Equal(suite.T(), name, name2, "user info table did not update correctly")
assert.Equal(suite.T(), groups, groups2, "user info table did not update correctly")
assert.Equal(suite.T(), 1, numRows, "there should be exactly one row in userinfo")
var dbgroups []string
groups = append(groups, "appleGroup", "bananaGroup")
name = "newName"
err = db.UpdateUserInfo(userID, name, email, groups)
assert.NoError(suite.T(), err, "could not insert updated user info: %v", err)
err = db.DB.QueryRow("SELECT groups FROM sda.userinfo WHERE id=$1", userID).Scan(pq.Array(&dbgroups))
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), groups, dbgroups)
}

0 comments on commit 8dbe9b0

Please sign in to comment.