Skip to content

Commit

Permalink
Merge pull request #1 from carlomorelli/port-to-python3
Browse files Browse the repository at this point in the history
Port to python3
  • Loading branch information
carlomorelli committed Apr 12, 2018
2 parents 6c47f86 + 296a5e9 commit 5dc595a
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 42 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# IDEs
.project
.pydevproject
*.iml
**/.idea/*

# Venv
**/venv/*


# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: python
python:
- "2.7"
- "3.6"
install:
- pip install -r requirements.txt
- pip install coveralls
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ FROM python:2.7
ADD . /acrewstic
WORKDIR /acrewstic
RUN pip install -r requirements.txt
CMD python acrewstic.py
CMD python src/acrewstic.py
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Music and media repository for your NAS, written in Python


## About
_acrewstic_ is implemented as a RESTful API using Python 2.7 with Flask library (http://flask.pocoo.org/).
_acrewstic_ is implemented as a RESTful API using Python3 with Flask library (http://flask.pocoo.org/).
A JS frontend will be provided in the future.

The API is implemented for POST, PUT, GET, DELETE methods. It can be triggered as follows using `curl`:
Expand Down
2 changes: 1 addition & 1 deletion acrewlib.py → src/acrewlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from redis import StrictRedis


class Store:
class Repository:

def __init__(self, host, port):
self.r = StrictRedis(host=host, port=port)
Expand Down
10 changes: 5 additions & 5 deletions acrewstic.py → src/acrewstic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from flask import Flask, jsonify, abort, request, make_response
from acrewlib import Store
from .acrewlib import Repository

app = Flask(__name__)

store = Store(host='localhost', port=6379)
repository = Repository(host='localhost', port=6379)


"""
Expand Down Expand Up @@ -69,10 +69,10 @@ def update_task(task_id):
if not request.get_json():
abort(400)
if 'title' in request.get_json():
if type(request.get_json()['title']) is not unicode:
if type(request.get_json()['title']) is not str:
abort(400)
if 'description' in request.get_json():
if type(request.get_json()['description']) is not unicode:
if type(request.get_json()['description']) is not str:
abort(400)
if 'done' in request.get_json():
if type(request.get_json()['done']) is not bool:
Expand Down Expand Up @@ -103,7 +103,7 @@ def get_version():
'version': '0.11.1'
}
try:
redis_info = store.r.info()
redis_info = repository.r.info()
except:
redis_info = '<<<Unable to connect to database>>>'
return jsonify({
Expand Down
File renamed without changes.
32 changes: 14 additions & 18 deletions tests/test_data_store.py → test/test_data_store.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import os
import acrewstic
import tempfile
import mockredis
import mock
from nose import with_setup
from acrewlib import Store
from src.acrewlib import Repository

TEST_KEY_STORE = 'test_key_store'
test_string = 'test1'
Expand All @@ -12,12 +9,12 @@
test_list = range(1, 11)


class TestDataStore:
class RepositoryTest:

@classmethod
def setup_class(self):
print "Setting up store..."
self.store = Store(host='localhost', port=6379)
def setup_class(cls):
print("Setting up store...")
cls.store = Repository(host='localhost', port=6379)

# self.db_filedescriptor, acrewstic.app.config['DATABASE']
# = tempfile.mkstemp()
Expand All @@ -27,8 +24,8 @@ def setup_class(self):
# acrewstic.init_db()

@classmethod
def teardown_class(self):
print "Shutting down store..."
def teardown_class(cls):
print("Shutting down store...")
# os.close(self.db_filedescriptor)
# os.unlink(acrewstic.app.config['DATABASE'])

Expand All @@ -49,7 +46,7 @@ def test_0_put_string(self):
self.store.append_item(TEST_KEY_STORE, test_string)
new_size = len(self.store.fetch_all(TEST_KEY_STORE))
assert new_size == current_size + 1
print "Successfully setting up string"
print("Successfully setting up string")

@with_setup(setup, teardown)
def test_1_put_int(self):
Expand All @@ -58,7 +55,7 @@ def test_1_put_int(self):
self.store.append_item(TEST_KEY_STORE, test_int)
new_size = len(self.store.fetch_all(TEST_KEY_STORE))
assert new_size == current_size + 1
print "Successfully setting up int"
print("Successfully setting up int")

@with_setup(setup, teardown)
def test_2_put_dict(self):
Expand All @@ -67,7 +64,7 @@ def test_2_put_dict(self):
self.store.append_item(TEST_KEY_STORE, test_dict)
new_size = len(self.store.fetch_all(TEST_KEY_STORE))
assert new_size == current_size + 1
print "Successfully setting up dict"
print("Successfully setting up dict")

@with_setup(setup, teardown)
def test_3_append_and_get(self):
Expand All @@ -79,12 +76,12 @@ def test_3_append_and_get(self):
new_size = len(self.store.fetch_all(TEST_KEY_STORE))
assert new_size == current_size + 3
item = self.store.get_item(TEST_KEY_STORE, 0)
assert isinstance(item, str) or isinstance(item, unicode)
assert isinstance(item, str)
item = self.store.get_item(TEST_KEY_STORE, 1)
assert isinstance(item, int)
item = self.store.get_item(TEST_KEY_STORE, 2)
assert isinstance(item, dict)
print "Successfully appending string, int and dict in correct order"
print("Successfully appending string, int and dict in correct order")

@with_setup(setup, teardown)
def test_4_set_and_get(self):
Expand All @@ -100,5 +97,4 @@ def test_4_set_and_get(self):
assert new_size == current_size
item = self.store.get_item(TEST_KEY_STORE, index)
assert item == index + 1
print "Successfully setting up list and retrieving items"

print("Successfully setting up list and retrieving items")
30 changes: 15 additions & 15 deletions tests/test_rest_api.py → test/test_rest_api.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import json
import mock
import acrewstic
from src import acrewstic
import mockredis


class TestRestApi:

@classmethod
def setup_class(self):
print "Setting up application..."
self.client = acrewstic.app.test_client()
self.client.testing = True
def setup_class(cls):
print("Setting up application...")
cls.client = acrewstic.app.test_client()
cls.client.testing = True

@classmethod
def teardown_class(self):
print "Shutting down application..."
def teardown_class(cls):
print("Shutting down application...")

@mock.patch('redis.StrictRedis', mockredis.mock_strict_redis_client)
def test_0_get_version(self):
Expand All @@ -23,22 +23,22 @@ def test_0_get_version(self):
assert result.status_code == 200
# mock_info.assert_called()
data = json.loads(result.data)
print data
print(data)
# assert data['redis_info']['version'] == 'fakeRedis'

def test_1_get_tasks(self):
result = self.client.get('/acrewstic/tasks')
assert result.status_code == 200
data = json.loads(result.data)
print "Receving data: %s" % data
print("Receving data: %s" % data)
assert len(data['tasks']) == 2

def test_2_get_task(self):
task_id = 2
result = self.client.get('/acrewstic/tasks/%s' % task_id)
assert result.status_code == 200
data = json.loads(result.data)
print "Receving data: %s" % data
print("Receving data: %s" % data)
assert len(data) == 1
assert data['task']['id'] == task_id

Expand Down Expand Up @@ -69,7 +69,7 @@ def test_4_update_task(self):
result = self.client.get('/acrewstic/tasks/%s' % task_id)
assert result.status_code == 200
data = json.loads(result.data)
print "Receving data: %s" % data
print("Receving data: %s" % data)
assert data['task']['id'] == task_id
assert data['task']['done']
assert data['task']['title'] == 'NewTitle2'
Expand All @@ -79,13 +79,13 @@ def test_5_delete_task(self):
result = self.client.delete('/acrewstic/tasks/%s' % task_id)
assert result.status_code == 200
data = json.loads(result.data)
print "Receving data: %s" % data
print("Receving data: %s" % data)
assert data['result']

def test_6_delete_unexisting_task(self):
task_id = 1
result = self.client.delete('/acrewstic/tasks/%s' % task_id)
print "Receving data: %s" % json.loads(result.data)
print("Receving data: %s" % json.loads(result.data))
assert result.status_code == 404
data = json.loads(result.data)
assert data['error'] == 'Not found'
Expand All @@ -101,7 +101,7 @@ def test_7_update_unexisting_task(self):
data=json.dumps(update_task),
content_type='application/json'
)
print "Receving data: %s" % json.loads(result.data)
print("Receving data: %s" % json.loads(result.data))
assert result.status_code == 404
data = json.loads(result.data)
assert data['error'] == 'Not found'
Expand All @@ -120,7 +120,7 @@ def test_8_update_with_with_wrong_encoding(self):

def test_9_get_unexisting_api(self):
result = self.client.get('/acrewstic/unexisting')
print "Receving data: %s" % json.loads(result.data)
print("Receving data: %s" % json.loads(result.data))
assert result.status_code == 404
data = json.loads(result.data)
assert data['error'] == 'Not found'

0 comments on commit 5dc595a

Please sign in to comment.