Coverage for CIResults/forms.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-23 13:11 +0000

1from django import forms 

2from django.db import transaction 

3from django.utils.functional import cached_property 

4 

5from collections import OrderedDict 

6 

7from .models import Test 

8 

9 

10class TestMassRenameForm(forms.Form): 

11 substring_from = forms.CharField() 

12 substring_to = forms.CharField() 

13 

14 @cached_property 

15 def affected_tests(self): 

16 tests = OrderedDict() 

17 

18 if self.is_valid(): 

19 substring_from = self.cleaned_data.get('substring_from') 

20 substring_to = self.cleaned_data.get('substring_to') 

21 

22 if substring_from is not None and substring_to is not None: 

23 tmp = Test.objects.order_by('testsuite__name', 'name').prefetch_related('testsuite') 

24 for test in tmp.filter(name__contains=substring_from): 

25 new_name = test.name.replace(substring_from, substring_to) 

26 tests[test] = new_name 

27 

28 return tests 

29 

30 @transaction.atomic 

31 def do_renaming(self): 

32 if self.is_valid(): 

33 for test, new_name in self.affected_tests.items(): 

34 test.rename(new_name)