Skip to content

Commit

Permalink
#29 adds url property to files interface
Browse files Browse the repository at this point in the history
  • Loading branch information
caseneuve committed Jan 16, 2021
1 parent 8f769bb commit 9a09dee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 6 additions & 2 deletions pythonanywhere/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ def __init__(self, path):
self.api = Files()

def __repr__(self):
user_url = self.api.base_url.replace("/api/v0", "")
return f"{user_url}{self.path[1:]}"
return self.url

def _make_pa_url(self, path):
return urljoin(self.api.base_url.split("api")[0], path)

@property
def url(self):
files_base = self.api.base_url.replace("/api/v0", "")
return f"{files_base[:-1]}{self.path}"

def contents(self):
try:
content = self.api.path_get(self.path)
Expand Down
16 changes: 11 additions & 5 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@
@pytest.mark.files
class TestPAPathInit(TestFiles):
def test_instantiates_correctly(self, mocker):
pa_path = PAPath('path')
pa_path = PAPath("path")

assert pa_path.path == 'path'
assert pa_path.path == "path"
assert type(pa_path.api) == Files

def test_repr_contains_correct_pythonanywhere_resource_url_for_instantiated_path(self):
def test_url_property_contains_correct_pythonanywhere_resource_url_for_instantiated_path(self):
path = self.home_dir_path

user_path = self.base_url.replace('/api/v0', '')
assert PAPath(path).__repr__() == f"{user_path}{path[1:]}"
url = PAPath(path).__repr__()

assert url == f"{self.base_url.replace('/api/v0', '')}{path[1:]}"

def test_repr_returns_url_property_value(self, mocker):
mock_url = mocker.patch("pythonanywhere.files.PAPath.url")

assert PAPath("path").__repr__() == mock_url

def test_make_pa_url_contains_pa_site_address(self, mocker):
mock_urljoin = mocker.patch("pythonanywhere.files.urljoin")
Expand Down

0 comments on commit 9a09dee

Please sign in to comment.