cbmono / hapijs-seed-mvc

hapi Seed Project: MVC, ES6, Gulp, Tests, Logging, Migrations, RDBMs, Documentation, etc.
MIT License
40 stars 24 forks source link

MySQL: API Test Fail #198

Closed psingh3 closed 7 years ago

psingh3 commented 7 years ago

Hi, Thank you for seed project I am getting Following error in Api test API Test: ToDo Lists POST /todo-lists should create a new ToDo List: failed should return status code 400: passed PUT /todo-lists/{id} TypeError: Cannot read property 'id' of undefined at _callee5$ (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/tests/api/todo-lists.js:69:16) at tryCatch (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/regenerator-runtime/runtime.js:64:40) **Expected Object({ statusCode: 400, error: 'Bad Request', message: 'child "id" fails because ["id" must be a number]', validation: Object({ source: 'params', keys: [ 'id' ] }) }) to be 1. Error: Expected Object({ statusCode: 400, error: 'Bad Request', message: 'child "id" fails because ["id" must be a number]', validation: Object({ source: 'params', keys: [ 'id' ] }) }) to be 1. at stack (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1640:17)** at buildExpectationResult (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1610:14) at Spec.expectationResultFactory (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:655:18) at Spec.addExpectationResult (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:342:34) at Expectation.addExpectationResult (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:599:21) at Expectation.toBe (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1564:12) at _callee19$ (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/tests/api/todos.js:176:22) at tryCatch (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/regenerator-runtime/runtime.js:64:40) at GeneratorFunctionPrototype.invoke [as _invoke] (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/regenerator-runtime/runtime.js:299:22) at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/regenerator-runtime/runtime.js:116:21) at step (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/tests/api/todos.js:21:191) at /Users/pssehmby/rubyProjects/hapijs-seed-mvc/tests/api/todos.js:21:361 at process._tickCallback (internal/process/next_tick.js:103:7)

11) API Test: ToDo's DELETE /todo-lists/{id} should end by deleting the created ToDo List 11.1) Expected 400 to be 200. Error: Expected 400 to be 200. at stack (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1640:17) at buildExpectationResult (/Users/pssehmby/rubyProjects/hapijs-seed-mvc/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1610:14) at Spec.expectationResultFactory (/Users/pssehmby/rubyProjects/hapi

Do you have an idea, what it could be? Thanks very much.

Parminder

cbmono commented 7 years ago

I cloned and installed the repo from scratch, and all tests are passing. Did you check:

  1. When running the (api) tests, needs to be running in a different terminal window
  2. There where you run npm run test:api, you need to do an NODE_ENV export: export NODE_ENV=dev
  3. Do you have access and write-rights to the DB? (did the migration/seed work?)
psingh3 commented 7 years ago

Yes I did same using export NODE_ENV=dev, and Yes migration work and there entries in DB work too

cbmono commented 7 years ago

The creation of a todo-list isn't working. Please try to create one with plain cURL:

curl -X POST "http://localhost:3000/todo-lists" -F "name=foo"
psingh3 commented 7 years ago

Thanks for your quick response, i run this curl and check DB entry and there is foo entry in DB. but then i re run api test and still getting same error PUT /todo-lists/{id} TypeError: Cannot read property 'id' of undefined, Thanks,

cbmono commented 7 years ago

I still think the problem is related to adding new entries to the DB. Based on the error you posted above:

POST /todo-lists
should create a new ToDo List: failed

Could you please test 2 more things?

  1. After running your API/Integration tests (npm run test:api), do you have a new entry with New ToDo List?
  2. Could you please check the value of createdId after running the tests? (console.log)
  3. Check here whether createdId still has the same value
ramangreat commented 7 years ago

HI Claudio,

I am also getting this problem. I am telling you the whole situation. I am only running the test "should create a new ToDo List", and the entry is being saved to the database, as I can see the new entry with value "New ToDo List". I have logged the response constant the output is:

