def test_remove_response(self, c, review_id, side_effect=None, err_type=None, err_msg=None): # Create mocks review_manager.Review = MagicMock() rev_object = MagicMock() self.offering.owner_organization = self.org self.org.managers = [self.user.pk] rev_object.offering = self.offering review_manager.Review.objects.get.return_value = rev_object # Call the side effect if needed if side_effect: side_effect(self) error = None try: rm = review_manager.ReviewManager() rm.remove_response(self.user, review_id) except Exception as e: error = e if not err_type: self.assertEquals(error, None) # Check calls self.assertEquals(rev_object.response, None) rev_object.save.assert_called_with() else: self.assertTrue(isinstance(error, err_type)) self.assertEquals(unicode(error), err_msg)
def test_remove_review(self, name, user_check, side_effect=None): # Create Mocks #if name == 'manager': # import ipdb; ipdb.set_trace() rev_object = MagicMock() rev_object.pk = '333333' rev_object.user = self.user rev_object.organization = self.org self.user.userprofile.rated_offerings = [self.offering.pk] self.offering.rating = 3.75 self.offering.comments = ['333333', '444444', '555555', '666666'] rev_object.offering = self.offering rev_object.rating = 3 review_manager.Review = MagicMock() review_manager.Review.objects.get.return_value = rev_object if side_effect: side_effect(self, rev_object) error = False try: rm = review_manager.ReviewManager() rm.remove_review(self.user, '333333') except: error = True # Check result self.assertFalse(error) self.assertFalse('333333' in self.offering) self.offering.save.assert_called_once_with() rev_object.delete.assert_called_once_with() # Check user or organization models user_check(self)
def test_create_review(self, review, exp_rating, side_effect=None, org_comment=False, err_type=None, err_msg=None): # Create review mock review_object = MagicMock() rev = MagicMock() rev.pk = '111111' review_object.objects.create.return_value = rev review_manager.Review = review_object # Create review manager rm = review_manager.ReviewManager() if side_effect: side_effect(self) exception = None try: rm.create_review(self.user, self.offering, review) except Exception as e: exception = e # Check calls and objects if not err_type: self.assertEquals(exception, None) if not self.offering.open: review_manager.Purchase.objects.get.assert_called_once_with( owner_organization=self.org, offering=self.offering) review_object.objects.create.assert_called_once_with( user=self.user, organization=self.org, offering=self.offering, timestamp=self.datetime, title=review['title'], comment=review['comment'], rating=review['rating']) self.assertEquals(self.offering.rating, exp_rating) self.assertEquals(self.offering.comments[0], rev.pk) if not org_comment: self.assertTrue( self.offering.pk in self.user.userprofile.rated_offerings) self.user.userprofile.save.assert_called_once_with() else: self.assertTrue({ 'user': self.user.pk, 'offering': self.offering.pk } in self.org.rated_offerings) self.org.save.assert_called_once_with() else: self.assertTrue(isinstance(exception, err_type)) self.assertEquals(unicode(e), err_msg)
def test_update_reviews(self, review_data, review_id, exp_rate, side_effect=None, err_type=None, err_msg=None): #import ipdb; ipdb.set_trace() # Create mocks rev_object = MagicMock() rev_object.user = self.user rev_object.organization = self.org self.offering.rating = 3.75 self.offering.comments = ['333333', '444444', '555555', '666666'] rev_object.offering = self.offering rev_object.rating = 4 review_manager.Review = MagicMock() review_manager.Review.objects.get.return_value = rev_object if side_effect: side_effect(self, rev_object) # Call the method error = None try: rm = review_manager.ReviewManager() rm.update_review(self.user, review_id, review_data) except Exception as e: error = e if not error: self.assertEquals(error, None) # Check new review info self.assertEquals(rev_object.title, review_data['title']) self.assertEquals(rev_object.comment, review_data['comment']) self.assertEquals(rev_object.rating, review_data['rating']) self.assertEquals(rev_object.timestamp, self.datetime) # Check info saved rev_object.save.assert_called_once_with() # Check new offering rating self.assertEquals(self.offering.rating, exp_rate) self.offering.save.assert_called_once_with() else: self.assertTrue(isinstance(error, err_type)) self.assertEquals(unicode(error), err_msg)
def test_create_response(self, response, review_id, side_effect=None, err_type=None, err_msg=None): # Create offering and user mocks review_manager.Review = MagicMock() rev_object = MagicMock() self.offering.owner_organization = self.org self.org.managers = [self.user.pk] rev_object.offering = self.offering review_manager.Review.objects.get.return_value = rev_object resp_object = MagicMock() review_manager.Response = MagicMock() review_manager.Response.return_value = resp_object # Create review mock if side_effect: side_effect(self) # Call the method error = None try: rm = review_manager.ReviewManager() rm.create_response(self.user, review_id, response) except Exception as e: error = e if not err_type: self.assertEquals(error, None) # Check calls self.assertEquals(rev_object.response, resp_object) rev_object.save.assert_called_once_with() review_manager.Response.assert_called_once_with( user=self.user, organization=self.org, timestamp=self.datetime, title=response['title'], response=response['comment']) else: self.assertTrue(isinstance(error, err_type)) self.assertEquals(unicode(e), err_msg)
def test_get_reviews(self, expected_result, start=None, limit=None, err_type=None, err_msg=None): # Create review mock rev = MagicMock() # Create review mock objects review1 = self._create_review_mock(REVIEW1) review2 = self._create_review_mock(REVIEW2) review3 = self._create_review_mock(REVIEW3) rev.objects.filter = MagicMock() filter_obj = MagicMock() filter_obj.order_by.return_value = [review1, review2, review3] rev.objects.filter.return_value = filter_obj review_manager.Review = rev # Call get reviews method excp = None rm = review_manager.ReviewManager() try: response = rm.get_reviews(self.offering, start, limit) except Exception as e: excp = e if not err_type: self.assertEquals(excp, None) # Check calls and returned data rev.objects.filter.assert_called_once_with(offering=self.offering) filter_obj.order_by.assert_called_once_with('-timestamp') self.assertEquals(response, expected_result) else: # Check raised exception self.assertTrue(isinstance(excp, err_type)) self.assertEquals(unicode(excp), err_msg)