diff options
Diffstat (limited to 'internal/db')
| -rw-r--r-- | internal/db/basic.go | 9 | ||||
| -rw-r--r-- | internal/db/bundb/basic.go | 4 | ||||
| -rw-r--r-- | internal/db/bundb/basic_test.go | 34 | 
3 files changed, 41 insertions, 6 deletions
| diff --git a/internal/db/basic.go b/internal/db/basic.go index 2a1141c8d..d94c98e45 100644 --- a/internal/db/basic.go +++ b/internal/db/basic.go @@ -62,12 +62,13 @@ type Basic interface {  	// The given interface i will be set to the result of the query, whatever it is. Use a pointer or a slice.  	Put(ctx context.Context, i interface{}) Error -	// UpdateByID updates i with id id. +	// UpdateByPrimaryKey updates all values of i based on its primary key.  	// The given interface i will be set to the result of the query, whatever it is. Use a pointer or a slice. -	UpdateByID(ctx context.Context, id string, i interface{}) Error +	UpdateByPrimaryKey(ctx context.Context, i interface{}) Error -	// UpdateOneByID updates interface i with database the given database id. It will update one field of key key and value value. -	UpdateOneByID(ctx context.Context, id string, key string, value interface{}, i interface{}) Error +	// UpdateOneByPrimaryKey sets one column of interface, with the given key, to the given value. +	// It uses the primary key of interface i to decide which row to update. This is usually the `id`. +	UpdateOneByPrimaryKey(ctx context.Context, key string, value interface{}, i interface{}) Error  	// UpdateWhere updates column key of interface i with the given value, where the given parameters apply.  	UpdateWhere(ctx context.Context, where []Where, key string, value interface{}, i interface{}) Error diff --git a/internal/db/bundb/basic.go b/internal/db/bundb/basic.go index d4de5bb0b..1e7880379 100644 --- a/internal/db/bundb/basic.go +++ b/internal/db/bundb/basic.go @@ -95,7 +95,7 @@ func (b *basicDB) DeleteWhere(ctx context.Context, where []db.Where, i interface  	return b.conn.ProcessError(err)  } -func (b *basicDB) UpdateByID(ctx context.Context, id string, i interface{}) db.Error { +func (b *basicDB) UpdateByPrimaryKey(ctx context.Context, i interface{}) db.Error {  	q := b.conn.  		NewUpdate().  		Model(i). @@ -105,7 +105,7 @@ func (b *basicDB) UpdateByID(ctx context.Context, id string, i interface{}) db.E  	return b.conn.ProcessError(err)  } -func (b *basicDB) UpdateOneByID(ctx context.Context, id string, key string, value interface{}, i interface{}) db.Error { +func (b *basicDB) UpdateOneByPrimaryKey(ctx context.Context, key string, value interface{}, i interface{}) db.Error {  	q := b.conn.NewUpdate().  		Model(i).  		Set("? = ?", bun.Safe(key), value). diff --git a/internal/db/bundb/basic_test.go b/internal/db/bundb/basic_test.go index e5f7e159a..acdfb6640 100644 --- a/internal/db/bundb/basic_test.go +++ b/internal/db/bundb/basic_test.go @@ -64,6 +64,40 @@ func (suite *BasicTestSuite) TestGetAllNotNull() {  	}  } +func (suite *BasicTestSuite) TestUpdateOneByPrimaryKeySetEmpty() { +	testAccount := suite.testAccounts["local_account_1"] + +	// try removing the note from zork +	err := suite.db.UpdateOneByPrimaryKey(context.Background(), "note", "", testAccount) +	suite.NoError(err) + +	// get zork out of the database +	dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID) +	suite.NoError(err) +	suite.NotNil(dbAccount) + +	// note should be empty now +	suite.Empty(dbAccount.Note) +} + +func (suite *BasicTestSuite) TestUpdateOneByPrimaryKeySetValue() { +	testAccount := suite.testAccounts["local_account_1"] + +	note := "this is my new note :)" + +	// try updating the note on zork +	err := suite.db.UpdateOneByPrimaryKey(context.Background(), "note", note, testAccount) +	suite.NoError(err) + +	// get zork out of the database +	dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID) +	suite.NoError(err) +	suite.NotNil(dbAccount) + +	// note should be set now +	suite.Equal(note, dbAccount.Note) +} +  func TestBasicTestSuite(t *testing.T) {  	suite.Run(t, new(BasicTestSuite))  } | 
