Setting up test scenarios with lots of relations

I’m finally getting round to figuring out tests on the service side and trying to work out how to create a scenario that will enable me to test some code that makes a simple change to a model that has many relations.

The model that I am updating in my code is for a payment, I am just testing that the status field is updated to ‘SUCCEEDED’.

But, the Payment has a foreign key relation to a Booking, the Booking to a Session, the Session to a Location and to an Activity, and both of those to an Organisation.

If I just create my Payment in my scenario then I get a foreign key related error when I run the test.


export const standard = defineScenario<Prisma.PaymentCreateArgs>({
  payment: {
    one: {
      data: {
        provider: 'STRIPE',
        currencyCode: 'gbp',
        amount: 800,
        status: 'NEW',
        bookingId: 'one',
        providerPaymentId: 'abc123',
      },
    },
  },
})

Is there another way to set this up than to create an organisation, location, activity, session, booking and payment in my scenario?

Or given that I’ll no doubt have to do that again for each other model I want to test, is there a way that I can set this up so that I can import things into my scenario to avoid duplication between scenarios?

Thanks

Having gone ahead and set this up the long way I worked out a few things.
Firstly, it wasn’t actually that much of a pain to define the related objects:

import type { Prisma } from '@prisma/client'

export const standard = defineScenario<Prisma.PaymentCreateArgs>({
  payment: {
    one: {
      data: {
        provider: 'STRIPE',
        currencyCode: 'gbp',
        amount: 800,
        status: 'NEW',
        booking: {
          create: {
            customerEmail: 'test@test.com',
            customerName: 'Testy McTestFace',
            status: 'NEW',
            session: {
              create: {
                start: new Date(),
                end: new Date(),
                capacity: 1,
                activity: {
                  create: {
                    name: 'activity',
                    organisation: {
                      create: {
                        name: 'org',
                      },
                    },
                  },
                },
                location: {
                  create: {
                    name: 'location',
                    organisation: {
                      create: {
                        name: 'org1',
                      },
                    },
                  },
                },
              },
            },
          },
        },
        providerPaymentId: 'abc123',
      },
    },
  },
})

Secondly I realised that I could extract that out into a separate value if I wanted to use it in other scenarios and import that from another file if needed:

import type { Prisma } from '@prisma/client'

const basicPayment = {
  one: {
    data: {
      provider: 'STRIPE',
      currencyCode: 'gbp',
      amount: 800,
      status: 'NEW',
      booking: {
        create: {
          customerEmail: 'test@test.com',
          customerName: 'Testy McTestFace',
          status: 'NEW',
          session: {
            create: {
              start: new Date(),
              end: new Date(),
              capacity: 1,
              activity: {
                create: {
                  name: 'activity',
                  organisation: {
                    create: {
                      name: 'org',
                    },
                  },
                },
              },
              location: {
                create: {
                  name: 'location',
                  organisation: {
                    create: {
                      name: 'org1',
                    },
                  },
                },
              },
            },
          },
        },
      },
      providerPaymentId: 'abc123',
    },
  },
}

export const standard = defineScenario<Prisma.PaymentCreateArgs>({
  payment: basicPayment,
})

And lastly, if I wanted to (although not sure whether this is a good idea), I could also manipulate the values before passing them into the scenario like this:
basicPayment.one.data.currencyCode = 'usd'