Passed
Pull Request — develop (#77)
by Angeline
04:09
created

test_cmd_aacgmv2.TestCmdAACGMV2.setup()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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