Add functional tests to my Go project
The tests should bring up an actual MySQL database (by connecting to a docker container already up and running from the docker compose config that runs the tests).
The test should create all of the tables in the database, add some test data, run the tests and then delete everything.
I want my generated ORM to use the Gorm hints on the table fields.
I can currently use Gorm Gen to generate the ORM. However it doesn't create any of the relations (nested structs with foreign key, many2many etc hints).
I can currently use Gorm Gen to generate the ORM. However it doesn't create any of the relations (nested structs with foreign key, many2many etc hints).
For example, In my database I have a User table with a many to many connection with an Account table. This many to many connection is facilitated in the database by a User_Account table with a user ID and account ID. The SQL to generate this for a MySQL database is this:Example
CREATE TABLE user (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
UNIQUE (id)
)
CREATE TABLE account (
id INT NOT NULL AUTO_INCREMENT,
UNIQUE (id)
)
CREATE TABLE user_account (
user_id INT NOT NULL,
account_id INT NOT NULL,
PRIMARY KEY (user_id, account_id),
FOREIGN KEY(account_id) REFERENCES account (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(user_id) REFERENCES user (id)
)
- table
userhas a n to m relation to tableAccount - this relation is facilitated by a
User_Accounttable
CREATE TABLE user (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
UNIQUE (id)
)
CREATE TABLE account (
id INT NOT NULL AUTO_INCREMENT,
UNIQUE (id)
)
CREATE TABLE user_account (
user_id INT NOT NULL,
account_id INT NOT NULL,
PRIMARY KEY (user_id, account_id),
FOREIGN KEY(account_id) REFERENCES account (id)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(user_id) REFERENCES user (id)
)
type User struct {
ID uint `json:"id" gorm:"primary_key"`
Accounts []Account `json:"accounts" gorm:"many2many:user_account"`
}
type Account struct {
ID uint `json:"id" gorm:"primary_key"`
Name string `json:"name"`
}
type User struct {
ID uint `json:"id" gorm:"primary_key"`
Accounts []Account `json:"accounts" gorm:"many2many:user_account"`
}
type Account struct {
ID uint `json:"id" gorm:"primary_key"`
Name string `json:"name"`
}
If I use g.GenerateAllTable() with Gen generate g I get all of the table structs generated with all of the gorm and json hints that I want above, but it doesn't include any of the relation information. I.e it doesn’t create the Accounts field Accounts []Account above with the many2many gorm hint many2many.
If instead I use:
accounts := g.GenerateModel("account")
users := g.GenerateModel("user", gen.FieldRelate(field.Many2Many, "Account", accounts,
&field.RelateConfig{
// RelateSlice: true,
GORMTag: field.GormTag{"foreignKey": []string{"AccountID"}, "references": []string{"ID"}},
}),
)
g.ApplyBasic(accounts, users)
accounts := g.GenerateModel("account")
users := g.GenerateModel("user",
gen.FieldRelate(field.Many2Many, "Account",
accounts,
&field.RelateConfig{
// RelateSlice: true,
GORMTag: field.GormTag{"foreignKey":[]string{"AccountID"},
"references": []string{"ID"}},
}),
)
g.ApplyBasic(accounts, users)