Skip to content

Commit 5ad042b

Browse files
authored
Fix download tests (#76)
* Fix download tests * Remove unfunctional mockup --------- Co-authored-by: thomas.grandjean <thomas.grandjean@developpement-durable.gouv.fr>
1 parent 1c90dd3 commit 5ad042b

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

cartiflette/download/download.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pebble import ThreadPool
1212
import s3fs
1313
import shutil
14+
import traceback
1415
from typing import Union
1516

1617
from cartiflette import BUCKET, PATH_WITHIN_BUCKET, FS, THREADS_DOWNLOAD
@@ -280,7 +281,8 @@ def func(args):
280281
else:
281282
paths = {}
282283
# cleanup temp files
283-
shutil.rmtree(result["root_cleanup"])
284+
if result["root_cleanup"]:
285+
shutil.rmtree(result["root_cleanup"])
284286

285287
del result["hash"], result["root_cleanup"], result["layers"]
286288
result["paths"] = paths
@@ -303,6 +305,7 @@ def func(args):
303305
break
304306
except Exception as e:
305307
logger.error(e)
308+
logger.error(traceback.format_exc())
306309
else:
307310
for args in combinations:
308311
files = deep_dict_update(files, func(args))

tests/mockups.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33
import requests
44
from requests_cache import CachedSession
5+
import s3fs
56
import logging
67

78
from tests.conftest import (
@@ -10,7 +11,9 @@
1011
CONTENT_DUMMY,
1112
)
1213

14+
from cartiflette import FS
1315
from cartiflette.download.dataset import Dataset
16+
from cartiflette.download.scraper import MasterScraper
1417

1518

1619
logging.basicConfig(level=logging.INFO)
@@ -19,31 +22,28 @@
1922
@pytest.fixture
2023
def mock_Dataset_without_s3(monkeypatch):
2124
monkeypatch.setattr(Dataset, "_get_last_md5", lambda x: None)
22-
monkeypatch.setattr("FS")
25+
# monkeypatch.setattr("FS")
2326

2427

2528
@pytest.fixture
2629
def total_mock_s3(monkeypatch):
2730
monkeypatch.setattr(Dataset, "_get_last_md5", lambda x: None)
2831

29-
def mock_unpack(x):
32+
def mock_unpack(self, x):
3033
return {
31-
x.provider: {
32-
x.dataset_family: {
33-
x.source: {
34-
x.territory: {
35-
x.year: {
36-
"downloaded": False,
37-
"paths": None,
38-
"hash": None,
39-
}
40-
}
41-
}
42-
}
43-
}
34+
"downloaded": False,
35+
"layers": None,
36+
"hash": None,
37+
"root_cleanup": None,
4438
}
4539

46-
monkeypatch.setattr(Dataset, "download_unpack", lambda x: mock_unpack)
40+
monkeypatch.setattr(MasterScraper, "download_unpack", mock_unpack)
41+
# monkeypatch.setattr("cartiflette.THREADS_DOWNLOAD", 1)
42+
43+
def mock_ls(folder):
44+
return [f"{folder}/md5.json"]
45+
46+
monkeypatch.setattr(FS, "ls", mock_ls)
4747

4848

4949
class MockResponse:

tests/test_download.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
import os
55
import requests_cache
66
import logging
7+
78
from cartiflette.download.dataset import Dataset
89
from cartiflette.download.scraper import MasterScraper
9-
1010
from cartiflette.download.download import _download_sources
11+
from cartiflette.download import download_all
1112
from cartiflette.utils import import_yaml_config
1213
from tests.conftest import (
1314
DUMMY_FILE_1,
1415
DUMMY_FILE_2,
1516
HASH_DUMMY,
1617
)
18+
1719
from tests.mockups import (
1820
mock_httpscraper_download_success,
1921
mock_httpscraper_download_success_corrupt_hash,
@@ -285,5 +287,6 @@ def test_sources_yaml(mock_Dataset_without_s3):
285287

286288

287289
def test_download_all(total_mock_s3):
288-
ret = _download_sources()
290+
ret = download_all()
289291
assert isinstance(ret, dict)
292+
assert len(ret) > 0

0 commit comments

Comments
 (0)