Skip to content

Commit

Permalink
refactorisation connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Zepmanbc committed Jun 8, 2019
1 parent 3fbeefd commit e0dc464
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 44 deletions.
2 changes: 1 addition & 1 deletion _notes/exemple.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import time

IP_CREO = "192.168.56.101"
# IP_CREO = "192.168.0.38"
IP_CREO = "192.168.0.38"
start_command = "C:/Users/Public/Documents/nitro_proe_remote.bat"
c = creopyson.Client(ip_adress=IP_CREO)
current_file = "toto.prt"
Expand Down
26 changes: 3 additions & 23 deletions creopyson/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,14 @@ def __init__(self, ip_adress="localhost", port=9056):
self.server = "http://{}:{}/creoson".format(ip_adress, port)
self.sessionId = ''

################################################
def connect(self):
"""Connect to CREOSON.
Define 'sessionId'.
Exit if server not found.
"""
request = {
"command": "connection",
"function": "connect"
}
try:
r = requests.post(self.server, data=json.dumps(request))
except requests.exceptions.RequestException as e:
sys.exit(e)
if r.status_code == 200:
try:
json_result = r.json()
except AttributeError:
print("No JSON result.")
if 'sessionId' in json_result.keys():
self.sessionId = json_result['sessionId']
else:
raise KeyError("No sessionID available.")
else:
raise ConnectionError()
self.sessionId = self.creoson_post("connection", "connect")

############################################################
# TODO refactoriser connect qui ressemble bcp à creoson_post
# TODO docstring
def creoson_post(self, command, function, data=None, key_data=None):
"""Send a POST request to creoson server.
Expand Down Expand Up @@ -83,9 +62,10 @@ def creoson_post(self, command, function, data=None, key_data=None):
return json_result["data"][key_data]
else:
raise KeyError("`{}` not in creoson result".format(key_data))
elif "sessionId" in json_result.keys():
return json_result["sessionId"]
return json_result.get("data", None)
# TODO tester la présence de data
# TODO ajouter un raise de chaque keyvalue error de tous les modules
else:
raise ConnectionError()

Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ def mk_creoson_post_list(monkeypatch):
def fake_func(client, command, function, data=None):
return ['information']
monkeypatch.setattr(creopyson.connection.Client, 'creoson_post', fake_func)


@pytest.fixture
def mk_creoson_post_sessionId(monkeypatch):
"""Mock creoson_post return dict."""
def fake_func(client, command, function, data=None, key_data=None):
return "123456"
monkeypatch.setattr(creopyson.connection.Client, 'creoson_post', fake_func)
20 changes: 3 additions & 17 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import pytest
import creopyson

from .fixtures import mk_creoson_post_None, mk_creoson_post_dict
from .fixtures import mk_creoson_post_None, mk_creoson_post_dict, \
mk_creoson_post_sessionId

# @pytest.fixture(autouse=True)
# def no_requests(monkeypatch):
Expand All @@ -22,27 +23,12 @@ def test_connection_wether_params_exists():
assert c.server == "http://here:1234/creoson"


def test_connection_connect_succed(monkeypatch):
def test_connection_connect_succed(mk_creoson_post_sessionId):
"""Test when connection is ok.
sessionId is created and retruened by creoson.
"""
class Mk_post():
def __init__(self, *args, **kwargs):
pass

def json(self):
results = {
"sessionId": "123456",
}
return results

@property
def status_code(self):
return 200

monkeypatch.setattr(requests, 'post', Mk_post)
c = creopyson.Client()
c.connect()
assert c.sessionId == "123456"
Expand Down
2 changes: 0 additions & 2 deletions tests/test_creo.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ def test_creo_list_dirs(mk_creoson_post_dict):
assert isinstance(result, (list))



def test_creo_list_files(mk_creoson_post_dict):
"""Test creo_list_dirs."""
c = creopyson.Client()
result = c.creo_list_files("filter_*")
assert isinstance(result, (list))



def test_creo_mkdir(mk_creoson_post_dict):
"""Test creo_mkdir."""
c = creopyson.Client()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def content(self):
"status": {
"error": False,
},
"data":{
"data": {
"dirname": "C:/CreosonServer-2.3.0-win64"
}
}
Expand Down

0 comments on commit e0dc464

Please sign in to comment.