在NodeJS中测试数据库相关代码 [英] Testing database related code in NodeJS

查看:121
本文介绍了在NodeJS中测试数据库相关代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PostgreSQL.

Using PostgreSQL.

我正在尝试为API建立适当的测试套件.到目前为止,它可以工作,但是测试直接在主数据库上完成.然后,我必须删除测试创建或在这种情况下进行编辑的所有内容.

I'm trying to set up a proper test suite for an API. So far it works but the tests are done on the main database directly. I then have to remove everything my tests created, or edited in such case.

我知道这是一件坏事(因为我会忘记还原更改或弄乱顺序).因此,我想创建一个具有相同结构和基础数据的测试数据库,然后再将其删除.在这种情况下,这种方法是好的方法吗?

I know it's a bad thing to do (because I can forget to revert a change, or mess up the sequences). So I would like to create a test database with the same structure and base data, and then delete it afterwards. Is this approach the good one to take in this case?

如果我想这样做,应该怎么做? NodeJS中是否可以执行SQL脚本?我尝试使用Shell脚本,但到目前为止,权限已完全混乱,因此我认为直接使用NodeJS会更容易.

And if I want to do it this way, how should I do it? Is there a way in NodeJS to execute an SQL script? I tried with a shell script but so far it's a been complete mess with the permissions, so I figured it would be easier with NodeJS directly.

我正在使用Mocha进行测试.

I'm using Mocha for my tests.

推荐答案

我建议使用单独的测试数据库.它可能很轻,您将想知道其中的数据(因此可以对其进行测试!).可以处理您所有业务规则的基本数据集可以作为SQL文件(或某些导出方法)导出.

I would suggest a separate test database. It can be light, and you will want to know the data that is in there (so you can test it!). A base dataset that can handle all your business rules can be exported as a SQL file (or some export method).

通常,您的应用程序将与数据库建立连接,并且您的测试框架将具有在测试开始之前运行方法的某种方法.您可以在此处指定测试数据库.您的数据库访问对象(DAO)或脚本,方法将以某种方式利用主连接,作为方法参数或require语句等.

Typically your application will have a connection to the database, and your test framework will have some approach to run a method prior to tests starting. It is here that you specify the test DB. Your database access objects (DAOs), or scripts, methods, will utilize the main connection in some way, either as a method parameter, or require statement, etc.

作为示例,我正在使用 knex模块连接到数据库并构建查询.我初始化并引用了其文档中指定的单个数据库连接.

As an example, I'm using the knex module to connect to the DB and build queries. I initialize and reference my single DB connection as specified in their docs.

var Knex = require( 'knex' );
Knex.knex = Knex.initialize( {
  client : 'mysql',
  connection : {
  host : 'my.domain.com',
  user : 'dbrole',
  password : 'password',
  database : 'productiondb',
  charset : 'utf8'
  }
} );

我的DAO像这样获得连接:

My DAOs get the connection like this:

var knex = require('knex').knex;

现在在单元测试中,在运行测试套件之前,我可以将连接设置为测试数据库

Now in my unit tests, before a test suite is run, I can set my connection to be the test DB

var Knex = require( 'knex' );
Knex.knex = Knex.initialize( {
  client : 'mysql',
  connection : {
  host : '127.0.0.1',
  user : 'root',
  password : 'root',
  database : 'testdb',
  charset : 'utf8'
  }
} );

就在那里!测试和生产中使用完全相同的代码,并且生产数据库与测试分离.这种模式可以与许多框架一起使用,因此您必须进行调整(如果它们妨碍了测试数据库,请清理测试,如果完成所有测试,则可以恢复为默认设置).

And there you have it! Exact same code is used in test and production, and your production DB is de-coupled from your tests. This pattern can work with a lot of frameworks, so you'll have to adapt (and clean up your tests if they are junking up the test DB, maybe a restore to default when all tests are complete).

顺便说一下,knex与postgre一起使用,是在纯节点JS中构建查询的一种有趣方式.它还可以执行原始SQL.

By the way, knex works with postgre and is a fun way to build queries in pure node JS. It can also execute raw SQL.

这篇关于在NodeJS中测试数据库相关代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