Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save binary data #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/c20_server/data_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ class DataRepository:
def __init__(self, base_path):
self.base_path = base_path

def save_data(self, directory_name, filename, contents):
def save_text_data(self, directory_name, filename, contents):
self.ensure_directory_exists(directory_name)
path = os.path.join(self.base_path, directory_name, filename)
with open(path, 'w') as file:
file.write(json.dumps(contents))

def save_binary_data(self, directory_name, filename, contents):
self.ensure_directory_exists(directory_name)
path = os.path.join(self.base_path, directory_name, filename)
with open(path, 'wb') as file:
file.write(contents)

def ensure_directory_exists(self, directory_name):
Path(os.path.join(self.base_path, directory_name))\
.mkdir(parents=True, exist_ok=True)
22 changes: 18 additions & 4 deletions src/c20_server/flask_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ def _return_result():
return {}, 400

update_job_manager(job_manager, client_data)
save_data(data_repository, client_data['data'])
save_text_data(data_repository, client_data['data'])

return {}, 200

@app.route('/upload_file', methods=['POST'])
def upload_file():
file = request.files['file']
contents = file.stream.read()
filename = file.filename

job_id = request.form['job_id']
client_id = request.form['client_id']

data_repository.save_text_data('fpp', 'filename', 'contents')

return {}, 200


@app.route('/report_failure', methods=['POST'])
def _report_failure():
client_data = request.json
Expand All @@ -67,11 +81,11 @@ def update_job_manager(job_manager, client_data):
print(job, '\n')


def save_data(data_repository, list_of_data_dicts):
def save_text_data(data_repository, list_of_data_dicts):
data_items = DataExtractor.extract(list_of_data_dicts)
for data_item in data_items:
data_repository.save_data(data_item.folder_name,
data_item.file_name, data_item.contents)
data_repository.save_text_data(data_item.folder_name,
data_item.file_name, data_item.contents)


def redis_connect():
Expand Down
2 changes: 1 addition & 1 deletion src/c20_server/spy_data_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SpyDataRepository:
def __init__(self):
self.saved_items = []

def save_data(self, directory_name, filename, contents):
def save_text_data(self, directory_name, filename, contents):
item = SavedItem(directory_name=directory_name,
filename=filename,
contents=contents)
Expand Down
19 changes: 19 additions & 0 deletions src/file_flask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

from flask import Flask, request

app = Flask(__name__)


@app.route('/', methods=['POST'])
def upload():

file = request.files['file']
print(file.stream.read())
print(file.filename)
print(request.form['job_id'])
print(request.form['client_id'])
return {}, 200


if __name__ == '__main__':
app.run()
10 changes: 10 additions & 0 deletions src/upload_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import requests

binary_data = bytearray([1, 2, 3, 4])

files = {'file': ('foo.bin', binary_data)}
data = {'job_id': 'job42',
'client_id': 93}

requests.post('http://localhost:5000/', files=files, data=data)
17 changes: 15 additions & 2 deletions tests/test_data_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@ def ensure_directory_exists(self, directory_name):
self.ensure_directory_exists_called = True


def test_class_saves_data(mocker):
def test_class_saves_text_data(mocker):
file_mock = mocker.patch('c20_server.data_repository.open', mock_open())
data_repo = DummyMakeDirDataRepository('data')
directory = 'FAA/DOC-ID/'
filename = 'document.json'
contents = {'id': 'FAA'}
data_repo.save_data(directory, filename, contents)
data_repo.save_text_data(directory, filename, contents)

file_mock.assert_called_once_with('data/FAA/DOC-ID/document.json', 'w')
file_mock().write.assert_called_once_with('{"id": "FAA"}')
assert data_repo.ensure_directory_exists_called


def test_class_saves_binary_data(mocker):
file_mock = mocker.patch('c20_server.data_repository.open', mock_open())
data_repo = DummyMakeDirDataRepository('data')
directory = 'FAA/DOC-ID/'
filename = 'file.bin'
contents = bytearray([1, 2, 3, 4])
data_repo.save_binary_data(directory, filename, contents)

file_mock.assert_called_once_with('data/FAA/DOC-ID/file.bin', 'wb')
file_mock().write.assert_called_once_with(bytearray([1, 2, 3, 4]))
assert data_repo.ensure_directory_exists_called
25 changes: 25 additions & 0 deletions tests/test_flask_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,28 @@ def test_run_server_with_no_redis_connection():
with patch('sys.exit') as exit_server:
redis_connect()
assert exit_server.called


def test_upload_file(job_manager):
data_repository_spy = SpyDataRepository()
app = create_app(job_manager, data_repository_spy, MockDatabase(True))
app.config['TESTING'] = True
client = app.test_client()

binary_data = bytearray([1, 2, 3, 4])

json_data = {
'client_id': '1',
'job_id': '1',
'file': (binary_data, 'filename.bin')
}

result = client.post('/upload_file', content_type='multipart/form-data', data=json_data)

assert result.status_code == 200

first_save = data_repository_spy.saved_items[0]

assert first_save.directory_name == 'foldername'
assert first_save.filename == 'filename'
assert first_save.contents == {}