MCPcopy Index your code
hub / github.com/coder/coder / TestNestedInTx

Function TestNestedInTx

coderd/database/db_test.go:50–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

48}
49
50func TestNestedInTx(t *testing.T) {
51 t.Parallel()
52 if testing.Short() {
53 t.SkipNow()
54 }
55
56 uid := uuid.New()
57 sqlDB := testSQLDB(t)
58 err := migrations.Up(sqlDB)
59 require.NoError(t, err, "migrations")
60
61 db := database.New(sqlDB)
62 err = db.InTx(func(outer database.Store) error {
63 return outer.InTx(func(inner database.Store) error {
64 //nolint:gocritic
65 require.Equal(t, outer, inner, "should be same transaction") // intxcheck:ignore // intentional: test asserts nested InTx returns same store
66
67 _, err := inner.InsertUser(context.Background(), database.InsertUserParams{
68 ID: uid,
69 Email: "coder@coder.com",
70 Username: "coder",
71 HashedPassword: []byte{},
72 CreatedAt: dbtime.Now(),
73 UpdatedAt: dbtime.Now(),
74 RBACRoles: []string{},
75 LoginType: database.LoginTypeGithub,
76 })
77 return err
78 }, nil)
79 }, nil)
80 require.NoError(t, err, "outer tx: %w", err)
81
82 user, err := db.GetUserByID(context.Background(), uid)
83 require.NoError(t, err, "user exists")
84 require.Equal(t, uid, user.ID, "user id expected")
85}
86
87func TestInTx_CapturesRollbackError(t *testing.T) {
88 t.Parallel()

Callers

nothing calls this directly

Calls 10

UpFunction · 0.92
NewFunction · 0.92
NowFunction · 0.92
SkipNowMethod · 0.80
testSQLDBFunction · 0.70
NewMethod · 0.65
InTxMethod · 0.65
InsertUserMethod · 0.65
GetUserByIDMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected