go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/dbloader/main_test.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package dbloader 9 10 import ( 11 "context" 12 "testing" 13 14 "go.charczuk.com/sdk/db" 15 "go.charczuk.com/sdk/db/dbgen" 16 "go.charczuk.com/sdk/db/migration" 17 "go.charczuk.com/sdk/logutil" 18 "go.charczuk.com/sdk/testutil" 19 "go.charczuk.com/sdk/uuid" 20 ) 21 22 // TestMain is the testing entrypoint. 23 func TestMain(m *testing.M) { 24 testutil.New(m, 25 testutil.OptWithDefaultDB( 26 db.OptLog(logutil.New()), 27 ), 28 testutil.OptBefore( 29 func(ctx context.Context) error { 30 // create the loader for migration(s) 31 migration.New( 32 migration.OptGroups( 33 migration.NewGroupWithAction( 34 dbgen.TableFrom(loaderForObjType{}), 35 ), 36 migration.NewGroupWithAction( 37 dbgen.TableFrom(loaderForObj{}, 38 dbgen.ForeignKey(loaderForObj{}, "type_id", loaderForObjType{}, "id"), 39 ), 40 ), 41 ), 42 ).Apply(context.Background(), testutil.DefaultDB()) 43 return nil 44 }, 45 ), 46 ).Run() 47 } 48 49 type loaderForObj struct { 50 ID uuid.UUID `db:"id,pk"` 51 Name string `db:"name"` 52 TypeID uuid.UUID `db:"type_id"` 53 Type *loaderForObjType `db:"-"` 54 } 55 56 func (loaderForObj) TableName() string { return "loader_for_obj" } 57 58 type loaderForObjType struct { 59 ID uuid.UUID `db:"id,pk"` 60 Name string `db:"name"` 61 } 62 63 func (loaderForObjType) TableName() string { return "loader_for_obj_type" }