Skip to content

Commit

Permalink
Added socket, and datetime examples. And fixed test runner to also ru…
Browse files Browse the repository at this point in the history
…n the examples
  • Loading branch information
vbabiy committed Feb 27, 2011
1 parent 166a438 commit 4298eb6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
Empty file added examples/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions examples/examples.py
Original file line number Diff line number Diff line change
@@ -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()

6 changes: 3 additions & 3 deletions scripts/tests
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 4298eb6

Please sign in to comment.