Skip to content

Commit

Permalink
Now use the same package structure as the main program when running t…
Browse files Browse the repository at this point in the history
…ests.
  • Loading branch information
jkarns275 committed Jun 4, 2018
1 parent a4bc4dc commit e3fb5d9
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def __init__(self, *args):
QtWidgets.QApplication.__init__(self, *args)

def main():
if sys.argv[1] == 'test':
import test
return

if Config.high_dpi == 'true':
# Enable High DPI display with PyQt5
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
Expand Down
10 changes: 5 additions & 5 deletions src/test/__main__.py → src/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import traceback
import sys
import os
from ..utils import *
from .test import Test
from .fail_test import FailTest
from .throw_test import ThrowTest
from .hapi_sources_test import HapiSourcesTest
from utils import *
from test.test import Test
from test.fail_test import FailTest
from test.throw_test import ThrowTest
from test.hapi_sources_test import HapiSourcesTest

tests: List[Test] = [Test(), FailTest(), ThrowTest(), HapiSourcesTest()]

Expand Down
2 changes: 1 addition & 1 deletion src/test/fail_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import *
from .test import Test
from test.test import Test

class FailTest(Test):
def __init__(self):
Expand Down
4 changes: 2 additions & 2 deletions src/test/hapi_sources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
}
}

from .test import Test
from test.test import Test
from PyQt5 import QtGui, QtWidgets, QtCore
from ..widgets.hapi_source_widget import HapiSourceWidget
from widgets.hapi_source_widget import HapiSourceWidget

class HapiSourcesTest(Test):
def __init__(self):
Expand Down
50 changes: 50 additions & 0 deletions src/test/molecule_info_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import sys
import threading
from time import sleep

sources = {
'hapi': {
'authors': ['R.V. Kochanov', 'I.E. Gordon', 'L.S. Rothman', 'P. Wcislo', 'C. Hill','J.S. Wilzewski'],
'title': 'HITRAN Application Programming Interface (HAPI): A comprehensive approach to working with spectroscopic data',
'year': '2016',
'doi': '10.1016/j.jqsrt.2016.03.005'
},
'hapiest': {
'authors': ['W. Matt', 'J. Karns', 'B. Cairo', 'M. Sova', 'E. Messer', 'D. Lohmann', 'R.V. Kochanov',
'I.E. Gordon', 'B. Tenbergen', 'S. Kanbur'],
'title': 'HAPIEST: A GUI for HAPI',
'year': '2018',
'doi': None
}
}

from test.test import Test
from PyQt5 import QtGui, QtWidgets, QtCore
from widgets.molecule_info_widget import MoleculeInfoWidget

class MoleculeInfoTest(Test):
def __init__(self):
Test.__init__(self)

def name(self) -> str:
return 'molecule view test'

def test(self) -> bool:
app = QtWidgets.QApplication(sys.argv)

window = QtWidgets.QMainWindow()
def close_window():
sleep(0.25)
# window.deleteLater()

t = threading.Thread(target=close_window)
t.start()
items = QtWidgets.QWidget(window)
layout = QtWidgets.QVBoxLayout()
for k, v in sources.items():
layout.addWidget(HapiSourceWidget(v['title'], v['authors'], v['year'], v['doi']))
items.setLayout(layout)
window.setCentralWidget(items)
window.show()
qt_result = app.exec_()
return qt_result == 0
51 changes: 51 additions & 0 deletions src/test/molecule_view_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import sys
import threading
from time import sleep

sources = {
'hapi': {
'authors': ['R.V. Kochanov', 'I.E. Gordon', 'L.S. Rothman', 'P. Wcislo', 'C. Hill','J.S. Wilzewski'],
'title': 'HITRAN Application Programming Interface (HAPI): A comprehensive approach to working with spectroscopic data',
'year': '2016',
'doi': '10.1016/j.jqsrt.2016.03.005'
},
'hapiest': {
'authors': ['W. Matt', 'J. Karns', 'B. Cairo', 'M. Sova', 'E. Messer', 'D. Lohmann', 'R.V. Kochanov',
'I.E. Gordon', 'B. Tenbergen', 'S. Kanbur'],
'title': 'HAPIEST: A GUI for HAPI',
'year': '2018',
'doi': None
}
}

