示例#1
0
    def test__get_random_beer_fact__calls_get_beer_fact_from_api_with_params__returns_expected_beer_fact(
            self, mock_get, mock_url):
        expected_data = {
            'data': {
                'name': 'test',
                'id': 'test_id',
                'abv': '1.0',
                'ibu': 99,
                'style': {
                    'description': 'test_description'
                }
            }
        }
        data = expected_data.get('data')
        expected_beer_fact = BeerFact(id=data.get('id'),
                                      name=data.get('name'),
                                      abv=data.get('abv'),
                                      style=data.get('style'),
                                      ibu=data.get('ibu'))
        mock_get.return_value = expected_data
        mock_url.return_value = 'http:/useless.org'
        beer = Beer()
        beer_fact = beer.get_random_beer_fact()

        self.assertEqual(beer_fact, expected_beer_fact)
        self.assertEqual(
            mock_get.call_args_list[0],
            call(url=mock_url(), params={'key': settings.BEER_API_KEY}))
示例#2
0
    def test__convert_result_to_beer_fact__paginated__returns_first_result_as_beer_fact(
            self):
        mock_api_response_data = {
            'currentPage':
            1,
            'data': [
                {
                    'name': 'test',
                    'id': 'test_id',
                    'abv': '1.0',
                    'ibu': 99,
                    'style': {
                        'description': 'test_description'
                    }
                },
            ]
        }
        data = mock_api_response_data['data'][0]
        expected_beer_fact = BeerFact(id=data.get('id'),
                                      name=data.get('name'),
                                      abv=data.get('abv'),
                                      style=data.get('style'),
                                      ibu=data.get('ibu'))
        beer = Beer()

        result = beer.convert_result_to_beer_fact(mock_api_response_data)

        self.assertEqual(result, expected_beer_fact)
示例#3
0
    def test__get_beer_fact__returns_beer_fact(self, mock_random_beer_fact):
        expected_beer_fact = BeerFact(
            name='test',
            id='test_1',
            abv=1.0,
            ibu=99,
            style=dict(description='test_description'))

        mock_random_beer_fact.return_value = expected_beer_fact
        view = BeerFactView()
        beer_fact = view.get_beer_fact()
        self.assertEqual(beer_fact, expected_beer_fact)
示例#4
0
    def test__convert_result_to_beer_fact__no_name__returns_empty_beer_fact(
            self):
        expected_beer_fact = BeerFact(id=None,
                                      name=None,
                                      abv=None,
                                      style=None,
                                      ibu=None)
        mock_api_response_data = {'data': {'id': 'test_id'}}
        beer = Beer()
        result = beer.convert_result_to_beer_fact(mock_api_response_data)

        self.assertEqual(result, expected_beer_fact)
示例#5
0
    def test__post__request_with_valid_query_params__returns_content_type_text_xml(
            self, mock_random_beer_fact):
        expected_beer_fact = BeerFact(
            name='test',
            id='test_1',
            abv=1.0,
            ibu=99,
            style=dict(description='test_description'))

        mock_random_beer_fact.return_value = expected_beer_fact

        rf = RequestFactory()
        request = rf.post({'beerid': 'test_id'})

        view = BeerFactView()
        response = view.post(request)

        self.assertEqual(response._headers.get('content-type'),
                         ('Content-Type', 'text/xml'))
    def test__text_beer_info_to_number__populated_beer_fact_no_abv__calls_message_creat_with_args(
            self, mock_twilio_client):
        assertable_mock = mock.Mock()
        mock_twilio_client.return_value = assertable_mock
        beer_fact = BeerFact(name='test',
                             id='test_1',
                             abv=None,
                             ibu=99,
                             style=dict(description='test_description'))
        from_ = '5551234'
        to_ = '8675309'

        view = BeerTextView()
        view.text_beer_info_to_number(beer_fact=beer_fact,
                                      from_=from_,
                                      to_=to_)
        self.assertTrue(
            assertable_mock.messages.create.call_args_list[0],
            mock.call(
                body='Hi there! You were listening to: test, Unknown, IBU: 99',
                from_='8675309',
                to='5551234'))
示例#7
0
    def test__build_response__populated_beer_fact_missing_style_description__returns_populated_voice_response(
            self):
        beer_fact = BeerFact(name='test',
                             id='test_1',
                             abv=None,
                             ibu=99,
                             style=dict())
        view = BeerFactView()
        response = view.build_response(beer_fact)

        expected_response = '<?xml version="1.0" encoding="UTF-8"?>' \
                            '<Response>' \
                            '<Say voice="Polly.Brian">Hello! I am going to drop a dank beer on you.</Say>' \
                            '<Gather action="/beertext?beerid=test_1" actionOnEmptyResult="false" ' \
                            'finishOnKey="" numDigits="1" timeout="200">' \
                            '<Say voice="Polly.Brian">Our beer today is test</Say>' \
                            '</Gather>' \
                            '<Say voice="Polly.Brian">Wow! You made it to the end. Be excellent to each other.</Say>' \
                            '<Hangup />' \
                            '</Response>'

        self.assertEqual(response.to_xml(), expected_response)
示例#8
0
    def test__post__request_with_valid_query_params__returns_expected_content(
            self, mock_build_response, mock_random_beer_fact):
        expected_beer_fact = BeerFact(
            name='test',
            id='test_1',
            abv=1.0,
            ibu=99,
            style=dict(description='test_description'))

        expected_response_data = b'It worked!'
        mock_response = mock.Mock()
        mock_response.to_xml.return_value = expected_response_data
        mock_build_response.return_value = mock_response
        mock_random_beer_fact.return_value = expected_beer_fact

        rf = RequestFactory()
        request = rf.post('/some/path/')

        view = BeerFactView()
        response = view.post(request)

        self.assertEqual(response.content, expected_response_data)
 def setUp(self) -> None:
     self.beer_fact = BeerFact(name='test',
                               id='test_1',
                               abv=1.0,
                               ibu=99,
                               style=dict(description='test_description'))