Commit 490cd72c authored by julian.gethmann's avatar julian.gethmann

Make tests compatible with older Pythons

parent 5934aaa3
...@@ -25,9 +25,9 @@ __copyright__ = "Julian Gethmann" ...@@ -25,9 +25,9 @@ __copyright__ = "Julian Gethmann"
__license__ = "mit" __license__ = "mit"
__all__ = ["Cassandra", "CassandraHelper", "Pvs"] __all__ = ["Cassandra", "CassandraHelper", "Pvs"]
try: # Py2 try: # Py3
from urllib.error import URLError from urllib.error import URLError
except ImportError: # Py3 except ImportError: # Py2
from urllib2 import URLError from urllib2 import URLError
...@@ -217,16 +217,14 @@ class CassandraHelper(object): ...@@ -217,16 +217,14 @@ class CassandraHelper(object):
except ValueError: except ValueError:
pass pass
except TypeError: except TypeError:
raise TypeError( raise TypeError("Time ({}) must either be a `datetime.datetime` or a string"
"Time ({}) must either be a `datetime.datetime` or a string" " as described in the docstring and not a {}.".format(
" as described in the docstring and not a {}.".format( time_string, type(time_string).__name__))
time_string, type(time_string).__name__))
if not ret: if not ret:
raise ValueError( raise ValueError("`time_string` must not be of the form `{}` and type {}! "
"`time_string` must not be of the form `{}` and type {}! " "See docstring for possible forms.".format(
"See docstring for possible forms.".format( time_string, type(time_string).__name__))
time_string, type(time_string).__name__))
else: else:
return ret return ret
...@@ -270,8 +268,8 @@ class CassandraHelper(object): ...@@ -270,8 +268,8 @@ class CassandraHelper(object):
values.append(entry["value"]) values.append(entry["value"])
# if entry["status"] == "OK" or entry["severity"]["level"] == "OK"] # if entry["status"] == "OK" or entry["severity"]["level"] == "OK"]
timestamps = [ timestamps = [
datetime.datetime.fromtimestamp(entry["time"] / 1e9) datetime.datetime.fromtimestamp(entry["time"] / 1e9) for entry in json_data
for entry in json_data if entry["severity"]["level"] == "OK" if entry["severity"]["level"] == "OK"
] ]
dataset = namedtuple("dataset", ["timestamps", "values"]) dataset = namedtuple("dataset", ["timestamps", "values"])
...@@ -359,17 +357,14 @@ class Cassandra(object): ...@@ -359,17 +357,14 @@ class Cassandra(object):
@staticmethod @staticmethod
def check_connection(): def check_connection():
# type: () -> bool
"""Return if the connection is ok or not """Return if the connection is ok or not
Returns: Returns:
bool: True if the connection is ok, False if an error occurred bool: True if the connection is ok, False if an error occurred
""" """
try: try:
tmp = Cassandra( tmp = Cassandra("2017-07-07T08:08:08", "2017-07-07T08:08:08", Pvs.pv["energy"], count=1)
"2017-07-07T08:08:08",
"2017-07-07T08:08:08",
Pvs.pv["energy"],
count=1)
tmp.timeout = 1 tmp.timeout = 1
tmp._download_cassandra_data() tmp._download_cassandra_data()
except (TimeoutError, URLError): except (TimeoutError, URLError):
...@@ -379,6 +374,7 @@ class Cassandra(object): ...@@ -379,6 +374,7 @@ class Cassandra(object):
return ok return ok
def gen_url(self): def gen_url(self):
# type: (...) -> str
"""Return the URL to be fetched from Cassandra. """Return the URL to be fetched from Cassandra.
If count is `None` then the count parameter is not included in the URL If count is `None` then the count parameter is not included in the URL
...@@ -425,9 +421,7 @@ class Cassandra(object): ...@@ -425,9 +421,7 @@ class Cassandra(object):
except URLError: except URLError:
pass pass
else: else:
raise URLError( raise URLError("Request had a timeout. Maybe you're not inside the ANKA-LAN")
"Request had a timeout. Maybe you're not inside the ANKA-LAN"
)
elif CassandraHelper.PY2: elif CassandraHelper.PY2:
import urllib2 as request import urllib2 as request
for err_count in range(self.RETRIES): for err_count in range(self.RETRIES):
...@@ -443,13 +437,12 @@ class Cassandra(object): ...@@ -443,13 +437,12 @@ class Cassandra(object):
"Request had a timeout. Maybe you're not inside the IBPT-CN-LAN or provided a wrong PV name" "Request had a timeout. Maybe you're not inside the IBPT-CN-LAN or provided a wrong PV name"
) )
else: else:
raise NotImplementedError( raise NotImplementedError("Not implemented for other versions than 2 or 3!")
"Not implemented for other versions than 2 or 3!")
return json_data return json_data
def dump_cassandra_data(self): def dump_cassandra_data(self):
# type: (bool) -> str # type: (...) -> str
"""Dump the JSON file to a file named like the PV and time that is returned """Dump the JSON file to a file named like the PV and time that is returned
Dump a JSON file fetched from the Cassandra `host` and return its name. Dump a JSON file fetched from the Cassandra `host` and return its name.
...@@ -466,6 +459,7 @@ class Cassandra(object): ...@@ -466,6 +459,7 @@ class Cassandra(object):
return path.abspath(self.json_file) return path.abspath(self.json_file)
def get_json_local(self): def get_json_local(self):
# type: (...) -> Tuple[List[datetime.datetime], List[Union[List[float], float]]]
""" Return timestamps and values for given Cassandra object and copy JSON file if neccessary.""" """ Return timestamps and values for given Cassandra object and copy JSON file if neccessary."""
if not path.isfile(self.json_file) or path.getsize(self.json_file) <= 2: if not path.isfile(self.json_file) or path.getsize(self.json_file) <= 2:
self.dump_cassandra_data() self.dump_cassandra_data()
......
...@@ -8,9 +8,6 @@ ...@@ -8,9 +8,6 @@
import datetime import datetime
import json import json
import os import os
import pathlib
from urllib import error, request
from urllib.error import URLError
import cassandra import cassandra
import pytest import pytest
...@@ -19,6 +16,18 @@ from cassandra.cassandra import Cassandra ...@@ -19,6 +16,18 @@ from cassandra.cassandra import Cassandra
# from cassandra.cassandra import * # from cassandra.cassandra import *
from .conftest import request_openurl from .conftest import request_openurl
try:
import pathlib
except ImportError:
pass
try: # Py3
from urllib import request
from urllib.error import URLError
except ImportError: # Py2
from urllib2 import URLError
import urllib2 as request
@pytest.mark.usefixtures("cleandir", "setup_") @pytest.mark.usefixtures("cleandir", "setup_")
class TestCassandraClass(object): class TestCassandraClass(object):
...@@ -32,7 +41,9 @@ class TestCassandraClass(object): ...@@ -32,7 +41,9 @@ class TestCassandraClass(object):
assert cas2.count is None assert cas2.count is None
assert cas2.start_time == start assert cas2.start_time == start
assert cas2.end_time == end assert cas2.end_time == end
assert pathlib.Path(cas2.directory).absolute() == pathlib.Path.cwd()
assert os.path.abspath(cas2.directory) == os.path.abspath(os.path.curdir)
# assert pathlib.Path(cas2.directory).absolute() == pathlib.Path.cwd()
assert cas2.json_filename == "A:SR:BeamInfo:01:Energy_10:10:10_10:10:30_None.json" assert cas2.json_filename == "A:SR:BeamInfo:01:Energy_10:10:10_10:10:30_None.json"
def test_gen_url(self, cas, setup_): def test_gen_url(self, cas, setup_):
...@@ -41,16 +52,15 @@ class TestCassandraClass(object): ...@@ -41,16 +52,15 @@ class TestCassandraClass(object):
assert cas.pv == pv assert cas.pv == pv
assert ( assert (
url == url ==
"http://ankasr-archiver.anka.kit.edu:9812/archive-access/api/1.0/archive/1/samples/A:SR:BeamInfo:01:Energy?start=1509613810000000000&end=1509613830000000000" "http://ankasr-archiver.anka.kit.edu:9812/archive-access/api/1.0/archive/1/samples/A:SR:BeamInfo:01:Energy?"
) "start=1509613810000000000&end=1509613830000000000")
cas2 = cassandra.cassandra.Cassandra( cas2 = cassandra.cassandra.Cassandra(start, end, pv, directory="/tmp", count=1000)
start, end, pv, directory="/tmp", count=1000)
url = cas2.gen_url() url = cas2.gen_url()
assert ( assert (
url == url ==
"http://ankasr-archiver.anka.kit.edu:9812/archive-access/api/1.0/archive/1/samples/A:SR:BeamInfo:01:Energy?start=1509613810000000000&end=1509613830000000000&count=1000" "http://ankasr-archiver.anka.kit.edu:9812/archive-access/api/1.0/archive/1/samples/A:SR:BeamInfo:01:Energy?"
) "start=1509613810000000000&end=1509613830000000000&count=1000")
def test_check_connection( def test_check_connection(
self, self,
...@@ -80,8 +90,7 @@ class TestCassandraClass(object): ...@@ -80,8 +90,7 @@ class TestCassandraClass(object):
def test_download_cassandra_data_online(self, cas, monkeypatch): def test_download_cassandra_data_online(self, cas, monkeypatch):
with open( with open(
os.path.dirname(os.path.abspath(__file__)) + os.path.dirname(os.path.abspath(__file__)) +
"/data/A:SR:BBB:01:X:SRAM:PEAKTUNE2_11:45:00_12:13:00_1000.json", "/data/A:SR:BBB:01:X:SRAM:PEAKTUNE2_11:45:00_12:13:00_1000.json", "r") as fobj:
"r") as fobj:
json_obj = json.loads(fobj.read()) json_obj = json.loads(fobj.read())
monkeypatch.setattr(request, "urlopen", request_openurl) monkeypatch.setattr(request, "urlopen", request_openurl)
...@@ -92,22 +101,23 @@ class TestCassandraClass(object): ...@@ -92,22 +101,23 @@ class TestCassandraClass(object):
def test_download_cassandra_data_offline(self, cas, monkeypatch): def test_download_cassandra_data_offline(self, cas, monkeypatch):
def offline(url, timeout): def offline(url, timeout):
raise error.URLError("Name or service not known") raise URLError("Name or service not known")
monkeypatch.setattr(request, "urlopen", offline) monkeypatch.setattr(request, "urlopen", offline)
with pytest.raises(error.URLError, match=r".*[Tt]imeout.*"): with pytest.raises(URLError, match=r".*[Tt]imeout.*"):
cas._download_cassandra_data() cas._download_cassandra_data()
def test_dump_cassandra_data(self, cas, cleandir, monkeypatch): def test_dump_cassandra_data(self, cas, cleandir, monkeypatch):
assert not pathlib.Path(cleandir / cas.json_filename).is_file() assert not os.path.isfile(os.path.join(cleandir, cas.json_filename))
# assert not pathlib.Path(cleandir / cas.json_filename).is_file()
monkeypatch.setattr(request, "urlopen", request_openurl) monkeypatch.setattr(request, "urlopen", request_openurl)
assert str(cleandir / cas.json_filename) == cas.dump_cassandra_data() assert str(cleandir / cas.json_filename) == cas.dump_cassandra_data()
assert pathlib.Path(cleandir / cas.json_filename).is_file() assert os.path.isfile(os.path.join(cleandir, cas.json_filename))
# assert pathlib.Path(cleandir / cas.json_filename).is_file()
with open(str(cleandir / cas.json_filename), "r") as got, open( with open(str(cleandir / cas.json_filename), "r") as got, open(
os.path.dirname(os.path.abspath(__file__)) + os.path.dirname(os.path.abspath(__file__)) +
"/data/A:SR:BBB:01:X:SRAM:PEAKTUNE2_11:45:00_12:13:00_1000.json", "/data/A:SR:BBB:01:X:SRAM:PEAKTUNE2_11:45:00_12:13:00_1000.json", "r") as expected:
"r") as expected:
assert json.load(expected) == json.load(got) assert json.load(expected) == json.load(got)
def test_with(self, setup_dl, monkeypatch): def test_with(self, setup_dl, monkeypatch):
...@@ -119,6 +129,7 @@ class TestCassandraClass(object): ...@@ -119,6 +129,7 @@ class TestCassandraClass(object):
assert isinstance(cas2[0][0], datetime.datetime) assert isinstance(cas2[0][0], datetime.datetime)
assert isinstance(cas2[1][0], float) assert isinstance(cas2[1][0], float)
if __name__ == "__main__": if __name__ == "__main__":
pass pass
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
...@@ -9,16 +9,23 @@ import datetime ...@@ -9,16 +9,23 @@ import datetime
import doctest import doctest
import json import json
import os import os
import pathlib import sys
import unittest import unittest
from collections import namedtuple
from typing import List, NamedTuple from typing import List, NamedTuple
from urllib import request
import cassandra import cassandra
import pytest import pytest
from cassandra.pd import pvs2pd from cassandra.pd import pvs2pd
try:
import pathlib
except ImportError:
pass
try:
from urllib import request
except ImportError:
import urllib2 as request
setup = NamedTuple("setup", [ setup = NamedTuple("setup", [
("start", datetime.datetime), ("start", datetime.datetime),
("end", datetime.datetime), ("end", datetime.datetime),
...@@ -30,26 +37,27 @@ class TestCassandraPD(object): ...@@ -30,26 +37,27 @@ class TestCassandraPD(object):
@pytest.fixture() @pytest.fixture()
def setup_pd(self) -> setup: def setup_pd(self) -> setup:
"""Return `start` and `end` times and a list of PVs"""
pv_list = ["nu_x", "nu_y"] pv_list = ["nu_x", "nu_y"]
start = datetime.datetime(2016, 8, 5, 18, 28, 36) start = datetime.datetime(2016, 8, 5, 18, 28, 36)
end = datetime.datetime(2016, 8, 5, 18, 28, end = datetime.datetime(2016, 8, 5, 18, 28, 36) + datetime.timedelta(seconds=15)
36) + datetime.timedelta(seconds=15)
# setup = namedtuple("setup", ["start", "end", "pv_list"]) # setup = namedtuple("setup", ["start", "end", "pv_list"])
return setup(start, end, pv_list) return setup(start, end, pv_list)
@staticmethod @staticmethod
def urlopen_nus(url: str, *args, **kwargs): def urlopen_nus(url: str, *args, **kwargs):
"""Return a filehandler with the JSON file of a tune readback for X and Y tune"""
if "A:SR:BBB:01:Y:SRAM:PEAKTUNE2" in url: if "A:SR:BBB:01:Y:SRAM:PEAKTUNE2" in url:
plane = "Y" plane = "Y"
elif "" in url: elif "" in url:
plane = "X" plane = "X"
else: else:
raise ValueError("Mocking failed!") raise ValueError("Mocking failed!")
return ( return (pathlib.Path(__file__).parent /
pathlib.Path(__file__).parent / "data/A:SR:BBB:01:{plane}:SRAM:PEAKTUNE2_18:28:36_18:28:51_None.json".format(
f"data/A:SR:BBB:01:{plane}:SRAM:PEAKTUNE2_18:28:36_18:28:51_None.json" plane=plane)).open("rb")
).open("rb")
@pytest.mark.skipif(sys.version_info < (3, 4), reason="requires python3.4")
def test_pvs2pd(self, monkeypatch, setup_pd, cleandir): def test_pvs2pd(self, monkeypatch, setup_pd, cleandir):
monkeypatch.setattr(request, "urlopen", self.urlopen_nus) monkeypatch.setattr(request, "urlopen", self.urlopen_nus)
ret = pvs2pd(*setup_pd) ret = pvs2pd(*setup_pd)
...@@ -65,7 +73,10 @@ class TestCassandraPD(object): ...@@ -65,7 +73,10 @@ class TestCassandraPD(object):
assert not pathlib.Path(cleandir / fn).is_file() assert not pathlib.Path(cleandir / fn).is_file()
pvs2pd(*setup_pd, save_local=True) pvs2pd(*setup_pd, save_local=True)
with open(str(cleandir / fn), "r") as got, open( with open(str(cleandir / fn), "r") as got, open(
os.path.dirname(os.path.abspath(__file__)) + f"/data/{fn}", "{abspath}/data/{fn}".format(
abspath=os.path.dirname(os.path.abspath(__file__)),
fn=fn,
),
"r", "r",
) as expected: ) as expected:
assert json.load(expected) == json.load(got) assert json.load(expected) == json.load(got)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment