Example #1
0
 def dict_to_model(self, dict_data, line_id):
     model = Formatter.dict_to_model(self, dict_data)
     model.line_id = line_id
     return model
Example #2
0
 def dict_to_model(self, api_data, user_id):
     model = Formatter.dict_to_model(self, api_data)
     model.user_id = user_id
     return model
Example #3
0
class TestFormatter(unittest.TestCase):

    def setUp(self):
        self._api_data_dict = {
            'api_key_1': 1,
            'api_key_2': '2',
            'api_key_3': [1, '2']
        }
        self._model_data_dict = {
            'model_key_1': 1,
            'model_key_2': '2',
            'model_key_3': [1, '2']
        }

        self._mapper = Mock()
        self._mapper.MAPPING = {
            'model_key_1': 'api_key_1',
            'model_key_2': 'api_key_2',
            'model_key_3': 'api_key_3'
        }
        self._mapper.add_links_to_dict = Mock()
        self._mapper.add_links_to_dict.side_effect = self.add_links_to_dict

        self._serializer = Mock(serializer)
        self._serializer.decode.side_effect = json.loads
        self._serializer.encode.side_effect = json.dumps

        self.model = Mock(
            model_key_1=1,
            model_key_2='2',
            model_key_3=[1, '2']
        )
        self.model.to_user_data.return_value = self._model_data_dict

        self._model_class = Mock(AbstractModels)
        self._model_class.from_user_data.return_value = self.model

        self.formatter = Formatter(self._mapper,
                                   self._serializer,
                                   self._model_class)

    def add_links_to_dict(self, data_dict, _):
        data_dict.update({'links': 'links'})

    def test_list_to_api(self):
        expected_result = '{"items": [{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}, {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}], "total": 2}'

        list_model = [self.model, self.model]

        result = self.formatter.list_to_api(list_model)

        assert_that(result, equal_to(expected_result))

    def test_list_to_api_with_total(self):
        expected_result = '{"items": [{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}, {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}], "total": 10}'
        total = 10

        list_model = [self.model, self.model]

        result = self.formatter.list_to_api(list_model, total)

        assert_that(result, equal_to(expected_result))

    @patch('xivo_restapi.helpers.mapper.map_to_api')
    def test_to_api(self, map_to_api):
        expected_result = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}'

        map_to_api.return_value = self._api_data_dict

        result = self.formatter.to_api(self.model)

        assert_that(result, equal_to(expected_result))
        self.model.to_user_data.assert_called_once_with()
        map_to_api.assert_called_once_with(self._mapper.MAPPING, self._model_data_dict)
        self._mapper.add_links_to_dict.assert_called_once_with(self._api_data_dict, self.model)
        self._serializer.encode.assert_called_once_with(self._api_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_to_model(self, map_to_model):
        expected_result = self.model
        api_data = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}'

        map_to_model.return_value = self._model_data_dict

        result = self.formatter.to_model(api_data)

        assert_that(result, equal_to(expected_result))
        self._serializer.decode.assert_called_once_with(api_data)
        map_to_model.assert_called_once_with(self._mapper.MAPPING, self._api_data_dict)
        self._model_class.from_user_data.assert_called_once_with(self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_dict_to_model(self, map_to_model):
        expected_result = self.model
        dict_data = {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}

        map_to_model.return_value = self._model_data_dict

        result = self.formatter.dict_to_model(dict_data)

        assert_that(result, equal_to(expected_result))
        map_to_model.assert_called_once_with(self._mapper.MAPPING, self._api_data_dict)
        self._model_class.from_user_data.assert_called_once_with(self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_update_model(self, map_to_model):
        my_model = Mock(model_key_1='lol',
                        model_key_2='lol',
                        model_key_3=['lol', 'lol'])

        def update_from_data(data_dict):
            for key, val in data_dict.iteritems():
                setattr(my_model, key, val)

        my_model.update_from_data.side_effect = update_from_data

        api_data = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}'

        map_to_model.return_value = self._model_data_dict

        self.formatter.update_model(api_data, my_model)

        assert_that(my_model, all_of(
            has_property('model_key_1', self._model_data_dict['model_key_1']),
            has_property('model_key_2', self._model_data_dict['model_key_2']),
            has_property('model_key_3', self._model_data_dict['model_key_3'])
        ))
        map_to_model.assert_called_once_with(self._mapper.MAPPING, self._api_data_dict)
        my_model.update_from_data.assert_called_once_with(self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_update_dict_model(self, map_to_model):
        my_model = Mock(model_key_1='lol',
                        model_key_2='lol',
                        model_key_3=['lol', 'lol'])

        def update_from_data(data_dict):
            for key, val in data_dict.iteritems():
                setattr(my_model, key, val)

        my_model.update_from_data.side_effect = update_from_data

        data_dict = {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}

        map_to_model.return_value = self._model_data_dict

        self.formatter.update_dict_model(data_dict, my_model)

        assert_that(my_model, all_of(
            has_property('model_key_1', self._model_data_dict['model_key_1']),
            has_property('model_key_2', self._model_data_dict['model_key_2']),
            has_property('model_key_3', self._model_data_dict['model_key_3'])
        ))
        map_to_model.assert_called_once_with(self._mapper.MAPPING, self._api_data_dict)
        my_model.update_from_data.assert_called_once_with(self._model_data_dict)
Example #4
0
 def dict_to_model(self, line_id, api_data):
     model = Formatter.dict_to_model(self, api_data)
     model.line_id = line_id
     return model
Example #5
0
 def dict_to_model(self, api_data, user_id):
     model = Formatter.dict_to_model(self, api_data)
     model.user_id = user_id
     return model
Example #6
0
 def dict_to_model(self, line_id, api_data):
     model = Formatter.dict_to_model(self, api_data)
     model.line_id = line_id
     return model
Example #7
0
class TestFormatter(unittest.TestCase):
    def setUp(self):
        self._api_data_dict = {
            'api_key_1': 1,
            'api_key_2': '2',
            'api_key_3': [1, '2']
        }
        self._model_data_dict = {
            'model_key_1': 1,
            'model_key_2': '2',
            'model_key_3': [1, '2']
        }

        self._mapper = Mock()
        self._mapper.MAPPING = {
            'model_key_1': 'api_key_1',
            'model_key_2': 'api_key_2',
            'model_key_3': 'api_key_3'
        }
        self._mapper.add_links_to_dict = Mock()
        self._mapper.add_links_to_dict.side_effect = self.add_links_to_dict

        self._serializer = Mock(serializer)
        self._serializer.decode.side_effect = json.loads
        self._serializer.encode.side_effect = json.dumps

        self.model = Mock(model_key_1=1, model_key_2='2', model_key_3=[1, '2'])
        self.model.to_user_data.return_value = self._model_data_dict

        self._model_class = Mock(AbstractModels)
        self._model_class.from_user_data.return_value = self.model

        self.formatter = Formatter(self._mapper, self._serializer,
                                   self._model_class)

    def add_links_to_dict(self, data_dict, _):
        data_dict.update({'links': 'links'})

    def test_list_to_api(self):
        expected_result = '{"items": [{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}, {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}], "total": 2}'

        list_model = [self.model, self.model]

        result = self.formatter.list_to_api(list_model)

        assert_that(result, equal_to(expected_result))

    def test_list_to_api_with_total(self):
        expected_result = '{"items": [{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}, {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}], "total": 10}'
        total = 10

        list_model = [self.model, self.model]

        result = self.formatter.list_to_api(list_model, total)

        assert_that(result, equal_to(expected_result))

    @patch('xivo_restapi.helpers.mapper.map_to_api')
    def test_to_api(self, map_to_api):
        expected_result = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"], "links": "links"}'

        map_to_api.return_value = self._api_data_dict

        result = self.formatter.to_api(self.model)

        assert_that(result, equal_to(expected_result))
        self.model.to_user_data.assert_called_once_with()
        map_to_api.assert_called_once_with(self._mapper.MAPPING,
                                           self._model_data_dict)
        self._mapper.add_links_to_dict.assert_called_once_with(
            self._api_data_dict, self.model)
        self._serializer.encode.assert_called_once_with(self._api_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_to_model(self, map_to_model):
        expected_result = self.model
        api_data = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}'

        map_to_model.return_value = self._model_data_dict

        result = self.formatter.to_model(api_data)

        assert_that(result, equal_to(expected_result))
        self._serializer.decode.assert_called_once_with(api_data)
        map_to_model.assert_called_once_with(self._mapper.MAPPING,
                                             self._api_data_dict)
        self._model_class.from_user_data.assert_called_once_with(
            self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_dict_to_model(self, map_to_model):
        expected_result = self.model
        dict_data = {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}

        map_to_model.return_value = self._model_data_dict

        result = self.formatter.dict_to_model(dict_data)

        assert_that(result, equal_to(expected_result))
        map_to_model.assert_called_once_with(self._mapper.MAPPING,
                                             self._api_data_dict)
        self._model_class.from_user_data.assert_called_once_with(
            self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_update_model(self, map_to_model):
        my_model = Mock(model_key_1='lol',
                        model_key_2='lol',
                        model_key_3=['lol', 'lol'])

        def update_from_data(data_dict):
            for key, val in data_dict.iteritems():
                setattr(my_model, key, val)

        my_model.update_from_data.side_effect = update_from_data

        api_data = '{"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}'

        map_to_model.return_value = self._model_data_dict

        self.formatter.update_model(api_data, my_model)

        assert_that(
            my_model,
            all_of(
                has_property('model_key_1',
                             self._model_data_dict['model_key_1']),
                has_property('model_key_2',
                             self._model_data_dict['model_key_2']),
                has_property('model_key_3',
                             self._model_data_dict['model_key_3'])))
        map_to_model.assert_called_once_with(self._mapper.MAPPING,
                                             self._api_data_dict)
        my_model.update_from_data.assert_called_once_with(
            self._model_data_dict)

    @patch('xivo_restapi.helpers.mapper.map_to_model')
    def test_update_dict_model(self, map_to_model):
        my_model = Mock(model_key_1='lol',
                        model_key_2='lol',
                        model_key_3=['lol', 'lol'])

        def update_from_data(data_dict):
            for key, val in data_dict.iteritems():
                setattr(my_model, key, val)

        my_model.update_from_data.side_effect = update_from_data

        data_dict = {"api_key_1": 1, "api_key_2": "2", "api_key_3": [1, "2"]}

        map_to_model.return_value = self._model_data_dict

        self.formatter.update_dict_model(data_dict, my_model)

        assert_that(
            my_model,
            all_of(
                has_property('model_key_1',
                             self._model_data_dict['model_key_1']),
                has_property('model_key_2',
                             self._model_data_dict['model_key_2']),
                has_property('model_key_3',
                             self._model_data_dict['model_key_3'])))
        map_to_model.assert_called_once_with(self._mapper.MAPPING,
                                             self._api_data_dict)
        my_model.update_from_data.assert_called_once_with(
            self._model_data_dict)