简体   繁体   English

使用postgreSQL和磁带(npm模块)无法测试INNER JOIN查询

[英]Failing TEST of INNER JOIN query using postgreSQL and Tape(npm module)

I have a SQL query which seems to be working fine when I use it directly in my PostgreSQL database, however I am struggling to make my Tape test for it pass as the expected output is not what I expect. 我有一个SQL查询,当我直接在我的PostgreSQL数据库中使用它时似乎工作正常,但是我正在努力使我的磁带测试通过,因为预期的输出不是我所期望的。

I have pasted my actual query in my pgcli/database interface and it works fine. 我已经在我的pgcli /数据库界面粘贴了我的实际查询,它工作正常。

  1. In my findQueries.js file I have the following function: 在我的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 }
  1. My test for the function: 我对该功能的测试:
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();
        }
    })
});
  1. When I runs the query in my database I get my desired output: 当我在我的数据库中运行查询时,我得到了我想要的输出:
SELECT products.name, categories.name 
FROM products 
INNER JOIN categories ON products.category_id = categories.id;

OUTPUT: OUTPUT:

+----------+------------+
| name     | name       |
|----------+------------|
| Banana   | Fruit      |
| Potato   | Vegetables |
| Sausages | Meat       |
| Apple    | Fruit      |
+----------+------------+

Tape/test failing report: 磁带/测试失败报告:

operator: deepEqual
      expected: |-
        [ { Banana: 'Fruit' }, { Potato: 'Vegetables' }, { Sausages: 'Meat' }, { Apple: 'Fruit' } ]
      actual: |-
        [ { name: 'Fruit' }, { name: 'Vegetables' }, { name: 'Meat' }, { name: 'Fruit' } ]

The first problem is that both columns returned by your query have the same name (ie name ). 第一个问题是查询返回的两列具有相同的名称(即name )。 Postgres happily deals with that, however that may cause ambiguity on the client end, especially if it uses the column names as keys, which seems to be the case here. Postgres很乐意处理这个问题,但这可能会导致客户端出现歧义,特别是如果它使用列名作为键,这似乎就是这种情况。 You would need to alias the columns in the result set, like: 您需要对结果集中的列进行别名,例如:

SELECT products.name pname, categories.name cname FROM ...

The second problem is that your expected result is not correct. 第二个问题是您的预期结果不正确。 Your driver seems to return an array of key/value pairs, where the keys are the column names. 您的驱动程序似乎返回一组键/值对,其中键是列名。 Hence you should check for the following output: 因此,您应该检查以下输出:

[ 
    { pname: 'Banana', cname: 'Fruit' }, 
    { pname: 'Potato', cname: 'Vegetables' }, 
    { pname: 'Sausages', cname: 'Meat' }, 
    { pname: 'Apple', cname: 'Fruit' } 
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM