Skip to content

[Bug?]: Prisma Model with Bytes field fails SDL generation in Service Test template #9773

@dthyresson

Description

@dthyresson

What's not working?

Given the following Prisma schema for a Postgres datasource:

model Key {
  id        Int   @id @default(autoincrement())
  publicKey Bytes
}

after migrating, when generating the sdl:

yarn rw g sdl Key  

The command fails:

Reverted because: Error applying template at .../dist/commands/generate/service/templates/test.ts.template for
Key: Cannot read properties of undefined (reading 'replace')

Likely due to use of replace in transformValue of the packages/cli/src/commands/generate/service/templates/test.ts.template file:

// Transforms an object or single value into something that's more suitable
   // for generating test cases
   // If a `type` is passed in, the string for creating an object of that type
   // will be generated
   // If no type, or a type we don't support, is passed in we'll default to
   // generating regular strings
   // Looks for quoted strings, either by single (') or double (") quotes.
   // When found
   // - Removes the quotes around `scenario` variables.
   // - Removes the quotes around `BigInt` fields.
  const transformValue = (obj, type) => {
    if (type === 'DateTime') {
      return `new Date('${obj.toISOString()}')`
    } else if (type === 'Decimal') {
      return `new Prisma.Decimal(${obj})`
    }

    return JSON.stringify(obj).replace(/['"].*?['"]/g, (string) => {
      if (string.match(/scenario\./)) {
        return string.replace(/['"]/g, '')
      }

      // BigInt
      if (string.match(/^\"\d+n\"$/)) {
        return string.substr(1, string.length - 2)
      }

      return string
    })
  } %>

Also, interestingly:

yarn rw g sdl Key --no-tests

Still attempts to generate the test file for the service and gives same error.

How do we reproduce the bug?

  1. Setup a Postgres datasource (needed for Bytes type)
  2. Use the above Key model
  3. yarn rw prima migrate dev
  4. yarn rw g sdl Key

See error when attempting the create the service test template.

Related Issues

See also #5841

What's your environment? (If it applies)

No response

Are you interested in working on this?

  • I'm interested in working on this

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions