Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from django.test import TestCase 

2from datetime import datetime, timedelta, time 

3from django.utils import timezone 

4from django.core.exceptions import ValidationError 

5from django.db.utils import IntegrityError 

6from policorp.models import Availability, Location, Task, Booking, User 

7from policorp.tests import aux 

8import json 

9 

10class TestLocation(TestCase): 

11 

12 fixtures = ['testsdata.json'] 

13 

14 loc1 = "Buenos Aires" 

15 loc2 = "Córdoba" 

16 loc3 = "Rosario" 

17 

18 def test_get_location_all(self): 

19 """ GIVEN 3 locations; WHEN requesting all locations; THEN 3 locations should be returned """ 

20 

21 self.assertEqual(len(Location.objects.get_all()), 3) 

22 

23 def test_get_location_all_gets_name(self): 

24 """ GIVEN 3 locations; WHEN requesting all locations; THEN location names should be returned """ 

25 

26 self.assertEqual(Location.objects.get_all()[0].name, self.loc1) 

27 self.assertEqual(Location.objects.get_all()[1].name, self.loc2) 

28 self.assertEqual(Location.objects.get_all()[2].name, self.loc3) 

29 

30 def test_location_serialize(self): 

31 """ GIVEN 3 locations; WHEN requesting json serialization; THEN id and name should be returned in json format """ 

32 l1 = Location.objects.get(pk=1) 

33 expected = {'id': 1, 'name': self.loc1} 

34 self.assertJSONEqual(json.dumps(l1.json()), expected) 

35 

36 def test_location_supervisor(self): 

37 """ GIVEN 1 location; WHEN assigning a supervisor; THEN a user is saved in supervisors field """ 

38 l1 = Location.objects.get(pk=1) 

39 u = User.objects.create_supervisor("foo", "foo@example.com", "example") 

40 l1.assign_supervisor(u) 

41 l1 = Location.objects.get(pk=1) 

42 self.assertIn(u, l1.supervisors.all()) 

43 

44 def test_location_supervisor_twice(self): 

45 """ GIVEN 1 location with an assigned supervisor; WHEN assigning a supervisor twice; THEN an exception occurs """ 

46 l1 = Location.objects.get(pk=1) 

47 u = User.objects.create_supervisor("foo", "foo@example.com", "example") 

48 l1.assign_supervisor(u) 

49 with self.assertRaises(ValidationError): 

50 l1.assign_supervisor(u) 

51 

52 def test_location_supervisor_user_is_not_supervisor_error(self): 

53 """ GIVEN 1 location; WHEN assigning a consumer user; THEN a ValidationError is raised """ 

54 l1 = Location.objects.get(pk=1) 

55 u = aux.createUser("foo", "foo@example.com", "example") 

56 with self.assertRaises(ValidationError): 

57 l1.assign_supervisor(u) 

58 

59 def test_location_remove_supervisor(self): 

60 """ GIVEN 1 location with an assigned supervisor; WHEN removing that supervisor; THEN the user is removed from supervisors field """ 

61 l1 = Location.objects.get(pk=1) 

62 u = User.objects.create_supervisor("foo", "foo@example.com", "example") 

63 l1.assign_supervisor(u) 

64 l1 = Location.objects.get(pk=1) 

65 self.assertIn(u, l1.supervisors.all()) 

66 l1.remove_supervisor(u) 

67 self.assertNotIn(u, l1.supervisors.all()) 

68 

69 def test_location_remove_not_existant_supervisor(self): 

70 """ GIVEN 1 location; WHEN removing a not assigned supervisor; THEN an exception is raised """ 

71 l1 = Location.objects.get(pk=1) 

72 u = User.objects.create_supervisor("foo", "foo@example.com", "example") 

73 with self.assertRaises(ValidationError): 

74 l1.remove_supervisor(u) 

75 

76 def test_get_supervised_locations(self): 

77 """ GIVEN a user supervising a location; WHEN I request all supervised locations for user; THEN location is in returned array """ 

78 user = User.objects.create_supervisor("foo", "foo@example.com", "example") 

79 location = Location.objects.get(pk=1) 

80 location.assign_supervisor(user) 

81 self.assertIn(location, user.supervisedLocations.all()) 

82 

83 def test_get_supervised_locations_not_included(self): 

84 """ GIVEN a user not supervising a location; WHEN I request all supervised locations for user; THEN location is not in returned array """ 

85 user = User.objects.create_supervisor("foo", "foo@example.com", "example") 

86 location = Location.objects.get(pk=1) 

87 self.assertNotIn(location, user.supervisedLocations.all()) 

88 

89class TestTask(TestCase): 

90 

91 def test_get_task_all(self): 

92 """ GIVEN 1 task; WHEN requesting all tasks; THEN 1 task should be returned """ 

93 task_name = "Device Installation" 

94 t = Task.objects.create_task(task_name, 30) 

95 self.assertEqual(len(Task.objects.get_all()), 1) 

96 

97 def test_get_task_all_gets_name(self): 

98 """ GIVEN 1 task with name "Device Installation"; WHEN requesting all tasks; THEN 1 task should be returned with name "Device Installation" """ 

99 task_name = "Device Installation" 

100 t = Task.objects.create_task(task_name, 15) 

101 self.assertEqual(len(Task.objects.get_all()), 1) 

102 self.assertEqual(Task.objects.get_all()[0].name, task_name) 

103 

104 def test_task_serialize_json(self): 

105 """ GIVEN 1 task with name "Device Installation"; WHEN requesting json serialization; THEN id and name should be returned in json format """ 

106 task_name = "Device Installation" 

107 duration = 60 

108 t = Task.objects.create_task(task_name, duration) 

109 j = {'id': 1, 'name': task_name, 'duration': duration} 

110 self.assertEqual(t.json()["name"], j["name"]) 

111 self.assertEqual(t.json()["duration"], j["duration"]) 

112 

113 def test_task_duration(self): 

114 """ GIVEN ; WHEN creating a task; THEN a duration in minutes must be provided """ 

115 task_name = "Device Installation" 

116 t = Task.objects.create_task(task_name, 30) 

117 self.assertEqual(Task.objects.get(name=task_name).duration, 30) 

118 

119 def test_task_duration_not_negative(self): 

120 """ GIVEN ; WHEN creating a task with negative duration; THEN an exception must be raised """ 

121 task_name = "Device Installation" 

122 with self.assertRaises(IntegrityError): 

123 Task.objects.create_task(task_name, -30) 

124 

125class TestUser(TestCase): 

126 

127 def test_user_is_supervisor_false_default(self): 

128 """ GIVEN ; WHEN creating a consumer user; THEN it is not supervisor by default """ 

129 u = User() 

130 u.username = "foo" 

131 u.email = "foo@example.com" 

132 u.set_password("example") 

133 u.save() 

134 u = User.objects.get(username="foo") 

135 self.assertFalse(u.is_supervisor) 

136 

137 def test_user_create_supervisor(self): 

138 """ GIVEN ; WHEN creating a supervisor user; THEN it is supervisor """ 

139 u = User.objects.create_supervisor("foo", "foo@example.com", "example") 

140 u = User.objects.filter(username="foo").first() 

141 self.assertTrue(u.is_supervisor) 

142 

143if __name__ == "__main__": 

144 unittest.main()