From 5a4b1d08cc7a9eedf8cd7f48fec26cd2d4897802 Mon Sep 17 00:00:00 2001 From: Ed Minnix Date: Sun, 5 Jan 2025 22:07:34 -0500 Subject: [PATCH] Add gorm tests --- .../flowsources/local/database/test_gorm.go | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/test_gorm.go diff --git a/go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/test_gorm.go b/go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/test_gorm.go new file mode 100644 index 000000000000..02dc1feee85b --- /dev/null +++ b/go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/test_gorm.go @@ -0,0 +1,45 @@ +package test + +import "gorm.io/gorm" + +type User struct{} + +// test querying an Association +func test_gorm_AssociationQuery(association *gorm.Association) { + association.Find(&User{}) // $ source +} + +// test querying a ConnPool +func test_gorm_ConnPoolQuery(connPool gorm.ConnPool) { + rows, err := connPool.QueryContext(nil, "SELECT * FROM users") // $ source + + if err != nil { + return + } + + defer rows.Close() + + userRow := connPool.QueryRowContext(nil, "SELECT * FROM users WHERE id = 1") // $ source + + ignore(userRow) +} + +// test querying a DB +func test_gorm_db(db *gorm.DB) { + db.Find(&User{}) // $ source + + db.FindInBatches(&User{}, 10, nil) // $ source + + db.FirstOrCreate(&User{}) // $ source + + db.FirstOrInit(&User{}) // $ source + + db.First(&User{}) // $ source + + db.Last(&User{}) // $ source + + db.Take(&User{}) // $ source + + db.Scan(&User{}) // $ source + +}