TestCmdAACGMV2.setup_method()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
"""Unit tests for the command line interface."""
2
import subprocess
3
import numpy as np
4
import os
5
import pytest
6
7
import aacgmv2
8
9
10
class TestCmdAACGMV2(object):
11
    """Unit tests for the command line interface."""
12
13
    def setup_method(self):
14
        """Run before every method to create a clean testing setup."""
15
        self.test_dir = os.path.join(aacgmv2.__path__[0], 'tests', 'test_data')
16
        self.output = os.path.join(self.test_dir, "output.txt")
17
        self.convert = os.path.join(self.test_dir, "test_convert.txt")
18
        self.single = os.path.join(self.test_dir,
19
                                   'test_convert_single_line.txt')
20
        self.mlt = os.path.join(self.test_dir, 'test_convert_mlt.txt')
21
        self.mlt_single = os.path.join(self.test_dir,
22
                                       'test_convert_mlt_single_line.txt')
23
        self.rtol = 1.0e-4
24
25
    def teardown_method(self):
26
        """Run after every method to clean up previous testing."""
27
        if os.path.isfile(self.output):
28
            os.remove(self.output)
29
30
        del self.test_dir, self.output, self.convert, self.single, self.mlt
31
        del self.mlt_single, self.rtol
32
33
    @pytest.mark.parametrize('pin,ref',
34
                             [([], [[57.4810, 93.5290, 1.04566],
35
                                    [58.5380, 93.9324, 1.0456],
36
                                    [59.5900, 94.3614, 1.04556]]),
37
                              (['-v'], [[51.6616, -66.6338, 306.1783],
38
                                        [52.6792, -66.7291, 306.5470],
39
                                        [53.6980, -66.8286, 306.91265]]),
40
                              (['-g'], [[57.6746, 93.6036, 1.0471],
41
                                        [58.7271, 94.0102, 1.0471],
42
                                        [59.7743, 94.4425, 1.0471]]),
43
                              (['-t'], [[57.4785, 93.5398, 1.04566],
44
                                        [58.5354, 93.9438, 1.04561],
45
                                        [59.5873, 94.3731, 1.04556]]),
46
                              (['-t', '-v'], [[51.6524, -66.6180, 306.1750],
47
                                              [52.6738, -66.7167, 306.5451],
48
                                              [53.6964, -66.8202, 306.9121]])])
49
    def test_convert_command_line(self, pin, ref):
50
        """Test the output from the command line routine.
51
52
        Parameters
53
        ----------
54
        pin : list
55
           List of potential flags
56
        ref : list
57
           List of expected output values
58
59
        """
60
        p_commands = ['python', '-m', 'aacgmv2', 'convert', '-i',
61
                      self.convert, '-d', '20150224', '-o',
62
                      self.output]
63
        p_commands.extend(pin)
64
        pin = subprocess.Popen(p_commands)
65
        pin.communicate()
66
        pin.wait()
67
        assert os.path.isfile(self.output)
68
        data = np.loadtxt(self.output)
69
        np.testing.assert_allclose(data, ref, rtol=self.rtol)
70
71
    def test_convert_today(self):
72
        """Test the shape of the output for today's date."""
73
        pin = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
74
                                self.convert, '-o', self.output])
75
        pin.communicate()
76
        pin.wait()
77
        assert os.path.isfile(self.output)
78
        data = np.loadtxt(self.output)
79
        assert data.shape == (3, 3)
80
81
    def test_convert_single_line(self):
82
        """Test the command line with a single line as input."""
83
        pin = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
84
                                self.single, '-d', '20150224', '-o',
85
                                self.output])
86
        pin.communicate()
87
        pin.wait()
88
        assert os.path.isfile(self.output)
89
        data = np.loadtxt(self.output)
90
        np.testing.assert_allclose(data, [57.4810, 93.5290, 1.04566],
91
                                   rtol=self.rtol)
92
93
    def test_main_help(self):
94
        """Test the help output."""
95
        pin = subprocess.Popen('python -m aacgmv2 -h', shell=True,
96
                               stdout=subprocess.PIPE)
97
        stdout, _ = pin.communicate()
98
        pin.wait()
99
        assert b'usage' in stdout
100
101
    def test_convert_stdin_stdout(self):
102
        """Test the ability to pipe in inputs and pipe out the outputs."""
103
        pin = subprocess.Popen(
104
            'echo 60 15 300 | python -m aacgmv2 convert -d 20150224',
105
            shell=True, stdout=subprocess.PIPE)
106
        stdout, _ = pin.communicate()
107
        pin.wait()
108
        assert b'57.48099346 93.52899517' in stdout
109
110
    @pytest.mark.parametrize('pin,ref',
111
                             [([], [9.0912, 9.8246, 10.5579]),
112
                              (['-v'], [-120.3687, 44.6313, -150.3687])])
113
    def test_convert_mlt_command_line(self, pin, ref):
114
        """Test the command line MLT conversion options.
115
116
        Parameters
117
        ----------
118
        pin : list
119
            List of input flags
120
        ref : list
121
            List of expected outputs
122
123
        """
124
        p_command = ['python', '-m', 'aacgmv2', 'convert_mlt', '-i',
125
                     self.mlt, '20150224140015', '-o', self.output]
126
        p_command.extend(pin)
127
        pin = subprocess.Popen(p_command)
128
        pin.communicate()
129
        pin.wait()
130
        assert os.path.isfile(self.output)
131
        data = np.loadtxt(self.output)
132
        np.testing.assert_allclose(data, ref, rtol=self.rtol)
133
134
    def test_convert_mlt_single_line(self):
135
        """Test the CLI for converting MLT."""
136
        pin = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert_mlt', '-i',
137
                                self.mlt_single, '20150224140015', '-o',
138
                                self.output])
139
        pin.communicate()
140
        pin.wait()
141
        assert os.path.isfile(self.output)
142
        data = np.loadtxt(self.output)
143
        np.testing.assert_allclose(data, 9.0912, rtol=self.rtol)
144
145
    @pytest.mark.parametrize('echo_command', [
146
        ('echo 12 | python -m aacgmv2 convert_mlt -v 20150224140015'),
147
        ('echo 12 | python -m aacgmv2 convert_mlt 20150224140015 -v')])
148
    def test_convert_mlt_stdin_stdout(self, echo_command):
149
        """Test MLT conversion through piped input and output.
150
151
        Parameters
152
        ----------
153
        echo_command : str
154
            Command to run starting with an echo pipe
155
156
        """
157
        pin = subprocess.Popen(echo_command, shell=True, stdout=subprocess.PIPE)
158
        stdout, _ = pin.communicate()
159
        pin.wait()
160
        assert b'44.63120804' in stdout
161