Commits

Anonymous committed afc1387

Tests refactoring #2.

Comments (0)

Files changed (1)

 ################################################################################
 
 class TestStoreInit(TestCaseWithSpec):
+    context = "Method Store.__init__()"
+
     def test_recognizes_all_standard_indexes(self):
         score = CheesecakeScore(cheesecake_run_output)
 
         check_subindices(score.code_kwalitee, sub_scores_code_kwalitee)
 
 
-class TestReadCheesecakeRuns(TestCaseWithSpec):
-    context = "Method _read_cheesecake_runs()"
-
+class TestCaseUsingReadCheesecakeRuns(TestCaseWithSpec):
+    """Use StoreMock to init Store object and override os.listdir without
+    fear of consequences.
+    """
     class StoreMock(Store, Mock):
         def __init__(self):
             Mock.__init__(self)
             self._runs_failure = []
             self._runs_success = []
             self.directory = '/log_directory'
+            self.closed = False
 
     def setup(self):
         self.original_os_listdir = os.listdir
     def teardown(self):
         os.listdir = self.original_os_listdir
 
+
+class TestReadCheesecakeRuns(TestCaseUsingReadCheesecakeRuns):
+    context = "Method Store._read_cheesecake_runs()"
+
     def test_doesnt_call_save_run_for_empty_log_directory(self):
-        store = self.StoreMock()
         os.listdir = returning([])
 
+        store = self.StoreMock()
         store._read_cheesecake_runs()
+
         check_calls_count(store, 'save_run', exactly=0)
 
     def test_calls_save_run_four_times_for_four_lognames_in_log_directory(self):
-        store = self.StoreMock()
         os.listdir = returning(['1-twill==0.8.5-success-42.log',
                                 '2-PIL==1.1.5-failure-42.log',
                                 '3-PIL==1.1.5-failure-42.log',
                                 '4-setuptools==0.6c1-success-42.log'])
 
+        store = self.StoreMock()
         store._read_cheesecake_runs()
+
         check_calls_count(store, 'save_run', exactly=4)
 
     def test_calls_save_run_three_times_for_three_lognames_with_successive_numbers(self):
-        store = self.StoreMock()
         os.listdir = returning(['12-rope==0.2-success-42.log',
                                 '13-ctypes==1.0.0-failure-42.log',
                                 '101-chimera==0.4.4-failure-42.log'])
 
+        store = self.StoreMock()
         store._read_cheesecake_runs()
+
         check_calls_count(store, 'save_run', exactly=3)
 
     def test_ignores_files_that_doesnt_match_cheesecake_logfile_pattern(self):
-        store = self.StoreMock()
         os.listdir = returning(['foobar',
                                 '1-twill==0.8.5-success-42.log',
                                 'some random file with spaces',
                                 'different.log'])
 
+        store = self.StoreMock()
         store._read_cheesecake_runs()
+
         check_calls_count(store, 'save_run', exactly=1)
 
     def test_calls_save_run_with_appropriate_cheesecake_run_object(self):
-        store = self.StoreMock()
         os.listdir = returning(['1-twill==0.8.5-success-42.log'])
 
         def check_run(mock, call, count):
                    and run.result == 'success' \
                    and run.execution_time == 42
 
+        store = self.StoreMock()
         store.mockSetExpectation('save_run', check_run)
         store._read_cheesecake_runs()
 
-class TestRunsCount(TestCaseWithSpec):
-    context = "Method _get_runs_count()"
-
-    class StoreMock(Store):
-        def __init__(self):
-            self.closed = False
-
-    def fill_with_keys(self, keys):
-        d = {}
-        for k in keys:
-            d[k] = 'whatever'
-        return d
+class TestRunsCount(TestCaseUsingReadCheesecakeRuns):
+    context = "Method Store._get_runs_count()"
 
     def test_returns_one_for_no_runs(self):
+        os.listdir = returning([])
+
         store = self.StoreMock()
-        store._runs_by_number = {}
+        store._read_cheesecake_runs()
 
         self.assertEqual(store.runs_count, 1)
 
     def test_returns_five_for_four_successive_runs_1_2_3_and_4(self):
+        os.listdir = returning(['1-twill==0.8.5-success-42.log',
+                                '2-PIL==1.1.5-failure-42.log',
+                                '3-PIL==1.1.5-failure-42.log',
+                                '4-setuptools==0.6c1-success-42.log'])
+
         store = self.StoreMock()
-        store._runs_by_number = self.fill_with_keys(range(1, 5))
+        store._read_cheesecake_runs()
 
         self.assertEqual(store.runs_count, 5)
 
     def test_returns_102_for_three_runs_and_last_with_number_101(self):
+        os.listdir = returning(['12-rope==0.2-success-42.log',
+                                '13-ctypes==1.0.0-failure-42.log',
+                                '101-chimera==0.4.4-failure-42.log'])
+
         store = self.StoreMock()
-        store._runs_by_number = self.fill_with_keys([12, 13, 101])
+        store._read_cheesecake_runs()
 
         self.assertEqual(store.runs_count, 102)
 
-class TestCaseWithTmpDir(unittest.TestCase):
-    def setUp(self):
-        self.tmp_dir = tempfile.mkdtemp()
+class TestGetFailures(TestCaseUsingReadCheesecakeRuns):
+    context = "Method Store._get_failures()"
 
-    def tearDown(self):
-        shutil.rmtree(self.tmp_dir)
+    def test_returns_empty_list_for_successive_lognames(self):
+        os.listdir = returning(['12-rope==0.2-success-42.log',
+                                '1-twill==0.8.5-success-42.log'])
 
-class ReadFailuresTest(TestCaseWithTmpDir):
-    """Test for Store._get_failures.
-    """
-    def _run_test_set(self, lognames, failures):
-        create_empty_files_in_directory(lognames, self.tmp_dir)
-        store = Store(directory=self.tmp_dir)
-        self.assertEqual(set(store.failures), set(failures))
+        store = self.StoreMock()
+        store._read_cheesecake_runs()
 
-    def test_set_1(self):
-        self._run_test_set(successive_lognames,
-                           filter(lambda log: 'failure' in log,
-                                  successive_lognames))
+        self.assertEqual(store.failures, [])
 
-    def test_set_2(self):
-        lognames = [
-            '12-rope==0.2-success-42.log',
-            '1-twill==0.8.5-success-42.log',
-        ]
-        self._run_test_set(lognames, [])
+    def test_returns_only_logs_with_failure_status(self):
+        store = self.StoreMock()
+        os.listdir = returning(['1-twill==0.8.5-success-42.log',
+                                '2-PIL==1.1.5-failure-42.log',
+                                '3-PIL==1.1.5-failure-42.log',
+                                '4-setuptools==0.6c1-success-42.log'])
+
+        store._read_cheesecake_runs()
+        self.assertEqual(set(store.failures),
+                         set(['2-PIL==1.1.5-failure-42.log',
+                              '3-PIL==1.1.5-failure-42.log']))
 
 
 class TestCaseWithTestDatbase(unittest.TestCase):