Commits

Vladimyr Pavlenko  committed f8c0829

Mock

  • Participants
  • Parent commits 80b11d6

Comments (0)

Files changed (3)

File friday_skiil_up/settings.py

 
 DATABASES = {
     'default': {
-        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
+        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
         'NAME': '',                      # Or path to database file if using sqlite3.
         'USER': '',                      # Not used with sqlite3.
         'PASSWORD': '',                  # Not used with sqlite3.

File library/tests.py

-"""
-This file demonstrates writing tests using the unittest module. These will pass
-when you run "manage.py test".
+# -*- coding: utf-8 -*-
 
-Replace this with more appropriate tests for your application.
-"""
+import __builtin__
 
 from django.test import TestCase
+from mock import patch, Mock, MagicMock
+import library.utils
+from datetime import datetime
 
+def get_mock_file():
+    mock_file = Mock()
+    mock_file.name = 'fake.dat'
+    mock_file.__iter__ =  lambda k: iter(xrange(10))
+    stat = mock_file.stat.return_value
+    stat.size, stat.access_time = 500, datetime(2012, 12, 29)
+    return mock_file
 
-class SimpleTest(TestCase):
-    def test_basic_addition(self):
-        """
-        Tests that 1 + 1 always equals 2.
-        """
-        self.assertEqual(1 + 1, 2)
+class MockTest(TestCase):
+
+    def test_patch(self):
+
+        with patch('library.utils.big_range') as perm_mock:
+            perm_mock.return_value = xrange(3)
+            library.utils.long_cycle()
+        # library.utils.long_cycle()
+
+
+    def test_object_spy(self):
+        mobj = Mock()
+
+        library.utils.object_manipulator(mobj)
+
+        attrs = ['called', 'call_count', 'call_args', 'call_args_list', 'method_calls', 'mock_calls']
+
+        for attr in attrs:
+            print attr, " => ", getattr(mobj, attr)
+
+
+    def test_mock_file(self):
+        f = get_mock_file()
+        for l in f:
+            print l
+
+    def test_builtin(self):
+        mock = MagicMock(return_value = ["qwe", "qwe"])
+        with patch('__builtin__.open', mock):
+            for l in open('filename', 'r'):
+                print l

File library/utils.py

 # -*- coding: utf-8 -*-
 
-from mock import patch
-import itertools  # важно: мы импортируем модуль, не сам метод
-
-
-__author__ = 'mindinpanic'
-
-from itertools import permutations
-
-def real(name):
-    if len(name) < 10:
-        raise ValueError('String too short to calculate statistics.')
+def big_range():
+    return xrange(1000000)
 
+def long_cycle():
     y = 0
-    p = permutations(xrange(len(name)), 10)
-    for i in p:
-        for j in i:
-            y += j
-            print y
-
-name = 'достаточно длинное имя'
-
+    for i in big_range():
+        y += i
+        print y
 
-@patch('itertools.permutations')
-def test(ip):
-    ip.return_value = range(5)
-    real(name)
-#    print list(itertools.permutations(xrange(10), 10))
 
-test()
+def object_manipulator(obj):
+    obj()
+    obj(1,2,3,4)
+    obj.run()
+    obj.update({'foo': 'bar'}, is_dummy=True)