![](/img/trans.png)
[英]When using (substack's) Tape module for testing, how do I run only one test in a file?
[英]Failing TEST of INNER JOIN query using postgreSQL and Tape(npm module)
我有一個SQL查詢,當我直接在我的PostgreSQL數據庫中使用它時似乎工作正常,但是我正在努力使我的磁帶測試通過,因為預期的輸出不是我所期望的。
我已經在我的pgcli /數據庫界面粘貼了我的實際查詢,它工作正常。
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;
OUTPUT:
+----------+------------+
| name | name |
|----------+------------|
| Banana | Fruit |
| Potato | Vegetables |
| Sausages | Meat |
| Apple | Fruit |
+----------+------------+
磁帶/測試失敗報告:
operator: deepEqual
expected: |-
[ { Banana: 'Fruit' }, { Potato: 'Vegetables' }, { Sausages: 'Meat' }, { Apple: 'Fruit' } ]
actual: |-
[ { name: 'Fruit' }, { name: 'Vegetables' }, { name: 'Meat' }, { name: 'Fruit' } ]
第一個問題是查詢返回的兩列具有相同的名稱(即name
)。 Postgres很樂意處理這個問題,但這可能會導致客戶端出現歧義,特別是如果它使用列名作為鍵,這似乎就是這種情況。 您需要對結果集中的列進行別名,例如:
SELECT products.name pname, categories.name cname FROM ...
第二個問題是您的預期結果不正確。 您的驅動程序似乎返回一組鍵/值對,其中鍵是列名。 因此,您應該檢查以下輸出:
[
{ pname: 'Banana', cname: 'Fruit' },
{ pname: 'Potato', cname: 'Vegetables' },
{ pname: 'Sausages', cname: 'Meat' },
{ pname: 'Apple', cname: 'Fruit' }
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.