Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added socket, and datetime examples. And fixed test runner to also ru…

…n the examples
  • Loading branch information...
commit 4298eb6d6b453cc8ce0ea1e647434a9da596e629 1 parent 166a438
@vbabiy vbabiy authored
View
0  examples/__init__.py
No changes.
View
47 examples/examples.py
@@ -0,0 +1,47 @@
+from chai import Chai
+import socket
+import datetime
+
+######################################
+## Mocking sockets
+######################################
+def connect():
+ sock = socket.socket()
+ sock.bind(('127.0.0.1', 10000))
+ return sock.recv(1024)
+
+class SocketTestCase(Chai):
+
+ def test_socket(self):
+ mock_socket = self.mock()
+ self.expect(socket, 'socket').returns(mock_socket)
+ # Note: Unfortunately we can't use self.expect(socket.socket) till python 3
+ # This is due to the way that the socket module is implemented in python 2
+
+ self.expect(mock_socket.bind).args(('127.0.0.1' , 10000))
+ self.expect(mock_socket.recv).args(1024).returns("HELLO WORLD")
+
+ self.assert_equals(connect(), "HELLO WORLD")
+
+######################################
+## Mocking datetime.now
+######################################
+def now():
+ return datetime.datetime.now()
+
+class DatetimeTestCase(Chai):
+
+ def test_now(self):
+ current_now = datetime.datetime.now() # Save for later
+
+ # NOTE: In c python you are not allow to mock built types so we have to mock
+ # the entire module.moc
+ mock_datetime = self.mock(datetime, 'datetime')
+ self.expect(mock_datetime.now).returns(current_now)
+
+ self.assert_equals(now(), current_now)
+
+if __name__ == '__main__':
+ import unittest2
+ unittest2.main()
+
View
6 scripts/tests
@@ -7,11 +7,11 @@ import sys
project_root = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0]
loader = unittest.TestLoader()
-# Test
+# Run Tests
suite = loader.discover('tests', pattern='*_test.py', top_level_dir=project_root)
unittest.TextTestRunner(verbosity=1).run(suite)
-# Examples
-suite = loader.discover('tests/examples', pattern='*.py', top_level_dir=project_root)
+# Run examples
+suite = loader.discover('examples', pattern='*.py', top_level_dir=project_root)
unittest.TextTestRunner(verbosity=1).run(suite)
Please sign in to comment.
Something went wrong with that request. Please try again.