def test_example_sentence_re(self): test_data = self.get_test_data() test_body = """Мне 18 лет. Я — студентка Новосибирского пединститута. Две лекции, две пары подряд. Преподаватель немного опаздывает. Так хочется спать…""" test_data['text_sections'][0]['body'] = test_body self.create_text(test_data=test_data) self.assertTrue(TextSection.objects.count()) text_section = TextSection.objects.all()[0] TextPhraseTranslation.create( text_phrase=TextWord.create(phrase='опаздывает', instance=0, text_section=text_section), phrase='be late', correct_for_context=True ) text_phrase = TextPhrase.objects.get(phrase='опаздывает', text_section=text_section) self.assertTrue(text_phrase) self.assertEquals('Преподаватель немного опаздывает.', text_phrase.sentence)
def test_delete_text_with_one_student_flashcard(self): test_data = self.get_test_data() test_data['text_sections'][0]['body'] = 'заявление неделю Число' text = self.create_text(test_data=test_data) text_sections = text.sections.all() заявление = TextWord.create(phrase='заявление', instance=0, text_section=text_sections[0]) TextPhraseTranslation.create( text_phrase=заявление, phrase='statement', correct_for_context=True ) test_student = Student.objects.filter()[0] test_student.add_to_flashcards(заявление) student_flashcard_session, _ = StudentFlashcardSession.objects.get_or_create(student=test_student) text.delete() # session is deleted if there's a single flashcard self.assertFalse(StudentFlashcardSession.objects.filter(pk=student_flashcard_session.pk).exists())
def test_delete_text_with_multiple_student_flashcards(self): test_data_one = self.get_test_data() test_data_two = self.get_test_data() test_data_one['text_sections'][0]['body'] = 'заявление' test_data_two['text_sections'][0]['body'] = 'неделю' text_one = self.create_text(test_data=test_data_one) text_two = self.create_text(test_data=test_data_two) text_one_section = text_one.sections.all() text_two_section = text_two.sections.all() заявление = TextWord.create(phrase='заявление', instance=0, text_section=text_one_section[0]) неделю = TextWord.create(phrase='неделю', instance=0, text_section=text_two_section[0]) TextPhraseTranslation.create( text_phrase=заявление, phrase='statement', correct_for_context=True ) TextPhraseTranslation.create( text_phrase=неделю, phrase='a week', correct_for_context=True ) test_student = Student.objects.filter()[0] test_student.add_to_flashcards(заявление) test_student.add_to_flashcards(неделю) student_flashcard_session, _ = StudentFlashcardSession.objects.get_or_create(student=test_student) text_one.delete() # session isn't deleted if there's more than one flashcard self.assertTrue(StudentFlashcardSession.objects.filter(pk=student_flashcard_session.pk).exists()) student_flashcard_session.refresh_from_db() self.assertTrue(student_flashcard_session.current_flashcard) self.assertTrue(student_flashcard_session.current_flashcard.phrase, неделю)
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: try: text_word_add_params = json.loads(request.body.decode('utf8')) jsonschema.validate(text_word_add_params, TextWord.to_add_json_schema()) except json.JSONDecodeError as decode_error: return HttpResponse(json.dumps( {'errors': { 'json': str(decode_error) }}), status=400) except jsonschema.ValidationError as validation_error: return HttpResponse(json.dumps( {'errors': { 'json': str(validation_error) }}), status=400) try: text_word_add_params['text_section'] = TextSection.objects.get( text=text_word_add_params.pop('text'), order=text_word_add_params['text_section']) text_word = TextWord.create(**text_word_add_params) text_word_dict = text_word.to_translations_dict() text_word_dict['id'] = text_word.pk return HttpResponse(json.dumps(text_word_dict)) except DatabaseError: return HttpResponseServerError( json.dumps({'errors': 'something went wrong'}))
def put(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: try: text_word_update_params = json.loads(request.body.decode('utf8')) jsonschema.validate(text_word_update_params, TextWord.to_update_json_schema()) except json.JSONDecodeError as decode_error: return HttpResponse(json.dumps( {'errors': { 'json': str(decode_error) }}), status=400) except jsonschema.ValidationError as validation_error: return HttpResponse(json.dumps( {'errors': { 'json': str(validation_error) }}), status=400) try: text_phrase = TextPhrase.objects.get(id=kwargs['pk']) updated_grammeme_params = text_word_update_params.pop('grammemes') with transaction.atomic(): updated = text_phrase._meta.managers[0].filter( pk=text_phrase.pk).update(**updated_grammeme_params) if updated: for grammeme, grammeme_value in updated_grammeme_params.items( ): setattr(text_phrase, grammeme, grammeme_value) text_word_dict = text_phrase.to_translations_dict() text_word_dict['id'] = text_phrase.pk return HttpResponse(json.dumps(text_word_dict)) except DatabaseError: return HttpResponseServerError( json.dumps({'errors': 'something went wrong'}))
def setUp(self): super(TestFlashcardStateMachine, self).setUp() self.instructor = self.new_instructor_client(Client()) self.student = self.new_student_client(Client()) self.text_test = TestText() self.text_test.instructor = self.instructor self.text_test.student = self.student self.test_student = Student.objects.filter()[0] test_data = self.get_test_data() test_data['text_sections'][0]['body'] = 'заявление неделю Число' test_data['text_sections'][1][ 'body'] = 'заявление неделю стрельбы вещи' self.text = self.text_test.create_text(test_data=test_data) text_sections = self.text.sections.all() self.text_phrases = [] TextPhraseTranslation.create(text_phrase=TextWord.create( phrase='заявление', instance=0, text_section=text_sections[0]), phrase='statement', correct_for_context=True) TextPhraseTranslation.create(text_phrase=TextWord.create( phrase='неделю', instance=0, text_section=text_sections[0]), phrase='week', correct_for_context=True) TextPhraseTranslation.create(text_phrase=TextWord.create( phrase='стрельбы', instance=0, text_section=text_sections[1]), phrase='shooting', correct_for_context=True) TextPhraseTranslation.create(text_phrase=TextWord.create( phrase='Число', instance=0, text_section=text_sections[0]), phrase='number', correct_for_context=True) TextPhraseTranslation.create(text_phrase=TextWord.create( phrase='вещи', instance=0, text_section=text_sections[1]), phrase='number', correct_for_context=True) self.text_phrases.append(TextPhrase.objects.get(phrase='заявление')) self.text_phrases.append(TextPhrase.objects.get(phrase='неделю')) self.text_phrases.append(TextPhrase.objects.get(phrase='стрельбы')) self.text_phrases.append(TextPhrase.objects.get(phrase='Число')) self.text_phrases.append(TextPhrase.objects.get(phrase='вещи')) self.test_flashcards = [] self.test_flashcards.append( self.test_student.add_to_flashcards(self.text_phrases[0])) self.test_flashcards.append( self.test_student.add_to_flashcards(self.text_phrases[1])) self.test_flashcards.append( self.test_student.add_to_flashcards(self.text_phrases[2])) self.test_flashcards.append( self.test_student.add_to_flashcards(self.text_phrases[3])) self.test_flashcards.append( self.test_student.add_to_flashcards(self.text_phrases[4]))