Skip to content

Commit

Permalink
MNT #481 apply black -l 75
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Jan 15, 2021
1 parent 5806cec commit bdcf00b
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 345 deletions.
5 changes: 2 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# :author: Pete R. Jemian
# :email: prjemian@gmail.com
# :copyright: (c) 2014-2020, Pete R. Jemian
#
# Distributed under the terms of the Creative Commons Attribution 4.0 International Public License.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

# advice: http://stackoverflow.com/questions/191673/preferred-python-unit-testing-framework?rq=1
# advice: http://stackoverflow.com/questions/17001010/how-to-run-unittest-discover-from-python-setup-py-test#21726329
Expand Down
10 changes: 5 additions & 5 deletions tests/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

# Copyright (c) 2017-2020, UChicago Argonne, LLC. See LICENSE file.

import os
import sys
import unittest

_path = os.path.join(os.path.dirname(__file__), '..',)
_path = os.path.join(os.path.dirname(__file__), "..",)
if _path not in sys.path:
sys.path.insert(0, _path)

Expand All @@ -20,6 +19,7 @@ def suite(*args, **kw):
from tests import test_filewriter
from tests import test_simple
from tests import test_utils

test_list = [
test_simple,
test_filewriter,
Expand All @@ -29,14 +29,14 @@ def suite(*args, **kw):
test_utils,
test_apsbss,
test_diffractometer,
]
]

test_suite = unittest.TestSuite()
for test in test_list:
test_suite.addTest(test.suite())
return test_suite


if __name__ == '__main__':
runner=unittest.TextTestRunner(verbosity=2)
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite())
9 changes: 4 additions & 5 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
Common methods for unit tests.
Expand All @@ -12,7 +11,7 @@
import sys


class Capture_stdout(list): # lgtm [py/missing-equals]
class Capture_stdout(list): # lgtm [py/missing-equals]

"""
Capture all printed output (to stdout) into list.
Expand All @@ -29,11 +28,11 @@ def __enter__(self):

def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
del self._stringio # free up some memory
sys.stdout = self._stdout


class Capture_stderr(list): # lgtm [py/missing-equals]
class Capture_stderr(list): # lgtm [py/missing-equals]

"""Capture stderr into list."""

Expand All @@ -45,5 +44,5 @@ def __enter__(self):

def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
del self._stringio # free up some memory
sys.stderr = self._stderr
70 changes: 42 additions & 28 deletions tests/test_apsbss.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
unit tests for beamtime info
"""
Expand All @@ -13,7 +12,7 @@
import uuid

_test_path = os.path.dirname(__file__)
_path = os.path.join(_test_path, '..')
_path = os.path.join(_test_path, "..")
if _path not in sys.path:
sys.path.insert(0, _path)

Expand All @@ -29,38 +28,39 @@ def using_APS_workstation():

def bss_IOC_available():
import epics

# try connecting with one of the PVs in the database
cycle = epics.PV(f"{BSS_TEST_IOC_PREFIX}esaf:cycle")
cycle.wait_for_connection(timeout=2)
return cycle.connected


class Test_Beamtime(unittest.TestCase):

def test_general(self):
self.assertEqual(apsbss.CONNECT_TIMEOUT, 5)
self.assertEqual(apsbss.POLL_INTERVAL, 0.01)
self.assertEqual(
apsbss.DM_APS_DB_WEB_SERVICE_URL,
"https://xraydtn01.xray.aps.anl.gov:11236")
"https://xraydtn01.xray.aps.anl.gov:11236",
)
self.assertIsNotNone(apsbss.api_bss)
self.assertIsNotNone(apsbss.api_esaf)

def test_iso2datetime(self):
self.assertEqual(
apsbss.iso2datetime("2020-06-30 12:31:45.067890"),
datetime.datetime(2020, 6, 30, 12, 31, 45, 67890)
datetime.datetime(2020, 6, 30, 12, 31, 45, 67890),
)

def test_not_at_aps(self):
self.assertTrue(True) # test something
self.assertTrue(True) # test something
if using_APS_workstation():
return

# do not try to test for fails using dm package, it has no timeout

def test_only_at_aps(self):
self.assertTrue(True) # test something
self.assertTrue(True) # test something
if not using_APS_workstation():
return

Expand All @@ -83,8 +83,11 @@ def test_only_at_aps(self):

def test_printColumns(self):
from tests.common import Capture_stdout

with Capture_stdout() as received:
apsbss.printColumns("1 2 3 4 5 6".split(), numColumns=3, width=3)
apsbss.printColumns(
"1 2 3 4 5 6".split(), numColumns=3, width=3
)
self.assertEqual(len(received), 2)
self.assertEqual(received[0], "1 3 5 ")
self.assertEqual(received[1], "2 4 6 ")
Expand All @@ -99,22 +102,23 @@ def test_printColumns(self):


class Test_EPICS(unittest.TestCase):

