Files
dvc/tests/unit/updater.py
T
Ruslan Kuprieiev 91ed40fbfa updater: don't use io.open when working with json
Otherwise we get this on python2:
```
======================================================================
ERROR: test_fetch (tests.unit.updater.TestUpdater)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/efiop/.pyenv/versions/2.7.15/envs/2.7.15-dvc/local/lib/
python2.7/site-packages/mock/mock.py", line 1305, in patched
    return func(*args, **keywargs)
  File "/home/efiop/git/dvc/tests/unit/updater.py", line 34, in test_fetch
    self.dvc.updater.fetch(detach=False)
  File "/home/efiop/git/dvc/dvc/updater.py", line 77, in fetch
    self._with_lock(self._get_latest_version, "fetching")
  File "/home/efiop/git/dvc/dvc/updater.py", line 39, in _with_lock
    func()
  File "/home/efiop/git/dvc/dvc/updater.py", line 91, in _get_latest_version
    json.dump(convert_to_unicode(info), fobj)
  File "/usr/lib/python2.7/json/__init__.py", line 190, in dump
    fp.write(chunk)
TypeError: write() argument 1 must be unicode, not str
-------------------- >> begin captured logging << --------------------
```

Signed-off-by: Ruslan Kuprieiev <[email protected]>
2019-02-12 01:40:07 +02:00

40 lines
1.1 KiB
Python

import os
import json
import mock
from dvc import VERSION
from tests.basic_env import TestDvc
class MockResponse(object):
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
def mocked_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
return MockResponse({"version": VERSION}, 200)
class TestUpdater(TestDvc):
@mock.patch("requests.get", side_effect=mocked_requests_get)
def test_fetch(self, mock_get):
self.assertFalse(os.path.exists(self.dvc.updater.updater_file))
self.dvc.updater.fetch(detach=False)
mock_get.assert_called_once()
self.assertTrue(os.path.isfile(self.dvc.updater.updater_file))
with open(self.dvc.updater.updater_file, "r") as fobj:
info = json.load(fobj)
self.assertEqual(info["version"], VERSION)