Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test fixture encoding #12296

Merged
merged 1 commit into from
Feb 11, 2018
Merged

Conversation

OttoWinter
Copy link
Member

@OttoWinter OttoWinter commented Feb 11, 2018

Description:

For the past week, I've constantly been getting errors when running the py.test tests from within PyCharm and ignored them since they had nothing to do with my code. Example:

tests/components/weather/test_darksky.py:29 (TestDarkSky.test_setup)
self = <tests.components.weather.test_darksky.TestDarkSky testMethod=test_setup>
mock_req = <requests_mock.mocker.Mocker object at 0x1032a5dd8>
mock_get_forecast = <MagicMock name='get_forecast' id='4348075424'>

    @requests_mock.Mocker()
    @patch('forecastio.api.get_forecast', wraps=forecastio.api.get_forecast)
    def test_setup(self, mock_req, mock_get_forecast):
        """Test for successfully setting up the forecast.io platform."""
        uri = (r'https://api.(darksky.net|forecast.io)\/forecast\/(\w+)\/'
               r'(-?\d+\.?\d*),(-?\d+\.?\d*)')
        mock_req.get(re.compile(uri),
>                    text=load_fixture('darksky.json'))

test_darksky.py:37: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../common.py:250: in load_fixture
    return fptr.read()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <encodings.ascii.IncrementalDecoder object at 0x1032a3828>
input = b'{\n    "alerts": [\n        {\n            "description": "...BEACH HAZARDS STATEMENT REMAINS IN EFFECT UNTIL 9 PM P...day",\n        "summary": "Clear for the hour."\n    },\n    "offset": -7,\n    "timezone": "America/Los_Angeles"\n}\n'
final = True

    def decode(self, input, final=False):
>       return codecs.ascii_decode(input, self.errors)[0]
E       UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 10233: ordinal not in range(128)

[...]/lib/python3.6/encodings/ascii.py:26: UnicodeDecodeError

Now I finally took some time to dig into this and manually setting the encoding in load_fixture seems to fix the problem. This shouldn't cause any issues on other systems since all fixtures are encoded with utf-8 anyway.

Side note: Is there any reason we're not using pathlib? I've used it in my own scripts for months now and it's so much easier to use (and probably safer) than doing os.path.join.

Checklist:

  • The code change is tested and works locally.
  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass

@homeassistant homeassistant added small-pr PRs with less than 30 lines. cla-signed labels Feb 11, 2018
@OttoWinter OttoWinter changed the title Fix fixture encoding Fix test fixture encoding Feb 11, 2018
@balloob
Copy link
Member

balloob commented Feb 11, 2018

pathlib is just a different way of doing the same thing. People have their preference. And no, there is no reason to migrate all our usages to pathlib 😉

@balloob balloob merged commit e4a826d into home-assistant:dev Feb 11, 2018
@balloob balloob mentioned this pull request Feb 22, 2018
@OttoWinter OttoWinter deleted the fix-fixture-encoding branch March 13, 2018 20:19
@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed small-pr PRs with less than 30 lines.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants