test_file_io.py 1.81 KB
Newer Older
Maximilian Dolling's avatar
Maximilian Dolling committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import unittest
import os
from src import astronauts_analysis
import shutil
import time
from datetime import datetime


class TestDataRead(unittest.TestCase):

    def test_no_data(self):
        # set wrong data path
        astronauts_analysis._ASTRONAUT_DATA = "data/astronauts.jsson"

        with self.assertRaises(ValueError):
            astronauts_analysis.perform_analysis()

        # restore correct data path
        astronauts_analysis._ASTRONAUT_DATA = "data/astronauts.json"


class TestResultWrite(unittest.TestCase):

    def test_output_files_exist(self):
        astronauts_analysis.perform_analysis()

        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_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()