def test_list(self): """ Should provide skills flatter overview """ _skill1 = SkillFactory(name='Skill1') _skill2 = SkillFactory(name='Skill2') _additional_category = CategoryFactory(owner=UserFactory( username='******')) self.client.force_login(self.user) response = self.client.get(reverse('skill-list')) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['skills']), 2) self.assertEqual(len(response.data['categories']), 4)
def test_details(self): """ Should provide skill detailed deep overview """ skill1 = SkillFactory(name='Skill1') _skill2 = SkillFactory(name='Skill2') _act1 = ActivityFactory(skill=skill1) _act2 = ActivityFactory(skill=skill1) self.client.force_login(self.user) response = self.client.get( reverse('skill-detail', kwargs={'pk': skill1.pk})) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['categories']), 2) self.assertEqual(len(response.data['activities']), 2)
def test_serialization(self, mocker): mocker.patch('django.utils.timezone.now', mock_now) skill = SkillFactory(name='Testname') s = SkillFlatSerializer(skill) assert len(s.data['categories']) == 2 assert s.data['name'] == 'Testname' assert parse_datetime(s.data['add_date']) == mock_now()
def test_destroy(self): skill1 = SkillFactory(name='Skill1') self.client.force_login(self.user) response = self.client.delete( reverse('skill-detail', kwargs={'pk': skill1.pk})) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(Skill.objects.filter(pk=skill1.pk).exists())
def test_remove_not_existing_category(self): skill = SkillFactory() self.client.force_login(self.user) response = self.client.post(reverse('skill-remove-category', kwargs={'pk': skill.pk}), data=(-123)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(len(skill.categories.all()), 2)
def test_update(self): skill = SkillFactory(owner=self.user) self.client.force_login(self.user) response = self.client.put(reverse('skill-detail', kwargs={'pk': skill.pk}), data={'name': 'newName'}) skill = Skill.objects.get(id=response.data['id']) self.assertEqual(skill.name, 'newName') self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_add_category(self): skill = SkillFactory() cat = CategoryFactory() self.client.force_login(self.user) response = self.client.post(reverse('skill-add-category', kwargs={'pk': skill.pk}), data=cat.pk) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(skill.categories.all()), 3)
def test_remove_category(self): skill = SkillFactory() categories = skill.categories.all() self.client.force_login(self.user) response = self.client.post(reverse('skill-remove-category', kwargs={'pk': skill.pk}), data=categories[1].pk) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(skill.categories.all()), 1) self.assertEqual(skill.categories.all()[0].pk, categories[0].pk)
def test_recent(self): skill = SkillFactory() act1 = ActivityFactory(skill=skill) act2 = ActivityFactory(skill=skill) act3 = ActivityFactory(skill=skill) self.client.force_login(self.user) response = self.client.get(reverse('activity-recent')) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data[act3.id]['id'], act3.id) self.assertEqual(response.data[act2.id]['id'], act2.id) self.assertEqual(response.data[act1.id]['id'], act1.id)
def test_update(self): request = create_user_request(APIRequestFactory().put) skill = SkillFactory(name='OldName') s = SkillFlatSerializer(instance=skill, data={ 'name': 'NewName' }, context={ 'request': request }) s.is_valid(raise_exception=True) skill = Skill.objects.get(id=s.save().id) assert skill.name == 'NewName' assert skill.owner == request.user
def test_update(self): with pytest.raises(serializers.ValidationError): request = create_user_request(APIRequestFactory().post) skill = SkillFactory(name='OldName') fake_cat = CategoryFactory() s = SkillDeepSerializer(instance=skill, data={ 'name': 'NewName', 'categories': [fake_cat.id] }, context={ 'request': request }) s.is_valid() s.save()
def test_serialization(self, mocker): mocker.patch('django.utils.timezone.now', mock_now) skill = SkillFactory(name='TestSkill') categories = skill.categories.all() act = ActivityFactory(title='Act1', category=categories[0], skill=skill) s = SkillDeepSerializer(skill, context={'request': None}) data = s.data data['add_date'] = parse_datetime(data['add_date']) data_act = data['activities'][act.id] data_act['add_date'] = parse_datetime(data_act['add_date']) data_act['modify_date'] = parse_datetime(data_act['modify_date']) assert data == dict( id=skill.id, name='TestSkill', add_date=mock_now(), categories={ categories[0].id: dict( id=categories[0].id, name=categories[0].name, display_order=categories[0].display_order, ), categories[1].id: dict( id=categories[1].id, name=categories[1].name, display_order=categories[1].display_order, ) }, activities={ act.id: dict( id=act.id, title=act.title, category=categories[0].id, skill=skill.id, description=act.description, add_date=mock_now(), modify_date=mock_now() ) } )
def test_create(self): skill = SkillFactory() cat = skill.categories.all()[0] self.client.force_login(self.user) response = self.client.post(reverse('activity-list'), data={ 'title': 'title', 'category': cat.pk, 'skill': skill.pk, 'description': 'desc', }) self.assertEqual(response.status_code, status.HTTP_201_CREATED) activity = Activity.objects.get(id=response.data['id']) self.assertEqual(activity.title, 'title') self.assertEqual(activity.description, 'desc') self.assertEqual(activity.category, cat) self.assertEqual(activity.skill, skill)
def test_create(self, mocker): mocker.patch('django.utils.timezone.now', mock_now) request = create_user_request(APIRequestFactory().post) skill = SkillFactory() categories = skill.categories.all() s = ActivityFlatSerializer(data=dict( title='NewAct', skill=skill.id, category=categories[0].id, description='desc', ), context={ 'request': request }) s.is_valid(raise_exception=True) act = Activity.objects.get(id=s.save().id) assert act.title == 'NewAct' assert act.skill.id == skill.id assert act.category.id == categories[0].id assert act.description == 'desc'
def test_serialization(self): skill = SkillFactory(name='TestSkill') categories = skill.categories.all() request = create_user_request(APIRequestFactory().post) s = SkillListSerializer({ 'skills': [skill], 'categories': categories, }) skills = s.data['skills'] res_cat = s.data['categories'] assert len(res_cat) == 2 assert len(skills) == 1 for i in (0, 1): cat_id = categories[i].id assert res_cat[cat_id]['id'] == categories[i].id assert res_cat[cat_id]['name'] == categories[i].name assert res_cat[cat_id]['is_base_category'] == categories[i].is_base_category