github.com/openfga/openfga@v1.5.4-rc1/pkg/storage/sqlcommon/sqlcommon_test.go (about)

     1  package sqlcommon
     2  
     3  import (
     4  	"database/sql"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/go-sql-driver/mysql"
     9  	openfgav1 "github.com/openfga/api/proto/openfga/v1"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/openfga/openfga/pkg/storage"
    13  )
    14  
    15  func TestHandleSQLError(t *testing.T) {
    16  	t.Run("duplicate_key_value_error_with_tuple_key_wraps_ErrInvalidWriteInput", func(t *testing.T) {
    17  		err := HandleSQLError(errors.New("duplicate key value"), &openfgav1.TupleKey{
    18  			Object:   "object",
    19  			Relation: "relation",
    20  			User:     "user",
    21  		})
    22  		require.ErrorIs(t, err, storage.ErrInvalidWriteInput)
    23  	})
    24  
    25  	t.Run("duplicate_entry_value_error_with_tuple_key_wraps_ErrInvalidWriteInput", func(t *testing.T) {
    26  		duplicateKeyError := &mysql.MySQLError{
    27  			Number:  1062,
    28  			Message: "Duplicate entry '' for key ''",
    29  		}
    30  		err := HandleSQLError(duplicateKeyError, &openfgav1.TupleKey{
    31  			Object:   "object",
    32  			Relation: "relation",
    33  			User:     "user",
    34  		})
    35  		require.ErrorIs(t, err, storage.ErrInvalidWriteInput)
    36  	})
    37  
    38  	t.Run("duplicate_entry_value_error_without_tuple_key_returns_collision", func(t *testing.T) {
    39  		duplicateKeyError := &mysql.MySQLError{
    40  			Number:  1062,
    41  			Message: "Duplicate entry '' for key ''",
    42  		}
    43  		err := HandleSQLError(duplicateKeyError)
    44  
    45  		require.ErrorIs(t, err, storage.ErrCollision)
    46  	})
    47  
    48  	t.Run("duplicate_key_value_error_without_tuple_key_returns_collision", func(t *testing.T) {
    49  		duplicateKeyError := errors.New("duplicate key value")
    50  		err := HandleSQLError(duplicateKeyError)
    51  		require.ErrorIs(t, err, storage.ErrCollision)
    52  	})
    53  
    54  	t.Run("sql.ErrNoRows_is_converted_to_storage.ErrNotFound_error", func(t *testing.T) {
    55  		err := HandleSQLError(sql.ErrNoRows)
    56  		require.ErrorIs(t, err, storage.ErrNotFound)
    57  	})
    58  }