class StudentTest(TestCase): fixtures = ['initial_data.json'] def setUp(self): self.client = Client() def test_login(self): response = self.client.get('/groups/') self.assertNotEqual(response.status_code, 200) response = self.client.post('/accounts/login/', {'username': '******', 'password': '******'}) self.assertEqual(response.status_code, 302) response = self.client.get('/groups/') self.assertEqual(response.status_code, 200) def test_add_group(self): self.group = Group( user=User.objects.get(id=1), name='Test group' ) self.group.save() group = Group.objects.get(name='Test group') self.assertEqual(group.id, self.group.id) return self.group def test_add_student_to_group(self): student = Student.objects.get(id=1) student.group = self.test_add_group() student.save()
def _add_object_to_db(self, name): if name == 'student': student = Student( first_name=self._random_value('first_name'), last_name=self._random_value('last_name'), ticket=self._random_value(), birthday=timezone.now(), ) student.save() if name == 'group': group = Group( title=self._random_value('title'), notes='note about group', ) group.save() if name == 'user': user = User( username=self._random_value('username'), first_name=self._random_value('first_name'), last_name=self._random_value('last_name'), email=self._random_value("email") + '@example.com', ) # User model have an unique field 'username'. # Generated 'username' can be not unique # in compare with existing objects. # In case of exception repeat creating object 'user'. try: user.save() except: self._add_object_to_db(name)
def test_str(self): group = Group(title='Some Title') self.assertEqual(str(group), 'Some Title') student = Student(id=1, first_name='Test', last_name='Student') group2 = Group(title='Another Title', leader=student) self.assertEqual(str(group2), 'Another Title (Test Student)')
def setUp(self): self.student = Student(first_name="Demo", last_name="Student") self.group = Group(title="Demo Group") self.time_now = datetime.now() self.exam = Exam(name="Math", date=self.time_now, teacher_name="Visiliy Ivanov", exam_group=self.group)
class ModelsTest(TestCase): def setUp(self): self.student = Student(first_name="Demo", last_name="Student") self.group = Group(title="Demo Group") self.time_now = datetime.now() self.exam = Exam(name="Math", date=self.time_now, teacher_name="Visiliy Ivanov", exam_group=self.group) def test_student_unicode(self): # check unicode string of Student model self.assertEqual(unicode(self.student), u"Demo Student") def test_group_unicode(self): # check unicode string of Group model self.assertEqual(unicode(self.group), "Demo Group") self.group.leader = self.student self.assertEqual(unicode(self.group), "Demo Group (Demo Student)") def test_journal_unicode(self): # check unicode string of Journal model student_journal = MonthJournal(student_name=self.student, date=date(datetime.today().year, datetime.today().month, 1)) self.assertEqual( unicode(student_journal), "Student: %d %d" % (date.today().month, date.today().year)) def test_exam_unicode(self): # check unicode string of Exam model self.assertEqual( unicode(self.exam), "Math (Demo Group, %s), completed - False" % self.time_now.date()) def test_result_unicode(self): result = Result(result_student=self.student, result_exam=self.exam, score=10) self.assertEqual(unicode(result), "Student Demo (Math %s)" % self.time_now.date()) def test_log_unicode(self): self.group.save() log = LogEntry.objects.all()[0] self.assertEqual( unicode(log), "INFO Group Model is added (%s)" % datetime.strftime(log.log_datetime, '%Y-%m-%d %H:%M:%S'))
def test_log_exam_change_groups(self): group = Group(title='ASD') group.save() exam = Exam(title='Spanish', exam_date=date(2013,1,1), presenter='Petro P.') exam.save() exam.exam_group.add(group) exam.save() self.out.seek(0) info_str = "exam {} have next groups after modified:\n".format(exam) for exam_group in Exam.objects.filter(title='Spanish')[0].exam_group.all(): info_str += exam_group.title + '\n' out_str = ''.join(self.out.readlines()[-4:-2]) self.assertEqual(out_str, info_str)
def test_str(self): exam = Exam(subject='Test', date='2017-11-11') self.assertEqual(str(exam), 'Test (2017-11-11)') group = Group(title='Some Title') exam2 = Exam(subject='Test2', group=group, date='2017-10-10') self.assertEqual(str(exam2), 'Some Title, Test2 (2017-10-10)')
def test_add_group(self): self.group = Group( user=User.objects.get(id=1), name='Test group' ) self.group.save() group = Group.objects.get(name='Test group') self.assertEqual(group.id, self.group.id) return self.group
def setUp(self): # add our root handler self.out = StringIO() self.handler = logging.StreamHandler(self.out) logging.root.addHandler(self.handler) self.time_now = datetime.now() self.group = Group(title='DemoGroup') self.student = Student(first_name='Demo', last_name="Student", ticket='1', birthday='1980-01-01', student_group=self.group) self.exam = Exam(name="Math", date=self.time_now, teacher_name="Visiliy Ivanov", exam_group=self.group) self.result = Result(result_student=self.student, result_exam=self.exam, score=10)
def handle(self, *args, **options): if options['groups'] is not None: for i in range(1, int(options['groups']) + 1): group = Group.objects.get_or_create(title="Start%s" % i) if options['students'] is not None: if Group.objects.count() == 0: group = Group.objects.get_or_create(title="Start1") for i in range(1, int(options['students']) + 1): group_for_student = Group.objects.all()[min( i, Group.objects.count())] student = Student.objects.get_or_create( first_name="Student%s" % i, last_name="Member%s" % i, birthday=datetime.today().date() - relativedelta(years=17 + i), ticket="12%s" % (i + 1), student_group=group_for_student) if options['exams'] is not None: if Group.objects.count() == 0: group = Group(title="Start1") group.save() for i in range(1, int(options['exams']) + 1): group_for_exam = Group.objects.all()[min( i, Group.objects.count())] exam = Exam.objects.get_or_create(name="Exam%s" % i, date=datetime.today(), teacher_name="Teacher%s" % i, exam_group=group_for_exam) if options['users'] is not None: for i in range(1, int(options['users']) + 1): try: user_old = User.objects.get(username='******' % i) except: user = User.objects.create_user(username='******' % i, email='*****@*****.**' % i, password='******', is_active=True)
def test_log_group_deleted_event(self): """Check logging signal for deleted group""" # add own root handler to catch group signals output out = StringIO() handler = logging.StreamHandler(out) logging.root.addHandler(handler) # now create and delete group, this should raise new message inside # our logger output file group = Group(title='Demo Group') group.save() group_id = group.id group.delete() # check output file content out.seek(0) self.assertEqual(out.readlines()[-1], "Group deleted: Demo Group (ID: %d)\n" % group_id) # remove our handler from root logger logging.root.removeHandler(handler)
def test_log_group_updated_added_event(self): """Check logging signal for newly created group""" # add own root handler to catch group signals output out = StringIO() handler = logging.StreamHandler(out) logging.root.addHandler(handler) # now create group, this should raise new message inside # our logger output file group = Group(title='Demo Group') group.save() # check output file content out.seek(0) self.assertEqual(out.readlines()[-1], "Group created: Demo Group (ID: %d)\n" % group.id) # now update existing student and check last line in out group.notes = 'some group note' group.save() out.seek(0) self.assertEqual(out.readlines()[-1], "Group updated: Demo Group (ID: %d)\n" % group.id) # remove our handler from root logger logging.root.removeHandler(handler)
def test_log_group_delete(self): group = Group(title='The Best Title') group.save() group_id = group.id group_title = group.title group.delete() self.out.seek(0) self.assertEqual(self.out.readlines()[-1].strip(), "Group deleted: {0} (ID: {1})".format(group_title, group_id))
def test_log_group_add_update_event(self): student = Student(first_name='Demo', last_name='Student') student.save() group = Group(title='TTE') group.save() self.out.seek(0) self.assertEqual(self.out.readlines()[-1], "Group added: %s (ID: %d)\n" % (group.title, group.id)) group.leader = student group.save() self.out.seek(0) self.assertEqual(self.out.readlines()[-1].strip(), "Group edited: {} (ID: {})".format(group.title, group.id))
def test_log_exam_add_update(self): group = Group(title='G-TTY') group.save() exam = Exam(title='Solphegio', exam_date=date(2016,5,12), presenter='Fedir Ivanovich') exam.save() self.out.seek(0) self.assertEqual(self.out.readlines()[-1].strip(), "Exam created: %s (ID: %d)" % (exam.title, exam.id)) exam.exam_group.add(group) exam.save() self.assertEqual(exam.exam_group ,Exam.objects.filter(title='Solphegio')[0].exam_group) group2 = Group(title='Phisics') group2.save() exam.exam_group.add(group2) exam.save() self.out.seek(0) self.assertEqual(self.out.readlines()[-1].strip(), "Exam updated: %s (ID: %d)" % (exam.title, exam.id))
def test_log_group_deleted_event(self): """Check logging group for deleted student""" group = Group(title='Demo Group 1') group.save() # now override signal # add own root handler to catch student signals output out = StringIO() handler = logging.StreamHandler(out) logging.root.addHandler(handler) # delete existing group and check logger output sid = group.id group.delete() out.seek(0) self.assertEqual(out.readlines()[-1], 'Group Demo Group 1 deleted (ID: %d)\n' % sid) # remove our handler from root logger logging.root.removeHandler(handler)
def test_log_group_updated_added_event(self): """Check logging group for newly created student""" # add own root handler to catch student signals output out = StringIO() handler = logging.StreamHandler(out) logging.root.addHandler(handler) # now create group, this should raise new message inside # our logger output file group = Group(title='Demo Group 1') group.save() # check output file content out.seek(0) self.assertEqual(out.readlines()[-1], 'Group Demo Group 1 added (ID: %d)\n' % group.id) # now update existing group and check last line in out group.title = 'Demo Group 2' group.save() out.seek(0) self.assertEqual(out.readlines()[-1], 'Group Demo Group 2 updated (ID: %d)\n' % group.id) # remove our handler from root logger logging.root.removeHandler(handler)
def handle(self, *args, **options): self.stdout.write( u'Function of automatic filling of the Database.' \ u' How many objects need to create? ') # students if 'students' in args: number = int(raw_input( u'Input the number of students: ')) for i in range(number): student = Student( first_name=random.choice(self.first_names), last_name=random.choice(self.last_names), middle_name=random.choice(self.middle_names), ticket=random.choice( self.ticket) + random.choice(self.ticket), student_group=Group.objects.all()[random.randint( 0, Group.objects.count() - 1)], birthday=datetime.today()) student.save() self.stdout.write( u'Objects %s create in database' % student) # groups if 'groups' in args: number = int(raw_input( u'Input the number of groups: ')) for i in range(number): group = Group( title=random.choice(self.title), leader=Student.objects.all()[random.randint( 0, Student.objects.count() - 1)], ) group.save() self.stdout.write( u'Objects %s create in database' % group) # exams if 'exams' in args: number = int(raw_input( u'Input the number of exams: ')) for i in range(number): exam = Exam( science=random.choice(self.science), teacher=random.choice(self.teacher_names), date=datetime.today(), groups=Group.objects.all()[random.randint( 0, Group.objects.count() - 1)]) exam.save() self.stdout.write( u'Objects %s create in database' % exam) # results if 'results' in args: number = int(raw_input( u'Input the number of exam results ')) for i in range(number): result = Result( exams=Exam.objects.all()[random.randint( 0, Exam.objects.count() - 1)], groups=Group.objects.all()[random.randint( 0, Group.objects.count() - 1)], students=Student.objects.all()[random.randint( 0, Student.objects.count() - 1)], result=random.choice(self.results)) result.save() self.stdout.write( u'Objects %s create in database' % result)
def handle(self, *args, **options): for name in self.models: if options[name] and not (0 < options[name] < 11): raise CommandError( 'ERROR! Number of %s objects must be in range 1..10!' % name) for name in self.models: if options[name]: for i in range(options[name]): r1, r2, r3 = randint(1, 9999), randint(1, 9999), randint( 1, 9999) if name == 'student': student = Student( first_name='Name-{r1:04}-{r2:04}-{r3:04}'.format( r1=r1, r2=r2, r3=r3), last_name='Surname-{r1:04}-{r2:04}-{r3:04}'.format( r1=r1, r2=r2, r3=r3), ticket=i + 1, birthday=timezone.now()) try: student.save() except Exception as e: self.stdout.write( 'ERROR! Student %s not saved!\n%s' % (student, e.message)) else: self.stdout.write( 'Student %s created successfully' % student) if name == 'group': group = Group( title='MtM-{r1:04}-{r2:04}-{r3:04}'.format(r1=r1, r2=r2, r3=r3), notes='Note-{r1:04}-{r2:04}-{r3:04}'.format(r1=r1, r2=r2, r3=r3)) try: group.save() except Exception as e: self.stdout.write( 'ERROR! Group %s not saved!\n%s' % (group, e.message)) else: self.stdout.write('Group %s created successfully' % group) if name == 'user': user = User( username='******'.format( r1=r1, r2=r2, r3=r3), first_name='Name-{r1:04}-{r2:04}-{r3:04}'.format( r1=r1, r2=r2, r3=r3), last_name='Surname-{r1:04}-{r2:04}-{r3:04}'.format( r1=r1, r2=r2, r3=r3), email='email-{r1:04}-{r2:04}-{r3:04}@example.com'. format(r1=r1, r2=r2, r3=r3)) try: user.save() except Exception as e: self.stdout.write('ERROR! User %s not saved!\n%s' % (user, e.message)) else: self.stdout.write('User %s created successfully' % user.username)
class SignalsTest(TestCase): def setUp(self): # add our root handler self.out = StringIO() self.handler = logging.StreamHandler(self.out) logging.root.addHandler(self.handler) self.time_now = datetime.now() self.group = Group(title='DemoGroup') self.student = Student(first_name='Demo', last_name="Student", ticket='1', birthday='1980-01-01', student_group=self.group) self.exam = Exam(name="Math", date=self.time_now, teacher_name="Visiliy Ivanov", exam_group=self.group) self.result = Result(result_student=self.student, result_exam=self.exam, score=10) def tearDown(self): # remove root handler logging.root.removeHandler(self.handler) def test_update_student(self): # create new group and student self.group.save() self.student.save() # test signal of created students self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Student added: Demo Student (ID: %s)\n' % self.student.id) self.student.ticket = 5 self.student.save() # test signal of updated students self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Student updated: Demo Student (ID: %s)\n' % self.student.id) def test_delete_student(self): # create new group and student self.group.save() self.student.save() # test signal of deleted students student_id = self.student.id self.student.delete() self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Student deleted: Demo Student (ID: %s)\n' % student_id) def test_update_group(self): # create new group self.group.save() # test signal of created students self.out.seek(0) self.assertEqual(self.out.readlines()[-1], 'Group added: DemoGroup (ID: %s)\n' % self.group.id) self.group.notes = 'abc' self.group.save() # test signal of updated groups self.out.seek(0) self.assertEqual(self.out.readlines()[-1], 'Group updated: DemoGroup (ID: %s)\n' % self.group.id) def test_delete_group(self): # create new group self.group.save() # test signal of deleted groups group_id = self.group.id self.group.delete() self.out.seek(0) self.assertEqual(self.out.readlines()[-1], 'Group deleted: DemoGroup (ID: %s)\n' % group_id) def test_update_exam(self): # create new group and exam self.group.save() self.exam.save() # test signal of created exams self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Exam added: Math for DemoGroup (ID: %s)\n' % self.exam.id) self.exam.teacher_name = 'Vasiliy Ivanov' self.exam.save() # test signal of updated exams self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Exam updated: Math for DemoGroup (ID: %s)\n' % self.exam.id) def test_delete_exam(self): # create new group and exam self.group.save() self.exam.save() # test signal of deleted exams exam_id = self.exam.id self.exam.delete() self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Exam deleted: Math for DemoGroup (ID: %s)\n' % exam_id) def test_update_result(self): # create new group, student, exam and result self.group.save() self.student.save() self.exam.save() self.result.save() # test signal of created results self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Result added: Student Demo Student got mark 10 for Math (ID: %s)\n' % self.result.id) self.result.score = 11 self.result.save() # test signal of updated results self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Result updated: Student Demo Student got mark 11 for Math (ID: %s)\n' % self.result.id) def test_delete_result(self): # create new group, student, exam and result self.group.save() self.student.save() self.exam.save() self.result.save() # test signal of deleted results result_id = self.result.id self.result.delete() self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Result deleted: Student Demo Student got mark 10 for Math (ID: %s)\n' % result_id) def test_update_journal(self): # create new group, student and journal self.group.save() self.student.save() student_journal = MonthJournal(student_name=self.student, date=date(datetime.today().year, datetime.today().month, 1)) student_journal.save() month_name = [ u'Січень', u'Лютий', u'Березень', u'Квітень', u'Травень', u'Червень', u'Липень', u'Серпень', u'Вересень', u'Жовтень', u'Листопад', u'Грудень' ] # test signal of created journals self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Journal added: Student Demo Student for %s (ID: %s)\n' % (month_name[student_journal.date.month - 1], student_journal.id)) student_journal.score = 11 student_journal.save() # test signal of updated journals self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Journal updated: Student Demo Student for %s (ID: %s)\n' % (month_name[student_journal.date.month - 1], student_journal.id)) def test_delete_journal(self): # create new group, student and journal self.group.save() self.student.save() student_journal = MonthJournal(student_name=self.student, date=date(datetime.today().year, datetime.today().month, 1)) student_journal.save() month_name = [ u'Січень', u'Лютий', u'Березень', u'Квітень', u'Травень', u'Червень', u'Липень', u'Серпень', u'Вересень', u'Жовтень', u'Листопад', u'Грудень' ] # test signal of deleted journals student_journal_id = student_journal.id student_journal.delete() self.out.seek(0) self.assertEqual( self.out.readlines()[-1], 'Journal deleted: Student Demo Student for %s (ID: %s)\n' % (month_name[student_journal.date.month - 1], student_journal_id)) def test_user_update(self): # create new user user = User.objects.create_user(username='******') # test signal of created users self.out.seek(0) self.assertEqual(self.out.readlines()[-1], 'User added: trivvet2\n') # test signal of updated users user.password = '******' user.save() self.out.seek(0) self.assertEqual(self.out.readlines()[-1], 'User updated: trivvet2\n') def test_user_deleted(self): # create new user user = User.objects.create_user(username='******') # test signal of deleted users user.delete() self.out.seek(0) self.assertEqual(self.out.readlines()[-1], 'User deleted: trivvet2\n')