Advertisement
Guest User

Untitled

a guest
Mar 31st, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You'll need to have MySQL running and your Node server running
  2.  * for these tests to pass. */
  3.  
  4. var mysql = require('mysql');
  5. var request = require('request'); // You might need to npm install the request module!
  6. var expect = require('chai').expect;
  7.  
  8. describe('Persistent Node Chat Server', function() {
  9.   var dbConnection;
  10.  
  11.   beforeEach(function(done) {
  12.     dbConnection = mysql.createConnection({
  13.       user: 'root', // had to change this
  14.       password: 'rawrzers', // had to change this
  15.       database: 'chat'
  16.     });
  17.     dbConnection.connect();
  18.  
  19.     var tablename = 'messages'; // TODO: fill this out
  20.  
  21.     /* Empty the db table before each test so that multiple tests
  22.      * (or repeated runs of the tests) won't screw each other up: */
  23.     dbConnection.query('truncate ' + tablename, done);
  24.   });
  25.  
  26.   afterEach(function() {
  27.     dbConnection.end();
  28.   });
  29.  
  30.   it('Should insert posted messages to the DB', function(done) {
  31.     // Post the user to the chat server.
  32.     request({
  33.       method: 'POST',
  34.       uri: 'http://127.0.0.1:3000/classes/users',
  35.       json: { username: 'Valjean' }
  36.     }, function () {
  37.       // Post a message to the node chat server:
  38.       request({
  39.         method: 'POST',
  40.         uri: 'http://127.0.0.1:3000/classes/messages',
  41.         json: {
  42.           username: 'Valjean',
  43.           message: 'In mercy\'s name, three days is all I need.',
  44.           roomname: 'Hello'
  45.         }
  46.       }, function () {
  47.         // Now if we look in the database, we should find the
  48.         // posted message there.
  49.  
  50.         // TODO: You might have to change this test to get all the data from
  51.         // your message table, since this is schema-dependent.
  52.         var queryString = 'SELECT messages.id, messages.text, messages.created_at, messages.updated_at, rooms.roomname, users.username FROM messages, users, rooms WHERE users.username=(SELECT username FROM users WHERE id=messages.user_id) AND rooms.roomname=(SELECT roomname FROM rooms WHERE id=messages.room_id)';
  53.         var queryArgs = [];
  54.  
  55.         dbConnection.query(queryString, queryArgs, function(err, results) {
  56.           // Should have one result:
  57.           expect(results.length).to.equal(1);
  58.  
  59.           // TODO: If you don't have a column named text, change this test.
  60.           expect(results[0].text).to.equal('In mercy\'s name, three days is all I need.');
  61.  
  62.           done();
  63.         });
  64.       });
  65.     });
  66.   });
  67.  
  68.   it('Should output all messages from the DB', function(done) {
  69.     // Let's insert a message into the db
  70.  
  71.     request({
  72.       method: 'POST',
  73.       uri: 'http://127.0.0.1:3000/classes/users',
  74.       json: { username: 'Valjean' }
  75.     }, function () {
  76.       // Post a message to the node chat server:
  77.       request({
  78.         method: 'POST',
  79.         uri: 'http://127.0.0.1:3000/classes/messages',
  80.         json: {
  81.           username: 'Valjean',
  82.           message: 'Men like you can never change!',
  83.           roomname: 'main'
  84.         }
  85.       }, function () {
  86.    
  87.         var queryString = 'SELECT messages.id, messages.text, messages.created_at, messages.updated_at, rooms.roomname, users.username FROM messages, users, rooms WHERE users.username=(SELECT username FROM users WHERE id=messages.user_id) AND rooms.roomname=(SELECT roomname FROM rooms WHERE id=messages.room_id)';
  88.         var queryArgs = [];
  89.         // TODO - The exact query string and query args to use
  90.         // here depend on the schema you design, so I'll leave
  91.         // them up to you. */
  92.  
  93.         // it looks like queryArgs is an array of values that get replaced at each '?' in the
  94.         // queryString respectively by the order which it appears
  95.         dbConnection.query(queryString, queryArgs, function(err) {
  96.           if (err) { throw err; }
  97.  
  98.           // Now query the Node chat server and see if it returns
  99.           // the message we just inserted:
  100.           request('http://127.0.0.1:3000/classes/messages', function(error, response, body) {
  101.             var messageLog = JSON.parse(body);
  102.  
  103.             console.log('---------------------------');
  104.             console.log('body: ', body);
  105.             console.log('messageLog: ', messageLog);
  106.             console.log('---------------------------');
  107.  
  108.             expect(messageLog[0].text).to.equal('Men like you can never change!');
  109.             expect(messageLog[0].roomname).to.equal('main');
  110.             done();
  111.  
  112.           });
  113.  
  114.         });
  115.       });
  116.     });
  117.   })
  118. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement