def test_load_by_id_with_preferences(self): """It loads a model instance by its ID and joins ingredient preferences""" mock_dao = mock.MagicMock() mock_dao.get_by_id.return_value = { 'id': 1, 'first_name': 'Adam', 'last_name': 'Anderson', 'preferences': [] } user_no_prefs = UserModel(mock_dao) user_no_prefs.load_by_id(1) self.assertEqual(user_no_prefs.preferences, []) mock_preferences = [ { 'ingredient_id': 1, 'coefficient': 0.5 }, { 'ingredient_id': 2, 'coefficient': 0.1 }, ] mock_dao.get_by_id.return_value = { 'id': 1, 'first_name': 'Adam', 'last_name': 'Anderson', 'preferences': mock_preferences } # expect value error to be raised if no record is found user_with_prefs = UserModel(dao=mock_dao) user_with_prefs.load_by_id(1) self.assertEqual(user_with_prefs.preferences, mock_preferences)
def test_delete(self): """It deletes a model instance from the database""" mock_dao = mock.MagicMock() mock_dao.delete.return_value = True user = UserModel(mock_dao) user.id = 1 # expect a user to be deleted from the dao self.assertTrue(user.delete()) self.assertTrue(mock_dao.delete.called) self.assertEqual(mock_dao.delete.call_args[0][0], 1) self.assertIsNone(user.id) # expect trying to delete a user with no mock_dao.delete.reset_mock() self.assertFalse(user.delete()) self.assertFalse(mock_dao.delete.called)
def test_validate(self): """It validates the models properties""" user = UserModel() # Expect user to be invalid because there is no last name user.first_name = "Tom" self.assertFalse(user.validate()) # Expect a user with first name and last name to be valid user.last_name = "Tabarau" self.assertTrue(user.validate()) # Expect a unicode name to be valid user.first_name = u"Tom" user.last_name = u"Tabarau" self.assertTrue(user.validate())
def test_load_by_id(self): """It loads a model instance by its ID""" mock_dao = mock.MagicMock() mock_dao.get_by_id.return_value = { 'id': 1, 'first_name': 'Adam', 'last_name': 'Anderson' } user = UserModel(mock_dao) user.load_by_id(1) self.assertEqual(user.id, 1) self.assertEqual(user.first_name, 'Adam') self.assertEqual(user.last_name, 'Anderson') # expect value error to be raised if no record is found mock_dao.get_by_id.return_value = None user_no_result = UserModel(dao=mock_dao) with self.assertRaises(ValueError): user_no_result.load_by_id(-1)
def test_to_dict(self): """It serlializes as a dict representation""" user = UserModel() user.id = 1 user.first_name = 'Karma' user.last_name = 'Kabana' self.assertDictEqual(user.to_dict(), { 'id': 1, 'first_name': 'Karma', 'last_name': 'Kabana' })
def test_full_name(self): """It returns a person's full name""" user = UserModel() user.first_name = 'Adam' user.last_name = 'Anderson' self.assertEqual(user.full_name, 'Adam Anderson')
def test_get_recommendations(self): """It gets recommended breakfasts""" user = UserModel() user.id = 1 user.first_name = 'Karma' user.last_name = 'Kabana' mock_dao = mock.MagicMock() mock_dao.list_all.return_value = [{ u'breakfast_id': 1, u'ingredient_id': 1, u'coefficient': 0.8 }, { u'breakfast_id': 1, u'ingredient_id': 2, u'coefficient': 0.8 }, { u'breakfast_id': 1, u'ingredient_id': 3, u'coefficient': 0.2 }, { u'breakfast_id': 2, u'ingredient_id': 1, u'coefficient': 0.0 }, { u'breakfast_id': 2, u'ingredient_id': 2, u'coefficient': 0.2 }, { u'breakfast_id': 2, u'ingredient_id': 3, u'coefficient': 0.9 }, { u'breakfast_id': 3, u'ingredient_id': 1, u'coefficient': 0.9 }, { u'breakfast_id': 3, u'ingredient_id': 2, u'coefficient': 0.5 }, { u'breakfast_id': 3, u'ingredient_id': 3, u'coefficient': 0.1 }] # Expect a value error if we have not loaded recommendations with self.assertRaises(ValueError): user.get_recommendations() # Expect an empty list if a user has no preferences user.preferences = {} self.assertEqual(user.get_recommendations(), []) # Expect an empty list if a user has no preferences user.preferences = {2: 0.4, 3: 0.5} recommendations = user.get_recommendations() self.assertEqual(len(recommendations), 3) self.assertEqual(recommendations[0]['breakfast_id'], 2) self.assertEqual(recommendations[1]['breakfast_id'], 1) self.assertEqual(recommendations[2]['breakfast_id'], 3) self.assertEqual(recommendations[2]['score'], 0.25)
def test_save(self, m_validate): """It saves a model instance""" mock_dao = mock.MagicMock() # Expect a new model to be created if no ID is set mock_dao.create.return_value = 101 user = UserModel(mock_dao) user.first_name = 'Carlton' user.last_name = 'Camaro' user.save() self.assertTrue(mock_dao.create.called) self.assertEqual(user.id, 101) self.assertDictEqual(mock_dao.create.call_args[1], { 'first_name': 'Carlton', 'last_name': 'Camaro' }) # Expect a model instance to be updated if an ID is set user.first_name = 'Darl' user.last_name = 'Dominic' user.save() self.assertTrue(mock_dao.update.called) self.assertDictEqual(mock_dao.update.call_args[1], { 'first_name': 'Darl', 'last_name': 'Dominic' }) # Expect a ValueError to be raised if validate returns False m_validate.return_value = False user.last_name = None with self.assertRaises(ValueError): user.save()
def test_populate(self): """It populates model properties from a map of values""" user = UserModel() user.populate({'first_name': 'Adam', 'last_name': 'Anderson'}) self.assertEqual(user.full_name, 'Adam Anderson')