jbrumwell / mock-knex

A mock knex adapter for simulating a database during testing
MIT License
240 stars 71 forks source link

Problem with join data in select #110

Closed opravil-jan closed 4 years ago

opravil-jan commented 4 years ago

Hello,

I have problem with mocking join query. Query and mock you can see below. When I run the test I get strange error. Can you point me the right direction what I'm doing wrong.

Thank you.

knex: 0.20.4 mock-knex: 0.4.7

Error:

select `uc_users`.*, `uc_user_types`.`price` as `type_price`, `uc_user_types`.`id` as `type_id`, `uc_user_types`.`name` as `type_name` from `uc_users` inner join `uc_user_types` on `uc_user_types`.`id` = `uc_users`.`type` and `uc_users`.`customer_id` = 11504 and `uc_users`.`provider_id` = 11011 and `uc_users`.`level` in ('B2C') and `uc_user_types`.`id` in (45394) where `uc_users`.`email` like '  TypeError: Cannot read property 'price' of undefined
      at Tracker.sendResult (test/unit/api/v1/repositories/users-repository-js/getUsersBy.js:71:50)
      at Queries.track (node_modules/mock-knex/dist/queries.js:68:22)
      at /home/opravil/Projects/nodejs/ipex/x-tmp/central-api-nodejs-services/node_modules/mock-knex/dist/platforms/knex/0.8/index.js:52:38
      at Promise._execute (node_modules/bluebird/js/release/debuggability.js:384:9)
      at Promise._resolveFromExecutor (node_modules/bluebird/js/release/promise.js:518:18)
      at new Promise (node_modules/bluebird/js/release/promise.js:103:10)
      at Client_MySQL._query (node_modules/mock-knex/dist/platforms/knex/0.8/index.js:51:10)
      at Client_MySQL.query (node_modules/knex/lib/client.js:168:17)
      at Runner.query (node_modules/knex/lib/runner.js:133:36)
      at /home/opravil/Projects/nodejs/ipex/x-tmp/central-api-nodejs-services/node_modules/knex/lib/runner.js:39:23
      at client.acquireConnection.catch.then (node_modules/knex/lib/runner.js:253:24)
      at tryCatcher (node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromise0 (node_modules/bluebird/js/release/promise.js:649:10)
      at Promise._settlePromises (node_modules/bluebird/js/release/promise.js:729:18)
      at _drainQueueStep (node_modules/bluebird/js/release/async.js:93:12)
      at _drainQueue (node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (node_modules/bluebird/js/release/async.js:102:5)
      at Immediate.Async.drainQueues [as _onImmediate] (node_modules/bluebird/js/release/async.js:15:14)ys.voipex.io' - Cannot read property 'price' of undefined

Query:

knexCmdb
    .select(
      'uc_users.*',
      'uc_user_types.price AS type_price',
      'uc_user_types.id AS type_id',
      'uc_user_types.name AS type_name'
    )
    .from('uc_users')
    .innerJoin('uc_user_types', function() {
      this.on('uc_user_types.id', 'uc_users.type')

      if (customerId) {
        this.on('uc_users.customer_id', knexCmdb.raw('?', [customerId]))
      }
      if (providerId) {
        this.on('uc_users.provider_id', knexCmdb.raw('?', [providerId]))
      }
      if (levels && Array.isArray(levels) && levels.length) {
        this.onIn('uc_users.level', levels)
      }

      if (types && Array.isArray(types) && types.length) {
        this.onIn('uc_user_types.id', types)
      }
    })
    .where('uc_users.email', 'LIKE', '%sys.voipex.io')

My mock looks like this:

      tracker.on('query', function sendResult(query, step) {
        query.response([
          {
            id: faker.random.number(),
            anvil_id: expected.id,
            email: expected.username,
            first_name: expected.firstName,
            last_name: expected.lastName,
            type_price: expected.type.price,
            type_id: expected.type.id,
            type_name: expected.type.name,
            operator_id: expected.operator.id,
            provider_id: expected.provider.id,
            customer_id: expected.customer.id,
            level: expected.level,
            pbx_roles: expected.roles.pbx.join(','),
            isp_login: faker.internet.email(),
            pbx_id: faker.random.number(),
            comm_roles: expected.roles.communicator.join(','),
            created_at: expected.createdAt,
            updated_at: expected.updatedAt
          }
        ])
      })
jbrumwell commented 4 years ago

@opravil-jan sorry for the delay I missed this issue, from the error it looks like expected.type.price is causing the error