from .test import Test
from PyQt5 import QtGui, QtWidgets, QtCore
from ..widgets.molecule_info_widget import MoleculeInfoWidget
from ..widgets.

class MoleculeInfoTest(Test):
def __init__(self):
Test.__init__(self)

def name(self) -> str:
return 'molecule view test'

def test(self) -> bool:
app = QtWidgets.QApplication(sys.argv)

window = QtWidgets.QMainWindow()
def close_window():
sleep(0.25)
# window.deleteLater()

t = threading.Thread(target=close_window)
t.start()
items = QtWidgets.QWidget(window)
layout = QtWidgets.QVBoxLayout()
for k, v in sources.items():
layout.addWidget(HapiSourceWidget(v['title'], v['authors'], v['year'], v['doi']))
items.setLayout(layout)
window.setCentralWidget(items)
window.show()
qt_result = app.exec_()
return qt_result == 0
2 changes: 1 addition & 1 deletion src/test/throw_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import *
from .test import Test
from test.test import Test

class ThrowTest(Test):
def __init__(self):
Expand Down
85 changes: 85 additions & 0 deletions src/widgets/molecule_info_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from PyQt5.QtCore.Qt import *
from PyQt5.QtWidgets import *
import json
import utils.log

class MoleculeInfoWidget(QScrollArea):

FIELDS = ['Formula', 'InChi', 'InChiKey', 'HITRANonline_ID', 'Categories', 'Aliases']

def __init__(self, json_file_name = None, parent = None):
QScrollArea.__init__(self, parent)

def create_field(text):
field_name = text.lower()
label = QLabel('<b>{}:</b>'.format(text))
value = QLabel()

label.setTextFormat(TextFormat.RichText)
value.setTextFormat(TextFormat.RichText)

self.__dict__[field_name + "_label"] = label
self.__dict__[field_name] = value

self.name = QLabel()
self.img = QWidget()

map(create_field, MoleculeInfoWidget.FIELDS)

self.aliases.setWordWrap(True)
self.categories.setWordWrap(True)

self.form_layout = QtWidgets.QFormLayout()
self.form_layout.setFieldGrowthPolicy(QtWidgets.QFormLayout.FieldsStayAtSizeHint)

map(lambda x: self.form_layout.addRow(self.__dict__[x.lower() + '_label'], self.__dict__[x.lower()]), MoleculeInfoWidget.FIELDS)

self.hlayout = QtWidgets.QHBoxLayout()
self.hlayout.addWidget(self.img)
self.hlayout.addWidget(self.name)

self.vlayout = QtWidgets.QVBoxLayout()
self.vlayout.addWidget(self.hlayout)
self.vlayout.addWidget(self.form_layout)
self.setWidget(self.vlayout)

if json_file_name == None:
pass
else:
self.data = None
try:
with open('res/molecules/{}.json'.format(json_file_name), 'r') as file:
text = file.read()
self.data = json.loads(text)
except Exception as e:
log('No such molecule \'{}\''.format(json_file_name))
if self.data != None:
self.restructure_aliases()
try:
self.name.setText(self.data['short_alias'])
self.formula.setText(self.data['ordinary_formula_html'])
if 'hitranonline_id' in self.data and self.data['hitranonline_id'] != None:
self.hitranonline_id.setText(str(self.data['hitranonline_id']))
self.inchi.setText(self.data['inchi'])
self.inchikey.setText(self.data['aliases']['inchikey'])

alias_text = ''
for ty, alias in self.data['aliases'].items():
alias_text = '{}<br>{}: {}'.format(alias_text, str(ty), str(alias))
self.aliases.setText(alias_text)

categories_text = ''
for categorie in self.data['categories']:
categories_text = '{}<br>{}'.format(categories_text, str(categorie))
self.categories.setText(categories_text)

except Exception as e:
err_log('Encountered error \'{}\' - likely a malformed molecule json file'.format(str(e)))

def restructure_aliases(self):
if 'aliases' in self.data:
reformatted = {}
for item in self.data['aliases'].items():
reformatted[item['type']] = item['alias']
self.data['aliases'] = reformatted

2 changes: 1 addition & 1 deletion test.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python3 -m src.test
python3 src test

0 comments on commit e3fb5d9

Please sign in to comment.