Completed
Pull Request — develop (#47)
by Angeline
01:12
created

TestCmdAACGMV2.test_convert_mlt_single_line()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 9
rs 9.95
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
from __future__ import division, absolute_import, unicode_literals, print_function
3
4
import subprocess
5
import numpy as np
6
import os
7
import pytest
8
from sys import version_info
9
10
import aacgmv2
11
12
class TestCmdAACGMV2:
13
    def setup(self):
14
        """Runs 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(self):
26
        """Runs 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
        p_commands = ['python', '-m', 'aacgmv2', 'convert', '-i',
52
                      self.convert, '-d', '20150224', '-o',
53
                      self.output]
54
        p_commands.extend(pin)
55
        p = subprocess.Popen(p_commands)
56
        p.communicate()
57
        p.wait()
58
        assert os.path.isfile(self.output)
59
        data = np.loadtxt(self.output)
60
        np.testing.assert_allclose(data, ref, rtol=self.rtol)
61
62
    @pytest.mark.skip(version_info.major == 3,
63
                      reason='only works locally for Python 3')
64
    def test_convert_today(self):
65
        """ Test the shape of the output for today's date """
66
        p = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
67
                              self.convert, '-o', self.output])
68
        p.communicate()
69
        p.wait()
70
        assert os.path.isfile(self.output)
71
        data = np.loadtxt(self.output)
72
        assert data.shape == (3,3)
73
        
74
    def test_convert_single_line(self):
75
        """ Test the command line with a single line as input """
76
        p = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert', '-i',
77
                              self.single, '-d', '20150224', '-o', self.output])
78
        p.communicate()
79
        p.wait()
80
        assert os.path.isfile(self.output)
81
        data = np.loadtxt(self.output)
82
        np.testing.assert_allclose(data, [57.4810, 93.5290, 1.04566],
83
                                   rtol=self.rtol)
84
85
    def test_convert_stdin_stdout(self):
86
        p = subprocess.Popen(
87
            'echo 60 15 300 | python -m aacgmv2 convert -d 20150224',
88
                             shell=True, stdout=subprocess.PIPE)
89
        stdout, _ = p.communicate()
90
        p.wait()
91
        assert b'57.48099198 93.52895314' in stdout
92
93
    @pytest.mark.parametrize('pin,ref',
94
                             [([], [9.0912, 9.8246, 10.5579]),
95
                              (['-v'], [-120.3687, 44.6313, -150.3687])
96
                             ])
97
    def test_convert_mlt_command_line(self, pin, ref):
98
        """ Test the command line MLT conversion options"""
99
        p_command = ['python', '-m', 'aacgmv2', 'convert_mlt', '-i',
100
                     self.mlt, '20150224140015', '-o', self.output]
101
        p_command.extend(pin)
102
        p = subprocess.Popen(p_command)
103
        p.communicate()
104
        p.wait()
105
        assert os.path.isfile(self.output)
106
        data = np.loadtxt(self.output)
107
        np.testing.assert_allclose(data, ref, rtol=self.rtol)
108
109
110
    def test_convert_mlt_single_line(self):
111
        p = subprocess.Popen(['python', '-m', 'aacgmv2', 'convert_mlt', '-i',
112
                              self.mlt_single, '20150224140015', '-o',
113
                              self.output])
114
        p.communicate()
115
        p.wait()
116
        assert os.path.isfile(self.output)
117
        data = np.loadtxt(self.output)
118
        np.testing.assert_allclose(data, 9.0912, rtol=self.rtol)
119
120
    @pytest.mark.parametrize('echo_command',
121
                             [('echo 12 | python -m aacgmv2 convert_mlt -v 20150224140015'),
122
                              ('echo 12 | python -m aacgmv2 convert_mlt 20150224140015 -v')])
123
    def test_convert_mlt_stdin_stdout(self, echo_command):
124
        p = subprocess.Popen(echo_command, shell=True, stdout=subprocess.PIPE)
125
        stdout, _ = p.communicate()
126
        p.wait()
127
        assert b'44.63126817' in stdout
128