Example #1
0
  def test_FetchAllPosts(self):
    postDAO = PostDAO(None)
    postDAO.findAll = MagicMock(return_value = self.postList)
    postController = PostController(postDAO)
    request = Request(None)

    response = postController.fetchAllPosts(request)
    actual = [
      {
        'id': None,
        'title': "Post 1",
        'description': "Test description",
        'url': "/post/None"
      },
      {
        'id': None,
        'title': "Post 2",
        'description': "Test description",
        'url': "/post/None"
      },
      {
        'id': None,
        'title': "Post 3",
        'description': "Test description",
        'url': "/post/None"
      }
    ]

    self.assertEqual(200, response.status_code)
    self.assertEquals(actual, response.data)
    postDAO.findAll.assert_called_with()
Example #2
0
  def test_CreatePostWhenPostDAOThrows(self):
    postDAO = PostDAO(None)
    postDAO.create = MagicMock(side_effect=IllegalArgumentException())
    postController = PostController(postDAO)
    request = Request({ 'id': 4, 'title': self.post4.title, 'description': self.post4.description })

    response = postController.createPost(request)

    self.assertEqual(400, response.status_code)
    postDAO.create.assert_called_with(self.post4)
Example #3
0
  def test_CreatePost(self):
    postDAO = PostDAO(None)
    postDAO.create = MagicMock()
    postController = PostController(postDAO)
    request = Request({ 'title': self.post4.title, 'description': self.post4.description })

    response = postController.createPost(request)

    self.assertEqual(204, response.status_code)
    postDAO.create.assert_called_with(self.post4)
Example #4
0
  def test_UpdatePostWithMismatchedIds(self):
    postDAO = PostDAO(None)
    postDAO.update = MagicMock()
    postController = PostController(postDAO)
    request = Request({'id': self.updatedPost3.id, 'title': self.updatedPost3.title, 'description': self.updatedPost3.description })
    updateId = str((self.updatedPost3.id + 1))

    response = postController.updatePost(updateId, request)

    self.assertEqual(400, response.status_code)
    postDAO.update.assert_not_called()
Example #5
0
  def test_UpdatePost(self):
    postDAO = PostDAO(None)
    postDAO.update = MagicMock()
    postController = PostController(postDAO)
    request = Request({'id': self.updatedPost3.id, 'title': self.updatedPost3.title, 'description': self.updatedPost3.description })
    updateId = str(self.updatedPost3.id)

    response = postController.updatePost(updateId, request)

    self.assertEqual(204, response.status_code)
    postDAO.update.assert_called_with(self.updatedPost3)
Example #6
0
  def test_DeletePostWhenPostDAOThrows(self):
    postDAO = PostDAO(None)
    postDAO.delete = MagicMock(side_effect=UnknownElementException())
    postController = PostController(postDAO)
    request = Request(None)
    deleteId = 3

    response = postController.deletePost(deleteId, request)

    self.assertEqual(404, response.status_code)
    postDAO.delete.assert_called_with(deleteId)
Example #7
0
  def test_DeletePost(self):
    postDAO = PostDAO(None)
    postDAO.delete = MagicMock()
    postController = PostController(postDAO)
    request = Request(None)
    deleteId = "3"

    response = postController.deletePost(deleteId, request)

    self.assertEqual(204, response.status_code)
    postDAO.delete.assert_called_with(deleteId)
Example #8
0
  def test_UpdatePostWhenPostDAOThrows(self):
    postDAO = PostDAO(None)
    postDAO.update = MagicMock(side_effect=UnknownElementException())
    postController = PostController(postDAO)
    request = Request({'id': self.updatedPost3.id, 'title': self.updatedPost3.title, 'description': self.updatedPost3.description })
    updateId = "3"

    response = postController.updatePost(updateId, request)

    self.assertEqual(404, response.status_code)
    postDAO.update.assert_called_with(self.updatedPost3)
Example #9
0
from rest_framework.decorators import api_view
from sqlalchemy import create_engine
from handlers import PostController
import sys, os
sys.path.append(
    os.path.realpath(os.path.dirname(__file__) + "/../../../dao/src"))
from PostDAO import PostDAO

postDAO = PostDAO(create_engine("postgres://*****:*****@database:5432/blog"))
postController = PostController(postDAO)


@api_view(['GET', 'POST'])
def post(request):
    if request.method == 'GET':
        return postController.fetchAllPosts(request)
    if request.method == 'POST':
        return postController.createPost(request)


@api_view(['PUT', 'DELETE'])
def post_by_id(request, id):
    if request.method == 'PUT':
        return postController.updatePost(id, request)
    if request.method == 'DELETE':
        return postController.deletePost(id, request)