Skip to content

Commit

Permalink
:update: add unittests for FileMiddleware and fix post method
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdd committed Jul 16, 2016
1 parent 0c007b0 commit caf3875
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
4 changes: 3 additions & 1 deletion link/middleware/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def post(self, data):
:type data: str
"""

with open(self.path, 'a') as f:
path = '{0}{1}'.format(os.sep, os.path.join(*self.path))

with open(path, 'a') as f:
f.write(data)

def put(self, data):
Expand Down
64 changes: 64 additions & 0 deletions link/middleware/test/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-

from b3j0f.utils.ut import UTCase
from mock import MagicMock, patch
from unittest import main

from link.middleware.core import Middleware
from link.middleware.file import FileMiddleware


class TestFile(UTCase):
def setUp(self):
patcher1 = patch('link.middleware.file.os.remove')
patcher2 = patch('link.middleware.file.os.path.exists')
patcher3 = patch('link.middleware.file.open')

self.remove = patcher1.start()
self.exists = patcher2.start()
self.open = patcher3.start()

self.addCleanup(patcher1.stop)
self.addCleanup(patcher2.stop)
self.addCleanup(patcher3.stop)

self.exists.return_value = True

self.file = MagicMock()
self.file.__enter__ = MagicMock(return_value=self.file)
self.file.__exit__ = MagicMock()
self.file.read = MagicMock(return_value='content')
self.file.write = MagicMock()

self.open.return_value = self.file

self.mid = Middleware.get_middleware_by_uri('file:///path')

def test_get(self):
result = self.mid.get()

self.assertEqual(result, 'content')
self.open.assert_called_with('/path')
self.file.read.assert_called_with()

def test_post(self):
self.mid.post('data')

self.open.assert_called_with('/path', 'a')
self.file.write.assert_called_with('data')

def test_put(self):
self.mid.put('data')

self.open.assert_called_with('/path', 'w')
self.file.write.assert_called_with('data')

def test_delete(self):
self.mid.delete(None)

self.exists.assert_called_with('/path')
self.remove.assert_called_with('/path')


if __name__ == '__main__':
main()

0 comments on commit caf3875

Please sign in to comment.