def test_index(self): # AAA's # Arrange from billtracker.views.default import home request: Request = testing.DummyRequest() test_user = User() test_user.id = 1 test_user.bills = [Bill(), Bill(), Bill()] # Act with unittest.mock.patch('billtracker.data.repository.get_user_by_id', return_value=test_user): info = home(request) # Assert self.assertIsNotNone(info['user'])
def test_index_viewmodel(self): # AAA's # Arrange request: Request = testing.DummyRequest() test_user = User() test_user.id = 1 test_user.bills = [Bill(), Bill(), Bill()] # Act with unittest.mock.patch('billtracker.data.repository.get_user_by_id', return_value=test_user): vm = IndexViewModel(request, user_id=1) # Assert self.assertIsNone(vm.error) self.assertIsNotNone(vm.user)
def test_details_vm_no_bill(self): request: Request = testing.DummyRequest() mock_user = User() mock_user.id = 74 request.matchdict['bill_id'] = 200 mock_user.bills = [Bill()] p1 = unittest.mock.patch('billtracker.data.repository.get_user_by_id', return_value=mock_user) p2 = unittest.mock.patch('billtracker.data.repository.get_bill_by_id', return_value=None) with p1, p2: vm = BillDetailsViewModel(request, mock_user.id) self.assertIsNotNone(vm.error) self.assertIn('The bill with ID {} was not found'.format(200), vm.error)
def test_details_get(self): from billtracker.views.default import details_get request: Request = testing.DummyRequest() user_id = 72 request.matchdict['bill_id'] = 700 mock_user = User() mock_user.id = user_id mock_user.bills = [Bill(), Bill(), Bill()] for b in mock_user.bills: b.user_id = user_id p1 = unittest.mock.patch('billtracker.data.repository.get_user_by_id', return_value=mock_user) p2 = unittest.mock.patch('billtracker.data.repository.get_bill_by_id', return_value=mock_user.bills[0]) with p1, p2: info = details_get(request) self.assertIsNotNone(info['user']) self.assertGreater(len(info['user'].bills), 0)
def test_details_vm_bill_not_from_user(self): request: Request = testing.DummyRequest() mock_user = User() mock_user.id = 74 bill = Bill() bill.id = 700 bill.user_id = mock_user.id + 1 request.matchdict['bill_id'] = bill.id mock_user.bills = [bill] p1 = unittest.mock.patch('billtracker.data.repository.get_user_by_id', return_value=mock_user) p2 = unittest.mock.patch('billtracker.data.repository.get_bill_by_id', return_value=bill) with p1, p2: vm = BillDetailsViewModel(request, mock_user.id) self.assertIsNotNone(vm.error) self.assertIsNone(vm.bill) self.assertIn('does not belong to user', vm.error)