コード例 #1
0
 def test_populate(self):
     """It populates model properties from a map of values"""
     pref = UserPreferenceModel()
     pref.populate({
         'id': 1,
         'user_id': 2,
         'ingredient_id': 3,
         'coefficient': 0.5
     })
     self.assertEqual(pref.id, 1)
     self.assertEqual(pref.user_id, 2)
     self.assertEqual(pref.ingredient_id, 3)
     self.assertEqual(pref.coefficient, 0.5)
コード例 #2
0
    def test_delete(self):
        """It deletes a model instance from the database"""
        mock_dao = mock.MagicMock()
        mock_dao.delete.return_value = True

        pref = UserPreferenceModel(mock_dao)
        pref.id = 1

        # expect a model instance to be deleted from the dao
        self.assertTrue(pref.delete())
        self.assertTrue(mock_dao.delete.called)
        self.assertEqual(mock_dao.delete.call_args[0][0], 1)
        self.assertIsNone(pref.id)

        # expect trying to delete a pref with no
        mock_dao.delete.reset_mock()
        self.assertFalse(pref.delete())
        self.assertFalse(mock_dao.delete.called)
コード例 #3
0
    def test_to_dict(self):
        """It serlializes as a dict representation"""

        pref = UserPreferenceModel()
        pref.id = 101
        pref.user_id = 1
        pref.ingredient_id = 2
        pref.coefficient = 0.9

        self.assertDictEqual(pref.to_dict(), {
            'id': 101,
            'user_id': 1,
            'ingredient_id': 2,
            'coefficient': 0.9
        })
コード例 #4
0
    def test_load_by_user_ingredient(self):
        """It loads a model instance by its ID"""
        mock_dao = mock.MagicMock()
        mock_dao.get_by_user_ingredient.return_value = {
            'id': 1,
            'user_id': 1,
            'ingredient_id': 4,
            'coefficient': 0.8
        }

        pref = UserPreferenceModel(mock_dao)
        pref.load_by_user_ingredient(1, 1)
        self.assertEqual(pref.id, 1)
        self.assertEqual(pref.user_id, 1)
        self.assertEqual(pref.ingredient_id, 4)
        self.assertEqual(pref.coefficient, 0.8)

        # expect value error to be raised if no record is found
        mock_dao.get_by_user_ingredient.return_value = None
        user_no_result = UserPreferenceModel(dao=mock_dao)
        with self.assertRaises(ValueError):
            user_no_result.load_by_user_ingredient(-1, 1)
コード例 #5
0
    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
        pref = UserPreferenceModel(mock_dao)
        pref.user_id = 1
        pref.ingredient_id = 2
        pref.coefficient = 0.5
        pref.save()
        self.assertTrue(mock_dao.create.called)
        self.assertEqual(pref.id, 101)
        self.assertDictEqual(mock_dao.create.call_args[1], {
            'user_id': 1,
            'ingredient_id': 2,
            'coefficient': 0.5
        })

        # Expect a model instance to be updated if an ID is set
        pref.coefficient = 0.9
        pref.save()
        self.assertTrue(mock_dao.update.called)
        self.assertDictEqual(mock_dao.update.call_args[1], {
            'id': 101,
            'user_id': 1,
            'ingredient_id': 2,
            'coefficient': 0.9
        })

        # Expect a ValueError to be raised if validate returns False
        m_validate.return_value = False
        pref.last_name = None
        with self.assertRaises(ValueError):
            pref.save()
コード例 #6
0
    def test_validate(self):
        """It validates the models properties"""
        pref = UserPreferenceModel()

        # Expect user to be invalid because there is no last name
        pref.user_id = 1
        self.assertFalse(pref.validate())

        # Expect a user with first name and last name to be valid
        pref.ingredient_id = 1
        pref.coefficient = 0.5
        self.assertTrue(pref.validate())

        # Expect a string id to be invalid
        pref.user_id = "1"
        self.assertFalse(pref.validate())
        pref.user_id = 1

        # Expect a coefficient value > 1 to be invalid
        pref.coefficient = 1.1
        self.assertFalse(pref.validate())
        pref.coefficient = 0.5