Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Can't test against data that already exists using trait('DatabaseTransaction') #15

Closed
SeedyROM opened this issue Mar 16, 2018 · 7 comments
Assignees

Comments

@SeedyROM
Copy link

I'm like 3 hours new to Adonis and I'm having trouble testing a simple list view that should return JSON.
Since the data already exists in my database, I get:

error: duplicate key value violates unique constraint "cities_pkey"
    at Connection.parseE (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/pg/lib/connection.js:545:11)
    at Connection.parseMessage (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/pg/lib/connection.js:370:19)
    at Socket.<anonymous> (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/pg/lib/connection.js:113:22)
    at Socket.emit (events.js:159:13)
    at addChunk (_stream_readable.js:265:12)
    at readableAddChunk (_stream_readable.js:252:11)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at TCP.onread (net.js:608:20)

Is there anyway to run tests in a different database? Something like in django or rails when you have a adonis_test database?

Here is my test code just incase I'm missing something important!

'use strict'

const { test, trait } = use('Test/Suite')('City')
const City = use('App/Models/City')

trait('DatabaseTransactions')
trait('Test/ApiClient')

test('get list of cities', async ({ client }) => {
  await City.create({
    code: 'NYC',
    name: 'New York City'
  })

  const response = await client.get('/api/v1/cities').end()

  response.assertStatus(200)
  response.assertJSONSubset([{
    code: 'NYC',
    name: 'New York City'
  }])
})

Do I need to use mocks for these? I think that's what I'm missing but there's not much information on it that I can find from cursory google searches.

@SeedyROM
Copy link
Author

Here is my attempt with Factory mocks, now I get a connection error from what I can only perceive to be the client handler.

'use strict'

const { test, trait } = use('Test/Suite')('City')
const City = use('App/Models/City')

const Factory = use('Factory')

trait('DatabaseTransactions')
trait('Test/ApiClient')

test('get list of cities', async ({ client }) => {
  const city = await Factory.model('App/Models/City').create()

  const response = await client.get('/api/v1/cities').end()

  response.assertStatus(200)
  response.assertJSONSubset([{
    code: city.code,
    name: cide.name
  }])
})

Here is what the test runner's output:


  City
    ✖ get list of cities (23ms)


   ERRORS 

  1. get list of cities
  Error: connect ECONNREFUSED 127.0.0.1:3000
    at Object._errnoException (util.js:999:13)
    at _exceptionWithHostPort (util.js:1020:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1207:14)

   FAILED 

  total       : 1
  failed      : 1
  time        : 126ms

Realizing now, this may have something to do with the fact that I'm using Nuxt.js and starting from an igniter and not the standard CLI command. Not sure how this could mess stuff up exactly?
However I don't think that would effect the standard test runner considering everything else has been fine all night.

Sorry if this is n00b question, I'm very interested in this framework and will be willing to submit a pull request if I can find the issue myself.

@RomainLanz
Copy link
Member

Hey @SeedyROM! 👋

Simply define an env.testing file like the documentation said http://adonisjs.com/docs/4.1/testing#_env_testing.

@SeedyROM
Copy link
Author

I'm a complete idiot, I skimmed over that document so hard! Thanks for the clarification! Also thanks for the late (PST) reply. I will try that and close this issue. Hope other half-awake noobies find this question.

👌🏻🔥

@SeedyROM
Copy link
Author

SeedyROM commented Mar 16, 2018

Thank you for marking this as a question and not an issue, that was my mistake.

So I've done what you've said (I also made a new DB for testing only, maybe this isn't correct?), but how do I run migrations for the test DB?

I'm getting an error saying the tables simply don't exist, which makes sense since the migrations haven't been run. I'm obviously missing something important. The docs shed no light on this from what I can see while perusing around.


  City
    ✖ get list of cities (21ms)


   ERRORS 

  1. get list of cities
  error: relation "cities" does not exist
    at Connection.parseE (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/pg/lib/connection.js:545:11)
    at Connection.parseMessage (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/pg/lib/connection.js:370:19)
    at Socket.<anonymous> (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/pg/lib/connection.js:113:22)
    at Socket.emit (events.js:159:13)
    at addChunk (_stream_readable.js:265:12)
    at readableAddChunk (_stream_readable.js:252:11)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at TCP.onread (net.js:608:20)
From previous event:
    at Client_PG._query (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/knex/lib/dialects/postgres/index.js:287:12)
    at Client_PG.query (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/knex/lib/client.js:199:17)
    at /home/rombus/Workspace/codetemp/js/tdr2/node_modules/knex/lib/transaction.js:274:21
From previous event:
    at Client_PG.trxClient.query (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/knex/lib/transaction.js:271:34)
    at Runner.<anonymous> (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/knex/lib/runner.js:149:36)
From previous event:
    at /home/rombus/Workspace/codetemp/js/tdr2/node_modules/knex/lib/runner.js:61:21
    at runCallback (timers.js:773:18)
    at tryOnImmediate (timers.js:734:5)
    at processImmediate [as _immediateCallback] (timers.js:711:5)
From previous event:
    at Runner.run (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/knex/lib/runner.js:47:31)
    at Builder.Target.then (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/knex/lib/interface.js:35:43)
    at Proxy.<anonymous> (/home/rombus/Workspace/codetemp/js/tdr2/node_modules/@adonisjs/lucid/lib/proxyGet.js:58:21)

   FAILED 

  total       : 1
  failed      : 1
  time        : 126ms

Thanks again for the timely responses.

@SeedyROM
Copy link
Author

SeedyROM commented Mar 16, 2018

https://github.com/adonisjs/adonis-blog-demo/blob/master/vowfile.js#L36

This is the correct setup for my testing purposes, I missed this link as well.

Please close this issue and have a good night!

@RomainLanz
Copy link
Member

Yeah, those lines are commented by default in your vowfile.js

@SeedyROM
Copy link
Author

I generated this app from a nuxt-create-app, which is clearly the main problem here! I will add an issue/question on Nuxt's repo to clarify this. I've obviously done way more modifications to the default template than testing, but this one bit me harder than any other.

Thanks so much for the clarifications!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants