@@ -32,6 +32,7 @@ import (
3232
3333 "github.com/codenotary/immudb/embedded/store"
3434 "github.com/codenotary/immudb/embedded/tbtree"
35+ "github.com/google/uuid"
3536 "github.com/stretchr/testify/require"
3637 "github.com/stretchr/testify/suite"
3738)
@@ -383,7 +384,7 @@ func TestTimestampCasts(t *testing.T) {
383384func TestUUIDAsPK (t * testing.T ) {
384385 engine := setupCommonTest (t )
385386
386- _ , _ , err := engine .Exec (context .Background (), nil , "CREATE TABLE IF NOT EXISTS uuid_table(id UUID, PRIMARY KEY id)" , nil )
387+ _ , _ , err := engine .Exec (context .Background (), nil , "CREATE TABLE IF NOT EXISTS uuid_table(id UUID, test INTEGER, PRIMARY KEY id)" , nil )
387388 require .NoError (t , err )
388389
389390 sel := EncodeSelector ("" , "uuid_table" , "id" )
@@ -426,6 +427,46 @@ func TestUUIDAsPK(t *testing.T) {
426427 require .Equal (t , row .ValuesByPosition [0 ], row .ValuesBySelector [sel ])
427428 })
428429
430+ t .Run ("must accept uuid string as an UUID" , func (t * testing.T ) {
431+ id := uuid .UUID ([16 ]byte {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x40 , 0x06 , 0x80 , 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f })
432+ _ , _ , err = engine .Exec (context .Background (), nil , "INSERT INTO uuid_table(id, test) VALUES(@uuid, 3)" , map [string ]interface {}{
433+ "uuid" : id .String (),
434+ })
435+ require .NoError (t , err )
436+
437+ r , err := engine .Query (context .Background (), nil , "SELECT id FROM uuid_table WHERE test = 3" , nil )
438+ require .NoError (t , err )
439+ defer r .Close ()
440+
441+ row , err := r .Read (context .Background ())
442+ require .NoError (t , err )
443+ require .Equal (t , UUIDType , row .ValuesBySelector [sel ].Type ())
444+
445+ require .Len (t , row .ValuesByPosition , 1 )
446+ require .Equal (t , row .ValuesByPosition [0 ], row .ValuesBySelector [sel ])
447+ require .Equal (t , id , row .ValuesByPosition [0 ].RawValue ())
448+ })
449+
450+ t .Run ("must accept byte slice as an UUID" , func (t * testing.T ) {
451+ id := uuid .UUID ([16 ]byte {0x10 , 0x01 , 0x02 , 0x03 , 0x04 , 0x40 , 0x06 , 0x80 , 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f })
452+ _ , _ , err = engine .Exec (context .Background (), nil , "INSERT INTO uuid_table(id, test) VALUES(@uuid, 4)" , map [string ]interface {}{
453+ "uuid" : id [:],
454+ })
455+ require .NoError (t , err )
456+
457+ r , err := engine .Query (context .Background (), nil , "SELECT id FROM uuid_table WHERE test = 4" , nil )
458+ require .NoError (t , err )
459+ defer r .Close ()
460+
461+ row , err := r .Read (context .Background ())
462+ require .NoError (t , err )
463+ require .Equal (t , UUIDType , row .ValuesBySelector [sel ].Type ())
464+
465+ require .Len (t , row .ValuesByPosition , 1 )
466+ require .Equal (t , row .ValuesByPosition [0 ], row .ValuesBySelector [sel ])
467+ require .Equal (t , id , row .ValuesByPosition [0 ].RawValue ())
468+ })
469+
429470}
430471
431472func TestUUIDNonPK (t * testing.T ) {
@@ -452,6 +493,46 @@ func TestUUIDNonPK(t *testing.T) {
452493 require .Equal (t , row .ValuesByPosition [0 ], row .ValuesBySelector [sel ])
453494 })
454495
496+ t .Run ("UUID as non PK must accept uuid string" , func (t * testing.T ) {
497+ id := uuid .UUID ([16 ]byte {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x40 , 0x06 , 0x80 , 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f })
498+ _ , _ , err = engine .Exec (context .Background (), nil , "INSERT INTO uuid_table(id, u, t) VALUES(2, @id, 't')" , map [string ]interface {}{
499+ "id" : id .String (),
500+ })
501+ require .NoError (t , err )
502+
503+ r , err := engine .Query (context .Background (), nil , "SELECT u FROM uuid_table WHERE id = 2 LIMIT 1" , nil )
504+ require .NoError (t , err )
505+ defer r .Close ()
506+
507+ row , err := r .Read (context .Background ())
508+ require .NoError (t , err )
509+ require .Equal (t , UUIDType , row .ValuesBySelector [sel ].Type ())
510+
511+ require .Len (t , row .ValuesByPosition , 1 )
512+ require .Equal (t , row .ValuesByPosition [0 ], row .ValuesBySelector [sel ])
513+ require .Equal (t , id , row .ValuesByPosition [0 ].RawValue ())
514+ })
515+
516+ t .Run ("UUID as non PK must accept byte slice" , func (t * testing.T ) {
517+ id := uuid .UUID ([16 ]byte {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x40 , 0x06 , 0x80 , 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f })
518+ _ , _ , err = engine .Exec (context .Background (), nil , "INSERT INTO uuid_table(id, u, t) VALUES(3, @id, 't')" , map [string ]interface {}{
519+ "id" : id [:],
520+ })
521+ require .NoError (t , err )
522+
523+ r , err := engine .Query (context .Background (), nil , "SELECT u FROM uuid_table WHERE id = 3 LIMIT 1" , nil )
524+ require .NoError (t , err )
525+ defer r .Close ()
526+
527+ row , err := r .Read (context .Background ())
528+ require .NoError (t , err )
529+ require .Equal (t , UUIDType , row .ValuesBySelector [sel ].Type ())
530+
531+ require .Len (t , row .ValuesByPosition , 1 )
532+ require .Equal (t , row .ValuesByPosition [0 ], row .ValuesBySelector [sel ])
533+ require .Equal (t , id , row .ValuesByPosition [0 ].RawValue ())
534+ })
535+
455536}
456537
457538func TestFloatType (t * testing.T ) {
0 commit comments