후 데이터베이스를 어떻게 정리할 수 있습니까?node.js의 테스트 후 데이터베이스 정리
내가 https://github.com/bmabey/database_cleaner 사용 레일에서,하지만 난 Node.js를
Node.js를 (v0.10.26), PostgreSQL의 (9.3.3), 모카, restify 및 knex 비슷한 뭔가를 찾을 did't. 대중 스키마가 테스트 사용자에 속하면
후 데이터베이스를 어떻게 정리할 수 있습니까?node.js의 테스트 후 데이터베이스 정리
내가 https://github.com/bmabey/database_cleaner 사용 레일에서,하지만 난 Node.js를
Node.js를 (v0.10.26), PostgreSQL의 (9.3.3), 모카, restify 및 knex 비슷한 뭔가를 찾을 did't. 대중 스키마가 테스트 사용자에 속하면
내가 테스트 사이의 데이터베이스를 청소 발견 쉬운 방법은 자신이 삭제하고 필요할 때 스키마를 다시 생성 할 수 있습니다,
DROP SCHEMA public CASCADE;
CREATE SCHEMA public AUTHORIZATION my_test_user;
이다. 데이터베이스의 공용 스키마에있는 모든 것을 삭제하십시오.
좋은 패키지가있어, 그것을 확인 :
https://github.com/emerleite/node-database-cleaner/ 나는 테스트를 실행 전에 데이터베이스 를 다시 전에 각 it
지침과 db-migrate 패키지를 데이터베이스 를 청소하기 위해 database-cleaner
package를 사용합니다.
아래 설정.
추가하려면 다음 devDependencies
당신의 package.json
: 여기
"devDependencies": {
"chai": "^3.5.0",
"database-cleaner": "^1.1.0",
"mocha": "^3.0.2",
"db-migrate": "^0.10.0-beta.15",
"db-migrate-pg": "^0.1.10",
...
}
프로젝트 구조입니다 : .
├── config
│ ├── cleaner-config.js
│ ├── db.js
│ └── ...
├── db
│ ├── database.json
│ └── migrations
│ ├── ...
│ └── sqls
│ └── ...
├── node_modules
├── scripts
│ └── test
├── ...
├── src
│ ├── db.js
│ ├── models
│ └── ...
└── test
├── init.js
└── src
└── ...
cleaner-config.js
:
module.exports = {
postgresql: {
skipTables: ['migrations']
}
};
데이터베이스 화면 구성을 얻기 위해 사용되는 config/db.js
여기서 n은 :
// Prepare default DB config
const defaultOptions = function(environment = 'development') {
const host = 'db';
const port = 5432;
const user = process.env.POSTGRES_USER;
const password = process.env.POSTGRES_PASSWORD;
var conf = {
host: host,
port: port,
user: user,
password: password,
database: process.env.POSTGRES_DB,
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000 // Keeps idle connections open for a 30 seconds
};
// Change the used database in test environment
if (environment === 'test') {
conf.database = require('../db/database.json').test.database;
}
return conf;
};
// Return database configuration for all environments
module.exports = {
development: defaultOptions(),
test: defaultOptions('test')
};
src/db.js
파일은 데이터베이스 연결 설정에 대한 책임 :
const PgPool = require('pg-pool');
// create a config to configure both pooling behavior and client options
const CONFIG = require('../config/db')[process.env.NODE_ENV || 'development'];
// Initializes connection pool
const pool = new PgPool(CONFIG);
module.exports = function(callback) {
pool.on('error', function(error, client) {
// if an error is encountered by a client while it sits idle in the pool
// the pool itself will emit an error event with both the error and
// the client which emitted the original error
// this is a rare occurrence but can happen if there is a network partition
// between your application and the database, the database restarts, etc.
// and so you might want to handle it and at least log it out
console.error('idle client error', error.message, error.stack);
});
// to run a query we can acquire a client from the pool,
// run a query on the client, and then return the client to the pool
pool.connect(function(error, client, done) {
if (error)
return console.error('error fetching client from pool', error);
callback(client, done);
});
};
test
데이터베이스가 database.json
에 하드 코드를 (또한으로 사용) :
{
"test": {
"driver": "pg",
"user": {
"ENV": "POSTGRES_USER"
},
"password": {
"ENV": "POSTGRES_PASSWORD"
},
"host": "db",
"database": "<prefix>_test"
},
"development": {
"driver": "pg",
"user": {
"ENV": "POSTGRES_USER"
},
"password": {
"ENV": "POSTGRES_PASSWORD"
},
"host": "db",
"database": {
"ENV": "POSTGRES_DB"
}
},
"sql-file": true
}
I로드 test/init.js
파일 끝에있는 모든 테스트 :
DB = {
client: null,
closeConnection: null
}
beforeEach(function(done) {
require('../src/db')(function(client, dbDone) {
DB.client = client;
DB.closeConnection = dbDone;
var DatabaseCleaner = require('database-cleaner');
var databaseCleaner = new DatabaseCleaner('postgresql');
databaseCleaner.clean(client, done);
});
});
// TODO: close connection only once - at the end of testing
afterEach(function(done) {
DB.client = null;
DB.closeConnection();
done();
});
require('./src/<some library>.test');
...
마지막으로 테스트를 실행하는 데 사용되는 scripts/test
스크립트없는 이상 :
#!/bin/bash
script_directory="$(cd "$(dirname "$0")" && pwd)"
project_directory=$script_directory/..
# Stop execution and exit on any error
set -e
cd $project_directory
db_name='<prefix>_test'
# Drop the DB
# Use the development environment because of this issue: https://github.com/db-migrate/node-db-migrate/issues/393
./node_modules/.bin/db-migrate --env development --migrations-dir db/migrations --config db/database.json db:drop $db_name
# Create the DB
# Use the development environment because of this issue: https://github.com/db-migrate/node-db-migrate/issues/393
./node_modules/.bin/db-migrate --env development --migrations-dir db/migrations --config db/database.json db:create $db_name
./node_modules/.bin/db-migrate --env test --migrations-dir db/migrations --config db/database.json up
NODE_ENV=test ./node_modules/.bin/mocha test/init.js
에게