Guest User

Untitled

a guest
Nov 16th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. const app = require('./../server/server.js');
  2. const request = require('supertest');
  3. const { getListingData } = require('./../server/models/models.js');
  4. const { generateDummyArray } = require('./../server/models/dummyData/generateListingsArray');
  5. const faker = require('faker');
  6. const mongoose = require('mongoose');
  7. const { Listing } = require('./../database/mongoDB/index.js');
  8. const MongoClient = require('mongodb').MongoClient;
  9.  
  10. ////////// API TESTS //////////
  11.  
  12. describe('Test the server API fetching', () => {
  13. test('It should respond 200 to root path', () => {
  14. return request(app)
  15. .get('/')
  16. .expect(200)
  17. })
  18.  
  19. test('It should respond 200 to GET listingdata for id 123', () => {
  20. return request(app)
  21. .get('/listingdata?id=123')
  22. .expect(200)
  23. })
  24.  
  25. test('It should respond 200 to GET neighborhooddata for id 5', () => {
  26. return request(app)
  27. .get('/neighborhooddata?id=5')
  28. .expect(200)
  29. })
  30.  
  31. test('It should respond 200 to GET landmarkdata', () => {
  32. return request(app)
  33. .get('/landmarksdata')
  34. .expect(200)
  35. })
  36.  
  37. })
  38.  
  39. ////////// POSTGRES TESTS //////////
  40.  
  41. const testValues = [faker.name.firstName(), Math.floor(Math.random() * 15) + 1, faker.lorem.paragraph(), faker.lorem.paragraph(), Number((Math.random() * 100).toFixed(6)), Number((Math.random() * 100).toFixed(6)), "2018-11-12 15:52:31.126-05", "2018-11-12 15:52:31.126-05"]
  42.  
  43. describe('Test raw Postgres READ / WRITE', () => {
  44.  
  45. beforeAll(() => {
  46. const { Pool } = require('pg')
  47. return pool = new Pool({
  48. user: 'root',
  49. host: 'localhost',
  50. database: 'neighborhood',
  51. password: 'hrnyc18',
  52. port: 5432
  53. })
  54. })
  55.  
  56. test('It should read 1 listing from the database in 0-10% position', (done) => {
  57. var testId = 100
  58. var sql = `
  59. SELECT * FROM listings WHERE id=${testId}
  60. `;
  61.  
  62. pool.query(sql, (err, result) => {
  63. if (err) {
  64. console.log(err);
  65. } else {
  66. expect(result.rows[0].id).toBe(100)
  67. done();
  68. }
  69. })
  70. })
  71.  
  72. test('It should read 1 listing from the database in 90%+ position', (done) => {
  73. var testId = 9000000
  74. var sql = `
  75. SELECT * FROM listings WHERE id=${testId}
  76. `;
  77.  
  78. pool.query(sql, (err, result) => {
  79. if (err) {
  80. console.log(err);
  81. } else {
  82. expect(result.rows[0].id).toBe(9000000)
  83. done();
  84. }
  85. })
  86. })
  87.  
  88. test('It should insert one new listing directly to Postgres', (done) => {
  89. var sql =
  90. `
  91. INSERT INTO listings ("hostFirstName", "neighbId", "neighbDesc", "gettingAroundDesc", "listingLat", "listingLong", "createdAt", "updatedAt")
  92. VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
  93. `;
  94. var begin = Date.now();
  95. pool.query(sql, testValues, (err, result) => {
  96. if (err) {
  97. console.log(err);
  98. } else {
  99. var end = Date.now();
  100. var timeSpent = ((end - begin) / 1000) / 60;
  101. console.log('PSQL insert time:', timeSpent)
  102. expect(result.rowCount).toBe(1)
  103. }
  104. })
  105. done();
  106. })
  107.  
  108. afterAll(() => {
  109. pool.end();
  110. })
  111.  
  112. })
  113.  
  114. //////// MONGODB TESTS //////////
  115.  
  116. const testListing = new Listing({
  117. "id": 11000000,
  118. "hostFirstName": faker.name.firstName(),
  119. "city": 'London',
  120. "region": 'England',
  121. "country": 'United Kingdom',
  122. "neighb": 1,
  123. "listingLat": Number((Math.random() * 100).toFixed(6)),
  124. "listingLong": Number((Math.random() * 100).toFixed(6)),
  125. "neighbDesc": faker.lorem.paragraph(),
  126. "gettingAroundDesc": faker.lorem.paragraph(),
  127. "feature1": faker.lorem.words(),
  128. "feature2": faker.lorem.words(),
  129. "feature3": faker.lorem.words(),
  130. "feature4": faker.lorem.words(),
  131. "feature5": faker.lorem.words(),
  132. "feature6": faker.lorem.words(),
  133. "feature7": faker.lorem.words()
  134. })
  135.  
  136. // At present, setting up mongo connection without Before All.
  137. // TODO: figure out how to access before all scope in subsequent tests.
  138.  
  139. const url = 'mongodb://localhost/neighborhood';
  140. const dbName = 'neighborhood';
  141. const client = new MongoClient(url);
  142. const connection = client.connect()
  143.  
  144. describe('Test raw MongoDB READ / WRITE', () => {
  145.  
  146. test('It should read 1 listing from the database in 0-10% position', (done) => {
  147. const connect = connection;
  148. connect.then(() => {
  149.  
  150. const db = client.db(dbName)
  151. const collection = db.collection('listings')
  152. collection.findOne({id: 100}, (err, result) => {
  153. if (err) {
  154. console.log(err);
  155. } else {
  156. expect(result.id).toBe(100);
  157. done();
  158. }
  159. })
  160.  
  161. })
  162. })
  163.  
  164. test('It should insert one new listing directly to MongoDB', (done) => {
  165. const connect = connection;
  166. connect.then(() => {
  167.  
  168. const db = client.db(dbName);
  169. const collection = db.collection('listings');
  170. collection.insertOne(testListing, (err, result) => {
  171. if(err) {
  172. console.log(err)
  173. } else {
  174. expect(result.result.n).toBe(1);
  175. done();
  176. }
  177. })
  178. })
  179. })
  180.  
  181. })
  182.  
  183. ////////// TEST RESULTS //////////
  184.  
  185. Test the server API fetching
  186. ✓ It should respond 200 to root path (103ms)
  187. ✓ It should respond 200 to GET listingdata for id 123 (44ms)
  188. ✓ It should respond 200 to GET neighborhooddata for id 5 (7ms)
  189. ✓ It should respond 200 to GET landmarkdata (5ms)
  190. Test raw Postgres READ / WRITE
  191. ✓ It should read 1 listing from the database in 0-10% position (12ms)
  192. ✓ It should read 1 listing from the database in 90%+ position (1ms)
  193. ✓ It should insert one new listing directly to Postgres
  194. Test raw MongoDB READ / WRITE
  195. ✓ It should read 1 listing from the database in 0-10% position (8ms)
  196. ✓ It should insert one new listing directly to MongoDB (3ms)
  197.  
  198. ////////// QUERY RESULTS //////////
  199.  
  200. RESULT FROM POSTGRES QUERY: {
  201. id: 9000000,
  202. hostFirstName: 'Summer',
  203. neighbId: 6,
  204. listingLat: 58.33624,
  205. listingLong: 85.158439,
  206. neighbDesc: 'Rerum nihil voluptate accusantium voluptatem veritatis qui voluptate. Provident exercitationem eaque ut ut aut pariatur atque sit qui. Fugit non corporis qui pariatur deserunt officiis et. Incidunt labore incidunt corporis ipsum aperiam quis quos et et. Aliquid suscipit quae qui quia fugiat reiciendis.',
  207. gettingAroundDesc: 'Eum qui est eius repellendus et dicta facere consequatur iste. Velit veritatis molestiae. Odit hic provident. Et quia et corrupti commodi voluptates laborum.',
  208. createdAt: 2018-11-12T21:18:15.213Z,
  209. updatedAt: 2018-11-12T21:18:15.213Z
  210. };
  211.  
  212. RESULT FROM MONGODB QUERY: {
  213. _id: 5beb458934ad5740cc306afa,
  214. id: 100,
  215. hostFirstName: 'Brock',
  216. city: 'London',
  217. region: 'England',
  218. country: 'United Kingdom',
  219. neighb: 'Brixton',
  220. listingLat: 29.65186,
  221. listingLong: 31.461485,
  222. neighbDesc: 'Libero quos magni. Harum dolores consequatur voluptatem corrupti earum facilis. Occaecati expedita quia repudiandae. Dolores omnis debitis.',
  223. gettingAroundDesc: 'Placeat assumenda ut repudiandae aut dolor deserunt et autem aspernatur. Error neque dolores ut ab dolor atque ea. Sed quas error officiis molestiae enim. Enim sit ipsa beatae dolores sint ut adipisci. Eaque commodi qui consequuntur deserunt ut mollitia maiores voluptate.',
  224. feature1: 'soluta illo est',
  225. feature2: 'maxime est qui',
  226. feature3: 'esse error rerum',
  227. feature4: 'inventore adipisci saepe',
  228. feature5: 'corporis vel voluptate',
  229. feature6: 'nisi quas omnis',
  230. feature7: 'nostrum facere praesentium',
  231. __v: 0
  232. };
Add Comment
Please, Sign In to add comment