Body {   url: 'http://localhost:3000/todo-lists',   status: 200,   statusText: 'OK',   headers:     Headers {      _headers:        { vary: [Object],         'content-type': [Object],         'cache-control': [Object],         'content-encoding': [Object],         date: [Object],         connection: [Object],         'transfer-encoding': [Object] } },   ok: true,   body:     Gunzip {      _opts: {},      _chunkSize: 16384,      _readableState:        ReadableState {         objectMode: false,         highWaterMark: 16384,         buffer: [Object],         length: 0,         pipes: null,         pipesCount: 0,         flowing: true,         ended: true,         endEmitted: true,         reading: false,         sync: false,         needReadable: false,         emittedReadable: false,         readableListening: false,         resumeScheduled: false,         defaultEncoding: 'utf8',         ranOut: false,         awaitDrain: 0,         readingMore: false,         decoder: null,         encoding: null },      readable: false,      domain: null,      _events: { end: [Object], error: [Object], data: [Function] },      _eventsCount: 3,      _maxListeners: undefined,      _writableState:        WritableState {         objectMode: false,         highWaterMark: 16384,         needDrain: false,         ending: true,         ended: true,         finished: true,         decodeStrings: true,         defaultEncoding: 'utf8',         length: 0,         writing: false,         corked: 0,         sync: false,         bufferProcessing: false,         onwrite: [Function: bound onwrite],         writecb: null,         writelen: 0,         bufferedRequest: null,         lastBufferedRequest: null,         pendingcb: 0,         prefinished: true,         errorEmitted: false,         bufferedRequestCount: 0,         corkedRequestsFree: [Object] },      writable: false,      allowHalfOpen: true,      _transformState:        TransformState {         afterTransform: [Function],         needTransform: false,         transforming: false,         writecb: null,         writechunk: null,         writeencoding: 'buffer' },      _flushFlag: 0,      _finishFlushFlag: 4,      _handle: null,      _hadError: false,      _buffer: <Buffer 5b 34 34 5d 00 00 00 00 00 00 00 00 00 00 00 00 48 40 00 00 00 00 00 00 b8 cf 84 02 01 00 00 00 50 fc 00 03 01 00 00 00 03 00 00 00 d2 03 f0 b1 d0 7b ... >,      _offset: 4,      _level: -1,      _strategy: 0,      _closed: [Getter] },   bodyUsed: true,   size: 0,   timeout: 0,   _raw: [ <Buffer 5b 34 34 5d> ],   _abort: false,   _bytes: 4 } Ramanundefined ( I have added console.log("Raman" + createdId); )       should create a new ToDo List: failed       should return status code 400: passed 
 Failures:  1) API Test: ToDo Lists POST /todo-lists should create a new ToDo List 1.1) Expected undefined to be greater than 0.

Then I doubted that the await async might be a problem, so I updated nodejs to 7.7.1(Current version) But again no success.

Then I added the following line just below (const body = await response.json();) console.log("Raman "+ body);

I am getting : POST /todo-lists Raman 47 should create a new ToDo List: failed should return status code 400: passed

Failures: 1) API Test: ToDo Lists POST /todo-lists should create a new ToDo List 1.1) Expected undefined to be greater than 0.

47 is the id of record which has been saved in the database.

I think this is the reason why body[0].id or body[0].name are being undefined. But still struggling to get it done. May be you able to understand it more.

Please advise. Regards, Raman

cbmono commented 7 years ago

Could it be that you are using MySQL instead of Postgres?

ramangreat commented 7 years ago

Yeah that can be the issue too. You are right it could be the different response from mysql.

ramangreat commented 7 years ago

I am getting the response when I console.log(response) just before the return statement in file BaseModel.RDMS.js

Builder { client: Client_MySQL { config: { connection: [Object], client: 'mysql' }, connectionSettings: { database: 'this_is_database', host: 'localhost', port: 3306, user: 'root', password: 'root' }, driver: { createConnection: [Function: createConnection], createPool: [Function: createPool], createPoolCluster: [Function: createPoolCluster], createQuery: [Function: createQuery], escape: [Function: escape], escapeId: [Function: escapeId], format: [Function: format] }, __cid: 'client0', pool: Pool { _factory: [Object], _inUseObjects: [Object], _draining: false, _waitingClients: [Object], _availableObjects: [Object], _asyncTestObjects: [], _count: 2, _removeIdleTimer: [Object], _removeIdleScheduled: true }, valueForUndefined: Raw { client: [Circular], sql: 'DEFAULT', bindings: undefined, _wrappedBefore: undefined, _wrappedAfter: undefined, _debug: undefined }, _events: { start: [Function], query: [Function], 'query-error': [Function], 'query-response': [Function] }, _eventsCount: 4, makeKnex: [Function: makeKnex] }, and: [Circular], _single: { table: 'todo_lists', only: false, insert: { name: 'New ToDo List', updated_at: [Object], created_at: [Object] } }, _statements: [], _method: 'insert', _debug: undefined, _joinFlag: 'inner', _boolFlag: 'and', _notFlag: false }

psingh3 commented 7 years ago

@cbmono I tested this with Postgres and it work fine without any error, So now i will try to check why that issue is with mysql, Thanks

cbmono commented 7 years ago

Hey @psingh3,

I will create a branch with a running MySQL version. If you manage to get it up & running from your side, please send a Pull Request.