Esempio n. 1
0
    def test_resource_find_pieces(self):
        ''' test that we can find a piece in the cake '''
        keys = ['red', 'blue', 'green', 'yellow', 'orange', 'purple']
        vals = dict((k, F(1,1)) for k in keys)
        cake = CollectionResource(keys)
        user = CollectionPreference('mark', vals)
        piece = cake.find_piece(user, 3)
        actual = CollectionResource(['red', 'blue', 'green'])
        self.assertEqual(piece.value, actual.value)

        vals = {'red':10, 'blue':20, 'green':30, 'yellow':15, 'orange':25}
        keys = vals.keys()
        cake = CollectionResource(keys)
        user = CollectionPreference('mark', vals)
        self.assertEqual(50, user.value_of(cake.find_piece(user, 50)))
        self.assertEqual(60, user.value_of(cake.find_piece(user, 60)))
        self.assertEqual(70, user.value_of(cake.find_piece(user, 70)))

        vals = {'red':10, 'blue':20, 'green':30, 'orange':40}
        keys = vals.keys()
        cake = CollectionResource(keys)
        user = CollectionPreference('mark', vals)
        self.assertEqual(50, user.value_of(cake.find_piece(user, 50)))
        self.assertEqual(60, user.value_of(cake.find_piece(user, 60)))
        self.assertEqual(70, user.value_of(cake.find_piece(user, 70)))

        self.assertRaises(ValueError, lambda: cake.find_piece(user, 150))
Esempio n. 2
0
    def test_preference_create(self):
        ''' test that the preference is created correctly '''
        vals  = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
        keys  = sorted(vals.keys())
        cake  = CollectionResource(keys)
        user1 = CollectionPreference('mark', vals)
        user2 = OrdinalPreference('john', keys)
        self.assertEqual(user1.value_of(cake), user2.value_of(cake))

        random.seed(1) # to ensure same test
        cake  = CollectionResource(['a'])
        user3 = OrdinalPreference.random(cake)
        self.assertNotEqual(user2.value_of(cake), user3.value_of(cake))
Esempio n. 3
0
    def test_preference_create(self):
        ''' test that the preference factory methods work '''
        path  = os.path.join(os.path.abspath('contrib'), 'data')
        path  = os.path.join(path, 'collection')
        path  = os.path.join(path, 'uniform')
        user1 = CollectionPreference.from_file(path)
        keys  = user1.values.keys()
        prefs = dict((p, 0.25) for p in keys)
        user2 = CollectionPreference('user2', prefs)
        cake  = CollectionResource(keys)
        self.assertEqual(user1.value_of(cake), user2.value_of(cake))

        users = [CollectionPreference.random(cake) for i in range(5)]
        for user in users:
            self.assertTrue(0.95 <= user.value_of(cake) <= 1.05)