From 4c6942aa0be7170ea6fdffabd1a9033507738971 Mon Sep 17 00:00:00 2001 From: Critter111 Date: Mon, 1 Jun 2020 23:54:33 +0530 Subject: [PATCH] added comments with tests --- package-lock.json | 47 ++++++++--- package.json | 5 +- src/controllers/comments.js | 39 ++++++++++ src/controllers/posts.js | 13 +++- src/controllers/users.js | 6 +- src/db/models.js | 79 ++++++++++--------- src/public/app/all-posts.js | 108 ++++++++++++++++++++------ src/public/app/comments.js | 25 ++++++ src/public/app/common.css | 2 - src/public/app/common.js | 2 +- src/public/app/my-posts.js | 4 +- src/public/app/navbar.js | 1 - src/public/app/singlepost.js | 95 ++++++++++++++++++++++ src/public/app/write-post.js | 3 +- src/public/components/all-posts.html | 5 +- src/public/components/comments.html | 10 +++ src/public/components/content.html | 34 ++++++++ src/public/components/my-posts.html | 2 +- src/public/components/singlepost.html | 32 ++++++++ src/public/components/write-post.html | 2 +- src/routes/posts/comments.js | 30 ++++++- src/routes/posts/index.js | 17 +++- src/server.js | 6 +- test/controllers/comments.test.js | 47 +++++++++++ test/controllers/posts.test.js | 58 ++++++++++++++ test/controllers/users.test.js | 42 +++++----- test/setup.js | 9 +-- test/test.db | Bin 0 -> 24576 bytes 28 files changed, 599 insertions(+), 124 deletions(-) create mode 100644 src/public/app/comments.js create mode 100644 src/public/app/singlepost.js create mode 100644 src/public/components/comments.html create mode 100644 src/public/components/content.html create mode 100644 src/public/components/singlepost.html create mode 100644 test/controllers/comments.test.js create mode 100644 test/controllers/posts.test.js create mode 100644 test/test.db diff --git a/package-lock.json b/package-lock.json index 8d33ccf..f2ea952 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.6", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.6.tgz", + "integrity": "sha512-FbNmu4F67d3oZMWBV6Y4MaPER+0EpE9eIYf2yaHhCWovc1dlXCZkqGX4NLHfVVr6umt20TNBdRzrNJIzIKfdbw==" }, "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..d0171db 100644 --- a/package.json +++ b/package.json @@ -5,15 +5,16 @@ "main": "index.js", "scripts": { "start": "node src/server.js", - "test": "mocha test/setup.js test/**/*.test.js", + "test": "NODE_ENV=testing mocha test/setup.js test/**/*.test.js", "cover": "nyc --reporter=lcov npm run test" }, "author": "", "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": { diff --git a/src/controllers/comments.js b/src/controllers/comments.js index e69de29..ed3c1a4 100644 --- a/src/controllers/comments.js +++ b/src/controllers/comments.js @@ -0,0 +1,39 @@ +const { Posts, Users, Comments } = require('../db/models') + +async function createNewComment(title, body, userId, postId) { + const Comment = await Comments.create({ + title, + body, + userId, + postId + }) + + return Comment + } + + + async function showAllComments(query) { + + //let postId = query.postId + let where = {} + if (query.postId) { where.postId = query.postId } + + const comment = await Comments.findAll({ + include: [ Users ], + where + }) + return comment + } + + /* async function showCommentsPost(Id){ + const commentsPost = await Comments.findAll({ + where: {postId:Id}, + include: [Users,Posts] + }) + return commentsPost + } */ + + module.exports = { + createNewComment, + showAllComments + } \ No newline at end of file diff --git a/src/controllers/posts.js b/src/controllers/posts.js index 8b87710..c98b8f0 100644 --- a/src/controllers/posts.js +++ b/src/controllers/posts.js @@ -26,9 +26,19 @@ async function findAllPosts(query) { return posts } +async function showpostbypostid(id) +{ + const p=await Posts.findOne({ + where:{id}, + include:[Users] + }); + return p; +} + module.exports = { createNewPost, - findAllPosts + findAllPosts, + showpostbypostid } /* Test Code */ @@ -53,6 +63,5 @@ async function task() { console.log(`${p.title}\nauthor: ${p.user.username}\n${p.body}\n==========\n`) } } - task() */ \ No newline at end of file diff --git a/src/controllers/users.js b/src/controllers/users.js index 1b52937..b588a59 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -10,9 +10,6 @@ async function createAnonUser() { } async function getUserById(id) { - if (!id) throw new Error('user id not provided') - if (typeof id !== 'number') throw new Error('user id should be integer') - return await Users.findOne({ where: { id } }) } @@ -38,6 +35,5 @@ async function task () { console.log(await createAnonUser()) console.log('---------------------') } - task() -*/ +*/ \ No newline at end of file diff --git a/src/db/models.js b/src/db/models.js index dd5870c..ab3e6fd 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -1,55 +1,58 @@ const Sequelize = require('sequelize') let db -if (process.env.NODE_ENV == 'testing') { - db = new Sequelize({ - dialect: 'sqlite', - storage: ':memory:', - }) +if(process.env.NODE_ENV == 'testing') { + db = new Sequelize({ + dialect: 'sqlite', + storage: __dirname + '/../../test/test.db' + }) } else { - db = new Sequelize({ - dialect: 'mysql', - database: 'cbsocialmediadb', - username: 'cbsocialuser', - password: 'cbsocialpass', - }) + db = new Sequelize({ + dialect: 'mysql', + database: 'cbsocialmediadb', + username: 'cbsocialuser', + password: 'cbsocialpass', + port: 3303 + }) } + + const COL_ID_DEF = { - type: Sequelize.DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, + type: Sequelize.DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true } const COL_USERNAME_DEF = { - type: Sequelize.DataTypes.STRING(30), - unique: true, - allowNull: false, + type: Sequelize.DataTypes.STRING(30), + unique: true, + allowNull: false } const COL_TITLE_DEF = { - type: Sequelize.DataTypes.STRING(140), - allowNull: false, + type: Sequelize.DataTypes.STRING(140), + allowNull: false } const Users = db.define('user', { - id: COL_ID_DEF, - username: COL_USERNAME_DEF, + id: COL_ID_DEF, + username: COL_USERNAME_DEF }) const Posts = db.define('post', { - id: COL_ID_DEF, - title: COL_TITLE_DEF, - body: { - type: Sequelize.DataTypes.TEXT, - allowNull: false, - }, + id: COL_ID_DEF, + title: COL_TITLE_DEF, + body: { + type: Sequelize.DataTypes.TEXT, + allowNull: false + } }) const Comments = db.define('comment', { - id: COL_ID_DEF, - title: COL_TITLE_DEF, - body: { - type: Sequelize.DataTypes.TEXT('tiny'), - }, + id: COL_ID_DEF, + title: COL_TITLE_DEF, + body: { + type: Sequelize.DataTypes.TEXT('tiny') + } }) Users.hasMany(Posts) @@ -61,9 +64,11 @@ Comments.belongsTo(Users) Posts.hasMany(Comments) Comments.belongsTo(Posts) + + module.exports = { - db, - Users, - Posts, - Comments, -} + db, + Users, + Posts, + Comments +} \ No newline at end of file diff --git a/src/public/app/all-posts.js b/src/public/app/all-posts.js index 04aa02d..41b0d8b 100644 --- a/src/public/app/all-posts.js +++ b/src/public/app/all-posts.js @@ -1,25 +1,89 @@ function loadPosts() { - $.get('/api/posts', (posts) => { - for (let p of posts) { - $('#posts-container').append( - $(` -
-
-
-
${p.title}
-
${p.user.username}
-

