|
| 1 | +import pygobbler as pyg |
| 2 | +import pytest |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture(scope="module") |
| 8 | +def setup(): |
| 9 | + _, staging, registry, url = pyg.start_gobbler() |
| 10 | + |
| 11 | + pyg.remove_project("test-usage", staging=staging, url=url) |
| 12 | + pyg.create_project("test-usage", staging=staging, url=url) |
| 13 | + |
| 14 | + tmp = tempfile.mkdtemp() |
| 15 | + with open(os.path.join(tmp, "blah.txt"), "w") as f: |
| 16 | + f.write("BAR") |
| 17 | + os.mkdir(os.path.join(tmp, "foo")) |
| 18 | + with open(os.path.join(tmp, "foo", "bar.txt"), "w") as f: |
| 19 | + f.write("1 2 3 4 5 6 7 8 9 10") |
| 20 | + |
| 21 | + pyg.upload_directory(project="test-usage", asset="quota", version="v1", directory=tmp, staging=staging, url=url) |
| 22 | + |
| 23 | + |
| 24 | +def test_refresh_usage(setup): |
| 25 | + _, staging, registry, url = pyg.start_gobbler() |
| 26 | + |
| 27 | + assert pyg.fetch_usage("test-usage", registry=registry, url=url) > 0 |
| 28 | + |
| 29 | + with open(os.path.join(registry, "test-usage", "..usage"), "w") as handle: |
| 30 | + handle.write('{ "total": 0 }') |
| 31 | + assert pyg.fetch_usage("test-usage", registry=registry, url=url) == 0 |
| 32 | + |
| 33 | + # Fixing the project usage. |
| 34 | + out = pyg.refresh_usage("test-usage", staging=staging, url=url) |
| 35 | + assert out > 0 |
| 36 | + assert pyg.fetch_usage("test-usage", registry=registry, url=url) == out |
0 commit comments