Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.test import TestCase
- from django.contrib.auth.models import User
- from django.db.models import Max, Min
- from okrsme.models import Objective, KeyResult
- from rest_framework.test import APIClient
- USERNAME = "tester"
- PASSWORD = "password"
- YEAR = 2017
- QUARTER = 1
- class ObjectiveTestCase(TestCase):
- def setUp(self):
- user = User.objects.create(username=USERNAME, password=PASSWORD)
- client = APIClient()
- client.force_authenticate(user=user)
- # try to create 6 objectives
- for i in range(1, 7):
- test_objective = {'year': YEAR, 'quarter': QUARTER, "text": "Test objective"}
- request = client.post('/objectives/', test_objective, format="json")
- client.logout()
- def test_5_objectives_per_quarter(self):
- """One user has exactly 5 objectives"""
- user = User.objects.get(username=USERNAME)
- self.assertIs(Objective.objects.filter(owner = user, year=YEAR, quarter=QUARTER).count() == 5, True)
- def test_first_objective_has_number_1(self):
- """The user's first objective has number 1"""
- user = User.objects.get(username=USERNAME)
- user_objectives = Objective.objects.filter(owner = user, year=YEAR, quarter=QUARTER)
- result = user_objectives.aggregate(Min('number'))["number__min"]
- self.assertIs(result == 1, True)
- def test_last_objective_has_number_5(self):
- """The user's last objective has number 5"""
- user = User.objects.get(username=USERNAME)
- user_objectives = Objective.objects.filter(owner = user, year=YEAR, quarter=QUARTER)
- result = user_objectives.aggregate(Max('number'))["number__max"]
- self.assertIs(result == 5, True)
- class KeyResultTestCase(TestCase):
- def setUp(self):
- user = User.objects.create(username=USERNAME, password=PASSWORD)
- client = APIClient()
- client.force_authenticate(user=user)
- test_objective_data = {"year": YEAR, "quarter": QUARTER, "text": "Test objective"}
- request = client.post('/objectives/', test_objective_data, format="json")
- # this part should possibly made serializer-agnostic
- test_objective = client.get('/objectives/%d/' % request.data['id'], format="json")
- objective_id = test_objective.data['id']
- # try to create 6 key results
- for i in range(1, 7):
- test_key_result = {'objective': objective_id, "text": "Test key result"}
- client.post('/key-results/', test_key_result, format="json")
- client.logout()
- def test_5_key_results_per_objective(self):
- """One user has maximum 5 key results"""
- user = User.objects.get(username=USERNAME)
- objective = Objective.objects.get(owner=user, year=YEAR, quarter=QUARTER, number=1)
- self.assertIs(KeyResult.objects.filter(objective=objective).count() == 5, True)
- def test_first_key_result_has_number_1(self):
- """The user's first objective has number 1"""
- user = User.objects.get(username=USERNAME)
- objective = Objective.objects.get(owner=user, year=YEAR, quarter=QUARTER, number=1)
- key_results = KeyResult.objects.filter(objective=objective)
- result = key_results.aggregate(Min('number'))["number__min"]
- self.assertIs(result == 1, True)
- def test_last_key_result_has_number_5(self):
- """The user's last objective has number 5"""
- user = User.objects.get(username=USERNAME)
- objective = Objective.objects.get(owner=user, year=YEAR, quarter=QUARTER, number=1)
- key_results = KeyResult.objects.filter(objective=objective)
- result = key_results.aggregate(Max('number'))["number__max"]
- self.assertIs(result == 5, True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement