Example_requireInserted demonstrates the use of the RequireInserted test assertion, which verifies that a single job was inserted.
()
| 28 | // Example_requireInserted demonstrates the use of the RequireInserted test |
| 29 | // assertion, which verifies that a single job was inserted. |
| 30 | func Example_requireInserted() { |
| 31 | ctx := context.Background() |
| 32 | |
| 33 | dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL()) |
| 34 | if err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | defer dbPool.Close() |
| 38 | |
| 39 | workers := river.NewWorkers() |
| 40 | river.AddWorker(workers, &RequiredWorker{}) |
| 41 | |
| 42 | config := initTestConfig(ctx, dbPool, &river.Config{ |
| 43 | Workers: workers, |
| 44 | }) |
| 45 | schemaOpts := &rivertest.RequireInsertedOpts{Schema: config.Schema} |
| 46 | |
| 47 | riverClient, err := river.NewClient(riverpgxv5.New(dbPool), config) |
| 48 | if err != nil { |
| 49 | panic(err) |
| 50 | } |
| 51 | |
| 52 | tx, err := dbPool.Begin(ctx) |
| 53 | if err != nil { |
| 54 | panic(err) |
| 55 | } |
| 56 | defer tx.Rollback(ctx) |
| 57 | |
| 58 | _, err = riverClient.InsertTx(ctx, tx, &RequiredArgs{ |
| 59 | Message: "Hello.", |
| 60 | }, nil) |
| 61 | if err != nil { |
| 62 | panic(err) |
| 63 | } |
| 64 | |
| 65 | // Required for purposes of our example here, but in reality t will be the |
| 66 | // *testing.T that comes from a test's argument. |
| 67 | t := &testing.T{} |
| 68 | |
| 69 | job := rivertest.RequireInsertedTx[*riverpgxv5.Driver](ctx, t, tx, &RequiredArgs{}, schemaOpts) |
| 70 | fmt.Printf("Test passed with message: %s\n", job.Args.Message) |
| 71 | |
| 72 | // Verify the same job again, and this time that it was inserted at the |
| 73 | // default priority and default queue. |
| 74 | _ = rivertest.RequireInsertedTx[*riverpgxv5.Driver](ctx, t, tx, &RequiredArgs{}, &rivertest.RequireInsertedOpts{ |
| 75 | Priority: 1, |
| 76 | Queue: river.QueueDefault, |
| 77 | Schema: config.Schema, |
| 78 | }) |
| 79 | |
| 80 | // Insert and verify one on a pool instead of transaction. |
| 81 | _, err = riverClient.Insert(ctx, &RequiredArgs{Message: "Hello from pool."}, nil) |
| 82 | if err != nil { |
| 83 | panic(err) |
| 84 | } |
| 85 | _ = rivertest.RequireInserted(ctx, t, riverpgxv5.New(dbPool), &RequiredArgs{}, schemaOpts) |
| 86 | |
| 87 | // Output: |
nothing calls this directly
no test coverage detected
searching dependent graphs…