- ${p.body.substr(0, 200)} - ...read more -

- Comment - Like + const user = JSON.parse(window.localStorage.user) + $.get('/api/posts', (posts) => { + for (let p of posts) { + /* $('#posts-container').append( + $(` +
+
+
+
${p.title}
+
${p.user.username}
+

+ ${p.body.substr(0, 200)} + ...read more +

+ +
+ + + +
+
+
+
-
- - `) - ) - } - }) -} + `) + ) */ + $('#posts-container').append( + $(` +
+
+
+
${p.title}
+
${p.user.username}
+

+ ${p.body.substr(0, 200)} + ...read more +

+
+ + + +
+
+
+
+
+
+ + `) + ) + + $(`#${p.id}`).click(()=>{ + + const postId = p.id + const userId = user.id + const title = user.username + const body = $(`#comment-${p.id}`).val() + + $.post('/api/comments', {postId, userId, title, body}, (data)=>{ + console.log('ok sent', data.postId) + }) + comments() + }) + + function comments(){ + $.get(`/api/comments?postId=${p.id}`, (comments)=>{ + $(`#commentListId-${p.id}`).empty() + for(let c of comments){ + $(`#ListId-${p.id}`).append( + $(` +
+
+
${c.user.username}
+

${c.body}

+
+
+ `) + ) + } + }) + } + + comments() + } + }) + } + diff --git a/src/public/app/comments.js b/src/public/app/comments.js new file mode 100644 index 0000000..89b5a39 --- /dev/null +++ b/src/public/app/comments.js @@ -0,0 +1,25 @@ +function loadComments(){ + $.get(`/api/post/${3}`, (comments) => { + console.log('bye') + for (let c of comments) { + $('#comments-container').append( + $(` +
+
+ ${c.title} +
+
+
+

${c.body}

+
Someone famous in ${c.user.username}
+
+
+
+ + `) + ) + } + + }) + +} \ No newline at end of file diff --git a/src/public/app/common.css b/src/public/app/common.css index 239ced6..1c61a12 100644 --- a/src/public/app/common.css +++ b/src/public/app/common.css @@ -11,5 +11,3 @@ background-color: var(--light); } -#content { -} \ No newline at end of file diff --git a/src/public/app/common.js b/src/public/app/common.js index 279854f..377570b 100644 --- a/src/public/app/common.js +++ b/src/public/app/common.js @@ -22,4 +22,4 @@ function loginIfNeeded() { console.log($('#nav-username')) $('#nav-username').text(currentUser.username) } -} +} \ No newline at end of file diff --git a/src/public/app/my-posts.js b/src/public/app/my-posts.js index 9708598..d614be8 100644 --- a/src/public/app/my-posts.js +++ b/src/public/app/my-posts.js @@ -2,6 +2,7 @@ function loadMyPosts() { const userId = JSON.parse(window.localStorage.user).id $.get(`/api/posts?userId=${userId}`, (posts) => { + for (let p of posts) { $('#posts-container').append( $(` @@ -22,6 +23,7 @@ function loadMyPosts() { `) ) + console.log(`${p.user.username}`) } }) -} +} \ No newline at end of file diff --git a/src/public/app/navbar.js b/src/public/app/navbar.js index ef92a71..c205050 100644 --- a/src/public/app/navbar.js +++ b/src/public/app/navbar.js @@ -1,4 +1,3 @@ - let navlinks = $('.navbar-nav .nav-link') navlinks.click((ev) => { diff --git a/src/public/app/singlepost.js b/src/public/app/singlepost.js new file mode 100644 index 0000000..c64bbe6 --- /dev/null +++ b/src/public/app/singlepost.js @@ -0,0 +1,95 @@ +$(()=>{ + $('#navbar').load('/components/navbar.html', loginIfNeeded()) + $('#footer').load('/components/footer.html') + $("#content").load("/Components/Content.html",()=>{ + const user = JSON.parse(window.localStorage.user).id + const params = new URLSearchParams(location.search); + const postid=params.get('q1'); + //const userid=params.get('q2'); + let url="/api/posts/"+postid; + $.get(url,(p)=>{ + $('#container').append( + $(` +
+
+
+
${p.title}
+
${p.user.username}
+

