Coverage for CIResults/tests/test_forms.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-19 09:20 +0000

1from django.test import TestCase 

2from unittest.mock import patch, call 

3 

4from CIResults.models import TestSuite, Test 

5 

6from CIResults.forms import TestMassRenameForm 

7 

8 

9class Test_TestMassRenameForm(TestCase): 

10 @patch('CIResults.models.Test.rename') 

11 def test_empty_form(self, mock_rename): 

12 form = TestMassRenameForm({}) 

13 

14 self.assertFalse(form.is_valid()) 

15 self.assertEqual(form.affected_tests, dict()) 

16 

17 form.do_renaming() 

18 mock_rename.assert_not_called() 

19 

20 @patch('CIResults.models.Test.rename') 

21 def test_valid_form(self, mock_rename): 

22 tests = [] 

23 ts = TestSuite.objects.create(name="testsuite", public=True) 

24 for i in range(4): 

25 name = "{}test_{}".format("old" if i % 2 == 0 else "", i) 

26 tests.append(Test.objects.create(testsuite=ts, name=name, public=True)) 

27 

28 form = TestMassRenameForm({'substring_from': 'oldtest_', 'substring_to': 'newtest_'}) 

29 self.assertTrue(form.is_valid()) 

30 self.assertEqual(form.affected_tests, {tests[0]: 'newtest_0', tests[2]: 'newtest_2'}) 

31 

32 form.do_renaming() 

33 self.assertEqual(mock_rename.call_args_list, [call('newtest_0'), call('newtest_2')])