Wednesday, 3 May 2017

Unit Test Functions Connecting to a Postgres DB

I am using mocha, nodejs and pg-promise connecting to a Postgres db. I am wondering how can I unit test a function that executes a DML? This is just a simple example:

export function insertUser(db, { firstName, lastName }) {
  db.one(`
    INSERT INTO users
    (
      firstName,
      lastName
    )
    VALUES
    (
      $<firstName>,
      $<lastName>
    )
  `, { firstName, lastName });
}

If there is a typo in the query, how can the unit test capture it? I can create a test db, creating temporary tables on the fly but this will cause my test to slow down.



via Chris

No comments:

Post a Comment