Skip to content

Commit efe20a0

Browse files
dolfandringaSylvainCorlay
authored andcommitted
Added test attempt that won't work because the server is started with subprocess.POpen, so we can't mock anything.
1 parent 1aada74 commit efe20a0

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

nbviewer/providers/s3/tests/__init__.py

Whitespace-only changes.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (C) Jupyter Development Team
3+
#
4+
# Distributed under the terms of the BSD License. The full license is in
5+
# the file COPYING, distributed as part of this software.
6+
# -----------------------------------------------------------------------------
7+
import io
8+
import json
9+
from copy import deepcopy
10+
from unittest.mock import patch
11+
12+
import boto3
13+
import requests
14+
15+
from ....tests.base import FormatHTMLMixin
16+
from ....tests.base import NBViewerTestCase
17+
18+
19+
MOCK_NOTEBOOK = {
20+
"cells": [
21+
{
22+
"cell_type": "code",
23+
"execution_count": None,
24+
"id": "b0939771-a810-4ee0-b440-dbbaeb4f1653",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [],
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": None,
32+
"id": "cc0d476a-d09c-4919-8dd2-c8d67f7431b3",
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [],
36+
},
37+
],
38+
"metadata": {
39+
"kernelspec": {
40+
"display_name": "Python 3 (ipykernel)",
41+
"language": "python",
42+
"name": "python3",
43+
},
44+
"language_info": {
45+
"codemirror_mode": {"name": "ipython", "version": 3},
46+
"file_extension": ".py",
47+
"mimetype": "text/x-python",
48+
"name": "python",
49+
"nbconvert_exporter": "python",
50+
"pygments_lexer": "ipython3",
51+
"version": "3.9.12",
52+
},
53+
},
54+
"nbformat": 4,
55+
"nbformat_minor": 5,
56+
}
57+
58+
59+
class MockBoto3:
60+
def download_fileobj(self, Bucket, Key, fileobj):
61+
"""Mock downloading fileobjects"""
62+
data = deepcopy(MOCK_NOTEBOOK)
63+
data["cells"][0]["source"] = [f"print({Bucket})", f"print({Key})"]
64+
bin_data = json.dumps(data).encode("utf-8")
65+
fileobj.write(bin_data)
66+
67+
def head_object(self, Bucket, Key):
68+
"""Mock getting key headers"""
69+
output_file = io.BytesIO()
70+
f = self.download_fileobj(Bucket, Key, output_file)
71+
f.seek(0)
72+
return {"ContentLength": len(f.read())}
73+
74+
75+
"""
76+
# This test won't work because the server is started through subprocess.POpen, so we can't mock boto3.
77+
78+
class S3TestCase(NBViewerTestCase):
79+
80+
@patch("boto3.client")
81+
def test_url(self, mock_boto3_client):
82+
mockBoto3 = MockBoto3()
83+
mock_boto3_client.return_value = mockBoto3
84+
with patch.object(mockBoto3, 'download_fileobj') as mock_download:
85+
bucket="my_bucket"
86+
key="my_file.ipynb"
87+
url = self.url(f"s3%3A//{bucket}/{key}")
88+
r = requests.get(url)
89+
self.assertEqual(r.status_code, 200)
90+
args = mock_download.call_args_list[-1][:2]
91+
self.assertEqual(args, (bucket, key))
92+
93+
94+
class FormatHTMLLocalFileDefaultTestCase(S3TestCase, FormatHTMLMixin):
95+
pass
96+
"""

0 commit comments

Comments
 (0)