def test_create_error_model_negative(self, kind, title, text, answer, em_title, em_text): counts = self.get_model_counts() data = self.default_data.copy() data['form-0-title'] = em_title data['form-0-text'] = em_text data.update( { 'form-TOTAL_FORMS': 1, 'unit_type': kind, 'text': text, 'title': title, 'answer_form-answer': answer, } ) # add answer (with no correct EM, so EM will not be added) response = self.post_valid_data(data) self.assertEqual(response.status_code, 200) # check that formset show error self.assertNotEqual(response.context['errors_formset'].errors, []) new_counts = self.get_model_counts() self.validate_model_counts(counts, new_counts, must_equal=False) # must be not equal # add ErrModel counts = self.get_model_counts() response = self.post_valid_data(data) self.assertEqual(response.status_code, 200) self.assertNotEqual(response.context['errors_formset'].errors, []) new_counts = self.get_model_counts() self.validate_model_counts(counts, new_counts, must_equal=True) # must be equal # check that no error models are present self.assertIsNone(UnitLesson.objects.filter(kind=UnitLesson.MISUNDERSTANDS).first())
def _get_graph_params(self, update): data = { 'filters': {}, 'labels': 'barcode', 'series': 'price', } data.update(update) return data
def _get_graph_params(self, update): data = { 'filters': { 'delivery_date__gte': '2016-01-01', 'delivery_date__lt': '2017-01-01', }, 'series': 'id', } data.update(update) return data
def test_create_error_model(self, kind, title, text, answer, em_title, em_text): counts = self.get_model_counts() data = self.default_data.copy() data['form-0-title'] = em_title data['form-0-text'] = em_text data.update( { 'form-TOTAL_FORMS': 1, 'unit_type': kind, 'text': text, 'title': title, 'answer_form-answer': answer, } ) response = self.post_valid_data(data) self.assertRedirects(response, self.url) new_counts = self.get_model_counts() self.validate_model_counts(counts, new_counts, must_equal=False) # must not be equal because we added EM # check that 1 error model is present self.assertIsNotNone(UnitLesson.objects.filter(kind=UnitLesson.MISUNDERSTANDS).first()) self.assertEqual(response.context['errors_formset'].errors, [])
def test_post_valid_data(self, kind, text, title, attachment, answer=''): counts = self.get_model_counts() data = { 'unit_type': kind, 'text': text, 'title': title, 'answer_form-answer': answer, } if attachment: with open(attachment, 'rb') as img: data.update({ 'attachment': img }) response = self.post_valid_data(data) else: response = self.post_valid_data(data) self.assertEqual(response.context['form'].errors, {}) self.assertEqual(response.context['answer_form'].errors, {}) self.assertEqual(response.context['errors_formset'].errors, []) new_counts = self.get_model_counts() if kind == EditUnitForm.KIND_CHOICES[0][0]: # ORCT # must not be equal because we added Answer self.validate_model_counts(counts, new_counts, must_equal=False) else: self.validate_model_counts(counts, new_counts, must_equal=True) # must not be equal because we added Answer ul = self.get_test_unitlesson() url = reverse('ctms:unit_edit', kwargs={ 'course_pk': self.get_test_course().id, 'courselet_pk': self.get_test_courseunit().id, 'pk': ul.id }) self.assertEqual(self.get_test_unitlesson().lesson.text, text) self.assertEqual(self.get_test_unitlesson().lesson.kind, kind) self.assertEqual(self.get_test_unitlesson().lesson.title, title) self.assertRedirects(response, url) self.check_context_keys(response)
def test_post_valid_data(self, kind, text, title, attachment, answer=''): counts = self.get_model_counts() data = { 'unit_type': kind, 'text': text, 'title': title, 'answer_form-answer': answer, } if attachment: with open(attachment) as img: data.update({ 'attachment': img }) response = self.post_valid_data(data) else: response = self.post_valid_data(data) self.assertEquals(response.context['form'].errors, {}) self.assertEquals(response.context['answer_form'].errors, {}) self.assertEquals(response.context['errors_formset'].errors, []) new_counts = self.get_model_counts() if kind == EditUnitForm.KIND_CHOICES[0][0]: # ORCT # must not be equal because we added Answer self.validate_model_counts(counts, new_counts, must_equal=False) else: self.validate_model_counts(counts, new_counts, must_equal=True) # must not be equal because we added Answer ul = self.get_test_unitlesson() url = reverse('ctms:unit_edit', kwargs={ 'course_pk': self.get_test_course().id, 'courslet_pk': self.get_test_courseunit().id, 'pk': ul.id }) self.assertEqual(self.get_test_unitlesson().lesson.text, text) self.assertEqual(self.get_test_unitlesson().lesson.kind, kind) self.assertEqual(self.get_test_unitlesson().lesson.title, title) self.assertRedirects(response, url) self.check_context_keys(response)
def test_post_invalid_data(self, kind, text, title, attachment): counts = self.get_model_counts() data = { 'unit_type': kind, 'text': text, 'title': title } if attachment: with open(attachment, 'rb') as img: data.update({ 'attachment': img }) response = self.post_valid_data(data) else: response = self.post_valid_data(data) new_counts = self.get_model_counts() self.validate_model_counts(counts, new_counts, must_equal=True) self.assertEqual( [_ for _ in response.context['form'].errors.get('attachment')], ['Upload a valid image. The file you uploaded was either not an image or a corrupted image.']) self.assertNotEqual(self.get_test_unitlesson().lesson.kind, kind) # self.assertEqual(self.get_test_unitlesson().unit.text, text) self.check_context_keys(response)
def test_post_invalid_data(self, kind, text, title, attachment): counts = self.get_model_counts() data = { 'unit_type': kind, 'text': text, 'title': title } if attachment: with open(attachment) as img: data.update({ 'attachment': img }) response = self.post_valid_data(data) else: response = self.post_valid_data(data) new_counts = self.get_model_counts() self.validate_model_counts(counts, new_counts, must_equal=True) self.assertNotEquals(response.context['form'].errors.get('attachment', []), []) self.assertNotEqual(self.get_test_unitlesson().lesson.kind, kind) # self.assertEqual(self.get_test_unitlesson().unit.text, text) self.check_context_keys(response)
def test_register_success(self, item): ''' 测试注册成功 :param item: :return: ''' data = json.loads(item.get("data")) # type: dict data.update({"mobile_phone": self.get_new_phone()}) log.debug("data=%s", data) resp = self.req.request(item.get("method"), item.get("url"), item.get("headers"), data) expected = json.loads(item.get("expected")) expected_code = expected.get("code") expected_msg = expected.get("msg") try: self.assertEqual(expected_code, resp.get("code")) self.assertEqual(expected_msg, resp.get("msg")) except AssertionError: log.exception("断言失败") # raise表示抛出当前异常,不然会造成测试通过,因为AssertionError被你捕获了 raise except Exception: log.exception("其他异常") raise
def test_delete_error_mode_delete_only_ul(self, kind, title, text, answer, em_title, em_text): """Test that when delete error model it delete only UnitLesson and not Lesson objects.""" # create error model data = self.default_data.copy() data.update( { 'form-TOTAL_FORMS': 1, 'unit_type': kind, 'text': text, 'title': title, 'answer_form-answer': answer, 'form-0-title': em_title, 'form-0-text': em_text, } ) response = self.post_valid_data(data) # delete this error model ul = UnitLesson.objects.filter(kind=UnitLesson.MISUNDERSTANDS).first() self.assertIsNotNone(ul) data.update({ 'form-0-DELETE': 'on', 'form-0-id': ul.lesson.id, 'form-0-ul_id': ul.id, }) counts_n = self.get_model_counts() response = self.post_valid_data(data) self.assertRedirects(response, self.url) new_counts = self.get_model_counts() self.validate_model_counts(counts_n, new_counts, must_equal=False) self.assertIsNone(UnitLesson.objects.filter(kind=UnitLesson.MISUNDERSTANDS, id=ul.id).first()) self.assertIsNotNone(Lesson.objects.filter(id=ul.lesson.id).first())