refactors and more tests, all tests passing

changed miniuri.py
changed test_miniuri.py
This commit is contained in:
russellballestrini 2015-10-14 17:37:03 -04:00
parent b2e669d4dc
commit 0843cc1f2b
2 changed files with 56 additions and 16 deletions

View file

@ -23,8 +23,7 @@ class Uri( object ):
"""build and return uri from attributes"""
scheme = path = filename = query = fragment = ''
if self.scheme: scheme = self.scheme + '://'
if self.path: path = '/'.join(self.path.split('/')[:-1])
if self.filename: filename = '/' + self.filename
if self.path: path = self.path
if self.query: query = '?' + self.query
if self.fragment: fragment = '#' + self.fragment
return ''.join([scheme,self.authority,path,filename,query,fragment])
@ -34,7 +33,7 @@ class Uri( object ):
"""parse and set all uri attributes"""
self.scheme = self.username = self.password = None
self.hostname = self.port = self.path = None
self.filename = self.query = self.fragment = None
self.filename = self.extension = self.query = self.fragment = None
if '://' in uri: # attempt to parse scheme
self.scheme, uri = uri.split( '://' )
@ -47,11 +46,10 @@ class Uri( object ):
if '#' in uri: # set fragment
uri, self.fragment = uri.split( '#' )
if '?' in uri: # set path and possibly query
if '?' in uri:
self.path, self.query = uri.split( '?' )
else: self.path = uri
self.filename = self.path.split( '/' )[-1]
else:
self.path = uri
@property
def authority( self ):
@ -91,8 +89,33 @@ class Uri( object ):
self.username, self.password = info.split( ':' )
@property
def extension(self):
"""return filename extension"""
return self.filename.split('.')[-1] if '.' in self.filename else None
def path(self):
"""return path"""
p = self._path if self._path is not None else ''
f = self.filename if self.filename is not None else ''
return p + f
@path.setter
def path(self, new_path):
if new_path:
self.filename = new_path.split('/')[-1]
self._path = new_path.rstrip(self.filename)
else:
self._path = None
self._filename = None
self._extension = None
@property
def filename(self):
"""return filename"""
if self._filename:
if self.extension:
return self._filename + '.' + self.extension
return self._filename
@filename.setter
def filename(self, new_filename):
if new_filename:
self.extension = new_filename.split('.')[-1] if '.' in new_filename else None
self._filename = new_filename.split('.')[0]

View file

@ -47,8 +47,26 @@ class TestMiniUri(TestCase):
def test_fragment(self):
self.assertEqual(self.u.fragment, '5')
# test_change_
# https://fox:pass@www.foxhop.net:81/path/filename.jpg?p=2#5
# test edge case URIs
def test_uri_no_path(self):
self.u = Uri('https://fox:pass@www.foxhop.net:81?p=2#5')
self.assertEqual(self.u.path, '')
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81?p=2#5')
def test_uri_filename_only_path(self):
self.u = Uri('https://fox:pass@www.foxhop.net:81/filename.jpg?p=2#5')
self.assertEqual(self.u.path, '/filename.jpg')
self.assertEqual(self.u.filename, 'filename.jpg')
self.assertEqual(self.u.extension, 'jpg')
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/filename.jpg?p=2#5')
def test_uri_trailing_slash_only_path(self):
self.u = Uri('https://fox:pass@www.foxhop.net:81/?p=2#5')
self.assertEqual(self.u.path, '/')
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/?p=2#5')
# test_change_attributes:
def test_change_uri(self):
self.u.uri = 'http://zell:sapp@wwws.foxhop.net:82/my/file.png?p=3#6'
@ -93,23 +111,22 @@ class TestMiniUri(TestCase):
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:82/path/filename.jpg?p=2#5')
def test_change_path(self):
# TODO: filename and extension not updated
self.u.path = '/my/file.png'
self.assertEqual(self.u.path, '/my/file.png')
self.assertEqual(self.u.filename, 'file.png')
self.assertEqual(self.u.extension, 'png')
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:82/my/file.png?p=2#5')
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/my/file.png?p=2#5')
def test_change_filename(self):
self.u.filename = 'file.png'
self.assertEqual(self.u.filename, 'file.png')
self.assertEqual(self.u.extension, 'png')
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/path/file.png?p=2#5')
def test_change_extension(self):
# TODO: attribute error
self.u.extension = 'png'
self.assertEqual(self.u.extension, 'png')
self.assertEqual(self.u.filename, 'filename.png')
self.assertEqual(self.u.extension, 'png')
self.assertEqual(self.u.uri, 'https://fox:pass@www.foxhop.net:81/path/filename.png?p=2#5')
def test_change_query(self):