Skip to content

Commit

Permalink
tests for _creoson_post refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
Zepmanbc committed Jun 15, 2019
1 parent 1d4f948 commit 0d1255b
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 14 deletions.
11 changes: 6 additions & 5 deletions creopyson/connection.py
Expand Up @@ -2,7 +2,7 @@
import requests
import json
import sys
from .exceptions import MissingKey
from .exceptions import MissingKey, ErrorJsonDecode


class Client(object):
Expand All @@ -28,8 +28,8 @@ def _creoson_post(self, command, function, data=None, key_data=None):
Args:
command (str): Command param for creoson.
function (str): Function param for creoson.
data (dict), optionnal: data params for creson request.
key_data (str): param name waited in result.
data (dict, optionnal): data params for creson request.
key_data (str, optionnal): param name waited in result.
Raises:
RuntimeError: error message from creoson.
Expand All @@ -52,12 +52,13 @@ def _creoson_post(self, command, function, data=None, key_data=None):
raise ConnectionError(e)

if r.status_code != 200:
raise ConnectionError("Status code :".format(r.status_code))
raise ConnectionError("Status code : {}".format(r.status_code))

try:
json_result = r.json()
except TypeError:
print("Cannot decode JSON, creoson result invalid.")
raise ErrorJsonDecode(
"Cannot decode JSON, creoson result invalid.")

if "status" not in json_result.keys():
raise MissingKey("Missing `status` in creoson result.")
Expand Down
6 changes: 6 additions & 0 deletions creopyson/exceptions.py
Expand Up @@ -11,3 +11,9 @@ class MissingKey(Error):
"""Raised when the input value is too small."""

pass


class ErrorJsonDecode(Error):
"""Raised when creoson result cannot be decoded."""

pass
232 changes: 223 additions & 9 deletions tests/test_connection.py
Expand Up @@ -4,6 +4,7 @@
import json
import pytest
import creopyson
from creopyson.exceptions import MissingKey, ErrorJsonDecode

from .fixtures import mk_creoson_post_None, mk_creoson_post_dict, \
mk_creoson_post_sessionId
Expand Down Expand Up @@ -47,7 +48,71 @@ def __init__(self, *args, **kwargs):
assert pytest_wrapped_e.type == ConnectionError


def test_connection__creoson_post_return_data(monkeypatch):

def test_connection_creoson_post_error_connection(monkeypatch):
"""Test _creoson_post connection server error."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

@property
def status_code(self):
return 500

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
with pytest.raises(ConnectionError) as pytest_wrapped_e:
c._creoson_post("function", "method", {})
assert pytest_wrapped_e.value.args[0] == "Status code : 500"


def test_connection_creoson_post_json_error(monkeypatch):
"""Test _creoson_post whether result can't be decoded with json."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
raise TypeError

@property
def status_code(self):
return 200
monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
with pytest.raises(ErrorJsonDecode) as pytest_wrapped_e:
c._creoson_post("function", "method", {})
assert pytest_wrapped_e.value.args[0] ==\
"Cannot decode JSON, creoson result invalid."


def test_connection_creoson_post_raise_RuntimeError(monkeypatch):
"""Test _creoson_post raise RuntimeError."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
results = {
"status": {
"error": True,
"message": "error message"
}
}
return results

@property
def status_code(self):
return 200

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
with pytest.raises(RuntimeError) as pytest_wrapped_e:
c._creoson_post("function", "method", {})
assert pytest_wrapped_e.value.args[0] == "error message"


def test_connection_creoson_post_return_data(monkeypatch):
"""Test _creoson_post returning data."""
class Mk_post():
def __init__(self, *args, **kwargs):
Expand All @@ -73,7 +138,7 @@ def status_code(self):
assert result == "creoson result"


def test_connection__creoson_post_return_None(monkeypatch):
def test_connection_creoson_post_return_None(monkeypatch):
"""Test _creoson_post returning None."""
class Mk_post():
def __init__(self, *args, **kwargs):
Expand All @@ -98,17 +163,60 @@ def status_code(self):
assert result is None


def test_connection__creoson_post_raise_Warning(monkeypatch):
"""Test _creoson_post raise Warning."""
def test_connection_creoson_post_no_status(monkeypatch):
"""Test _creoson_post raise error if no `status` in return."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
results = {"status": {}}
return results

@property
def status_code(self):
return 200

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
with pytest.raises(MissingKey) as pytest_wrapped_e:
c._creoson_post("function", "method", {})
assert pytest_wrapped_e.value.args[0] ==\
"Missing `error` in status' creoson's result."


def test_connection_creoson_post_no_error(monkeypatch):
"""Test _creoson_post raise error if no `error` in return."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
results = {}
return results

@property
def status_code(self):
return 200

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
with pytest.raises(MissingKey) as pytest_wrapped_e:
c._creoson_post("function", "method", {})
assert pytest_wrapped_e.value.args[0] ==\
"Missing `status` in creoson result."


def test_connection_creoson_post_no_sessionId(monkeypatch):
"""Test _creoson_post raise error if no `sessionId` in return."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
results = {
"status": {
"error": True,
"message": "error message"
"error": False
}
}
return results
Expand All @@ -119,9 +227,115 @@ def status_code(self):

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
with pytest.raises(RuntimeError) as pytest_wrapped_e:
c._creoson_post("function", "method", {})
assert pytest_wrapped_e.value.args[0] == "error message"
with pytest.raises(MissingKey) as pytest_wrapped_e:
c._creoson_post("connection", "connect", {})
assert pytest_wrapped_e.value.args[0] ==\
"Missing `sessionId` in creoson result."


def test_connection_creoson_post_sessionId(monkeypatch):
"""Test _creoson_post return sessionId."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
results = {
"status": {
"error": False
},
"sessionId": 12345
}
return results

@property
def status_code(self):
return 200

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
result = c._creoson_post("connection", "connect", {})
assert result == 12345


def test_connection_creoson_post_no_data(monkeypatch):
"""Test _creoson_post raise error if no data in return."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
results = {
"status": {
"error": False
}
}
return results

@property
def status_code(self):
return 200

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
with pytest.raises(MissingKey) as pytest_wrapped_e:
c._creoson_post("function", "method", {}, key_data="fakedata")
assert pytest_wrapped_e.value.args[0] ==\
"Missing `data` in creoson return"


def test_connection_creoson_post_no_datakey(monkeypatch):
"""Test _creoson_post raise error if no datakey in return."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
results = {
"status": {
"error": False
},
"data": {}
}
return results

@property
def status_code(self):
return 200

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
with pytest.raises(MissingKey) as pytest_wrapped_e:
c._creoson_post("function", "method", {}, key_data="fakedata")
assert pytest_wrapped_e.value.args[0] ==\
"Missing `fakedata` in creoson result"


def test_connection_creoson_post_datakey(monkeypatch):
"""Test _creoson_post raise error if no datakey in return."""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
results = {
"status": {
"error": False
},
"data": {
"fakedata": "fakevalue"
}
}
return results

@property
def status_code(self):
return 200

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
result = c._creoson_post("function", "method", {}, key_data="fakedata")
assert result == "fakevalue"


def test_connection_disconnect_ok(mk_creoson_post_None):
Expand Down

0 comments on commit 0d1255b

Please sign in to comment.