prisma-test-utils-tmp
[![CircleCI](https://circleci.com/gh/prisma/prisma-test-utils/tree/master.svg?style=shield)](https://circleci.com/gh/prisma/prisma-test-utils/tree/master) [![codecov](https://codecov.io/gh/prisma/prisma-test-utils/branch/master/graph/badge.svg)](https://c
Last updated 8 months ago by weakky .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install prisma-test-utils-tmp 
SYNC missed versions from official npm registry.

???? prisma-test-utils

CircleCI codecov npm version

⚠️ This project is temporarily unmaintained. Please reach out in the #prisma2-preview channel on the Prisma Slack if you're interested in collaborating on it. We're planning to pick up development soon again (see this issue).

In testing workflows, generating seed data usually includes a lot of boilerplate. We rely on hardcoded fixtures that need to be migrated with changing code.

prisma-test-utils solve that by generating test util functions based on your Prisma schema. As your application evolves, the generated data also evolves deterministically.

Features

  • ???? Data model agnostic: Optimised for you datamodel.
  • ???? Flexible: Cherry picked default settings.
  • ???? Out-of-the-box usage: Plug-in generator for your Prisma schema.
  • ???? Seeds mock data: Populates your database with mock data.
  • ???? Per-test database: Creates an isolated database for each test.

Installation

No installation! Comes preinstalled with prisma2! ????

Configuration

generator test-utils {
  provider = "prisma-test-utils"
  output = "node*modules/@generated/test-utils"
}

Usage

prisma-test-utils pack two incredibly useful functions. The first one, pool, can be used to create a pool of databases that you can use during testing, and are wiped after you've finished. The second one, seed, helps you populate your data with vast amount of data.

Database Pools

We can configure our pool requirements before running any test cases.

import SQLitePool, { Pool } from '@generated/prisma-test-utils'

let pool: Pool

beforeAll(async () => {
  pool = new SQLitePool({
    pool: {
      min: 3,
      max: 5,
    },
  })
})

This allows us to request an isolated database per test case

test('one of my parallel tests', async () => {
  /* Acquire new db instance. */
  const db = await pool.getDBInstance()

  // Write the test case logic
  const client = new Photon({
    datasources: {
      db: db.url,
    },
  })

  /* Release the instance. */
  client.disconnect()
  pool.releaseDBInstance(db)
})

API

/* All pool instances. */

class Pool {
  async getDBInstance(): Promise<DBInstance>
  async releaseDBInstance(db: DBInstance): Promise<void>
  async run<T>(fn: (db: DBInstance) => Promise<T>): Promise<T>
  async drain(): Promise<void>
}

/* PostgreSQL */

interface PostgreSQLConnection {
  host: string
  port: number
  user: string
  password?: string
  database: string
  schema: string
}

interface PostgreSQLPoolOptions {
  connection: (id: string) => PostgreSQLConnection
  pool?: {
    max?: number
  }
}

/* MySQL */

interface MySQLConnection {
  host: string
  port: string
  user: string
  password?: string
  database: string
}

interface MySQLPoolOptions {
  connection: (id string) => MySQLConnection
  pool?: {
    max?: number
  }
}

/* SQLite */

interface SQLitePoolOptions {
  databasePath: (id: string) => string
  pool?: {
    max?: number
  }
}

Seeding

import Photon from '@generated/photon'
import seed from '@generated/test-utils/seed'

test('test with seed data', async () => {
  await seed({
    client,
    models: kit => ({
      _: {
        /* Default number of instances. */
        amount: 500,
      },
      Blog: {
        factory: {
          /* Use functions from the kit. */
          name: kit.faker.sentence,
          /* Define custom mocks. */
          description: 'My custom blog description',
          /* Define custom mock functions. */
          entry: () => {
            return `A generated entry from the function.`
          },
          /* Manage relations. */
          posts: {
            max: 100,
          },
        },
      },
    }),
  })

  const blogs = await client.blogs()
})

Options

It is possible to selectively override the seed generation making the seeding workflow very flexible.

All options are autogenerated and checked at compile time. You'll be warned about any relation constraints that your datamodel presents.

beforeAll(async () => {
  const data = await seed(
    photon,
    bag => ({
      Post: {
        amount: 5,
        factory: {
          published: 'false',
        },
      },
    }),
    {
      seed: 42,
      silent: false,
      instances: 5,
    },
  )
})

LICENSE

MIT @ Prisma

Current Tags

  • 0.0.1                                ...           latest (8 months ago)

1 Versions

  • 0.0.1                                ...           8 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (7)
Dev Dependencies (28)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |