diff --git a/package-lock.json b/package-lock.json index 8d33ccf..02f37f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -317,9 +317,9 @@ "dev": true }, "@types/node": { - "version": "13.13.5", - "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.5.tgz", - "integrity": "sha512-3ySmiBYJPqgjiHA7oEaIo2Rzz0HrOZ7yrNO5HWyaE5q0lQ3BppDZ3N53Miz8bw2I7gh1/zir2MGVZBvpb1zq9g==" + "version": "14.0.5", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.5.tgz", + "integrity": "sha512-90hiq6/VqtQgX8Sp0EzeIsv3r+ellbGj4URKj5j30tLlZvRUpnAe9YbYnjl3pJM93GyXU0tghHhvXHq+5rnCKA==" }, "abbrev": { "version": "1.1.1", @@ -439,6 +439,11 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, + "bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" + }, "binary-extensions": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.0.0.tgz", @@ -1706,14 +1711,14 @@ } }, "moment": { - "version": "2.25.3", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.25.3.tgz", - "integrity": "sha512-PuYv0PHxZvzc15Sp8ybUCoQ+xpyPWvjOuK72a5ovzp2LI32rJXOiIfyoFoYvG3s6EwwrdkMyWuRiEHSZRLJNdg==" + "version": "2.26.0", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.26.0.tgz", + "integrity": "sha512-oIixUO+OamkUkwjhAVE18rAMfRJNsNe/Stid/gwHSOfHrOtw9EhAY2AHvdKZ/k/MggcYELFCJz/Sn2pL8b8JMw==" }, "moment-timezone": { - "version": "0.5.28", - "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.28.tgz", - "integrity": "sha512-TDJkZvAyKIVWg5EtVqRzU97w0Rb0YVbfpqyjgu6GwXCAohVRqwZjf4fOzDE6p1Ch98Sro/8hQQi65WDXW5STPw==", + "version": "0.5.31", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.31.tgz", + "integrity": "sha512-+GgHNg8xRhMXfEbv81iDtrVeTcWt0kWmTEY1XQK14dICTXnWJnT0dxdlPspwqF3keKMVPXwayEsk1DI0AA/jdA==", "requires": { "moment": ">= 2.9.0" } @@ -1723,6 +1728,24 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "mysql": { + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz", + "integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==", + "requires": { + "bignumber.js": "9.0.0", + "readable-stream": "2.3.7", + "safe-buffer": "5.1.2", + "sqlstring": "2.3.1" + }, + "dependencies": { + "sqlstring": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz", + "integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A=" + } + } + }, "mysql2": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-2.1.0.tgz", @@ -2511,9 +2534,9 @@ "integrity": "sha1-1WgS4cAXpuTnw+Ojeh2m143TyT4=" }, "sequelize": { - "version": "5.21.7", - "resolved": "https://registry.npmjs.org/sequelize/-/sequelize-5.21.7.tgz", - "integrity": "sha512-+JrS5Co7CN53cOFFFaUb+xqQP00wD1Ag9xGLBLoUko2KhRZxjm+UDkqAVPHTUp87McLwJaCPkKv61GPhBVloRQ==", + "version": "5.21.11", + "resolved": "https://registry.npmjs.org/sequelize/-/sequelize-5.21.11.tgz", + "integrity": "sha512-ZJw3Hp+NS7iHcTz4fHlKvIBm4I7xYibYRCP4HhSyMB26xgqFYFOXTaeWbHD2UUwAFaksTLw5ntzfpA9kE33SVA==", "requires": { "bluebird": "^3.5.0", "cls-bluebird": "^2.1.0", diff --git a/package.json b/package.json index 9ad48f4..3a7fc88 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,9 @@ "license": "ISC", "dependencies": { "express": "^4.17.1", + "mysql": "^2.18.1", "mysql2": "^2.1.0", - "sequelize": "^5.21.7", + "sequelize": "^5.21.11", "sqlite3": "^4.2.0" }, "devDependencies": { @@ -21,5 +22,16 @@ "chai-as-promised": "^7.1.1", "mocha": "^7.2.0", "nyc": "^15.0.1" - } + }, + "directories": { + "test": "test" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/shivam7374/Social_Media_Sample_Project_2020_May.git" + }, + "bugs": { + "url": "https://github.com/shivam7374/Social_Media_Sample_Project_2020_May/issues" + }, + "homepage": "https://github.com/shivam7374/Social_Media_Sample_Project_2020_May#readme" } diff --git a/src/controllers/comments.js b/src/controllers/comments.js index e69de29..4c917ac 100644 --- a/src/controllers/comments.js +++ b/src/controllers/comments.js @@ -0,0 +1,30 @@ + +const { Posts, Comments } = require('../db/models') + +async function createNewComment(postId, title, body) { + const comment = await Comments.create({ + title, + body, + postId, + }) + + return comment +} + +async function findAllCommentsbyPosts(id) { + // TODO: Handle query params + const posts = await Posts.findAll( { + where:{ + id + }, include: [ Comments ] + } + ) + + return posts + } + + module.exports = { + createNewComment, + findAllCommentsbyPosts + } + \ No newline at end of file diff --git a/src/db/testing.db b/src/db/testing.db new file mode 100644 index 0000000..14f2e3c Binary files /dev/null and b/src/db/testing.db differ diff --git a/src/public/app/all-posts.js b/src/public/app/all-posts.js index 04aa02d..665e383 100644 --- a/src/public/app/all-posts.js +++ b/src/public/app/all-posts.js @@ -12,7 +12,7 @@ function loadPosts() { ${p.body.substr(0, 200)} ...read more

- Comment + Comment Like diff --git a/src/public/app/comment.js b/src/public/app/comment.js new file mode 100644 index 0000000..0bd0f7c --- /dev/null +++ b/src/public/app/comment.js @@ -0,0 +1,52 @@ +function loadPostsComments() { + let start, end + start = (document.URL.search("comments/") + 9) + end = document.URL.search("/endpoint") + let id = parseInt(document.URL.slice(start, end)) + $.get('http://localhost:8483/api/posts/comments/' + id, (post) => { + console.log("++++++++++++++++++") + console.log(post) + console.log("++++++++++++++++++")// working till here + { + $('#posts-container').append( + $(` +
+
+
+
${post[0].title}
+
${post[0].title}
+

+ ${post[0].body} + ...read more +

+ Like +
+
+
+

COMMENTS

+ + `) + ) + } + for (let p of post[0].comments) { + $('#posts-container').append( + $(` + +
+ +
+
+ ${p.title} +
+
+

${p.body}

+ +
+
+ + + `) + ) + } + }) + } \ No newline at end of file diff --git a/src/public/app/my-posts.js b/src/public/app/my-posts.js index 9708598..309c445 100644 --- a/src/public/app/my-posts.js +++ b/src/public/app/my-posts.js @@ -14,7 +14,7 @@ function loadMyPosts() { ${p.body.substr(0, 200)} ...read more

- Comment + Comment Like diff --git a/src/public/app/write-post.js b/src/public/app/write-post.js index e92aa9c..ce49ecc 100644 --- a/src/public/app/write-post.js +++ b/src/public/app/write-post.js @@ -1,11 +1,12 @@ -$('#write-btn').click(() => { +$("#write-btn").click(() => { const userId = JSON.parse(window.localStorage.user).id - const title = $('#p-title').val() - const body = $('#p-body').val() + const title = $("#p-title").val() + const body = $("#p-body").val() - $.post('/api/posts', { userId, title, body }, (data) => { - $('#content').load('/components/my-posts.html') - $('.nav-item .active').removeClass('active') - $("[data-components='my-posts']").addClass('active') + $.post("/api/posts", { userId, title, body }, async (data) => { + // console.log(data) + await $("#content").load("/components/my-posts.html") + await $(".nav-item .active").removeClass("active") + await $("[data-components='my-posts']").addClass("active") }) }) diff --git a/src/public/components/all-posts.html b/src/public/components/all-posts.html index 242a75f..0d9209d 100644 --- a/src/public/components/all-posts.html +++ b/src/public/components/all-posts.html @@ -5,7 +5,7 @@

Recent Posts

- + \ No newline at end of file diff --git a/src/public/components/comment.html b/src/public/components/comment.html new file mode 100644 index 0000000..86cb5bd --- /dev/null +++ b/src/public/components/comment.html @@ -0,0 +1,73 @@ + + + + + + Document + + + + + + + + +
+

Post With Comments

+ +
+
+
+ + + + + + + + + + +
+
+
+

Write Comments

+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ +
+
+ + + + \ No newline at end of file diff --git a/src/public/components/my-posts.html b/src/public/components/my-posts.html index 127bc0b..5657e12 100644 --- a/src/public/components/my-posts.html +++ b/src/public/components/my-posts.html @@ -5,7 +5,7 @@

Recent Posts

- + \ No newline at end of file diff --git a/src/routes/posts/comments.js b/src/routes/posts/comments.js index 632f4c2..1c487f5 100644 --- a/src/routes/posts/comments.js +++ b/src/routes/posts/comments.js @@ -1,7 +1,37 @@ const { Router } = require('express') +const { + createNewComment,findAllCommentsbyPosts +} = require('../../controllers/comments') +const path=require('path') -const commentsRoute = Router() + +const route = Router() + +route.get('/:id', async (req, res) => { + const posts = await findAllCommentsbyPosts(req.params.id) + res.status(200).send(posts) +}) + +route.post('/', async (req, res) => { + // console.log("************") + // console.log(req.body) + // console.log("************") + const { postId, title, body } = req.body + + if ((!postId) || (!title) || (!body)) { + return res.status(400).send({ + error: 'Need postId, title and body to create post' + }) + } + + const post = await createNewComment(postId, title, body) + res.status(201).send(post) +}) +route.get('/:id/endpoint', async (req,res)=>{ + res.sendFile(path.join(__dirname,'../../public/components','comment.html')) + // /media/singla/New Volume/Github Projects/Mock-Message-Site/Social-Media-Project/src +}) module.exports = { - commentsRoute + commentsRoute:route } \ No newline at end of file diff --git a/src/server.js b/src/server.js index 5d06647..b91617e 100644 --- a/src/server.js +++ b/src/server.js @@ -3,6 +3,8 @@ const express = require('express') const { db } = require('./db/models') const { usersRoute } = require('./routes/users') const { postsRoute } = require('./routes/posts') +const { commentsRoute } = require('./routes/posts/comments.js') + const app = express() app.use(express.json()) @@ -10,12 +12,13 @@ app.use(express.urlencoded({extended: true})) app.use('/api/users', usersRoute) app.use('/api/posts', postsRoute) +app.use('/api/posts/comments', commentsRoute) app.use('/', express.static(__dirname + '/public')) db.sync() .then(() => { - app.listen(8383, () => { - console.log('server started on http://localhost:8383') + app.listen(8483, () => { + console.log('server started on http://localhost:8483') }) }) .catch((err) => { diff --git a/terminal-test-run.txt b/terminal-test-run.txt new file mode 100644 index 0000000..8c069e3 --- /dev/null +++ b/terminal-test-run.txt @@ -0,0 +1,188 @@ +singla@singla-Inspiron-3542:/media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May$ npm run test + +> social-media-project@1.0.0 test /media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May +> mocha test/setup.js test/**/*.test.js + + + +Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(30) NOT NULL UNIQUE, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL); +Executing (default): PRAGMA INDEX_LIST(`users`) +Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_users_1`) +Executing (default): CREATE TABLE IF NOT EXISTS `posts` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE); +Executing (default): PRAGMA INDEX_LIST(`posts`) +(sequelize) Warning: SQLite does not support TEXT with options. Plain `TEXT` will be used instead. +>> Check: https://www.sqlite.org/datatype3.html +Executing (default): CREATE TABLE IF NOT EXISTS `comments` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, `postId` INTEGER REFERENCES `posts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE); +Executing (default): PRAGMA INDEX_LIST(`comments`) + controllers/comments +Executing (default): INSERT INTO `comments` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`postId`) VALUES (NULL,$1,$2,$3,$4,$5); + ✓ should create comment with postId, title and body assigned (221ms) +Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `comments`.`id` AS `comments.id`, `comments`.`title` AS `comments.title`, `comments`.`body` AS `comments.body`, `comments`.`createdAt` AS `comments.createdAt`, `comments`.`updatedAt` AS `comments.updatedAt`, `comments`.`userId` AS `comments.userId`, `comments`.`postId` AS `comments.postId` FROM `posts` AS `post` LEFT OUTER JOIN `comments` AS `comments` ON `post`.`id` = `comments`.`postId` WHERE `post`.`id` = 1; + ✓ should find all commments by postId + + controllers/posts +Executing (default): INSERT INTO `posts` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,$1,$2,$3,$4,$5); + ✓ should create post with userId, title and body assigned (197ms) +Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`; + ✓ should find all posts +Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`; + ✓ should find all posts of a user by its userid + + controllers/users +Executing (default): INSERT INTO `users` (`id`,`username`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3); + 1) should create anonymous user + 2) should find user by userid + ✓ should through error for non number userid + 3) should find user by username + + + 6 passing (572ms) + 3 failing + + 1) controllers/users + should create anonymous user: + SequelizeUniqueConstraintError: Validation error + at Query.formatError (node_modules/sequelize/lib/dialects/sqlite/query.js:416:16) + at Query._handleQueryResponse (node_modules/sequelize/lib/dialects/sqlite/query.js:73:18) + at Statement.afterExecute (node_modules/sequelize/lib/dialects/sqlite/query.js:250:31) + at Statement.replacement (node_modules/sqlite3/lib/trace.js:19:31) + + 2) controllers/users + should find user by userid: + TypeError: Cannot read property 'id' of null + at Context.it (test/controllers/users.test.js:21:51) + + 3) controllers/users + should find user by username: + TypeError: Cannot read property 'username' of null + at Context.it (test/controllers/users.test.js:37:57) + + + +npm ERR! code ELIFECYCLE +npm ERR! errno 3 +npm ERR! social-media-project@1.0.0 test: `mocha test/setup.js test/**/*.test.js` +npm ERR! Exit status 3 +npm ERR! +npm ERR! Failed at the social-media-project@1.0.0 test script. +npm ERR! This is probably not a problem with npm. There is likely additional logging output above. + +npm ERR! A complete log of this run can be found in: +npm ERR! /home/singla/.npm/_logs/2020-05-30T06_48_24_509Z-debug.log + + +singla@singla-Inspiron-3542:/media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May$ npm run test + +> social-media-project@1.0.0 test /media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May +> mocha test/setup.js test/**/*.test.js + + + +Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(30) NOT NULL UNIQUE, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL); +Executing (default): PRAGMA INDEX_LIST(`users`) +Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_users_1`) +Executing (default): CREATE TABLE IF NOT EXISTS `posts` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE); +Executing (default): PRAGMA INDEX_LIST(`posts`) +(sequelize) Warning: SQLite does not support TEXT with options. Plain `TEXT` will be used instead. +>> Check: https://www.sqlite.org/datatype3.html +Executing (default): CREATE TABLE IF NOT EXISTS `comments` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, `postId` INTEGER REFERENCES `posts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE); +Executing (default): PRAGMA INDEX_LIST(`comments`) + controllers/comments +Executing (default): INSERT INTO `comments` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`postId`) VALUES (NULL,$1,$2,$3,$4,$5); + ✓ should create comment with postId, title and body assigned (259ms) +Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `comments`.`id` AS `comments.id`, `comments`.`title` AS `comments.title`, `comments`.`body` AS `comments.body`, `comments`.`createdAt` AS `comments.createdAt`, `comments`.`updatedAt` AS `comments.updatedAt`, `comments`.`userId` AS `comments.userId`, `comments`.`postId` AS `comments.postId` FROM `posts` AS `post` LEFT OUTER JOIN `comments` AS `comments` ON `post`.`id` = `comments`.`postId` WHERE `post`.`id` = 1; + ✓ should find all commments by postId + + controllers/posts +Executing (default): INSERT INTO `posts` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,$1,$2,$3,$4,$5); + ✓ should create post with userId, title and body assigned (185ms) +Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`; + ✓ should find all posts +Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`; + ✓ should find all posts of a user by its userid + + controllers/users +Executing (default): INSERT INTO `users` (`id`,`username`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3); + 1) should create anonymous user + 2) should find user by userid + ✓ should through error for non number userid + 3) should find user by username + + + 6 passing (607ms) + 3 failing + + 1) controllers/users + should create anonymous user: + SequelizeUniqueConstraintError: Validation error + at Query.formatError (node_modules/sequelize/lib/dialects/sqlite/query.js:416:16) + at Query._handleQueryResponse (node_modules/sequelize/lib/dialects/sqlite/query.js:73:18) + at Statement.afterExecute (node_modules/sequelize/lib/dialects/sqlite/query.js:250:31) + at Statement.replacement (node_modules/sqlite3/lib/trace.js:19:31) + + 2) controllers/users + should find user by userid: + TypeError: Cannot read property 'id' of null + at Context.it (test/controllers/users.test.js:21:51) + + 3) controllers/users + should find user by username: + TypeError: Cannot read property 'username' of null + at Context.it (test/controllers/users.test.js:37:57) + + + +npm ERR! code ELIFECYCLE +npm ERR! errno 3 +npm ERR! social-media-project@1.0.0 test: `mocha test/setup.js test/**/*.test.js` +npm ERR! Exit status 3 +npm ERR! +npm ERR! Failed at the social-media-project@1.0.0 test script. +npm ERR! This is probably not a problem with npm. There is likely additional logging output above. + +npm ERR! A complete log of this run can be found in: +npm ERR! /home/singla/.npm/_logs/2020-05-30T06_48_36_130Z-debug.log + + + +singla@singla-Inspiron-3542:/media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May$ npm run test + +> social-media-project@1.0.0 test /media/singla/New Volume/Github Projects/Social_Media_Sample_Project_2020_May +> mocha test/setup.js test/**/*.test.js + + + +Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(30) NOT NULL UNIQUE, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL); +Executing (default): PRAGMA INDEX_LIST(`users`) +Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_users_1`) +Executing (default): CREATE TABLE IF NOT EXISTS `posts` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE); +Executing (default): PRAGMA INDEX_LIST(`posts`) +(sequelize) Warning: SQLite does not support TEXT with options. Plain `TEXT` will be used instead. +>> Check: https://www.sqlite.org/datatype3.html +Executing (default): CREATE TABLE IF NOT EXISTS `comments` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(140) NOT NULL, `body` TEXT, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, `postId` INTEGER REFERENCES `posts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE); +Executing (default): PRAGMA INDEX_LIST(`comments`) + controllers/comments +Executing (default): INSERT INTO `comments` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`postId`) VALUES (NULL,$1,$2,$3,$4,$5); + ✓ should create comment with postId, title and body assigned (233ms) +Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `comments`.`id` AS `comments.id`, `comments`.`title` AS `comments.title`, `comments`.`body` AS `comments.body`, `comments`.`createdAt` AS `comments.createdAt`, `comments`.`updatedAt` AS `comments.updatedAt`, `comments`.`userId` AS `comments.userId`, `comments`.`postId` AS `comments.postId` FROM `posts` AS `post` LEFT OUTER JOIN `comments` AS `comments` ON `post`.`id` = `comments`.`postId` WHERE `post`.`id` = 1; + ✓ should find all commments by postId + + controllers/posts +Executing (default): INSERT INTO `posts` (`id`,`title`,`body`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,$1,$2,$3,$4,$5); + ✓ should create post with userId, title and body assigned (180ms) +Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`; + ✓ should find all posts +Executing (default): SELECT `post`.`id`, `post`.`title`, `post`.`body`, `post`.`createdAt`, `post`.`updatedAt`, `post`.`userId`, `user`.`id` AS `user.id`, `user`.`username` AS `user.username`, `user`.`createdAt` AS `user.createdAt`, `user`.`updatedAt` AS `user.updatedAt` FROM `posts` AS `post` LEFT OUTER JOIN `users` AS `user` ON `post`.`userId` = `user`.`id`; + ✓ should find all posts of a user by its userid + + controllers/users +Executing (default): INSERT INTO `users` (`id`,`username`,`createdAt`,`updatedAt`) VALUES (NULL,$1,$2,$3); + ✓ should create anonymous user (174ms) +Executing (default): SELECT `id`, `username`, `createdAt`, `updatedAt` FROM `users` AS `user` WHERE `user`.`id` = 43; + ✓ should find user by userid + ✓ should through error for non number userid +Executing (default): SELECT `id`, `username`, `createdAt`, `updatedAt` FROM `users` AS `user` WHERE `user`.`username` = 'slim-chalk'; + ✓ should find user by username + + + 9 passing (748ms) \ No newline at end of file diff --git a/test/controllers/comments.test.js b/test/controllers/comments.test.js new file mode 100644 index 0000000..1fe8a33 --- /dev/null +++ b/test/controllers/comments.test.js @@ -0,0 +1,21 @@ +const { expect } = require('chai') +const { + createNewComment, + findAllCommentsbyPosts +} = require('../../src/controllers/comments') + +describe('controllers/comments',()=>{ + let testingComment = null + + it.only('should create comment with postId, title and body assigned', async ()=>{ + testingComment = await createNewComment(1, 'Coder', 'Arnav Bhaiya') + expect(testingComment.postId).to.equal(1) + expect(testingComment.title).to.equal('Coder') + expect(testingComment.body).to.equal('Arnav Bhaiya') + } ) + it('should find all commments by postId', async ()=>{ + let where ={postId:1} + let allComments = await findAllCommentsbyPosts(where.postId) + expect(allComments).to.be.an("array") + }) +}) \ No newline at end of file diff --git a/test/controllers/posts.test.js b/test/controllers/posts.test.js new file mode 100644 index 0000000..0bc2f50 --- /dev/null +++ b/test/controllers/posts.test.js @@ -0,0 +1,30 @@ +const {expect}=require('chai') +const { + createNewPost, + findAllPosts +}=require('../../src/controllers/posts') + +describe('controllers/posts', ()=>{ + let testingPost = null + + it('should create post with userId, title and body assigned', async ()=>{ + testingPost = await createNewPost(1, 'Coder', 'Arnav Bhaiya') + expect(testingPost.userId).to.equal(1) + expect(testingPost.title).to.equal('Coder') + expect(testingPost.body).to.equal('Arnav Bhaiya') + } ) + + it('should find all posts', async ()=>{ + let where ={} + let allPosts = await findAllPosts(where) + expect(allPosts).to.be.an("array") + }) + + it('should find all posts of a user by its userid', async ()=>{ + let where ={ + userId: 1 + } + let allPosts = await findAllPosts(where) + expect(allPosts).to.be.an("array") + }) +}) \ No newline at end of file diff --git a/test/setup.js b/test/setup.js index adb4f2d..d77d164 100644 --- a/test/setup.js +++ b/test/setup.js @@ -6,5 +6,5 @@ const chai = require('chai') chai.use(require('chai-as-promised')) before(async () => { - await db.sync() + await db.sync({force:true}) }) \ No newline at end of file