Ejemplo n.º 1
0
class TestDefaultParametersValue(unittest.TestCase):
    """
    """

    def setUp(self):
        self.client = Spore(name='my_client', base_url='http://my_url.org',
                            methods={
                                'my_method': {
                                    'method': 'GET',
                                    'path': '/api'
                                },
                                'my_req_method': {
                                    'method': 'GET',
                                    'path': '/api',
                                    'required_params': ['format']
                                }, 'my_opt_method': {
                                    'method': 'GET',
                                    'path': '/api',
                                    'optional_params': ['format']
                                }, 'my_both_method': {
                                    'method': 'GET',
                                    'path': '/api',
                                    'required_params': ['format'],
                                    'optional_params': ['username']
                                }, 'my_super_method': {
                                    'method': 'GET',
                                    'path': '/api',
                                    'required_params': ['format',
                                                        'last_name'],
                                    'optional_params': ['first_name']
                                }})
        self.client.add_default('format', 'json')
        self.client.add_default('username', 'toto')

    def test_default(self):
        self.assertEqual(self.client.defaults['format'], 'json')
        self.assertEqual(self.client.defaults['username'], 'toto')

    def test_method_default(self):
        self.assertEqual(self.client.my_method.defaults['format'], 'json')
        self.assertEqual(self.client.my_method.defaults['username'], 'toto')

    def test_remove_default(self):
        self.client.remove_default('format')
        self.assertEqual(self.client.defaults['username'], 'toto')

    def test_method_remove_default(self):
        self.client.remove_default('format')
        self.assertEqual(self.client.my_method.defaults['username'], 'toto')

    def test_remove_default_not_existing(self):
        self.client.remove_default('id')
        self.assertEqual(self.client.defaults['format'], 'json')
        self.assertEqual(self.client.defaults['username'], 'toto')

    def test_not_impacted(self):
        params = self.client.my_method.build_params()
        self.assertListEqual(params, [])

    def test_required(self):
        params = self.client.my_req_method.build_params()
        self.assertListEqual(params, [('format', 'json')])

    def test_optional(self):
        params = self.client.my_opt_method.build_params()
        self.assertListEqual(params, [('format', 'json')])

    def test_both(self):
        params = self.client.my_both_method.build_params()
        self.assertListEqual(sorted(params), 
                             [('format', 'json'), ('username', 'toto')])

    def test_with_params(self):
        params = self.client.my_super_method.build_params(last_name='toto')
        self.assertListEqual(sorted(params), 
                             [('format', 'json'), ('last_name', 'toto')])

    def test_missing(self):
        with self.assertRaises(errors.SporeMethodCallError):
            self.client.my_super_method.build_params()