+ ${p.body} + +

+
+ + + +
+
+
+
+
+
+ + + `) + ) + $(`#${p.id}`).click(()=>{ + + const postId = p.id + const userId = user.id + const title = user.username + const body = $(`#comment-${p.id}`).val() + + $.post('/api/comments', {postId, userId, title, body}, (data)=>{ + console.log('ok sent', data.postId) + }) + comments() + }) + + function comments(){ + $.get(`/api/comments?postId=${p.id}`, (comments)=>{ + //$(`#commentListId-${p.id}`).empty() + for(let c of comments){ + $(`#ListId-${p.id}`).append( + $(` + +
+
${c.user.username}
+

${c.body}

+
+
+ `) + ) + } + }) + } + comments() + console.log(p) + + + + }) + + }) +}) + + +function loginIfNeeded() { + window.currentUser = window.localStorage.user + ? JSON.parse(window.localStorage.user) + : null + if (!currentUser) { + $.post('/api/users', {}, (user) => { + if (user) { + console.log('registered current user as ', user.username) + window.localStorage.user = JSON.stringify(user) + currentUser = user + $('#nav-username').text(currentUser.username) + } + }) + } else { + console.log('resuming session as ', currentUser.username) + console.log($('#nav-username')) + $('#nav-username').text(currentUser.username) + } +} \ No newline at end of file diff --git a/src/public/app/write-post.js b/src/public/app/write-post.js index e92aa9c..a501762 100644 --- a/src/public/app/write-post.js +++ b/src/public/app/write-post.js @@ -1,5 +1,6 @@ $('#write-btn').click(() => { const userId = JSON.parse(window.localStorage.user).id + console.log(userId + "asdasdas") const title = $('#p-title').val() const body = $('#p-body').val() @@ -8,4 +9,4 @@ $('#write-btn').click(() => { $('.nav-item .active').removeClass('active') $("[data-components='my-posts']").addClass('active') }) -}) +}) \ No newline at end of file diff --git a/src/public/components/all-posts.html b/src/public/components/all-posts.html index 242a75f..ee68b10 100644 --- a/src/public/components/all-posts.html +++ b/src/public/components/all-posts.html @@ -5,7 +5,8 @@

Recent Posts

- + \ No newline at end of file + + diff --git a/src/public/components/comments.html b/src/public/components/comments.html new file mode 100644 index 0000000..a01e3e5 --- /dev/null +++ b/src/public/components/comments.html @@ -0,0 +1,10 @@ +
+

Comments

+ +
+
+ + + diff --git a/src/public/components/content.html b/src/public/components/content.html new file mode 100644 index 0000000..2df0a32 --- /dev/null +++ b/src/public/components/content.html @@ -0,0 +1,34 @@ + + +
+ +
\ 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/public/components/singlepost.html b/src/public/components/singlepost.html new file mode 100644 index 0000000..57f5df9 --- /dev/null +++ b/src/public/components/singlepost.html @@ -0,0 +1,32 @@ + + + + + + Post + + + + + + + + +
+ + + + \ No newline at end of file diff --git a/src/public/components/write-post.html b/src/public/components/write-post.html index 12dc14a..25308fb 100644 --- a/src/public/components/write-post.html +++ b/src/public/components/write-post.html @@ -25,4 +25,4 @@

Write Post

- + \ No newline at end of file diff --git a/src/routes/posts/comments.js b/src/routes/posts/comments.js index 632f4c2..4b30947 100644 --- a/src/routes/posts/comments.js +++ b/src/routes/posts/comments.js @@ -1,7 +1,33 @@ const { Router } = require('express') +const { + createNewComment, + showAllComments +} = require('../../controllers/comments') -const commentsRoute = Router() +const route = Router() + +route.get('/', async (req, res)=>{ + console.log("test") + const comments = await showAllComments(req.query) + res.status(200).send(comments) +}) + +/* route.get('/post/:id', async (req, res) => { + const comment = await showCommentsPost(req.params.id) + res.status(400).send(comment) +}) */ + +route.post('/', async (req, res) => { + const { title, body, userId, postId } = req.body + + if((!title) || (!body) || (!userId) || (!postId)){ + res.status.send({error:'title or body or userId or postId not valid'}) + } + const newComment = await createNewComment(title,body,userId,postId) + res.status(200).send(newComment) + +}) module.exports = { - commentsRoute + commentsRoute: route } \ No newline at end of file diff --git a/src/routes/posts/index.js b/src/routes/posts/index.js index aa107c2..0c62cd4 100644 --- a/src/routes/posts/index.js +++ b/src/routes/posts/index.js @@ -1,11 +1,26 @@ const { Router } = require('express') const { findAllPosts, - createNewPost + createNewPost, + showpostbypostid } = require('../../controllers/posts') const route = Router() +route.get("/:id",async(req,res)=>{ + let post; + if(isNaN(parseInt(req.params.id))) + { + post=await showpostsbykeyword(req.params.id); + } + else + { + post=await showpostbypostid(req.params.id); + } + + res.send(post); +}) + route.get('/', async (req, res) => { const posts = await findAllPosts(req.query) diff --git a/src/server.js b/src/server.js index 5d06647..70f3ac6 100644 --- a/src/server.js +++ b/src/server.js @@ -2,7 +2,8 @@ const express = require('express') const { db } = require('./db/models') const { usersRoute } = require('./routes/users') -const { postsRoute } = require('./routes/posts') +const { postsRoute } = require('./routes/posts/index') +const { commentsRoute } = require('./routes/posts/comments') const app = express() app.use(express.json()) @@ -10,6 +11,7 @@ app.use(express.urlencoded({extended: true})) app.use('/api/users', usersRoute) app.use('/api/posts', postsRoute) +app.use('/api/comments', commentsRoute) app.use('/', express.static(__dirname + '/public')) db.sync() @@ -21,4 +23,4 @@ db.sync() .catch((err) => { console.error(new Error('Could not start database')) console.error(err) - }) + }) \ 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..4297c9b --- /dev/null +++ b/test/controllers/comments.test.js @@ -0,0 +1,47 @@ +const { expect } = require('chai') + +const { + createNewComment, + showAllComments +} = require('../../src/controllers/comments') + +describe('controllers/comments', () => { + let createComment = null + + it('it should create a comment', async() => { + createComment = await createNewComment('title','body',1,1) + + expect(createComment).to.have.property('title') + expect(createComment).to.have.property('body') + expect(createComment).to.have.property('userId') + expect(createComment).to.have.property('postId') + expect(createComment.title).to.be.a('string') + expect(createComment.body).to.be.a('string') + expect(createComment.userId).to.be.a('number') + expect(createComment.postId).to.be.a('number') + }) + + it('it should show all comments', async() => { + + let query ={ + postId: createComment.postId + } + console.log(query + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz") + let foundcomment = await showAllComments(query) + console.log(foundcomment[0].title + "===================== foundcomment =====================") + + expect(foundcomment[0]).to.property('title') + expect(foundcomment[0]).to.property('body') + expect(foundcomment[0]).to.property('userId') + expect(foundcomment[0]).to.property('postId') + expect(foundcomment[0].title).to.equal(createComment.title) + expect(foundcomment[0].body).to.equal(createComment.body) + expect(foundcomment[0].userId).to.equal(createComment.userId) + expect(foundcomment[0].postId).to.equal(createComment.postId) + + + }) + + + }) + diff --git a/test/controllers/posts.test.js b/test/controllers/posts.test.js new file mode 100644 index 0000000..d415c26 --- /dev/null +++ b/test/controllers/posts.test.js @@ -0,0 +1,58 @@ +const { expect } = require('chai') + +const { + createNewPost, + findAllPosts, + showpostbypostid +} = require('../../src/controllers/posts') + +console.log("working or not ===============================") + +describe('controller/posts', () => { + + let createdPost = null + + it('should create new post', async() => { + + createdPost = await createNewPost(1,'title1','body1') + console.log(createdPost + '=========================================') + + expect(createdPost).to.have.property('title') + expect(createdPost).to.have.property('body') + expect(createdPost).to.have.property('userId') + + }) + + it('should find all posts', async ()=>{ + + /* let query = {} + + let foundPost = findAllPosts(query)*/ + + let query ={ + + } + let foundPost = await findAllPosts(query) + let array = [] + for(let f of foundPost){ + array.push(f) + } + + expect(array[0].title).to.equal(createdPost.title) + expect(array[0].body).to.equal(createdPost.body) + + }) + + it('should show post by id', async() => { + + let query ={ + userId: createdPost.userId + } + let foundPost = await showpostbypostid(query.userId) + + expect(foundPost.title).to.equal(createdPost.title) + expect(foundPost.body).to.equal(createdPost.body) + + }) + +}) \ No newline at end of file diff --git a/test/controllers/users.test.js b/test/controllers/users.test.js index 265e52c..fc99c6f 100644 --- a/test/controllers/users.test.js +++ b/test/controllers/users.test.js @@ -1,41 +1,37 @@ -const { expect } = require('chai') -const { - createAnonUser, - getUserById, - getUserByUsername +const { expect } =require('chai') + +const{ + createAnonUser, + getUserById, + getUserByUsername } = require('../../src/controllers/users') describe('controllers/users', () => { + let createdUser = null - it('should create anonymous user', async () => { + it('should create anonymous user', async() => { + + + createdUser = await createAnonUser() - createdUser = await createAnonUser() expect(createdUser).to.have.property('username') - expect(createdUser.id).to.be.a('number') + expect(createdUser.id).to.be.a('number') + + }) - it('should find user by userid', async () => { - + it('should find user by userid', async() => { let foundUser = await getUserById(createdUser.id) expect(foundUser.username).to.equal(createdUser.username) - }) - it('should through error for non number userid', async () => { - - await expect(getUserById('sss')).to.be.rejectedWith('user id should be integer') - await expect(getUserById(null)).to.be.rejectedWith('user id not provided') - await expect(getUserById(true)).to.be.rejectedWith('user id should be integer') - await expect(getUserById(0)).to.be.rejectedWith('user id not provided') - - }) - - it('should find user by username', async () => { - + it('should find user by username', async() => { let foundUser = await getUserByUsername(createdUser.username) expect(foundUser.id).to.equal(createdUser.id) + expect(foundUser.username).to.equal(createdUser.username) + }) -}) +}) \ No newline at end of file diff --git a/test/setup.js b/test/setup.js index adb4f2d..523f6dd 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1,10 +1,7 @@ -// setup testing environment before requiring anything -process.env.NODE_ENV = 'testing' - const { db } = require('../src/db/models') -const chai = require('chai') -chai.use(require('chai-as-promised')) before(async () => { - await db.sync() + //console.log('running before all tests') + + await db.sync() }) \ No newline at end of file diff --git a/test/test.db b/test/test.db new file mode 100644 index 0000000000000000000000000000000000000000..529cefb2649fa2c4a94651530c9341d20dcebba5 GIT binary patch literal 24576 zcmeI)y>H`W90%~naok?eB~OnNG7OcTrBb6@BRkGZFA{=~8n8&*TwV~J>NN2k(WP-N zb|e_7;4kTpU}8HlFt9K&A+fN-h{VWa=Thvvw1qOX($|vXm!JLfPih0t zW>1~TY1d;K*&>2K-erss@_>&Ue4Gy=pNY>G_*4kZubX{9vhVzoq<<0Veu>a6`cvv< z@|@3NfB*y_009U<00Izz00i!iz}Z0Bl()BquXeod$FMFbLZ2IYE!SYb=I!D zSJRm_Uv05nYtXltVYc-T^d@UGje4bd#6HrGSf$fGG)%r45`E6wVM zmFBLRSJKQpY%{Y{tMTxO)BoIJZT%B}+up?PdUn6!S?quxwr$k)@V3)&|I)B)PmEBj zn)>^CQ#Y%6i&;}1b6+((%??d=px1PMVl92v5`R^v;UAP$E3InfKn z#drj3A-Fs0p4yN2p6M^jLV$hN3UUV5Klq77_K(~c*TVLb_(%EYmexXdH0zkgla7D$ z+`u=Tj%~JkkGH`F-@jk@>vWJ zfB*y_009U<00Izz00bZafg1@lB#C4Uqu(7pvnS5f-RphY9X`(~Ib}~N?kOr$_sgnU zF66UCO=THHDJwj7S+pLP$c|x*r<3u}-Ww0PBWG!cyqZ;u#dYZ-(T2FtK-cL#cf%P= z<@|nG(X#pC;}sc~qxD$0flKGhcPN!}g{-!(txmTPZ3saFPn_wfKeSzU`ILi{Eh5!U0009U<00Izz00bZa0SG|gzX?1PGr~g6 zMA{N}gmBd(9^ELYTZo~Jg33eebup7zs2wEf??j|O6Z#wdlTR=}00Izz00bZa0SG_< z0uX=z1R!uX1n!ICwlII?CY}&PSqS?4OT77sg#VUEBF5LvULl$F{~r_j4YlZ2>ig9A zZk!}q2>}Q|00Izz00bZa0SG_<0=HYhls3s*vSB2G?>ngeXB^aU2mARO0ENf-6+PmY zqh|aX`TS1)xm6wZgM5*wDPAL=req7H{dMh&M9tXc`7T}=(3BPVmZPR{Wxm-v14?Oi zzJ;j&{2x#KK F{|0y&S-=1Q literal 0 HcmV?d00001