使用 postgreSQL 和 Tape(npm 模块)的 INNER JOIN 查询测试失败
Failing TEST of INNER JOIN query using postgreSQL and Tape(npm module)
我有一个 SQL 查询,当我直接在我的 PostgreSQL 数据库中使用它时,它似乎工作正常,但是我正在努力使它的磁带测试按预期通过输出不是我所期望的。
我已将我的实际查询粘贴到我的 pgcli/database 界面中,它工作正常。
- 在我的 findQueries.js 文件中,我有以下功能:
const findAllFoodItems = cb => {
dbConnection.query(
'SELECT products.name, categories.name FROM products INNER JOIN categories ON products.category_id = categories.id;',
(err, res) => {
if (err) {
cb(err);
} else {
cb(null, res.rows);
}
}
)
}
module.exports = { findAllFoodItems }
- 我对函数的测试:
test("Check findAllFoodItems queries correctly", t => {
findAllFoodItems((err, res) => {
if(err) {
t.error(err, "Unable to findAllFoodItems");
t.end();
} else {
const expected = [{"Banana": "Fruit"}, {"Potato": "Vegetables"}, {"Sausages": "Meat"}, {"Apple": "Fruit"}];
t.deepEquals(res, expected);
t.end();
}
})
});
- 当我在我的数据库中运行查询时,我得到了我想要的输出:
SELECT products.name, categories.name
FROM products
INNER JOIN categories ON products.category_id = categories.id;
输出:
+----------+------------+
| name | name |
|----------+------------|
| Banana | Fruit |
| Potato | Vegetables |
| Sausages | Meat |
| Apple | Fruit |
+----------+------------+
Tape/test 失败报告:
operator: deepEqual
expected: |-
[ { Banana: 'Fruit' }, { Potato: 'Vegetables' }, { Sausages: 'Meat' }, { Apple: 'Fruit' } ]
actual: |-
[ { name: 'Fruit' }, { name: 'Vegetables' }, { name: 'Meat' }, { name: 'Fruit' } ]
第一个问题是您的查询 return 的两列具有相同的名称(即 name
)。 Postgres 很乐意处理这个问题,但是这可能会在客户端造成歧义,特别是如果它使用列名作为键,这里似乎就是这种情况。您需要为结果集中的列添加别名,例如:
SELECT products.name pname, categories.name cname FROM ...
第二个问题是您的预期结果不正确。您的驱动程序似乎 return 一个 key/value 对数组,其中键是列名。因此,您应该检查以下输出:
[
{ pname: 'Banana', cname: 'Fruit' },
{ pname: 'Potato', cname: 'Vegetables' },
{ pname: 'Sausages', cname: 'Meat' },
{ pname: 'Apple', cname: 'Fruit' }
]
我有一个 SQL 查询,当我直接在我的 PostgreSQL 数据库中使用它时,它似乎工作正常,但是我正在努力使它的磁带测试按预期通过输出不是我所期望的。
我已将我的实际查询粘贴到我的 pgcli/database 界面中,它工作正常。
- 在我的 findQueries.js 文件中,我有以下功能:
const findAllFoodItems = cb => {
dbConnection.query(
'SELECT products.name, categories.name FROM products INNER JOIN categories ON products.category_id = categories.id;',
(err, res) => {
if (err) {
cb(err);
} else {
cb(null, res.rows);
}
}
)
}
module.exports = { findAllFoodItems }
- 我对函数的测试:
test("Check findAllFoodItems queries correctly", t => {
findAllFoodItems((err, res) => {
if(err) {
t.error(err, "Unable to findAllFoodItems");
t.end();
} else {
const expected = [{"Banana": "Fruit"}, {"Potato": "Vegetables"}, {"Sausages": "Meat"}, {"Apple": "Fruit"}];
t.deepEquals(res, expected);
t.end();
}
})
});
- 当我在我的数据库中运行查询时,我得到了我想要的输出:
SELECT products.name, categories.name
FROM products
INNER JOIN categories ON products.category_id = categories.id;
输出:
+----------+------------+
| name | name |
|----------+------------|
| Banana | Fruit |
| Potato | Vegetables |
| Sausages | Meat |
| Apple | Fruit |
+----------+------------+
Tape/test 失败报告:
operator: deepEqual
expected: |-
[ { Banana: 'Fruit' }, { Potato: 'Vegetables' }, { Sausages: 'Meat' }, { Apple: 'Fruit' } ]
actual: |-
[ { name: 'Fruit' }, { name: 'Vegetables' }, { name: 'Meat' }, { name: 'Fruit' } ]
第一个问题是您的查询 return 的两列具有相同的名称(即 name
)。 Postgres 很乐意处理这个问题,但是这可能会在客户端造成歧义,特别是如果它使用列名作为键,这里似乎就是这种情况。您需要为结果集中的列添加别名,例如:
SELECT products.name pname, categories.name cname FROM ...
第二个问题是您的预期结果不正确。您的驱动程序似乎 return 一个 key/value 对数组,其中键是列名。因此,您应该检查以下输出:
[
{ pname: 'Banana', cname: 'Fruit' },
{ pname: 'Potato', cname: 'Vegetables' },
{ pname: 'Sausages', cname: 'Meat' },
{ pname: 'Apple', cname: 'Fruit' }
]