[damned-lies] Replaced mocked_checkout by real mock patching



commit f9508d1ff44799fd4a47d42c22baad6bbe8b3985
Author: Claude Paroz <claude 2xlibre net>
Date:   Fri Oct 26 09:37:35 2018 +0200

    Replaced mocked_checkout by real mock patching

 stats/tests/tests.py    | 22 +++++++++-------------
 vertimus/tests/tests.py |  7 +++++--
 2 files changed, 14 insertions(+), 15 deletions(-)
---
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index d75e9bf2..0a708694 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -5,6 +5,7 @@ import tempfile
 from datetime import date
 from pathlib import Path
 from unittest import skipUnless
+from unittest.mock import patch
 
 from django.conf import settings
 from django.contrib.auth.models import User
@@ -31,13 +32,6 @@ except ImportError:
     has_translate_subtitle_support = False
 
 
-def mocked_checkout(branch):
-    """ Replace real vcs checkout by a mocked method """
-    if branch.name == 'trunk2':
-        raise
-    return
-
-
 class ModuleTestCase(TestCase):
     fixtures = ['sample_data.json']
     SYS_DEPENDENCIES = (
@@ -102,7 +96,10 @@ class ModuleTestCase(TestCase):
         br = Branch(module=mod, name='master')
         with patch_shell_command() as cmds:
             br.checkout()
-        self.assertTrue('git clone https://gitlab.gnome.org/GNOME/eog.git %s/git/eog' % settings.SCRATCHDIR 
in cmds)
+        self.assertTrue(
+            'git clone https://gitlab.gnome.org/GNOME/eog.git %s/git/eog' % settings.SCRATCHDIR in cmds,
+            "%s is not the expected 'git clone' command." % cmds[0]
+        )
 
     def test_branch_methods(self):
         branch = Branch(module=self.mod, name='master')
@@ -210,11 +207,11 @@ class ModuleTestCase(TestCase):
         self.assertFalse(checkout_path.exists())
 
     def test_create_unexisting_branch(self):
-        """ Try to create a non-existing branch """
+        """Creating a non-existing branch should raise a ValidationError."""
         Branch.checkout_on_creation = True
-        Branch.checkout = mocked_checkout
-        branch = Branch(name="trunk2", module=self.mod)
-        self.assertRaises(ValidationError, branch.clean)
+        with patch('stats.tests.tests.Branch.checkout', side_effect=OSError()):
+            branch = Branch(name="trunk2", module=self.mod)
+            self.assertRaises(ValidationError, branch.clean)
 
     def test_edit_branches_form(self):
         coord = User.objects.get(username='coord')
@@ -473,7 +470,6 @@ class TestModuleBase(TestCase):
     def setUpTestData(cls):
         super().setUpTestData()
         Branch.checkout_on_creation = False
-        Branch.checkout = mocked_checkout
         cls.mod = Module.objects.create(
             name="testmodule", vcs_type='git',
             bugs_base='https://gitlab.gnome.org/GNOME/testmodule/issues',
diff --git a/vertimus/tests/tests.py b/vertimus/tests/tests.py
index 70642404..407affa6 100644
--- a/vertimus/tests/tests.py
+++ b/vertimus/tests/tests.py
@@ -2,6 +2,7 @@ import os
 import shutil
 import tempfile
 from pathlib import Path
+from unittest.mock import patch
 from xml.dom.minidom import parseString
 
 from django.conf import settings
@@ -800,7 +801,8 @@ class DocsBuildingTests(TeamsAndRolesMixin, TestModuleBase):
             action.apply_on(state, {'send_to_ml': action.send_mail_to_ml, 'comment': "Done by translator."})
         self.assertTrue(action.can_build)
         self.assertIsNone(action.build_url)
-        response = self.client.post(reverse('action-build-help', args=[action.pk]))
+        with patch('stats.models.Branch.checkout'):
+            response = self.client.post(reverse('action-build-help', args=[action.pk]))
         self.assertRedirects(
             response, '/HTML/%d/index.html' % action.pk, fetch_redirect_response=False
         )
@@ -825,7 +827,8 @@ class DocsBuildingTests(TeamsAndRolesMixin, TestModuleBase):
             action.apply_on(state, {'send_to_ml': action.send_mail_to_ml, 'comment': "Done by translator."})
         self.assertTrue(action.can_build)
         self.assertIsNone(action.build_url)
-        response = self.client.post(reverse('action-build-help', args=[action.pk]))
+        with patch('stats.models.Branch.checkout'):
+            response = self.client.post(reverse('action-build-help', args=[action.pk]))
         self.assertRedirects(
             response, '/HTML/%d/index.html' % action.pk, fetch_redirect_response=False
         )


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]