Commit dc9973c3 authored by Maximilian Dolling's avatar Maximilian Dolling

added example tests

parent 570c98ca
Pipeline #10103 passed with stage
in 1 minute and 38 seconds
......@@ -91,6 +91,10 @@ def calculate_age(born: pd.Timestamp) -> int:
Returns:
int
"""
if not isinstance(born, pd.Timestamp):
raise TypeError(f'expected {pd.Timestamp}, got {type(born)}')
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
......
import unittest
import pandas as pd
from src import astronauts_analysis
class TestStringMethods(unittest.TestCase):
class TestCalculateAge(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_return_type(self):
birth_date = pd.Timestamp('1950-01-01')
self.assertEqual(type(astronauts_analysis.calculate_age(birth_date)), int)
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
class TestStringMethods2(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
def test_arg_type(self):
birth_date = '1950-01-01'
with self.assertRaises(TypeError):
s.split(2)
astronauts_analysis.calculate_age(birth_date)
if __name__ == '__main__':
......
import unittest
import os
from src import astronauts_analysis
import shutil
import time
from datetime import datetime
class TestStringMethods(unittest.TestCase):
class TestDataRead(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_no_data(self):
# set wrong data path
astronauts_analysis._ASTRONAUT_DATA = "data/astronauts.jsson"
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
with self.assertRaises(ValueError):
astronauts_analysis.perform_analysis()
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
# restore correct data path
astronauts_analysis._ASTRONAUT_DATA = "data/astronauts.json"
class TestStringMethods2(unittest.TestCase):
class TestResultWrite(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_output_files_exist(self):
astronauts_analysis.perform_analysis()
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
self.assertTrue(os.path.isfile('results/boxplot.png'))
self.assertTrue(os.path.isfile('results/combined_histogram.png'))
self.assertTrue(os.path.isfile('results/female_humans_in_space.png'))
self.assertTrue(os.path.isfile('results/humans_in_space.png'))
self.assertTrue(os.path.isfile('results/male_humans_in_space.png'))
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
def test_output_files_overwritten(self):
# remove previous generated outputs
shutil.rmtree('results')
# generate output
astronauts_analysis.perform_analysis()
# wait certain time, to get a good delta to previous edits
wait_seconds = 10
time.sleep(wait_seconds)
# overwrite previous generated output
astronauts_analysis.perform_analysis()
# calculate how much time passed since the output was generated
timedelta = datetime.utcnow() - datetime.utcfromtimestamp(os.path.getmtime('results/boxplot.png'))
# test that time passed < wait time
self.assertLess(timedelta.total_seconds(), wait_seconds)
def preparations():
# remove previous generated results
shutil.rmtree('results')
if __name__ == '__main__':
preparations()
unittest.main()
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