def test_save_processes_keywords_correctly(self): """ Saving the form should update the keywords correctly. - Removed keywords should be removed - New keywords should be added - Remaining keywords should remain """ user = UserFactory.create() task = TaskFactory.create() TaskKeywordFactory.create_batch(3, task=task) data = { 'keywords': 'test3, new_keyword', 'team': task.team.id, } for field in ('name', 'short_description', 'execution_time', 'difficulty', 'repeatable', 'instructions', 'is_draft'): data[field] = getattr(task, field) form = TaskForm(instance=task, data=data) form.save(user) removed_keyword = TaskKeyword.objects.filter(task=task, name='test1') eq_(len(removed_keyword), 0) added_keyword = TaskKeyword.objects.filter(task=task, name='new_keyword') eq_(len(added_keyword), 1) kept_keyword = TaskKeyword.objects.filter(task=task, name='test3') eq_(len(kept_keyword), 1) # double-check on the keywords_list property eq_(task.keywords_list, 'test3, new_keyword')
def get_forms(self): kwargs = {'initial': None} if self.request.method == 'POST': kwargs['data'] = self.request.POST batch_form = TaskImportBatchForm(instance=None, prefix='batch', **kwargs) criterion_formset = TaskInvalidCriteriaFormSet(prefix='criterion', **kwargs) kwargs['initial'] = { 'end_date': date.today() + timedelta(days=30), 'repeatable': False, 'owner': self.request.user } task_form = TaskForm(instance=None, prefix='task', **kwargs) forms = { 'criterion_formset': criterion_formset, 'batch_form': batch_form, 'task_form': task_form } # Create a hidden form for each possible PreviewConfirmationForm stage. # These forms are used to signal what the next stage should be. make_stage = lambda x: PreviewConfirmationForm(data={'stage': x}) stages = PreviewConfirmationForm.submission_stages forms.update({'stage_form__' + s: make_stage(s) for s in stages}) return forms
def test_initial_contains_empty_list_of_keywords_for_new_task(self): """ Initial should contain an empty list of keywords for a new task. """ task = TaskFactory.create() form = TaskForm(instance=task) eq_(form.initial['keywords'], '')
def test_initial_contains_list_of_keywords_for_existing_task(self): """ Initial should contain the list of keywords from the task instance. """ task = TaskFactory.create() TaskKeywordFactory.create_batch(3, task=task) form = TaskForm(instance=task) eq_(form.initial['keywords'], 'test1, test2, test3')
def test_save_does_not_add_a_blank_keyword(self): """ Saving the form should not add a blank keyword when keywords are empty. """ user = UserFactory.create() task = TaskFactory.create() data = { 'keywords': ' ', 'team': task.team.id, } for field in ('name', 'short_description', 'execution_time', 'difficulty', 'repeatable', 'instructions', 'is_draft'): data[field] = getattr(task, field) form = TaskForm(instance=task, data=data) form.save(user) eq_(task.keyword_set.count(), 0)
def test_form_widgets_have_expected_class(self): """ Classes specified in attrs in form definition should be populated into the widgets """ form = TaskForm(instance=None) for field in ('name', 'short_description', 'instructions', 'why_this_matters', 'prerequisites'): eq_(form[field].field.widget.attrs['class'], 'fill-width')
def get_filled_taskform(task, **kwargs): """ Returns a TaskForm populated with initial task fields and additional fields provided as keyword arguments. Additional fields overwrite any matching initial fields. """ data = {'team': task.team.id} for field in ('name', 'short_description', 'execution_time', 'difficulty', 'repeatable', 'instructions', 'is_draft'): data[field] = getattr(task, field) data.update(kwargs) return TaskForm(instance=task, data=data)