Skip to content

Commit

Permalink
SIM115 on CoverStore (#10300)
Browse files Browse the repository at this point in the history
* SIM115 on CoverStore

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ananyakaligal and pre-commit-ci[bot] authored Jan 13, 2025
1 parent 22a15e1 commit 46c9ec6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
5 changes: 2 additions & 3 deletions openlibrary/coverstore/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ def write(self, data, params=None):
prefix = params.get('olid', '')
filename = self.make_filename(prefix)
path = os.path.join(self.root, filename)
f = open(path, 'w')
f.write(data)
f.close()
with open(path, 'w') as f:
f.write(data)
return filename

def read(self, filename):
Expand Down
12 changes: 8 additions & 4 deletions openlibrary/coverstore/tests/test_coverstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def image_dir(tmpdir):
@pytest.mark.parametrize(('prefix', 'path'), image_formats)
def test_write_image(prefix, path, image_dir):
"""Test writing jpg, gif and png images"""
data = open(join(static_dir, path), 'rb').read()
with open(join(static_dir, path), 'rb') as file:
data = file.read()
assert coverlib.write_image(data, prefix) is not None

def _exists(filename):
Expand All @@ -40,7 +41,8 @@ def _exists(filename):
assert _exists(prefix + '-M.jpg')
assert _exists(prefix + '-L.jpg')

assert open(coverlib.find_image_path(prefix + '.jpg'), 'rb').read() == data
with open(coverlib.find_image_path(prefix + '.jpg'), 'rb') as file:
assert file.read() == data


def test_bad_image(image_dir):
Expand Down Expand Up @@ -74,9 +76,11 @@ def test_serve_file(image_dir):
path = static_dir + "/logos/logo-en.png"

assert coverlib.read_file('/dev/null') == b''
assert coverlib.read_file(path) == open(path, "rb").read()
with open(path, "rb") as file:
assert coverlib.read_file(path) == file.read()

assert coverlib.read_file(path + ":10:20") == open(path, "rb").read()[10 : 10 + 20]
with open(path, "rb") as file:
assert coverlib.read_file(path + ":10:20") == file.read()[10 : 10 + 20]


def test_server_image(image_dir):
Expand Down
33 changes: 18 additions & 15 deletions openlibrary/coverstore/tests/test_webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def upload(self, olid, path):
b = self.browser

path = join(static_dir, path)
content_type, data = utils.urlencode({'olid': olid, 'data': open(path).read()})
with open(path) as file:
content_type, data = utils.urlencode({'olid': olid, 'data': file.read()})
b.open('/b/upload2', data, {'Content-Type': content_type})
return json.loads(b.data)['id']

Expand All @@ -88,12 +89,14 @@ def static_path(self, path):
class TestDB:
def test_write(self, setup_db, image_dir):
path = static_dir + '/logos/logo-en.png'
data = open(path).read()
with open(path) as file:
data = file.read()
d = coverlib.save_image(data, category='b', olid='OL1M')

assert 'OL1M' in d.filename
path = config.data_root + '/localdisk/' + d.filename
assert open(path).read() == data
with open(path) as file:
assert file.read() == data


class TestWebapp(WebTestCase):
Expand All @@ -114,16 +117,12 @@ def test_touch(self):
id2 = self.upload('OL1M', 'logos/logo-it.png')

assert id1 < id2
assert (
b.open('/b/olid/OL1M.jpg').read()
== open(static_dir + '/logos/logo-it.png').read()
)
with open(static_dir + '/logos/logo-it.png') as file:
assert b.open('/b/olid/OL1M.jpg').read() == file.read()

b.open('/b/touch', urllib.parse.urlencode({'id': id1}))
assert (
b.open('/b/olid/OL1M.jpg').read()
== open(static_dir + '/logos/logo-en.png').read()
)
with open(static_dir + '/logos/logo-en.png') as file:
assert b.open('/b/olid/OL1M.jpg').read() == file.read()

def test_delete(self, setup_db):
b = self.browser
Expand All @@ -137,7 +136,8 @@ def test_upload(self):
b = self.browser

path = join(static_dir, 'logos/logo-en.png')
filedata = open(path).read()
with open(path) as file:
filedata = file.read()
content_type, data = utils.urlencode({'olid': 'OL1234M', 'data': filedata})
b.open('/b/upload2', data, {'Content-Type': content_type})
assert b.status == 200
Expand All @@ -147,7 +147,8 @@ def test_upload(self):

def test_upload_with_url(self, monkeypatch):
b = self.browser
filedata = open(join(static_dir, 'logos/logo-en.png')).read()
with open(join(static_dir, 'logos/logo-en.png')) as file:
filedata = file.read()
source_url = 'http://example.com/bookcovers/1.jpg'

mock = Mock()
Expand Down Expand Up @@ -201,11 +202,13 @@ def test_archive(self):
for f in files:
f.id = self.upload(f.olid, f.filename)
f.path = join(static_dir, f.filename)
assert b.open('/b/id/%d.jpg' % f.id).read() == open(f.path).read()
with open(f.path) as file:
assert b.open('/b/id/%d.jpg' % f.id).read() == file.read()

archive.archive()

for f in files:
d = self.jsonget('/b/id/%d.json' % f.id)
assert 'tar:' in d['filename']
assert b.open('/b/id/%d.jpg' % f.id).read() == open(f.path).read()
with open(f.path) as file:
assert b.open('/b/id/%d.jpg' % f.id).read() == file.read()

0 comments on commit 46c9ec6

Please sign in to comment.