Skip to content

Commit

Permalink
fixed bug where wrong SQL function was called
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilbert Mena committed Jan 8, 2024
1 parent f422e35 commit 80cf9b0
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ func removeGlobalLockOwnedByThisMachineTypeSQL() string {
return "UPDATE GLOBAL_LOCK SET UnlockTimestamp = NOW() WHERE StateMachineType = ? AND LookupKey = ? AND (UnlockTimestamp IS NULL OR UnlockTimestamp > NOW());"
}
func removeGlobalLockOwnedByThisMachineType(tx *sql.Tx, sm *StateMachine) error {
_, err := tx.Exec(removeGlobalLockOwnedByThisInstanceSQL(), sm.Name, sm.LookupKey)
_, err := tx.Exec(removeGlobalLockOwnedByThisMachineTypeSQL(), sm.Name, sm.LookupKey)
if err != nil {
return err
}
41 changes: 41 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,47 @@ import (
"github.com/DATA-DOG/go-sqlmock"
)

func TestRemoveGlobalLockOwnedByThisMachineType(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()

stateMachine := &StateMachine{
Name: "TestMachine",
LookupKey: "TestKey",
}

mock.ExpectBegin()

// Start a mock transaction
tx, err := db.Begin()
if err != nil {
t.Fatalf("an error '%s' was not expected when starting a transaction", err)
}

// Mock the SQL execution
mock.ExpectExec(escapeRegexChars(removeGlobalLockOwnedByThisMachineTypeSQL())).
WithArgs(stateMachine.Name, stateMachine.LookupKey).
WillReturnResult(sqlmock.NewResult(0, 1)) // Mocking one record updated

// Call the function
if err := removeGlobalLockOwnedByThisMachineType(tx, stateMachine); err != nil {
t.Errorf("error was not expected while removing global lock: %s", err)
}

mock.ExpectCommit()

// Commit the transaction
tx.Commit()

// Check if the expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}

func TestDeleteLock(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {

0 comments on commit 80cf9b0

Please sign in to comment.