UI 测试的夹具(JSON 文件)
Fixtures (JSON file) for UI Testing
是否有人使用固定装置(json 文件)将数据加载到 postgres 数据库并执行 UI 测试?
使用 node-postgres 和 node-pg-copy-streams 可以从 postgres 数据库加载和 remove/delete 数据。
代码和示例:
[https://github.com/AmitKulkarni9/fixtures-tests][1]
LoadData.js
const fs = require('fs');
const path = require('path');
const {Client} = require('pg');
const copyFrom = require('pg-copy-streams').from;
const config = require('./config.json');
// Getting connection parameters from config.json
const host = config.host;
const user = config.user;
const pw = config.pw;
const db = config.db;
const port = config.port;
const conString = `postgres://${user}:${pw}@${host}:${port}/${db}`;
class LoadData {
async Load (targetTable) {
try {
const inputFile = path.join(__dirname, `/data/${targetTable}.csv`);
const client = new Client({connectionString: conString,});
client.connect(); // gets connection
const stream = client.query(copyFrom(`COPY ${targetTable} FROM STDIN WITH CSV HEADER`));
const fileStream = fs.createReadStream(inputFile);
fileStream.on('error', (error) =>{
console.log(`Error in creating read stream ${error}`);
client.end();
return;
})
stream.on('error', (error) => {
console.log(`Error in creating stream ${error}`);
client.end();
return;
})
stream.on('finish', () => {
console.log(`Completed loading data into ${targetTable}`);
client.end();
})
fileStream.pipe(stream);
return;
} catch (e) {
console.error(e);
client.end();
return;
}
}
}
module.exports = LoadData;
RemoveData.js
const path = require('path');
const {Client} = require('pg');
const config = require('./config.json');
// Getting connection parameters from config.json
const host = config.host;
const user = config.user;
const pw = config.pw;
const db = config.db;
const port = config.port;
const conString = `postgres://${user}:${pw}@${host}:${port}/${db}`;
class RemoveData {
async Remove (table) {
try {
const client = new Client({connectionString: conString,});
client.connect(); // gets connection
client.query(`DELETE FROM ${table} WHERE id IN (,,) RETURNING *`, [1,2,3], (err, result) => {
if(err) {
console.log(err);
client.end();
return;
} else {
console.log(`Deleted data from ${table}`);
//let results= result.rows;
//console.log(results);
client.end();
return;
}
});
} catch (e) {
console.error(e);
client.end();
return;
}
}
}
module.exports = RemoveData;
是否有人使用固定装置(json 文件)将数据加载到 postgres 数据库并执行 UI 测试?
使用 node-postgres 和 node-pg-copy-streams 可以从 postgres 数据库加载和 remove/delete 数据。 代码和示例: [https://github.com/AmitKulkarni9/fixtures-tests][1]
LoadData.js
const fs = require('fs');
const path = require('path');
const {Client} = require('pg');
const copyFrom = require('pg-copy-streams').from;
const config = require('./config.json');
// Getting connection parameters from config.json
const host = config.host;
const user = config.user;
const pw = config.pw;
const db = config.db;
const port = config.port;
const conString = `postgres://${user}:${pw}@${host}:${port}/${db}`;
class LoadData {
async Load (targetTable) {
try {
const inputFile = path.join(__dirname, `/data/${targetTable}.csv`);
const client = new Client({connectionString: conString,});
client.connect(); // gets connection
const stream = client.query(copyFrom(`COPY ${targetTable} FROM STDIN WITH CSV HEADER`));
const fileStream = fs.createReadStream(inputFile);
fileStream.on('error', (error) =>{
console.log(`Error in creating read stream ${error}`);
client.end();
return;
})
stream.on('error', (error) => {
console.log(`Error in creating stream ${error}`);
client.end();
return;
})
stream.on('finish', () => {
console.log(`Completed loading data into ${targetTable}`);
client.end();
})
fileStream.pipe(stream);
return;
} catch (e) {
console.error(e);
client.end();
return;
}
}
}
module.exports = LoadData;
RemoveData.js
const path = require('path');
const {Client} = require('pg');
const config = require('./config.json');
// Getting connection parameters from config.json
const host = config.host;
const user = config.user;
const pw = config.pw;
const db = config.db;
const port = config.port;
const conString = `postgres://${user}:${pw}@${host}:${port}/${db}`;
class RemoveData {
async Remove (table) {
try {
const client = new Client({connectionString: conString,});
client.connect(); // gets connection
client.query(`DELETE FROM ${table} WHERE id IN (,,) RETURNING *`, [1,2,3], (err, result) => {
if(err) {
console.log(err);
client.end();
return;
} else {
console.log(`Deleted data from ${table}`);
//let results= result.rows;
//console.log(results);
client.end();
return;
}
});
} catch (e) {
console.error(e);
client.end();
return;
}
}
}
module.exports = RemoveData;