class Employee1Test(APITestCase): """ Testing class for salaried workers """ def setUp(self): """ Set up the Testing class: -self.employee = salary worker -self.employee = daily worker -self.employee3 = daily worker with no legal status """ super(Employee1Test, self).setUp() self.employee = Employee(**employee1_data) self.employee.save() def test_calculate_gross_pay_salary(self): """ Test that the pay is correct for 2 week period of a salaried worker """ #test 2 weeks pay gross_pay = self.employee._calculate_gross_pay(date(2014, 7, 1), date(2014, 7, 15)) self.assertEqual(gross_pay, 9000) #Test 1 month pay gross_pay = self.employee._calculate_gross_pay(date(2014, 7, 1), date(2014, 7, 31)) self.assertEqual(gross_pay, 18000) def test_social_security_deduction(self): """ Test whether the social security is properly deducted """ pay_post_deduction = self.employee._apply_social_security_deduction( Decimal('10000')) self.assertEqual(pay_post_deduction, 9500) def test_tax_deduction(self): """ Test whether the tax is propery deducted -tax is currently set for 500baht per person """ pay_post_tax = self.employee._apply_tax_deduction(Decimal('9500')) self.assertEqual(pay_post_tax, 9000) def test_net_pay(self): """ Test whether the net pay is calculated property """ net_pay = self.employee.calculate_net_pay(date(2014, 7, 1), date(2014, 7, 15)) self.assertEqual(net_pay, 8050)
class Employee1Test(APITestCase): """ Testing class for salaried workers """ def setUp(self): """ Set up the Testing class: -self.employee = salary worker -self.employee = daily worker -self.employee3 = daily worker with no legal status """ super(Employee1Test, self).setUp() self.employee = Employee(**employee1_data) self.employee.save() def test_calculate_gross_pay_salary(self): """ Test that the pay is correct for 2 week period of a salaried worker """ # test 2 weeks pay gross_pay = self.employee._calculate_gross_pay(date(2014, 7, 1), date(2014, 7, 15)) self.assertEqual(gross_pay, 9000) # Test 1 month pay gross_pay = self.employee._calculate_gross_pay(date(2014, 7, 1), date(2014, 7, 31)) self.assertEqual(gross_pay, 18000) def test_social_security_deduction(self): """ Test whether the social security is properly deducted """ pay_post_deduction = self.employee._apply_social_security_deduction(Decimal("10000")) self.assertEqual(pay_post_deduction, 9500) def test_tax_deduction(self): """ Test whether the tax is propery deducted -tax is currently set for 500baht per person """ pay_post_tax = self.employee._apply_tax_deduction(Decimal("9500")) self.assertEqual(pay_post_tax, 9000) def test_net_pay(self): """ Test whether the net pay is calculated property """ net_pay = self.employee.calculate_net_pay(date(2014, 7, 1), date(2014, 7, 15)) self.assertEqual(net_pay, 8050)
class Employee3Test(APITestCase): """ Testing class for daily worker """ def setUp(self): """ Set up the Testing class: -self.employee = salary worker -self.employee = daily worker -self.employee3 = daily worker with no legal status """ super(Employee3Test, self).setUp() self.employee = Employee(**employee3_data) self.employee.save() for day in xrange(1, 15): hour = 20 if day % 2 > 0 else 17 a = Attendance(employee=self.employee, start_time=datetime(2014, 7, day, 8, 0), end_time=datetime(2014, 7, day, hour, 0)) a.save() def test_calculate_daily_wage(self): """ Test that the daily wage is correctly calculated Tests: -regular with perfect in out -regular day with slightly over in out -day with over time -sunday with no overtime -sunday with overtime """ #Regular day with perfect in out a = Attendance(employee=self.employee, start_time=datetime(2014, 7, 1, 8, 0), end_time=datetime(2014, 7, 1, 17, 0)) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 300) #Regular day with slightly imperfect in out a = Attendance(employee=self.employee, start_time=datetime(2014, 7, 1, 8, 0), end_time=datetime(2014, 7, 1, 17, 14)) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 300) #Day with over time a = Attendance(employee=self.employee, start_time=datetime(2014, 7, 1, 8, 0), end_time=datetime(2014, 7, 1, 20, 0)) self.assertEqual(a.total_time, 11) self.assertEqual(a.overtime, 3) self.assertEqual(a.regular_time, 8) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 468.75) #Sunday with no overtime a = Attendance(employee=self.employee, start_time=datetime(2014, 7, 6, 8, 0), end_time=datetime(2014, 7, 6, 17, 0)) self.assertEqual(a.total_time, 8) self.assertEqual(a.overtime, 0) self.assertEqual(a.regular_time, 8) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 600) #Sunday with over time a = Attendance(employee=self.employee, start_time=datetime(2014, 7, 6, 8, 0), end_time=datetime(2014, 7, 6, 20, 0)) self.assertEqual(a.total_time, 11) self.assertEqual(a.overtime, 3) self.assertEqual(a.regular_time, 8) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 937.5) def test_calculate_gross_pay_wage(self): """ Test that the pay is correct for 2 week period of a salaried worker """ #test 2 weeks pay gross_pay = self.employee._calculate_gross_pay(date(2014, 7, 1), date(2014, 7, 15)) self.assertEqual(gross_pay, 6150) #Test 1 month pay gross_pay = self.employee._calculate_gross_pay(date(2014, 7, 1), date(2014, 7, 31)) self.assertEqual(gross_pay, 6150) def test_social_security_deduction(self): """ Test whether the social security is properly deducted """ pay_post_deduction = self.employee._apply_social_security_deduction( Decimal('10000')) self.assertEqual(pay_post_deduction, 10000) def test_tax_deduction(self): """ Test whether the tax is propery deducted -tax is currently set for 500baht per person """ pay_post_tax = self.employee._apply_tax_deduction(Decimal('9500')) self.assertEqual(pay_post_tax, 9500) def test_net_pay(self): """ Test whether the net pay is calculated property """ net_pay = self.employee.calculate_net_pay(date(2014, 7, 1), date(2014, 7, 15)) self.assertEqual(net_pay, 6150)
class AttendanceTest(APITestCase): """ Testing class for attendance """ def setUp(self): """ Set up the Testing clas: """ super(AttendanceTest, self).setUp() #self.client.client.login(username='******', password='******') self.shift = Shift(start_time=time(8, 0), end_time=time(17, 0)) self.shift.save() self.employee = Employee(shift=self.shift, **employee2_data) self.employee.save() # Regular attendance self.attendance = Attendance( date=date(2014, 7, 1), start_time=timezone('Asia/Bangkok').localize( datetime(2014, 7, 1, 7, 30, 0)), end_time=timezone('Asia/Bangkok').localize( datetime(2014, 7, 1, 23, 33, 0)), employee=self.employee, shift=self.shift) self.attendance.save() # Sunday attendance self.sunday_attendance = Attendance( date=date(2016, 2, 7), start_time=timezone('Asia/Bangkok').localize( datetime(2016, 2, 7, 8, 02, 0)), end_time=timezone('Asia/Bangkok').localize( datetime(2016, 2, 7, 23, 15, 0)), employee=self.employee, shift=self.shift) self.sunday_attendance.save() self.tz = timezone('Asia/Bangkok') def test_start_time_property(self): """Test that the getter and setter for start time work properly """ a = Attendance(employee=self.employee, date=date(2016, 3, 21)) d1 = datetime(2016, 3, 21, 8, 11, 0) # Test start time without timezone a.start_time = d1 self.assertEqual(a._start_time, self.tz.localize(d1)) self.assertEqual(a.start_time, self.tz.localize(d1)) # Test start time with timezone a.start_time = self.tz.localize(d1) self.assertEqual(a._start_time, self.tz.localize(d1)) self.assertEqual(a.start_time, self.tz.localize(d1)) def test_end_time_property(self): """Test that the getter and setter for start time work properly """ a = Attendance(employee=self.employee, date=date(2016, 3, 21)) d1 = datetime(2016, 3, 21, 15, 29, 0) # Test start time without timezone a.end_time = d1 self.assertEqual(a._end_time, self.tz.localize(d1)) self.assertEqual(a.end_time, self.tz.localize(d1)) # Test start time with timezone a.end_time = self.tz.localize(d1) self.assertEqual(a._end_time, self.tz.localize(d1)) self.assertEqual(a.end_time, self.tz.localize(d1)) def test_regular_attedance_regular_hours(self): """Test the regular hours of a regular attedance """ self.attendance.calculate_times() self.assertEqual(self.attendance.regular_time, Decimal('8.0')) self.assertEqual(self.attendance.overtime, Decimal('0')) def test_regular_attedance_with_overtime_enabled(self): """Test the regular hours of a regular attedance """ self.attendance.enable_overtime = True self.attendance.calculate_times() self.assertEqual(self.attendance.regular_time, Decimal('8.0')) self.assertEqual(self.attendance.overtime, Decimal('6.5')) def test_regular_attendance_gross_wage(self): """Test the gross wage of a regular attendance """ self.attendance.calculate_times() self.attendance.calculate_gross_wage() self.assertEqual(self.attendance.regular_pay, Decimal('550')) self.assertEqual(self.attendance.overtime_pay, Decimal('0')) self.assertEqual(self.attendance.lunch_pay, Decimal('0')) self.assertEqual(self.attendance.gross_wage, Decimal('550')) def test_regular_attendance_gross_wage_with_overtime_enabled(self): """Test the gross wage of a regular attendance with overtime enabled """ self.attendance.enable_overtime = True self.attendance.calculate_times() self.attendance.calculate_gross_wage() self.assertEqual(self.attendance.regular_pay, Decimal('550')) # Calculate expected overtime pay ot_rate = (Decimal('550') / Decimal('8')) * Decimal('1.5') self.assertEqual(self.attendance.overtime_pay, ot_rate * Decimal('6.5')) self.assertEqual(self.attendance.lunch_pay, Decimal('0')) # Test gross wage self.assertEqual(self.attendance.gross_wage, Decimal('550') + (ot_rate * Decimal('6.5'))) def test_regular_attendance_net_wage(self): """Test the gross wage of a regular attendance """ self.attendance.calculate_times() self.attendance.calculate_net_wage() self.assertEqual(self.attendance.gross_wage, Decimal('550')) self.assertEqual(self.attendance.reimbursement, Decimal('30')) self.assertEqual(self.attendance.net_wage, Decimal('580')) def test_regular_attendance_net_wage_with_lunch(self): """Test the gross wage of a regular attendance """ self.attendance.receive_lunch_overtime = True self.attendance.calculate_times() self.attendance.calculate_net_wage() self.assertEqual(self.attendance.gross_wage, Decimal('653.125')) self.assertEqual(self.attendance.reimbursement, Decimal('60')) self.assertEqual(self.attendance.net_wage, Decimal('713.125')) def test_regular_attendance_net_wage_where_clockin_late(self): """Test the net wage where an employee is late """ logger.debug( "\n\n\n\nTesting late clocking for regular attendance\n\n\n") # Change start time so employee is late self.attendance.start_time = self.tz.localize( datetime(2014, 7, 1, 8, 15, 0)) self.attendance.calculate_times() self.attendance.calculate_net_wage() self.assertEqual(self.attendance.regular_time, Decimal('7.5')) self.assertEqual(self.attendance.reimbursement, Decimal('0')) self.assertEqual(self.attendance.net_wage, Decimal('515.625')) def test_sunday_attendance_regular_hours(self): """Test the regular hours of a regular attedance """ logger.debug("\n\n\n\nTesting Sunday Regular attendance hours\n\n\n") self.sunday_attendance.calculate_times() self.assertEqual(self.sunday_attendance.regular_time, Decimal('8.0')) self.assertEqual(self.sunday_attendance.overtime, Decimal('0')) def test_sunday_attendance_with_overtime_enabled(self): """Test the regular hours of a regular attedance """ self.sunday_attendance.enable_overtime = True self.sunday_attendance.calculate_times() self.assertEqual(self.sunday_attendance.regular_time, Decimal('8.0')) self.assertEqual(self.sunday_attendance.overtime, Decimal('6')) def test_sunday_attendance_gross_wage(self): """Test the gross wage of a sunday attendance """ self.sunday_attendance.calculate_times() self.sunday_attendance.calculate_gross_wage() self.assertEqual(self.sunday_attendance.regular_pay, Decimal('1100')) self.assertEqual(self.sunday_attendance.overtime_pay, Decimal('0')) self.assertEqual(self.sunday_attendance.lunch_pay, Decimal('0')) self.assertEqual(self.sunday_attendance.gross_wage, Decimal('1100')) def test_sunday_attendance_gross_wage_with_overtime_enabled(self): """Test the gross wage of a sunday attendance """ self.sunday_attendance.enable_overtime = True self.sunday_attendance.calculate_times() self.sunday_attendance.calculate_gross_wage() self.assertEqual(self.sunday_attendance.regular_pay, Decimal('1100')) # Calculate expected overtime pay ot_rate = (Decimal('550') / Decimal('8')) * Decimal('3') self.assertEqual(self.sunday_attendance.overtime_pay, ot_rate * Decimal('6')) self.assertEqual(self.sunday_attendance.lunch_pay, Decimal('0')) # Test gross wage self.assertEqual(self.sunday_attendance.gross_wage, Decimal('1100') + (ot_rate * Decimal('6')))
class AttendanceTest(APITestCase): """ Testing class for attendance """ def setUp(self): """ Set up the Testing clas: """ super(AttendanceTest, self).setUp() # self.client.client.login(username='******', password='******') self.shift = Shift(start_time=time(8, 0), end_time=time(17, 0)) self.shift.save() self.employee = Employee(shift=self.shift, **employee2_data) self.employee.save() # Regular attendance self.attendance = Attendance( date=date(2014, 7, 1), start_time=timezone("Asia/Bangkok").localize(datetime(2014, 7, 1, 7, 30, 0)), end_time=timezone("Asia/Bangkok").localize(datetime(2014, 7, 1, 23, 33, 0)), employee=self.employee, shift=self.shift, ) self.attendance.save() # Sunday attendance self.sunday_attendance = Attendance( date=date(2016, 2, 7), start_time=timezone("Asia/Bangkok").localize(datetime(2016, 2, 7, 8, 02, 0)), end_time=timezone("Asia/Bangkok").localize(datetime(2016, 2, 7, 23, 15, 0)), employee=self.employee, shift=self.shift, ) self.sunday_attendance.save() self.tz = timezone("Asia/Bangkok") def test_start_time_property(self): """Test that the getter and setter for start time work properly """ a = Attendance(employee=self.employee, date=date(2016, 3, 21)) d1 = datetime(2016, 3, 21, 8, 11, 0) # Test start time without timezone a.start_time = d1 self.assertEqual(a._start_time, self.tz.localize(d1)) self.assertEqual(a.start_time, self.tz.localize(d1)) # Test start time with timezone a.start_time = self.tz.localize(d1) self.assertEqual(a._start_time, self.tz.localize(d1)) self.assertEqual(a.start_time, self.tz.localize(d1)) def test_end_time_property(self): """Test that the getter and setter for start time work properly """ a = Attendance(employee=self.employee, date=date(2016, 3, 21)) d1 = datetime(2016, 3, 21, 15, 29, 0) # Test start time without timezone a.end_time = d1 self.assertEqual(a._end_time, self.tz.localize(d1)) self.assertEqual(a.end_time, self.tz.localize(d1)) # Test start time with timezone a.end_time = self.tz.localize(d1) self.assertEqual(a._end_time, self.tz.localize(d1)) self.assertEqual(a.end_time, self.tz.localize(d1)) def test_regular_attedance_regular_hours(self): """Test the regular hours of a regular attedance """ self.attendance.calculate_times() self.assertEqual(self.attendance.regular_time, Decimal("8.0")) self.assertEqual(self.attendance.overtime, Decimal("0")) def test_regular_attedance_with_overtime_enabled(self): """Test the regular hours of a regular attedance """ self.attendance.enable_overtime = True self.attendance.calculate_times() self.assertEqual(self.attendance.regular_time, Decimal("8.0")) self.assertEqual(self.attendance.overtime, Decimal("6.5")) def test_regular_attendance_gross_wage(self): """Test the gross wage of a regular attendance """ self.attendance.calculate_times() self.attendance.calculate_gross_wage() self.assertEqual(self.attendance.regular_pay, Decimal("550")) self.assertEqual(self.attendance.overtime_pay, Decimal("0")) self.assertEqual(self.attendance.lunch_pay, Decimal("0")) self.assertEqual(self.attendance.gross_wage, Decimal("550")) def test_regular_attendance_gross_wage_with_overtime_enabled(self): """Test the gross wage of a regular attendance with overtime enabled """ self.attendance.enable_overtime = True self.attendance.calculate_times() self.attendance.calculate_gross_wage() self.assertEqual(self.attendance.regular_pay, Decimal("550")) # Calculate expected overtime pay ot_rate = (Decimal("550") / Decimal("8")) * Decimal("1.5") self.assertEqual(self.attendance.overtime_pay, ot_rate * Decimal("6.5")) self.assertEqual(self.attendance.lunch_pay, Decimal("0")) # Test gross wage self.assertEqual(self.attendance.gross_wage, Decimal("550") + (ot_rate * Decimal("6.5"))) def test_regular_attendance_net_wage(self): """Test the gross wage of a regular attendance """ self.attendance.calculate_times() self.attendance.calculate_net_wage() self.assertEqual(self.attendance.gross_wage, Decimal("550")) self.assertEqual(self.attendance.reimbursement, Decimal("30")) self.assertEqual(self.attendance.net_wage, Decimal("580")) def test_regular_attendance_net_wage_with_lunch(self): """Test the gross wage of a regular attendance """ self.attendance.receive_lunch_overtime = True self.attendance.calculate_times() self.attendance.calculate_net_wage() self.assertEqual(self.attendance.gross_wage, Decimal("653.125")) self.assertEqual(self.attendance.reimbursement, Decimal("60")) self.assertEqual(self.attendance.net_wage, Decimal("713.125")) def test_regular_attendance_net_wage_where_clockin_late(self): """Test the net wage where an employee is late """ logger.debug("\n\n\n\nTesting late clocking for regular attendance\n\n\n") # Change start time so employee is late self.attendance.start_time = self.tz.localize(datetime(2014, 7, 1, 8, 15, 0)) self.attendance.calculate_times() self.attendance.calculate_net_wage() self.assertEqual(self.attendance.regular_time, Decimal("7.5")) self.assertEqual(self.attendance.reimbursement, Decimal("0")) self.assertEqual(self.attendance.net_wage, Decimal("515.625")) def test_sunday_attendance_regular_hours(self): """Test the regular hours of a regular attedance """ logger.debug("\n\n\n\nTesting Sunday Regular attendance hours\n\n\n") self.sunday_attendance.calculate_times() self.assertEqual(self.sunday_attendance.regular_time, Decimal("8.0")) self.assertEqual(self.sunday_attendance.overtime, Decimal("0")) def test_sunday_attendance_with_overtime_enabled(self): """Test the regular hours of a regular attedance """ self.sunday_attendance.enable_overtime = True self.sunday_attendance.calculate_times() self.assertEqual(self.sunday_attendance.regular_time, Decimal("8.0")) self.assertEqual(self.sunday_attendance.overtime, Decimal("6")) def test_sunday_attendance_gross_wage(self): """Test the gross wage of a sunday attendance """ self.sunday_attendance.calculate_times() self.sunday_attendance.calculate_gross_wage() self.assertEqual(self.sunday_attendance.regular_pay, Decimal("1100")) self.assertEqual(self.sunday_attendance.overtime_pay, Decimal("0")) self.assertEqual(self.sunday_attendance.lunch_pay, Decimal("0")) self.assertEqual(self.sunday_attendance.gross_wage, Decimal("1100")) def test_sunday_attendance_gross_wage_with_overtime_enabled(self): """Test the gross wage of a sunday attendance """ self.sunday_attendance.enable_overtime = True self.sunday_attendance.calculate_times() self.sunday_attendance.calculate_gross_wage() self.assertEqual(self.sunday_attendance.regular_pay, Decimal("1100")) # Calculate expected overtime pay ot_rate = (Decimal("550") / Decimal("8")) * Decimal("3") self.assertEqual(self.sunday_attendance.overtime_pay, ot_rate * Decimal("6")) self.assertEqual(self.sunday_attendance.lunch_pay, Decimal("0")) # Test gross wage self.assertEqual(self.sunday_attendance.gross_wage, Decimal("1100") + (ot_rate * Decimal("6")))
class Employee3Test(APITestCase): """ Testing class for daily worker """ def setUp(self): """ Set up the Testing class: -self.employee = salary worker -self.employee = daily worker -self.employee3 = daily worker with no legal status """ super(Employee3Test, self).setUp() self.employee = Employee(**employee3_data) self.employee.save() for day in xrange(1, 15): hour = 20 if day % 2 > 0 else 17 a = Attendance( employee=self.employee, start_time=datetime(2014, 7, day, 8, 0), end_time=datetime(2014, 7, day, hour, 0), ) a.save() def test_calculate_daily_wage(self): """ Test that the daily wage is correctly calculated Tests: -regular with perfect in out -regular day with slightly over in out -day with over time -sunday with no overtime -sunday with overtime """ # Regular day with perfect in out a = Attendance( employee=self.employee, start_time=datetime(2014, 7, 1, 8, 0), end_time=datetime(2014, 7, 1, 17, 0) ) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 300) # Regular day with slightly imperfect in out a = Attendance( employee=self.employee, start_time=datetime(2014, 7, 1, 8, 0), end_time=datetime(2014, 7, 1, 17, 14) ) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 300) # Day with over time a = Attendance( employee=self.employee, start_time=datetime(2014, 7, 1, 8, 0), end_time=datetime(2014, 7, 1, 20, 0) ) self.assertEqual(a.total_time, 11) self.assertEqual(a.overtime, 3) self.assertEqual(a.regular_time, 8) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 468.75) # Sunday with no overtime a = Attendance( employee=self.employee, start_time=datetime(2014, 7, 6, 8, 0), end_time=datetime(2014, 7, 6, 17, 0) ) self.assertEqual(a.total_time, 8) self.assertEqual(a.overtime, 0) self.assertEqual(a.regular_time, 8) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 600) # Sunday with over time a = Attendance( employee=self.employee, start_time=datetime(2014, 7, 6, 8, 0), end_time=datetime(2014, 7, 6, 20, 0) ) self.assertEqual(a.total_time, 11) self.assertEqual(a.overtime, 3) self.assertEqual(a.regular_time, 8) wage = self.employee._calculate_daily_wages(a) self.assertEqual(wage, 937.5) def test_calculate_gross_pay_wage(self): """ Test that the pay is correct for 2 week period of a salaried worker """ # test 2 weeks pay gross_pay = self.employee._calculate_gross_pay(date(2014, 7, 1), date(2014, 7, 15)) self.assertEqual(gross_pay, 6150) # Test 1 month pay gross_pay = self.employee._calculate_gross_pay(date(2014, 7, 1), date(2014, 7, 31)) self.assertEqual(gross_pay, 6150) def test_social_security_deduction(self): """ Test whether the social security is properly deducted """ pay_post_deduction = self.employee._apply_social_security_deduction(Decimal("10000")) self.assertEqual(pay_post_deduction, 10000) def test_tax_deduction(self): """ Test whether the tax is propery deducted -tax is currently set for 500baht per person """ pay_post_tax = self.employee._apply_tax_deduction(Decimal("9500")) self.assertEqual(pay_post_tax, 9500) def test_net_pay(self): """ Test whether the net pay is calculated property """ net_pay = self.employee.calculate_net_pay(date(2014, 7, 1), date(2014, 7, 15)) self.assertEqual(net_pay, 6150)
class SupplyAPITestCase(APITestCase): def setUp(self): """ Set up the view -login the user """ super(SupplyAPITestCase, self).setUp() self.create_user() self.client.login(username="******", password="******") self.supplier = Supplier(**base_supplier) self.supplier.save() self.supplier2 = Supplier.objects.create(**base_supplier) self.supply = Supply.create(**base_supply) self.assertIsNotNone(self.supply.pk) self.supply2 = Supply.create(**base_supply) self.assertIsNotNone(self.supply2.pk) self.supply3 = Supply.create(**base_supply) self.assertIsNotNone(self.supply3.pk) self.supply4 = Supply.create(**base_supply) self.assertIsNotNone(self.supply4.pk) self.product = Product(supplier=self.supplier, supply=self.supply) self.product.save() self.employee = Employee(first_name="John", last_name="Smith") self.employee.save() def create_user(self): self.user = User.objects.create_user("test", "*****@*****.**", "test") self.ct = ContentType(app_label="supplies") self.ct.save() self._create_and_add_permission("view_cost", self.user) self._create_and_add_permission("change_supply", self.user) self._create_and_add_permission("add_supply", self.user) self._create_and_add_permission("add_quantity", self.user) self._create_and_add_permission("subtract_quantity", self.user) def _create_and_add_permission(self, codename, user): p = Permission(content_type=self.ct, codename=codename) p.save() user.user_permissions.add(p) def test_get_list(self): """ Tests that a standard get call works. """ # Testing standard GET resp = self.client.get("/api/v1/supply/") self.assertEqual(resp.status_code, 200) # Tests the returned data resp_obj = resp.data self.assertIn("results", resp_obj) self.assertEqual(len(resp_obj["results"]), 4) def test_get(self): """ Tests getting a supply that doesn't have the price where the user is not authorized to view the price """ resp = self.client.get("/api/v1/supply/1/") self.assertEqual(resp.status_code, 200) obj = resp.data # self.assertEqual(Decimal(obj['cost']), Decimal('100')) self.assertIn("description", obj) self.assertEqual(obj["description"], "test") self.assertIn("type", obj) self.assertEqual(obj["type"], "wood") resp = self.client.get("/api/v1/supply/1/?country=TH") self.assertEqual(resp.status_code, 200) obj = resp.data self.assertEqual(obj["quantity"], 10.8) self.assertIn("suppliers", obj) self.assertEqual(len(obj["suppliers"]), 1) supplier = obj["suppliers"][0] def stest_get_log(self): """ Tests gettings the log for all the supplies """ resp = self.client.get("/api/v1/supply/log/") # self.assertEqual(resp.status_code, 200) obj = resp.data # self.assertIsInstance(obj, list) def test_get_without_price(self): """ Tests getting a supply that doesn't have the price where the user is not authorized to view the price """ # Delete the view cost permission from the user self.user.user_permissions.remove(Permission.objects.get(codename="view_cost", content_type=self.ct)) # tests the response resp = self.client.get("/api/v1/supply/1/") self.assertEqual(resp.status_code, 200) # Tests the data returned obj = resp.data self.assertNotIn("cost", obj) def test_get_types(self): """ Tests getting the different types used to describe supplies """ resp = self.client.get("/api/v1/supply/type/") # self.assertEqual(resp.status_code, 200) type_list = resp.data # self.assertIn('wood', type_list) def test_post_single_supplier(self): """ Tests posting to the server """ # Test creating an objects. self.assertEqual(Supply.objects.count(), 4) resp = self.client.post("/api/v1/supply/", format="json", data=base_supply) self.assertEqual(resp.status_code, 201, msg=resp) # Tests the dat aturned obj = resp.data self.assertEqual(obj["id"], 5) self.assertEqual(obj["width"], "100.00") self.assertEqual(obj["depth"], "200.00") self.assertEqual(obj["height"], "300.00") self.assertEqual(obj["description"], "test") self.assertEqual(obj["height_units"], "yd") self.assertEqual(obj["width_units"], "m") self.assertEqual(obj["notes"], "This is awesome") self.assertIn("type", obj) self.assertEqual(obj["type"], "wood") self.assertIn("suppliers", obj) self.assertEqual(len(obj["suppliers"]), 1) # Test Supplier supplier = obj["suppliers"][0] self.assertEqual(supplier["reference"], "A2234") self.assertEqual(supplier["cost"], Decimal("100")) # TEsts the object created supply = Supply.objects.order_by("-id").all()[0] supply.supplier = supply.suppliers.all()[0] self.assertEqual(supply.id, 5) self.assertEqual(supply.width, 100) self.assertEqual(supply.depth, 200) self.assertEqual(supply.height, 300) # self.assertEqual(supply.reference, 'A2234') self.assertEqual(supply.description, "test") # self.assertEqual(supply.cost, 100) self.assertEqual(supply.height_units, "yd") self.assertEqual(supply.width_units, "m") self.assertEqual(supply.notes, "This is awesome") self.assertIsNotNone(supply.type) self.assertEqual(supply.type, "wood") self.assertIsNotNone(supply.suppliers) self.assertEqual(supply.suppliers.count(), 1) def test_posting_with_custom_type(self): """ Testing creating a new resource via POST that has a custom type """ # Testing returned types pre POST resp0 = self.client.get("/api/v1/supply/type/", format="json") self.assertEqual(resp0.status_code, 200, msg=resp0) type_list = resp0.data self.assertNotIn("egg", type_list) self.assertIn("wood", type_list) self.assertEqual(len(type_list), 1) # POST modified_supply = base_supply.copy() modified_supply["type"] = "egg" resp = self.client.post("/api/v1/supply/", format="json", data=modified_supply) self.assertEqual(resp.status_code, 201) # Tests the response obj = resp.data self.assertIn("type", obj) self.assertNotIn("custom-type", obj) self.assertEqual(obj["type"], "egg") """ resp2 = self.client.get('/api/v1/supply/type/', format='json') self.assertHttpOK(resp2) type_list = self.deserialize(resp2) self.assertIn('egg', type_list) self.assertIn('wood', type_list) self.assertEqual(len(type_list), 2) """ def test_put(self): """ Tests adding quantity to the item """ # Validate original data supply = Supply.objects.get(pk=1) supply.country = "TH" self.assertEqual(supply.quantity, 10.8) self.assertEqual(Log.objects.all().count(), 0) # Prepare modified data for PUT modified_data = base_supply.copy() modified_data["description"] = "new" modified_data["type"] = "Glue" modified_data["quantity"] = "11" # Tests the api and the response self.assertEqual(Supply.objects.count(), 4) resp = self.client.put("/api/v1/supply/1/?country=TH", format="json", data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) # Tests the returned data obj = resp.data self.assertEqual(obj["type"], "Glue") self.assertEqual(float(obj["quantity"]), 11) self.assertEqual(obj["description"], "new") self.assertFalse(obj.has_key("quantity_th")) self.assertFalse(obj.has_key("quantity_kh")) # Tests the resource in the database supply = Supply.objects.get(pk=1) supply.country = "TH" self.assertEqual(supply.type, "Glue") self.assertEqual(supply.country, "TH") self.assertEqual(supply.description, "new") self.assertEqual(supply.quantity, 11) self.assertEqual(Log.objects.all().count(), 1) log = Log.objects.all()[0] self.assertEqual(log.action, "ADD") self.assertEqual(log.quantity, Decimal("0.2")) self.assertEqual(log.message, "Added 0.2ml to new") def test_put_without_quantity_change(self): """ Tests adding quantity to the item """ # Validate original data supply = Supply.objects.get(pk=1) supply.country = "TH" self.assertEqual(supply.quantity, 10.8) self.assertEqual(Log.objects.all().count(), 0) # Prepare modified data for PUT modified_data = base_supply.copy() modified_data["description"] = "new" modified_data["type"] = "Glue" modified_data["quantity"] = "10.8" modified_data["width_units"] = "cm" modified_data["depth_units"] = "cm" # Tests the api and the response self.assertEqual(Supply.objects.count(), 4) resp = self.client.put("/api/v1/supply/1/?country=TH", format="json", data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) # Tests the returned data obj = resp.data self.assertEqual(obj["type"], "Glue") self.assertEqual(float(obj["quantity"]), 10.8) self.assertEqual(obj["description"], "new") self.assertEqual(obj["width_units"], "cm") self.assertEqual(obj["depth_units"], "cm") self.assertFalse(obj.has_key("quantity_th")) self.assertFalse(obj.has_key("quantity_kh")) # Tests the resource in the database supply = Supply.objects.get(pk=1) supply.country = "TH" self.assertEqual(supply.type, "Glue") self.assertEqual(supply.country, "TH") self.assertEqual(supply.description, "new") self.assertEqual(supply.quantity, 10.8) self.assertEqual(Log.objects.all().count(), 0) def test_put_subtracting_quantity_to_0(self): """ Tests adding quantity to the item """ # Validate original data supply = Supply.objects.get(pk=1) supply.country = "TH" supply.quantity = 1 supply.save() self.assertEqual(supply.quantity, 1) self.assertEqual(Log.objects.all().count(), 0) # Prepare modified data for PUT modified_data = base_supply.copy() modified_data["description"] = "new" modified_data["type"] = "Glue" modified_data["quantity"] = "0" # Tests the api and the response self.assertEqual(Supply.objects.count(), 4) resp = self.client.put("/api/v1/supply/1/?country=TH", format="json", data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) # Tests the returned data obj = resp.data self.assertEqual(obj["quantity"], 0) self.assertFalse(obj.has_key("quantity_th")) self.assertFalse(obj.has_key("quantity_kh")) # Tests the resource in the database supply = Supply.objects.get(pk=1) supply.country = "TH" self.assertEqual(supply.quantity, 0) log = Log.objects.all().order_by("-id")[0] self.assertEqual(Log.objects.all().count(), 1) self.assertEqual(log.quantity, 1) self.assertEqual(log.action, "SUBTRACT") @unittest.skip("Not yet implemented") def test_add(self): """ Tests adding a quantity to the specific url """ self.assertEqual(Supply.objects.get(pk=1).quantity, float("10.8")) resp = self.client.post("/api/v1/supply/1/add/?quantity=5", format="json") self.assertEqual(Supply.objects.get(pk=1).quantity, float("15.8")) @unittest.skip("Not yet implemented") def test_subract(self): """ Tests adding a quantity to the specific url """ self.assertEqual(Supply.objects.get(pk=1).quantity, float("10.8")) resp = self.client.post("/api/v1/supply/1/subtract/?quantity=5", format="json") self.assertEqual(Supply.objects.get(pk=1).quantity, float("5.8")) def test_put_add_quantity(self): """ Tests adding quantity to the item """ modified_data = base_supply.copy() modified_data["quantity"] = "14" modified_data["description"] = "new" # Tests the api and the response self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float("10.8")) resp = self.client.put("/api/v1/supply/1/", format="json", data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float("14")) self.assertEqual(Supply.objects.get(pk=1).description, "new") # Tests the returned data obj = resp.data self.assertEqual(float(obj["quantity"]), float("14")) def test_put_subtract_quantity(self): """ Tests adding quantity to the item """ modified_data = base_supply.copy() modified_data["quantity"] = "8" # Tests the api and the response self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float("10.8")) resp = self.client.put("/api/v1/supply/1/", format="json", data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float("8")) # Tests the returned data obj = resp.data self.assertEqual(float(obj["quantity"]), float("8")) def test_put_to_create_new_product(self): """ Tests adding a new supplier/product to the supply """ logger.debug("\n\nTest creating a new supply/product via PUT\n") modified_data = copy.deepcopy(base_supply) modified_data["suppliers"] = [ {"id": 1, "supplier": {"id": 1}, "reference": "BOO", "cost": "123.45"}, { "reference": "A4", "cost": "19.99", "purchasing_units": "ml", "quantity_per_purchasing_unit": 4, "supplier": {"id": 2}, }, ] resp = self.client.put("/api/v1/supply/1/", format="json", data=modified_data) self.assertEqual(resp.status_code, 200, msg=resp) self.assertEqual(Supply.objects.count(), 4) obj = resp.data self.assertIn("suppliers", obj) self.assertEqual(len(obj["suppliers"]), 2) supplier1 = obj["suppliers"][0] self.assertEqual(supplier1["reference"], "BOO") self.assertEqual(supplier1["cost"], Decimal("123.45")) supplier2 = obj["suppliers"][1] self.assertEqual(supplier2["reference"], "A4") self.assertEqual(supplier2["cost"], Decimal("19.99")) self.assertEqual(supplier2["purchasing_units"], "ml") self.assertEqual(supplier2["quantity_per_purchasing_unit"], Decimal("4")) self.assertEqual(supplier2["supplier"]["id"], 2) def test_updating_multiple_supplies(self): """ Test that we can update the quantities of multiple supplies """ data = [ {"id": 1, "description": "poooo", "quantity": 9, "employee": {"id": 1}}, {"id": 2, "quantity": 12.3, "employee": {"id": 1}}, ] resp = self.client.put("/api/v1/supply/", format="json", data=data) self.assertEqual(resp.status_code, 200, msg=resp) supplies = resp.data supply1 = supplies[0] self.assertEqual(supply1["quantity"], 9) self.assertEqual(supply1["description"], u"poooo") supply1_obj = Supply.objects.get(pk=1) self.assertEqual(supply1_obj.quantity, 9) supply2 = supplies[1] self.assertEqual(supply2["quantity"], Decimal("12.3")) supply2_obj = Supply.objects.get(pk=2) self.assertEqual(supply2_obj.quantity, 12.3) log1 = Log.objects.all()[0] self.assertEqual(log1.action, "SUBTRACT") self.assertEqual(log1.quantity, Decimal("1.8")) self.assertIsNotNone(log1.employee) self.assertEqual(log1.employee.id, 1) self.assertEqual(log1.employee.first_name, "John") self.assertEqual(log1.employee.last_name, "Smith") log2 = Log.objects.all()[1] self.assertEqual(log2.action, "ADD") self.assertEqual(log2.quantity, Decimal("1.5")) self.assertIsNotNone(log2.employee) self.assertEqual(log2.employee.id, 1) self.assertEqual(log2.employee.first_name, "John") self.assertEqual(log2.employee.last_name, "Smith")
class SupplyAPITest(APITestCase): def setUp(self): """ Set up the view -login the user """ super(SupplyAPITest, self).setUp() self.create_user() self.client.login(username='******', password='******') self.supplier = Supplier(**base_supplier) self.supplier.save() self.supplier2 = Supplier.objects.create(**base_supplier) self.supply = Supply.create(**base_supply) self.assertIsNotNone(self.supply.pk) self.supply2 = Supply.create(**base_supply) self.assertIsNotNone(self.supply2.pk) self.supply3 = Supply.create(**base_supply) self.assertIsNotNone(self.supply3.pk) self.supply4 = Supply.create(**base_supply) self.assertIsNotNone(self.supply4.pk) self.product = Product(supplier=self.supplier, supply=self.supply) self.product.save() self.employee = Employee(first_name="John", last_name="Smith") self.employee.save() def create_user(self): self.user = User.objects.create_user('test', '*****@*****.**', 'test') self.ct = ContentType(app_label='supplies') self.ct.save() self._create_and_add_permission('view_cost', self.user) self._create_and_add_permission('change_supply', self.user) self._create_and_add_permission('add_supply', self.user) self._create_and_add_permission('add_quantity', self.user) self._create_and_add_permission('subtract_quantity', self.user) def _create_and_add_permission(self, codename, user): p = Permission(content_type=self.ct, codename=codename) p.save() user.user_permissions.add(p) def test_get_list(self): """ Tests that a standard get call works. """ #Testing standard GET resp = self.client.get('/api/v1/supply/') self.assertEqual(resp.status_code, 200) #Tests the returned data resp_obj = resp.data self.assertIn('results', resp_obj) self.assertEqual(len(resp_obj['results']), 4) def test_get(self): """ Tests getting a supply that doesn't have the price where the user is not authorized to view the price """ resp = self.client.get('/api/v1/supply/1/') self.assertEqual(resp.status_code, 200) obj = resp.data #self.assertEqual(Decimal(obj['cost']), Decimal('100')) self.assertIn('description', obj) self.assertEqual(obj['description'], 'test') self.assertIn('type', obj) self.assertEqual(obj['type'], 'wood') resp = self.client.get('/api/v1/supply/1/?country=TH') self.assertEqual(resp.status_code, 200) obj = resp.data self.assertEqual(obj['quantity'], 10.8) self.assertIn('suppliers', obj) self.assertEqual(len(obj['suppliers']), 1) supplier = obj['suppliers'][0] def stest_get_log(self): """ Tests gettings the log for all the supplies """ resp = self.client.get('/api/v1/supply/log/') #self.assertEqual(resp.status_code, 200) obj = resp.data #self.assertIsInstance(obj, list) def test_get_without_price(self): """ Tests getting a supply that doesn't have the price where the user is not authorized to view the price """ #Delete the view cost permission from the user self.user.user_permissions.remove( Permission.objects.get(codename='view_cost', content_type=self.ct)) #tests the response resp = self.client.get('/api/v1/supply/1/') self.assertEqual(resp.status_code, 200) #Tests the data returned obj = resp.data self.assertNotIn("cost", obj) def test_get_types(self): """ Tests getting the different types used to describe supplies """ resp = self.client.get('/api/v1/supply/type/') #self.assertEqual(resp.status_code, 200) type_list = resp.data #self.assertIn('wood', type_list) def test_post_single_supplier(self): """ Tests posting to the server """ #Test creating an objects. self.assertEqual(Supply.objects.count(), 4) resp = self.client.post('/api/v1/supply/', format='json', data=base_supply) self.assertEqual(resp.status_code, 201, msg=resp) #Tests the dat aturned obj = resp.data self.assertEqual(obj['id'], 5) self.assertEqual(obj['width'], '100.00') self.assertEqual(obj['depth'], '200.00') self.assertEqual(obj['height'], '300.00') self.assertEqual(obj['description'], 'test') self.assertEqual(obj['height_units'], 'yd') self.assertEqual(obj['width_units'], 'm') self.assertEqual(obj['notes'], 'This is awesome') self.assertIn('type', obj) self.assertEqual(obj['type'], 'wood') self.assertIn('suppliers', obj) self.assertEqual(len(obj['suppliers']), 1) #Test Supplier supplier = obj['suppliers'][0] self.assertEqual(supplier['reference'], 'A2234') self.assertEqual(supplier['cost'], Decimal('100')) #TEsts the object created supply = Supply.objects.order_by('-id').all()[0] supply.supplier = supply.suppliers.all()[0] self.assertEqual(supply.id, 5) self.assertEqual(supply.width, 100) self.assertEqual(supply.depth, 200) self.assertEqual(supply.height, 300) #self.assertEqual(supply.reference, 'A2234') self.assertEqual(supply.description, 'test') #self.assertEqual(supply.cost, 100) self.assertEqual(supply.height_units, 'yd') self.assertEqual(supply.width_units, 'm') self.assertEqual(supply.notes, 'This is awesome') self.assertIsNotNone(supply.type) self.assertEqual(supply.type, 'wood') self.assertIsNotNone(supply.suppliers) self.assertEqual(supply.suppliers.count(), 1) def test_posting_with_custom_type(self): """ Testing creating a new resource via POST that has a custom type """ #Testing returned types pre POST resp0 = self.client.get('/api/v1/supply/type/', format='json') self.assertEqual(resp0.status_code, 200, msg=resp0) type_list = resp0.data self.assertNotIn('egg', type_list) self.assertIn('wood', type_list) self.assertEqual(len(type_list), 1) #POST modified_supply = base_supply.copy() modified_supply['type'] = 'egg' resp = self.client.post('/api/v1/supply/', format='json', data=modified_supply) self.assertEqual(resp.status_code, 201) #Tests the response obj = resp.data self.assertIn('type', obj) self.assertNotIn('custom-type', obj) self.assertEqual(obj['type'], 'egg') """ resp2 = self.client.get('/api/v1/supply/type/', format='json') self.assertHttpOK(resp2) type_list = self.deserialize(resp2) self.assertIn('egg', type_list) self.assertIn('wood', type_list) self.assertEqual(len(type_list), 2) """ def test_put(self): """ Tests adding quantity to the item """ #Validate original data supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.quantity, 10.8) self.assertEqual(Log.objects.all().count(), 0) #Prepare modified data for PUT modified_data = base_supply.copy() modified_data['description'] = 'new' modified_data['type'] = 'Glue' modified_data['quantity'] = '11' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) resp = self.client.put('/api/v1/supply/1/?country=TH', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) #Tests the returned data obj = resp.data self.assertEqual(obj['type'], 'Glue') self.assertEqual(float(obj['quantity']), 11) self.assertEqual(obj['description'], 'new') self.assertFalse(obj.has_key('quantity_th')) self.assertFalse(obj.has_key('quantity_kh')) #Tests the resource in the database supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.type, 'Glue') self.assertEqual(supply.country, 'TH') self.assertEqual(supply.description, 'new') self.assertEqual(supply.quantity, 11) self.assertEqual(Log.objects.all().count(), 1) log = Log.objects.all()[0] self.assertEqual(log.action, 'ADD') self.assertEqual(log.quantity, Decimal('0.2')) self.assertEqual(log.message, "Added 0.2ml to new") def test_put_without_quantity_change(self): """ Tests adding quantity to the item """ #Validate original data supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.quantity, 10.8) self.assertEqual(Log.objects.all().count(), 0) #Prepare modified data for PUT modified_data = base_supply.copy() modified_data['description'] = 'new' modified_data['type'] = 'Glue' modified_data['quantity'] = '10.8' modified_data['width_units'] = 'cm' modified_data['depth_units'] = 'cm' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) resp = self.client.put('/api/v1/supply/1/?country=TH', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) #Tests the returned data obj = resp.data self.assertEqual(obj['type'], 'Glue') self.assertEqual(float(obj['quantity']), 10.8) self.assertEqual(obj['description'], 'new') self.assertEqual(obj['width_units'], 'cm') self.assertEqual(obj['depth_units'], 'cm') self.assertFalse(obj.has_key('quantity_th')) self.assertFalse(obj.has_key('quantity_kh')) #Tests the resource in the database supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.type, 'Glue') self.assertEqual(supply.country, 'TH') self.assertEqual(supply.description, 'new') self.assertEqual(supply.quantity, 10.8) self.assertEqual(Log.objects.all().count(), 0) def test_put_subtracting_quantity_to_0(self): """ Tests adding quantity to the item """ #Validate original data supply = Supply.objects.get(pk=1) supply.country = 'TH' supply.quantity = 1 supply.save() self.assertEqual(supply.quantity, 1) self.assertEqual(Log.objects.all().count(), 0) #Prepare modified data for PUT modified_data = base_supply.copy() modified_data['description'] = 'new' modified_data['type'] = 'Glue' modified_data['quantity'] = '0' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) resp = self.client.put('/api/v1/supply/1/?country=TH', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) #Tests the returned data obj = resp.data self.assertEqual(obj['quantity'], 0) self.assertFalse(obj.has_key('quantity_th')) self.assertFalse(obj.has_key('quantity_kh')) #Tests the resource in the database supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.quantity, 0) log = Log.objects.all().order_by('-id')[0] self.assertEqual(Log.objects.all().count(), 1) self.assertEqual(log.quantity, 1) self.assertEqual(log.action, 'SUBTRACT') @unittest.skip('Not yet implemented') def test_add(self): """ Tests adding a quantity to the specific url """ self.assertEqual(Supply.objects.get(pk=1).quantity, float('10.8')) resp = self.client.post('/api/v1/supply/1/add/?quantity=5', format='json') self.assertEqual(Supply.objects.get(pk=1).quantity, float('15.8')) @unittest.skip('Not yet implemented') def test_subract(self): """ Tests adding a quantity to the specific url """ self.assertEqual(Supply.objects.get(pk=1).quantity, float('10.8')) resp = self.client.post('/api/v1/supply/1/subtract/?quantity=5', format='json') self.assertEqual(Supply.objects.get(pk=1).quantity, float('5.8')) def test_put_add_quantity(self): """ Tests adding quantity to the item """ modified_data = base_supply.copy() modified_data['quantity'] = '14' modified_data['description'] = 'new' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float('10.8')) resp = self.client.put('/api/v1/supply/1/', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float('14')) self.assertEqual(Supply.objects.get(pk=1).description, 'new') #Tests the returned data obj = resp.data self.assertEqual(float(obj['quantity']), float('14')) def test_put_subtract_quantity(self): """ Tests adding quantity to the item """ modified_data = base_supply.copy() modified_data['quantity'] = '8' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float('10.8')) resp = self.client.put('/api/v1/supply/1/', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float('8')) #Tests the returned data obj = resp.data self.assertEqual(float(obj['quantity']), float('8')) def test_put_to_create_new_product(self): """ Tests adding a new supplier/product to the supply """ logger.debug("\n\nTest creating a new supply/product via PUT\n") modified_data = copy.deepcopy(base_supply) modified_data['suppliers'] = [{ 'id': 1, 'supplier': { 'id': 1 }, 'reference': 'BOO', 'cost': '123.45' }, { 'reference': 'A4', 'cost': '19.99', 'purchasing_units': 'ml', 'quantity_per_purchasing_unit': 4, 'supplier': { 'id': 2 } }] resp = self.client.put('/api/v1/supply/1/', format='json', data=modified_data) self.assertEqual(resp.status_code, 200, msg=resp) self.assertEqual(Supply.objects.count(), 4) obj = resp.data self.assertIn('suppliers', obj) self.assertEqual(len(obj['suppliers']), 2) supplier1 = obj['suppliers'][0] self.assertEqual(supplier1['reference'], 'BOO') self.assertEqual(supplier1['cost'], Decimal('123.45')) supplier2 = obj['suppliers'][1] self.assertEqual(supplier2['reference'], 'A4') self.assertEqual(supplier2['cost'], Decimal('19.99')) self.assertEqual(supplier2['purchasing_units'], 'ml') self.assertEqual(supplier2['quantity_per_purchasing_unit'], Decimal('4')) self.assertEqual(supplier2['supplier']['id'], 2) def test_updating_multiple_supplies(self): """ Test that we can update the quantities of multiple supplies """ data = [{ 'id': 1, 'description': 'poooo', 'quantity': 9, 'employee': { 'id': 1 } }, { 'id': 2, 'quantity': 12.3, 'employee': { 'id': 1 } }] resp = self.client.put('/api/v1/supply/', format='json', data=data) self.assertEqual(resp.status_code, 200, msg=resp) supplies = resp.data supply1 = supplies[0] self.assertEqual(supply1['quantity'], 9) self.assertEqual(supply1['description'], u'poooo') supply1_obj = Supply.objects.get(pk=1) self.assertEqual(supply1_obj.quantity, 9) supply2 = supplies[1] self.assertEqual(supply2['quantity'], Decimal('12.3')) supply2_obj = Supply.objects.get(pk=2) self.assertEqual(supply2_obj.quantity, 12.3) log1 = Log.objects.all()[0] self.assertEqual(log1.action, "SUBTRACT") self.assertEqual(log1.quantity, Decimal('1.8')) self.assertIsNotNone(log1.employee) self.assertEqual(log1.employee.id, 1) self.assertEqual(log1.employee.first_name, "John") self.assertEqual(log1.employee.last_name, "Smith") log2 = Log.objects.all()[1] self.assertEqual(log2.action, "ADD") self.assertEqual(log2.quantity, Decimal('1.5')) self.assertIsNotNone(log2.employee) self.assertEqual(log2.employee.id, 1) self.assertEqual(log2.employee.first_name, "John") self.assertEqual(log2.employee.last_name, "Smith") def test_bulk_update_with_new_supply(self): """ Test that we can update the quantities of multiple supplies """ data = [{ 'id': 1, 'description': 'poooo', 'quantity': 9, 'employee': { 'id': 1 } }, { 'id': 2, 'quantity': 12.3, 'employee': { 'id': 1 } }, { 'description': 'slat wood', 'quantity': 10 }] resp = self.client.put('/api/v1/supply/', format='json', data=data) self.assertEqual(resp.status_code, 200, msg=resp) supplies = resp.data supply1 = supplies[0] self.assertEqual(supply1['quantity'], 9) self.assertEqual(supply1['description'], u'poooo') supply1_obj = Supply.objects.get(pk=1) self.assertEqual(supply1_obj.quantity, 9) supply2 = supplies[1] self.assertEqual(supply2['quantity'], Decimal('12.3')) supply2_obj = Supply.objects.get(pk=2) self.assertEqual(supply2_obj.quantity, 12.3) supply3 = supplies[2] logger.debug(supply3) self.assertEqual(supply3['description'], 'slat wood') self.assertEqual(supply3['quantity'], Decimal('10')) supply3_obj = Supply.objects.get(pk=supply3['id']) self.assertEqual(supply3_obj.description, 'slat wood') self.assertEqual(supply3_obj.quantity, 10) log1 = Log.objects.all()[0] self.assertEqual(log1.action, "SUBTRACT") self.assertEqual(log1.quantity, Decimal('1.8')) self.assertIsNotNone(log1.employee) self.assertEqual(log1.employee.id, 1) self.assertEqual(log1.employee.first_name, "John") self.assertEqual(log1.employee.last_name, "Smith") log2 = Log.objects.all()[1] self.assertEqual(log2.action, "ADD") self.assertEqual(log2.quantity, Decimal('1.5')) self.assertIsNotNone(log2.employee) self.assertEqual(log2.employee.id, 1) self.assertEqual(log2.employee.first_name, "John") self.assertEqual(log2.employee.last_name, "Smith")
class EquipmentTestCase(APITestCase): def setUp(self): self.equipment = Equipment(**base_equipment) self.equipment.save() self.employee = Employee(first_name="John", last_name="Smith", department="Carpentry") self.employee.save() self.image = S3Object() self.image.save() def test_get_list(self): """ Test GET on api to get a list resources """ resp = self.client.get("/api/v1/equipment/", format='json') self.assertEqual(resp.status_code, 200) resp = resp.data self.assertIsInstance(resp, dict) self.assertIsInstance(resp['results'], list) self.assertEqual(len(resp['results']), 1) def test_get(self): """ Test GET on api to get a resource """ resp = self.client.get("/api/v1/equipment/1/", format='json') self.assertEqual(resp.status_code, 200, msg=resp) obj = resp.data self.assertIsInstance(obj, dict) self.assertIn('id', obj) self.assertEqual(obj['id'], 1) self.assertIn('description', obj) self.assertEqual(obj['description'], 'F-50') self.assertIn('brand', obj) self.assertEqual(obj['brand'], 'Red King') self.assertIn('status', obj) self.assertEqual(obj['status'], 'Checked In') def test_post(self): """ Test creating resource via POST """ data = {'description': 'Jigsaw', 'brand': 'Makita', 'status': 'Checked In', 'image': { 'id': 1 }} resp = self.client.post("/api/v1/equipment/", data=data, format='json') self.assertEqual(resp.status_code, 201, msg=resp) obj = resp.data self.assertIsInstance(obj, dict) self.assertIn('id', obj) self.assertEqual(obj['id'], 2) self.assertIn('description', obj) self.assertEqual(obj['description'], 'Jigsaw') self.assertIn('brand', obj) self.assertEqual(obj['brand'], 'Makita') self.assertIn('status', obj) self.assertEqual(obj['status'], 'Checked In') self.assertIn('image', obj) self.assertIn('id', obj['image']) self.assertEqual(obj['image']['id'], 1) #Test that resource saved to database self.assertEqual(Equipment.objects.all().count(), 2) obj = Equipment.objects.all().order_by('id')[1] self.assertIsNotNone(obj.id) self.assertEqual(obj.id, 2) self.assertEqual(obj.description, 'Jigsaw') self.assertEqual(obj.brand, "Makita") self.assertEqual(obj.status, "Checked In") self.assertIsNotNone(obj.image) def test_post_empty_equipment(self): """ Test creating resource via POST where the equipment only has an image """ data = {'image': {'id': 1}} resp = self.client.post("/api/v1/equipment/", data=data, format='json') self.assertEqual(resp.status_code, 201, msg=resp) obj = resp.data self.assertIsInstance(obj, dict) self.assertIn('id', obj) self.assertEqual(obj['id'], 2) self.assertIn('description', obj) self.assertIsNone(obj['description']) self.assertIn('brand', obj) self.assertIsNone(obj['brand']) self.assertIn('status', obj) self.assertIsNone(obj['status']) self.assertIn('image', obj) self.assertIn('id', obj['image']) self.assertEqual(obj['image']['id'], 1) #Test that resource saved to database self.assertEqual(Equipment.objects.all().count(), 2) obj = Equipment.objects.all().order_by('id')[1] self.assertIsNotNone(obj.id) self.assertEqual(obj.id, 2) self.assertIsNone(obj.description, ) self.assertIsNone(obj.brand) self.assertIsNone(obj.status) self.assertIsNotNone(obj.image) def test_put(self): """ Test updating resource via PUT """ modified_data = base_equipment.copy() modified_data['brand'] = 'Maktec' modified_data['status'] = "Checked Out" resp = self.client.put("/api/v1/equipment/1/", data=modified_data, format='json') self.assertEqual(resp.status_code, 200, msg=resp) obj = resp.data self.assertIn('id', obj) self.assertEqual(obj['id'], 1) self.assertIn('description', obj) self.assertEqual(obj['description'], 'F-50') self.assertIn('brand', obj) self.assertEqual(obj['brand'], "Maktec") self.assertIn('status', obj) self.assertEqual(obj['status'], "Checked Out") def test_update_with_employee_checkout(self): """ Test updating the equipment checkout with a particular employee """ modified_data = base_equipment.copy() modified_data['employee'] = {'id': 1} modified_data['status'] = "Checked Out" resp = self.client.put("/api/v1/equipment/1/", data=modified_data, format="json") self.assertEqual(resp.status_code, 200, msg=resp) obj = resp.data #Test response self.assertIn('id', obj) self.assertEqual(obj['id'], 1) self.assertIn('description', obj) self.assertEqual(obj['description'], "F-50") self.assertIn('brand', obj) self.assertEqual(obj['brand'], "Red King") self.assertIn('status', obj) self.assertEqual(obj['status'], 'Checked Out') self.assertIn('employee', obj) self.assertIsInstance(obj['employee'], dict) self.assertIn('id', obj['employee']) self.assertEqual(obj['employee']['id'], 1) #Test server response equipment = Equipment.objects.get(pk=1) self.assertEqual(equipment.id, 1) self.assertEqual(equipment.description, "F-50") self.assertEqual(equipment.brand, "Red King") self.assertEqual(equipment.status, "Checked Out") self.assertIsNotNone(equipment.employee) self.assertEqual(equipment.employee.id, 1) self.assertEqual(equipment.employee.first_name, "John") def test_update_bulk_with_employee(self): """ Test updating multiple equipments with employee """ equipment2 = Equipment(description="Jigsaw", brand="Makita", status="Checked Out", employee=self.employee) equipment2.save() data = [{'id': 1, 'description': "F-50", 'brand': 'Red King', 'status': 'Checked Out', 'employee': {'id': 1}}, {'id': 2, 'description': 'Jigsaw', 'brand': 'Makita', 'status': 'Checked In', 'employee': {'id': 1}}] resp = self.client.put("/api/v1/equipment/", data=data, format="json") self.assertEqual(resp.status_code, 200) #Test the response data = resp.data self.assertIsInstance(data, list) equip1 = data[0] self.assertEqual(equip1['id'], 1) self.assertEqual(equip1['description'], "F-50") self.assertEqual(equip1['brand'], 'Red King') self.assertEqual(equip1['status'], 'Checked Out') self.assertIn('employee', equip1) self.assertIsInstance(equip1['employee'], dict) self.assertIn('id', equip1['employee']) self.assertEqual(equip1['employee']['id'], 1) equip2 = data[1] self.assertEqual(equip2['id'], 2) self.assertEqual(equip2['description'], "Jigsaw") self.assertEqual(equip2['brand'], 'Makita') self.assertEqual(equip2['status'], 'Checked In') self.assertNotIn('employee', equip2) #Test instances in database equip1 = Equipment.objects.get(pk=1) self.assertEqual(equip1.id, 1) self.assertEqual(equip1.description, "F-50") self.assertEqual(equip1.brand, "Red King") self.assertEqual(equip1.status, "Checked Out") self.assertIsNotNone(equip1.employee) self.assertEqual(equip1.employee.id, 1) equip2 = Equipment.objects.get(pk=2) self.assertEqual(equip2.id, 2) self.assertEqual(equip2.description, "Jigsaw") self.assertEqual(equip2.brand, "Makita") self.assertEqual(equip2.status, "Checked In") self.assertIsNone(equip2.employee) def test_updating_bulk_with_different_data(self): """ Test updating list of equipment with varying amounts of information """ # Create new equipment to test equip1 = Equipment.objects.create(pk=188) equip2 = Equipment.objects.create(pk=189) equip3 = Equipment.objects.create(pk=190) # Data to submit via PUT data = [ {'id': 188}, {'id': 189, 'description': 'saw'}, {'id': 190, 'description': 'jigsaw', 'brand': 'Makita'} ] resp = self.client.put('/api/v1/equipment/', data=data, format='json')
class SupplyAPITest(APITestCase): def setUp(self): """ Set up the view -login the user """ super(SupplyAPITest, self).setUp() self.create_user() self.client.login(username='******', password='******') self.supplier = Supplier(**base_supplier) self.supplier.save() self.supplier2 = Supplier.objects.create(**base_supplier) self.supply = Supply.create(**base_supply) self.assertIsNotNone(self.supply.pk) self.supply2 = Supply.create(**base_supply) self.assertIsNotNone(self.supply2.pk) self.supply3 = Supply.create(**base_supply) self.assertIsNotNone(self.supply3.pk) self.supply4 = Supply.create(**base_supply) self.assertIsNotNone(self.supply4.pk) self.product = Product(supplier=self.supplier, supply=self.supply) self.product.save() self.employee = Employee(first_name="John", last_name="Smith") self.employee.save() def create_user(self): self.user = User.objects.create_user('test', '*****@*****.**', 'test') self.ct = ContentType(app_label='supplies') self.ct.save() self._create_and_add_permission('view_cost', self.user) self._create_and_add_permission('change_supply', self.user) self._create_and_add_permission('add_supply', self.user) self._create_and_add_permission('add_quantity', self.user) self._create_and_add_permission('subtract_quantity', self.user) def _create_and_add_permission(self, codename, user): p = Permission(content_type=self.ct, codename=codename) p.save() user.user_permissions.add(p) def test_get_list(self): """ Tests that a standard get call works. """ #Testing standard GET resp = self.client.get('/api/v1/supply/') self.assertEqual(resp.status_code, 200) #Tests the returned data resp_obj = resp.data self.assertIn('results', resp_obj) self.assertEqual(len(resp_obj['results']), 4) def test_get(self): """ Tests getting a supply that doesn't have the price where the user is not authorized to view the price """ resp = self.client.get('/api/v1/supply/1/') self.assertEqual(resp.status_code, 200) obj = resp.data #self.assertEqual(Decimal(obj['cost']), Decimal('100')) self.assertIn('description', obj) self.assertEqual(obj['description'], 'test') self.assertIn('type', obj) self.assertEqual(obj['type'], 'wood') resp = self.client.get('/api/v1/supply/1/?country=TH') self.assertEqual(resp.status_code, 200) obj = resp.data self.assertEqual(obj['quantity'], 10.8) self.assertIn('suppliers', obj) self.assertEqual(len(obj['suppliers']), 1) supplier = obj['suppliers'][0] def stest_get_log(self): """ Tests gettings the log for all the supplies """ resp = self.client.get('/api/v1/supply/log/') #self.assertEqual(resp.status_code, 200) obj = resp.data #self.assertIsInstance(obj, list) def test_get_without_price(self): """ Tests getting a supply that doesn't have the price where the user is not authorized to view the price """ #Delete the view cost permission from the user self.user.user_permissions.remove(Permission.objects.get(codename='view_cost', content_type=self.ct)) #tests the response resp = self.client.get('/api/v1/supply/1/') self.assertEqual(resp.status_code, 200) #Tests the data returned obj = resp.data self.assertNotIn("cost", obj) def test_get_types(self): """ Tests getting the different types used to describe supplies """ resp = self.client.get('/api/v1/supply/type/') #self.assertEqual(resp.status_code, 200) type_list = resp.data #self.assertIn('wood', type_list) def test_post_single_supplier(self): """ Tests posting to the server """ #Test creating an objects. self.assertEqual(Supply.objects.count(), 4) resp = self.client.post('/api/v1/supply/', format='json', data=base_supply) self.assertEqual(resp.status_code, 201, msg=resp) #Tests the dat aturned obj = resp.data self.assertEqual(obj['id'], 5) self.assertEqual(obj['width'], '100.00') self.assertEqual(obj['depth'], '200.00') self.assertEqual(obj['height'], '300.00') self.assertEqual(obj['description'], 'test') self.assertEqual(obj['height_units'], 'yd') self.assertEqual(obj['width_units'], 'm') self.assertEqual(obj['notes'], 'This is awesome') self.assertIn('type', obj) self.assertEqual(obj['type'], 'wood') self.assertIn('suppliers', obj) self.assertEqual(len(obj['suppliers']), 1) #Test Supplier supplier = obj['suppliers'][0] self.assertEqual(supplier['reference'], 'A2234') self.assertEqual(supplier['cost'], Decimal('100')) #TEsts the object created supply = Supply.objects.order_by('-id').all()[0] supply.supplier = supply.suppliers.all()[0] self.assertEqual(supply.id, 5) self.assertEqual(supply.width, 100) self.assertEqual(supply.depth, 200) self.assertEqual(supply.height, 300) #self.assertEqual(supply.reference, 'A2234') self.assertEqual(supply.description, 'test') #self.assertEqual(supply.cost, 100) self.assertEqual(supply.height_units, 'yd') self.assertEqual(supply.width_units, 'm') self.assertEqual(supply.notes, 'This is awesome') self.assertIsNotNone(supply.type) self.assertEqual(supply.type, 'wood') self.assertIsNotNone(supply.suppliers) self.assertEqual(supply.suppliers.count(), 1) def test_posting_with_custom_type(self): """ Testing creating a new resource via POST that has a custom type """ #Testing returned types pre POST resp0 = self.client.get('/api/v1/supply/type/', format='json') self.assertEqual(resp0.status_code, 200, msg=resp0) type_list = resp0.data self.assertNotIn('egg', type_list) self.assertIn('wood', type_list) self.assertEqual(len(type_list), 1) #POST modified_supply = base_supply.copy() modified_supply['type'] = 'egg' resp = self.client.post('/api/v1/supply/', format='json', data=modified_supply) self.assertEqual(resp.status_code, 201) #Tests the response obj = resp.data self.assertIn('type', obj) self.assertNotIn('custom-type', obj) self.assertEqual(obj['type'], 'egg') """ resp2 = self.client.get('/api/v1/supply/type/', format='json') self.assertHttpOK(resp2) type_list = self.deserialize(resp2) self.assertIn('egg', type_list) self.assertIn('wood', type_list) self.assertEqual(len(type_list), 2) """ def test_put(self): """ Tests adding quantity to the item """ #Validate original data supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.quantity, 10.8) self.assertEqual(Log.objects.all().count(), 0) #Prepare modified data for PUT modified_data = base_supply.copy() modified_data['description'] = 'new' modified_data['type'] = 'Glue' modified_data['quantity'] = '11' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) resp = self.client.put('/api/v1/supply/1/?country=TH', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) #Tests the returned data obj = resp.data self.assertEqual(obj['type'], 'Glue') self.assertEqual(float(obj['quantity']), 11) self.assertEqual(obj['description'], 'new') self.assertFalse(obj.has_key('quantity_th')) self.assertFalse(obj.has_key('quantity_kh')) #Tests the resource in the database supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.type, 'Glue') self.assertEqual(supply.country, 'TH') self.assertEqual(supply.description, 'new') self.assertEqual(supply.quantity, 11) self.assertEqual(Log.objects.all().count(), 1) log = Log.objects.all()[0] self.assertEqual(log.action, 'ADD') self.assertEqual(log.quantity, Decimal('0.2')) self.assertEqual(log.message, "Added 0.2ml to new") def test_put_without_quantity_change(self): """ Tests adding quantity to the item """ #Validate original data supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.quantity, 10.8) self.assertEqual(Log.objects.all().count(), 0) #Prepare modified data for PUT modified_data = base_supply.copy() modified_data['description'] = 'new' modified_data['type'] = 'Glue' modified_data['quantity'] = '10.8' modified_data['width_units'] = 'cm' modified_data['depth_units'] = 'cm' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) resp = self.client.put('/api/v1/supply/1/?country=TH', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) #Tests the returned data obj = resp.data self.assertEqual(obj['type'], 'Glue') self.assertEqual(float(obj['quantity']), 10.8) self.assertEqual(obj['description'], 'new') self.assertEqual(obj['width_units'], 'cm') self.assertEqual(obj['depth_units'], 'cm') self.assertFalse(obj.has_key('quantity_th')) self.assertFalse(obj.has_key('quantity_kh')) #Tests the resource in the database supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.type, 'Glue') self.assertEqual(supply.country, 'TH') self.assertEqual(supply.description, 'new') self.assertEqual(supply.quantity, 10.8) self.assertEqual(Log.objects.all().count(), 0) def test_put_subtracting_quantity_to_0(self): """ Tests adding quantity to the item """ #Validate original data supply = Supply.objects.get(pk=1) supply.country = 'TH' supply.quantity = 1 supply.save() self.assertEqual(supply.quantity, 1) self.assertEqual(Log.objects.all().count(), 0) #Prepare modified data for PUT modified_data = base_supply.copy() modified_data['description'] = 'new' modified_data['type'] = 'Glue' modified_data['quantity'] = '0' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) resp = self.client.put('/api/v1/supply/1/?country=TH', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) #Tests the returned data obj = resp.data self.assertEqual(obj['quantity'], 0) self.assertFalse(obj.has_key('quantity_th')) self.assertFalse(obj.has_key('quantity_kh')) #Tests the resource in the database supply = Supply.objects.get(pk=1) supply.country = 'TH' self.assertEqual(supply.quantity, 0) log = Log.objects.all().order_by('-id')[0] self.assertEqual(Log.objects.all().count(), 1) self.assertEqual(log.quantity, 1) self.assertEqual(log.action, 'SUBTRACT') @unittest.skip('Not yet implemented') def test_add(self): """ Tests adding a quantity to the specific url """ self.assertEqual(Supply.objects.get(pk=1).quantity, float('10.8')) resp = self.client.post('/api/v1/supply/1/add/?quantity=5', format='json') self.assertEqual(Supply.objects.get(pk=1).quantity, float('15.8')) @unittest.skip('Not yet implemented') def test_subract(self): """ Tests adding a quantity to the specific url """ self.assertEqual(Supply.objects.get(pk=1).quantity, float('10.8')) resp = self.client.post('/api/v1/supply/1/subtract/?quantity=5', format='json') self.assertEqual(Supply.objects.get(pk=1).quantity, float('5.8')) def test_put_add_quantity(self): """ Tests adding quantity to the item """ modified_data = base_supply.copy() modified_data['quantity'] = '14' modified_data['description'] = 'new' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float('10.8')) resp = self.client.put('/api/v1/supply/1/', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float('14')) self.assertEqual(Supply.objects.get(pk=1).description, 'new') #Tests the returned data obj = resp.data self.assertEqual(float(obj['quantity']), float('14')) def test_put_subtract_quantity(self): """ Tests adding quantity to the item """ modified_data = base_supply.copy() modified_data['quantity'] = '8' #Tests the api and the response self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float('10.8')) resp = self.client.put('/api/v1/supply/1/', format='json', data=modified_data) self.assertEqual(resp.status_code, 200) self.assertEqual(Supply.objects.count(), 4) self.assertEqual(Supply.objects.get(pk=1).quantity, float('8')) #Tests the returned data obj = resp.data self.assertEqual(float(obj['quantity']), float('8')) def test_put_to_create_new_product(self): """ Tests adding a new supplier/product to the supply """ logger.debug("\n\nTest creating a new supply/product via PUT\n") modified_data = copy.deepcopy(base_supply) modified_data['suppliers'] = [{'id': 1, 'supplier': {'id': 1}, 'reference': 'BOO', 'cost': '123.45'}, {'reference': 'A4', 'cost': '19.99', 'purchasing_units': 'ml', 'quantity_per_purchasing_unit': 4, 'supplier': {'id': 2}}] resp = self.client.put('/api/v1/supply/1/', format='json', data=modified_data) self.assertEqual(resp.status_code, 200, msg=resp) self.assertEqual(Supply.objects.count(), 4) obj = resp.data self.assertIn('suppliers', obj) self.assertEqual(len(obj['suppliers']), 2) supplier1 = obj['suppliers'][0] self.assertEqual(supplier1['reference'], 'BOO') self.assertEqual(supplier1['cost'], Decimal('123.45')) supplier2 = obj['suppliers'][1] self.assertEqual(supplier2['reference'], 'A4') self.assertEqual(supplier2['cost'], Decimal('19.99')) self.assertEqual(supplier2['purchasing_units'], 'ml') self.assertEqual(supplier2['quantity_per_purchasing_unit'], Decimal('4')) self.assertEqual(supplier2['supplier']['id'], 2) def test_updating_multiple_supplies(self): """ Test that we can update the quantities of multiple supplies """ data = [{'id': 1, 'description': 'poooo', 'quantity': 9, 'employee': {'id': 1}}, {'id':2, 'quantity': 12.3, 'employee': {'id': 1}}] resp = self.client.put('/api/v1/supply/', format='json', data=data) self.assertEqual(resp.status_code, 200, msg=resp) supplies = resp.data supply1 = supplies[0] self.assertEqual(supply1['quantity'], 9) self.assertEqual(supply1['description'], u'poooo') supply1_obj = Supply.objects.get(pk=1) self.assertEqual(supply1_obj.quantity, 9) supply2 = supplies[1] self.assertEqual(supply2['quantity'], Decimal('12.3')) supply2_obj = Supply.objects.get(pk=2) self.assertEqual(supply2_obj.quantity, 12.3) log1 = Log.objects.all()[0] self.assertEqual(log1.action, "SUBTRACT") self.assertEqual(log1.quantity, Decimal('1.8')) self.assertIsNotNone(log1.employee) self.assertEqual(log1.employee.id, 1) self.assertEqual(log1.employee.first_name, "John") self.assertEqual(log1.employee.last_name, "Smith") log2 = Log.objects.all()[1] self.assertEqual(log2.action, "ADD") self.assertEqual(log2.quantity, Decimal('1.5')) self.assertIsNotNone(log2.employee) self.assertEqual(log2.employee.id, 1) self.assertEqual(log2.employee.first_name, "John") self.assertEqual(log2.employee.last_name, "Smith") def test_bulk_update_with_new_supply(self): """ Test that we can update the quantities of multiple supplies """ data = [{'id': 1, 'description': 'poooo', 'quantity': 9, 'employee': {'id': 1}}, {'id':2, 'quantity': 12.3, 'employee': {'id': 1}}, {'description': 'slat wood', 'quantity': 10}] resp = self.client.put('/api/v1/supply/', format='json', data=data) self.assertEqual(resp.status_code, 200, msg=resp) supplies = resp.data supply1 = supplies[0] self.assertEqual(supply1['quantity'], 9) self.assertEqual(supply1['description'], u'poooo') supply1_obj = Supply.objects.get(pk=1) self.assertEqual(supply1_obj.quantity, 9) supply2 = supplies[1] self.assertEqual(supply2['quantity'], Decimal('12.3')) supply2_obj = Supply.objects.get(pk=2) self.assertEqual(supply2_obj.quantity, 12.3) supply3 = supplies[2] logger.debug(supply3) self.assertEqual(supply3['description'], 'slat wood') self.assertEqual(supply3['quantity'], Decimal('10')) supply3_obj = Supply.objects.get(pk=supply3['id']) self.assertEqual(supply3_obj.description, 'slat wood') self.assertEqual(supply3_obj.quantity, 10) log1 = Log.objects.all()[0] self.assertEqual(log1.action, "SUBTRACT") self.assertEqual(log1.quantity, Decimal('1.8')) self.assertIsNotNone(log1.employee) self.assertEqual(log1.employee.id, 1) self.assertEqual(log1.employee.first_name, "John") self.assertEqual(log1.employee.last_name, "Smith") log2 = Log.objects.all()[1] self.assertEqual(log2.action, "ADD") self.assertEqual(log2.quantity, Decimal('1.5')) self.assertIsNotNone(log2.employee) self.assertEqual(log2.employee.id, 1) self.assertEqual(log2.employee.first_name, "John") self.assertEqual(log2.employee.last_name, "Smith")