def setUp(self):
self.bss = None
self.manager = os.path.abspath(os.path.join(
os.path.dirname(apsbss.__file__),
"apsbss_ioc.sh"
))
self.manager = os.path.abspath(
os.path.join(os.path.dirname(apsbss.__file__), "apsbss_ioc.sh")
)
self.ioc_name = "test_apsbss"
cmd = f"{self.manager} restart {self.ioc_name} {BSS_TEST_IOC_PREFIX}"
cmd = (
f"{self.manager} restart {self.ioc_name} {BSS_TEST_IOC_PREFIX}"
)
self.ioc_process = subprocess.Popen(
cmd.encode().split(),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False)
time.sleep(.5) # allow the IOC to start
shell=False,
)
time.sleep(0.5) # allow the IOC to start

def tearDown(self):
if self.bss is not None:
Expand All @@ -128,7 +132,8 @@ def tearDown(self):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False)
shell=False,
)
# self.ioc_process.communicate(cmd.encode().split())
self.manager = None

Expand Down Expand Up @@ -171,7 +176,9 @@ def test_EPICS(self):
self.assertNotEqual(self.bss.proposal.beamline_name.get(), "harpo")
self.assertEqual(self.bss.proposal.beamline_name.get(), beamline)
self.assertEqual(self.bss.esaf.aps_cycle.get(), cycle)
self.assertEqual(self.bss.esaf.sector.get(), beamline.split("-")[0])
self.assertEqual(
self.bss.esaf.sector.get(), beamline.split("-")[0]
)

# epicsUpdate
# Example ESAF on sector 9
Expand All @@ -195,11 +202,13 @@ def test_EPICS(self):
with Capture_stdout():
apsbss.epicsUpdate(BSS_TEST_IOC_PREFIX)
self.assertEqual(
self.bss.esaf.title.get(),
"Commission 9ID and USAXS")
self.bss.esaf.title.get(), "Commission 9ID and USAXS"
)
self.assertTrue(
self.bss.proposal.title.get().startswith(
"2019 National School on Neutron & X-r"))
"2019 National School on Neutron & X-r"
)
)

with Capture_stdout():
apsbss.epicsClear(BSS_TEST_IOC_PREFIX)
Expand All @@ -209,9 +218,9 @@ def test_EPICS(self):


class Test_MakeDatabase(unittest.TestCase):

def test_general(self):
from tests.common import Capture_stdout

with Capture_stdout() as db:
apsbss_makedb.main()
self.assertEqual(len(db), 384)
Expand All @@ -221,17 +230,22 @@ def test_general(self):
self.assertEqual(db[13], 'record(stringout, "$(P)status")')
self.assertEqual(db[28], 'record(stringout, "$(P)esaf:id")')
self.assertEqual(db[138], ' field(ONAM, "ON")')
self.assertEqual(db[285], 'record(bo, "$(P)proposal:user5:piFlag") {')
self.assertEqual(
db[285], 'record(bo, "$(P)proposal:user5:piFlag") {'
)


class Test_ProgramCommands(unittest.TestCase):

def setUp(self):
self.sys_argv0 = sys.argv[0]
sys.argv = [self.sys_argv0,]
sys.argv = [
self.sys_argv0,
]

def tearDown(self):
sys.argv = [self.sys_argv0,]
sys.argv = [
self.sys_argv0,
]

def test_no_options(self):
args = apsbss.get_options()
Expand Down Expand Up @@ -313,13 +327,13 @@ def suite(*args, **kw):
Test_EPICS,
Test_MakeDatabase,
Test_ProgramCommands,
]
]
test_suite = unittest.TestSuite()
for test_case in test_list:
test_suite.addTest(unittest.makeSuite(test_case))
return test_suite


if __name__ == "__main__":
runner=unittest.TextTestRunner()
runner = unittest.TextTestRunner()
runner.run(suite())
10 changes: 5 additions & 5 deletions tests/test_commandlist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
simple unit tests for this package
"""
Expand All @@ -8,13 +7,14 @@
import unittest

PATH = os.path.dirname(__file__)
_path = os.path.join(PATH, '..')
_path = os.path.join(PATH, "..")
if _path not in sys.path:
sys.path.insert(0, _path)

from apstools import utils as APS_utils
from apstools import plans as APS_plans


class Test_CommandList(unittest.TestCase):

xl_file = os.path.join(PATH, "demo3.xlsx")
Expand All @@ -26,7 +26,7 @@ class Test_CommandList(unittest.TestCase):
def test_ExcelFile(self):
# just a spreadsheet for testing (early version of a command file)
commands = APS_plans.get_command_list(self.xl_file)
self.assertEqual(len(commands), 9) # rows
self.assertEqual(len(commands), 9) # rows
table = APS_utils.command_list_as_table(commands, show_raw=False)
received = str(table).strip()
expected = """
Expand Down Expand Up @@ -127,13 +127,13 @@ def test_NotCommandList(self):
def suite(*args, **kw):
test_list = [
Test_CommandList,
]
]
test_suite = unittest.TestSuite()
for test_case in test_list:
test_suite.addTest(unittest.makeSuite(test_case))
return test_suite


if __name__ == "__main__":
runner=unittest.TextTestRunner()
runner = unittest.TextTestRunner()
runner.run(suite())
Loading

0 comments on commit bdcf00b

Please sign in to comment.