initial commit

master
Greg Gauthier 2019-05-23 10:28:51 +01:00
commit ad592ce845
7 changed files with 137 additions and 0 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
.pytest_cache/

18
Pipfile 100644
View File

@ -0,0 +1,18 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
flask = "*"
hoverpy = "*"
pytest = "*"
[packages]
flask = "*"
hoverpy = "*"
pytest = "*"
[requires]
python_version = "3.7"

0
README.md 100644
View File

0
app/__init__.py 100644
View File

66
app/simple.py 100644
View File

@ -0,0 +1,66 @@
import threading
from random import randint
from secrets import choice
from string import ascii_letters
from flask import Flask, json, request
app = Flask(__name__)
@app.route("/")
def index():
return "Hello World!"
@app.route("/version")
def hello():
return json_response(
{
"application": "Simple Api",
"version": 0.1
}
)
@app.route("/randoms")
def randoms():
return json_response(rnd())
@app.route("/hashname", methods=['POST'])
def hashname():
content = request.json
name = content['name']
return json_response(hashit(name))
def json_response(data):
response = app.response_class(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
return response
def rnd():
rnum = randint(1, 80)
rstr = ''.join(choice(ascii_letters) for _ in range(rnum))
jsrnum = {
"number": rnum,
"string": rstr
}
return jsrnum
def hashit(name):
jshash = {
"name": name,
"hash": hash(name.encode('utf-8'))
}
return jshash
if __name__ == "__main__":
# threading.Thread(target=app.run).start()
app.run(host="127.0.0.1", port=5000)

26
tests/test_hov.py 100644
View File

@ -0,0 +1,26 @@
import os
from subprocess import Popen, PIPE
import requests
from hoverpy import capture
class TestUsingHoverpy:
def setup_class(self):
# startup the simple server (only needed for this example)
Popen("sh pipenv run python app/simple.py", shell=True, stdout=PIPE, stderr=PIPE)
def teardown_class(self):
# deletes the captured session
os.remove("tests/test_version_call.db")
@capture("tests/test_version_call.db", recordMode="once")
def test_version_call(self):
resp = requests.get("http://127.0.0.1:5000/version")
assert resp.json()['version'] == 0.1
# FROM THE DEMO:
# @capture("tests/test_time2.db", recordMode="once")
# def test_time3(self):
# time = requests.get("http://time.jsontest.com")
# assert list(time.json().keys()).index('time') > 0

View File

@ -0,0 +1,26 @@
import json
from app import simple
class TestSimple:
def setup(self):
self.app = simple.app.test_client()
def teardown(self):
pass
def test_version(self):
resp = self.app.get('/version')
rjson = json.loads(resp.data)
assert rjson['version'] == 0.1
def test_random_numbers(self):
resp = self.app.get('/randoms')
rjson = json.loads(resp.data)
assert rjson['number'] <= 80
def test_random_string(self):
resp = self.app.get('/randoms')
rjson = json.loads(resp.data)
assert len(rjson['string']) <= 80