tests best practices

This commit is contained in:
Ace 2023-11-21 15:18:19 +01:00
parent 96a972e31f
commit 59c59e80f5
18 changed files with 177 additions and 304 deletions

2
.gitignore vendored
View File

@ -146,7 +146,7 @@ dmypy.json
/logs
# inkycal tests
/inkycal/tests/tmp/
/tests/tmp/
!/inkycal/tests/*.py
/docsource/._build/

View File

@ -83,12 +83,11 @@ class Inkyimage:
@staticmethod
def preview(image):
""""Previews an image on gpicview (only works on Rapsbian with Desktop).
"""
path = '/home/pi/Desktop/'
image.save(path + 'temp.png')
os.system("gpicview " + path + 'temp.png')
os.system('rm ' + path + 'temp.png')
"""Previews an image on gpicview (only works on Rapsbian with Desktop)."""
path = '~/temp'
image.save(path + '/temp.png')
os.system("gpicview " + path + '/temp.png')
os.system('rm ' + path + '/temp.png')
def _image_loaded(self):
"""returns True if image was loaded"""

View File

@ -1,5 +1,3 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Stocks Module for Inkycal Project
@ -15,6 +13,7 @@ import logging
import os
from PIL import Image
from matplotlib import pyplot
from inkycal.custom import write, internet_available
from inkycal.modules.template import inkycal_module
@ -71,17 +70,14 @@ class Stocks(inkycal_module):
im_colour = Image.new('RGB', size=im_size, color='white')
# Create tmp path
tmpPath = '/tmp/inkycal_stocks/'
tmpPath = 'temp/'
try:
if not os.path.exists(tmpPath):
print(f"Creating tmp directory {tmpPath}")
os.mkdir(tmpPath)
except OSError:
print(f"Creation of tmp directory {tmpPath} failed")
else:
print(f"Successfully created tmp directory {tmpPath} ")
# Check if internet is available
if internet_available() == True:
if internet_available():
logger.info('Connection test passed')
else:
raise Exception('Network could not be reached :/')
@ -89,7 +85,7 @@ class Stocks(inkycal_module):
# Set some parameters for formatting feeds
line_spacing = 1
text_bbox = self.font.getbbox("hg")
line_height = text_bbox[3] - text_bbox[1] + line_spacing
line_height = text_bbox[3] + line_spacing
line_width = im_width
max_lines = (im_height // (line_height + line_spacing))
@ -204,7 +200,7 @@ class Stocks(inkycal_module):
else:
parsed_tickers_colour.append("")
if (_ < len(tickerCount)):
if _ < len(tickerCount):
parsed_tickers.append("")
parsed_tickers_colour.append("")
@ -225,9 +221,10 @@ class Stocks(inkycal_module):
logger.info(f'chartSpace is...{im_width} {im_height}')
logger.info(f'open chart ...{chartPath}')
chartImage = Image.open(chartPath)
chartImage.thumbnail((im_width / 4, line_height * 4), Image.BICUBIC)
chartImage.thumbnail((int(im_width / 4), int(line_height * 4)), Image.BICUBIC)
pyplot.close()
chartPasteX = im_width - (chartImage.width)
chartPasteX = im_width - chartImage.width
chartPasteY = line_height * 5 * _
logger.info(f'pasting chart image with index {_} to...{chartPasteX} {chartPasteY}')
@ -258,6 +255,3 @@ class Stocks(inkycal_module):
# Save image of black and colour channel in image-folder
return im_black, im_colour
if __name__ == '__main__':
print('running module in standalone/debug mode')

View File

@ -1,75 +0,0 @@
import unittest
from inkycal.modules import Stocks as Module
tests = [
{
"position": 1,
"name": "Stocks",
"config": {
"size": [528, 20],
"tickers": ['TSLA', 'AMD', 'NVDA', '^DJI', 'BTC-USD', 'EURUSD=X'],
"padding_x": 10, "padding_y": 10, "fontsize": 12, "language": "en"
}
},
{
"position": 1,
"name": "Stocks",
"config": {
"size": [528, 20],
"tickers": [],
"padding_x": 10, "padding_y": 10, "fontsize": 12, "language": "en"
}
},
{
"position": 1,
"name": "Stocks",
"config": {
"size": [528, 200],
"tickers": ['TSLA', 'AMD', 'NVDA', '^DJI', 'BTC-USD', 'EURUSD=X'],
"padding_x": 10, "padding_y": 10, "fontsize": 12, "language": "en"
}
},
{
"position": 1,
"name": "Stocks",
"config": {
"size": [528, 800],
"tickers": ['TSLA', 'AMD', 'NVDA', '^DJI', 'BTC-USD', 'EURUSD=X'],
"padding_x": 10, "padding_y": 10, "fontsize": 12, "language": "en"
}
},
{
"position": 1,
"name": "Stocks",
"config": {
"size": [528, 100],
"tickers": "TSLA,AMD,NVDA,^DJI,BTC-USD,EURUSD=X",
"padding_x": 10, "padding_y": 10, "fontsize": 12, "language": "en"
}
},
{
"position": 1,
"name": "Stocks",
"config": {
"size": [528, 400],
"tickers": ['TSLA', 'AMD', 'NVDA', '^DJI', 'BTC-USD', 'EURUSD=X'],
"padding_x": 10, "padding_y": 10, "fontsize": 14, "language": "en"
}
},
]
class module_test(unittest.TestCase):
def test_get_config(self):
print('getting data for web-ui...', end = "")
Module.get_config()
print('OK')
def test_generate_image(self):
for test in tests:
print(f'test {tests.index(test)+1} generating image..')
module = Module(test)
module.generate_image()
print('OK')
if __name__ == '__main__':
unittest.main()

View File

@ -1,4 +1,3 @@
#!python
"""
Tests config
"""

View File

@ -1,56 +1,50 @@
#!python3
"""
iCalendar parser test (ical_parser)
"""
import logging
import os
import sys
import unittest
from urllib.request import urlopen
import arrow
from inkycal.modules.ical_parser import iCalendar
from inkycal.tests import Config
from tests import Config
ical = iCalendar()
test_ical = Config.TEST_ICAL_URL
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
class ical_parser_test(unittest.TestCase):
class TestIcalendar(unittest.TestCase):
def test_load_url(self):
print('testing loading via URL...', end="")
logger.info('testing loading via URL...')
ical.load_url(test_ical)
print('OK')
logger.info('OK')
def test_get_events(self):
print('testing parsing of events...', end="")
logger.info('testing parsing of events...')
ical.get_events(arrow.now(), arrow.now().shift(weeks=30))
print('OK')
logger.info('OK')
def test_sorting(self):
print('testing sorting of events...', end="")
logger.info('testing sorting of events...')
ical.sort()
print('OK')
logger.info('OK')
def test_show_events(self):
print('testing if events can be shown...', end="")
logger.info('testing if events can be shown...')
ical.show_events()
print('OK')
logger.info('OK')
def test_laod_from_file(self):
print('testing loading from file...', end="")
logger.info('testing loading from file...')
dummy = str(urlopen(test_ical, timeout=10).read().decode())
with open('dummy.ical', mode="w", encoding="utf-8") as file:
file.write(dummy)
ical.load_from_file('dummy.ical')
print('OK')
logger.info('OK')
os.remove('dummy.ical')
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()

View File

@ -1,17 +1,19 @@
#!python3
"""
inkycal_agenda unittest
"""
import logging
import sys
import unittest
from inkycal.modules import Agenda as Module
from inkycal.modules import Agenda
from inkycal.modules.inky_image import Inkyimage
from inkycal.tests import Config
from tests import Config
preview = Inkyimage.preview
merge = Inkyimage.merge
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
sample_url = Config.SAMPLE_ICAL_URL
tests = [
@ -61,25 +63,13 @@ tests = [
]
class module_test(unittest.TestCase):
def test_get_config(self):
print('getting data for web-ui...', end="")
Module.get_config()
print('OK')
class TestAgenda(unittest.TestCase):
def test_generate_image(self):
for test in tests:
print(f'test {tests.index(test) + 1} generating image..')
module = Module(test)
logger.info(f'test {tests.index(test) + 1} generating image..')
module = Agenda(test)
im_black, im_colour = module.generate_image()
print('OK')
logger.info('OK')
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()

View File

@ -1,20 +1,21 @@
#!python3
"""
inkycal_calendar unittest
"""
import logging
import sys
import unittest
from inkycal.modules import Calendar as Module
from inkycal.modules import Calendar
from inkycal.modules.inky_image import Inkyimage
from inkycal.tests import Config
from tests import Config
preview = Inkyimage.preview
merge = Inkyimage.merge
sample_url = Config.SAMPLE_ICAL_URL
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
tests = [
{
"name": "Calendar",
@ -67,25 +68,13 @@ tests = [
]
class module_test(unittest.TestCase):
def test_get_config(self):
print('getting data for web-ui...', end="")
Module.get_config()
print('OK')
class TestCalendar(unittest.TestCase):
def test_generate_image(self):
for test in tests:
print(f'test {tests.index(test) + 1} generating image..', end="")
module = Module(test)
module = Calendar(test)
im_black, im_colour = module.generate_image()
print('OK')
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()

View File

@ -1,17 +1,18 @@
#!python3
"""
inkycal_feeds unittest
"""
import logging
import sys
import unittest
from inkycal.modules import Feeds as Module
from inkycal.modules import Feeds
from inkycal.modules.inky_image import Inkyimage
from inkycal.tests import Config
from tests import Config
preview = Inkyimage.preview
merge = Inkyimage.merge
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
tests = [
{
"name": "Feeds",
@ -43,25 +44,14 @@ tests = [
]
class module_test(unittest.TestCase):
def test_get_config(self):
print('getting data for web-ui...', end="")
Module.get_config()
print('OK')
class TestFeeds(unittest.TestCase):
def test_generate_image(self):
for test in tests:
print(f'test {tests.index(test) + 1} generating image..')
module = Module(test)
logger.info(f'test {tests.index(test) + 1} generating image..')
module = Feeds(test)
im_black, im_colour = module.generate_image()
print('OK')
logger.info('OK')
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()

View File

@ -1,19 +1,16 @@
#!python3
"""
inkycal_image unittest
"""
import logging
import sys
import unittest
import requests
from PIL import Image
from inkycal.modules import Inkyimage as Module
from inkycal.modules.inky_image import Inkyimage
from inkycal.tests import Config
from tests import Config
preview = Inkyimage.preview
merge = Inkyimage.merge
@ -23,6 +20,9 @@ im = Image.open(requests.get(url, stream=True).raw)
im.save("test.png", "PNG")
test_path = "test.png"
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
tests = [
{
"name": "Inkyimage",
@ -104,25 +104,13 @@ tests = [
]
class module_test(unittest.TestCase):
def test_get_config(self):
print('getting data for web-ui...', end="")
Module.get_config()
print('OK')
class TestInkyImage(unittest.TestCase):
def test_generate_image(self):
for test in tests:
print(f'test {tests.index(test) + 1} generating image..')
logger.info(f'test {tests.index(test) + 1} generating image..')
module = Module(test)
im_black, im_colour = module.generate_image()
print('OK')
logger.info('OK')
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()

View File

@ -1,17 +1,19 @@
#!python3
"""
inkycal_jokes unittest
"""
import logging
import sys
import unittest
from inkycal.modules import Jokes as Module
from inkycal.modules import Jokes
from inkycal.modules.inky_image import Inkyimage
from inkycal.tests import Config
from tests import Config
preview = Inkyimage.preview
merge = Inkyimage.merge
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
tests = [
{
"name": "Jokes",
@ -46,25 +48,13 @@ tests = [
]
class module_test(unittest.TestCase):
def test_get_config(self):
print('getting data for web-ui...', end="")
Module.get_config()
print('OK')
class TestJokes(unittest.TestCase):
def test_generate_image(self):
for test in tests:
print(f'test {tests.index(test) + 1} generating image..')
module = Module(test)
logger.info(f'test {tests.index(test) + 1} generating image..')
module = Jokes(test)
im_black, im_colour = module.generate_image()
print('OK')
logger.info('OK')
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()

View File

@ -1,18 +1,16 @@
#!python3
"""
Slideshow test (inkycal_slideshow)
"""
import logging
import os
import sys
import unittest
import requests
from PIL import Image
from inkycal.modules import Slideshow as Module
from inkycal.modules import Slideshow
from inkycal.modules.inky_image import Inkyimage
from inkycal.tests import Config
from tests import Config
preview = Inkyimage.preview
merge = Inkyimage.merge
@ -31,6 +29,9 @@ for count, url in enumerate(im_urls):
test_path = "tmp"
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
tests = [
{
"name": "Slideshow",
@ -134,24 +135,20 @@ tests = [
]
class module_test(unittest.TestCase):
def test_get_config(self):
print('getting data for web-ui...', end="")
Module.get_config()
print('OK')
class TestSlideshow(unittest.TestCase):
def test_generate_image(self):
for test in tests:
print(f'test {tests.index(test) + 1} generating image..')
module = Module(test)
logger.info(f'test {tests.index(test) + 1} generating image..')
module = Slideshow(test)
im_black, im_colour = module.generate_image()
print('OK')
logger.info('OK')
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
def test_switch_to_next_image(self):
print(f'testing switching to next images..')
module = Module(tests[0])
logger.info(f'testing switching to next images..')
module = Slideshow(tests[0])
im_black, im_colour = module.generate_image()
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
@ -164,12 +161,4 @@ class module_test(unittest.TestCase):
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
print('OK')
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()
logger.info('OK')

57
tests/test_inkycal_stocks.py Executable file
View File

@ -0,0 +1,57 @@
import logging
import unittest
from inkycal.modules import Stocks
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
tests = [
{
"position": 1,
"name": "Stocks",
"config": {
"size": [400, 100],
"tickers": ['TSLA', 'AMD', 'NVDA', '^DJI', 'BTC-USD', 'EURUSD=X'],
"padding_x": 10, "padding_y": 10, "fontsize": 12, "language": "en"
}
},
{
"position": 1,
"name": "Stocks",
"config": {
"size": [400, 200],
"tickers": [],
"padding_x": 10, "padding_y": 10, "fontsize": 12, "language": "en"
}
},
{
"position": 1,
"name": "Stocks",
"config": {
"size": [400, 300],
"tickers": ['TSLA', 'AMD', 'NVDA', '^DJI', 'BTC-USD', 'EURUSD=X'],
"padding_x": 10, "padding_y": 10, "fontsize": 12, "language": "en"
}
},
{
"position": 1,
"name": "Stocks",
"config": {
"size": [400, 400],
"tickers": ['TSLA', 'AMD', 'NVDA', '^DJI', 'BTC-USD', 'EURUSD=X'],
"padding_x": 10, "padding_y": 10, "fontsize": 12, "language": "en"
}
}
]
class TestStocks(unittest.TestCase):
def test_generate_image(self):
for test in tests:
logger.info(f'test {tests.index(test) + 1} generating image..')
module = Stocks(test)
module.generate_image()
logger.info('OK')

View File

@ -1,19 +1,23 @@
#!python3
"""
Inkycal Text module
"""
import logging
import os
import sys
import unittest
from inkycal.modules import TextToDisplay as Module
from inkycal.modules import TextToDisplay
from inkycal.modules.inky_image import Inkyimage
from inkycal.tests import Config
from tests import Config
preview = Inkyimage.preview
merge = Inkyimage.merge
temp_path = f"{Config.TEMP_PATH}/temp.txt"
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
dummy_data = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', ' Donec feugiat facilisis neque vel blandit.',
'Integer viverra dolor risus.', ' Etiam neque tellus, sollicitudin at nisi a, mollis ornare enim.',
@ -85,33 +89,20 @@ class TestTextToDisplay(unittest.TestCase):
def setUp(self):
self.temp_path = temp_path
if not os.path.exists(self.temp_path):
print("could not find temporary file, creating now.")
logger.info("could not find temporary file, creating now.")
with open(self.temp_path, encoding="utf-8", mode="w") as file:
file.writelines(dummy_data)
def test_get_config(self):
print('getting data for web-ui...', end="")
Module.get_config()
print('OK')
def test_generate_image(self):
for test in tests:
print(f'test {tests.index(test) + 1} generating image..')
module = Module(test)
logger.info(f'test {tests.index(test) + 1} generating image..')
module = TextToDisplay(test)
im_black, im_colour = module.generate_image()
print('OK')
logger.info('OK')
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
def tearDown(self):
if os.path.exists(self.temp_path):
print("deleting temporary file.")
logger.info("deleting temporary file.")
os.remove(self.temp_path)
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()

View File

@ -1,19 +1,21 @@
#!python3
"""
inkycal_todoist unittest
"""
import logging
import sys
import unittest
from inkycal.modules import Todoist as Module
from inkycal.modules import Todoist
from inkycal.modules.inky_image import Inkyimage
from inkycal.tests import Config
from tests import Config
preview = Inkyimage.preview
merge = Inkyimage.merge
api_key = Config.TODOIST_API_KEY
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
tests = [
{
"name": "Todoist",
@ -30,29 +32,16 @@ tests = [
]
class module_test(unittest.TestCase):
def test_get_config(self):
print('getting data for web-ui...', end="")
Module.get_config()
print('OK')
class TestTodoist(unittest.TestCase):
def test_generate_image(self):
if api_key:
for test in tests:
print(f'test {tests.index(test) + 1} generating image..')
module = Module(test)
module = Todoist(test)
im_black, im_colour = module.generate_image()
print('OK')
if Config.USE_PREVIEW:
preview(merge(im_black, im_colour))
else:
print('No api key given, omitting test')
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()

View File

@ -1,20 +1,22 @@
#!python3
"""
inkycal_weather unittest
"""
import logging
import sys
import unittest
from inkycal.modules import Weather as Module
from inkycal.modules import Weather
from inkycal.modules.inky_image import Inkyimage
from inkycal.tests import Config
from tests import Config
preview = Inkyimage.preview
merge = Inkyimage.merge
owm_api_key = Config.OPENWEATHERMAP_API_KEY
location = '2825297'
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
tests = [
{
"position": 1,
@ -171,27 +173,14 @@ tests = [
]
class module_test(unittest.TestCase):
def test_get_config(self):
print('getting data for web-ui...', end="")
Module.get_config()
print('OK')
class TestWeather(unittest.TestCase):
def test_generate_image(self):
for test in tests:
print(f'test {tests.index(test) + 1} generating image..')
module = Module(test)
logger.info(f'test {tests.index(test) + 1} generating image..')
module = Weather(test)
im_black, im_colour = module.generate_image()
print('OK')
logger.info('OK')
if Config.USE_PREVIEW:
merged = merge(im_black, im_colour)
preview(merged)
if __name__ == '__main__':
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
unittest.main()