Completed
Pull Request — master (#20)
by
unknown
03:41
created

test_cmd   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 74
dl 0
loc 106
rs 10
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A test_convert_single_line() 0 8 1
A test_invalid_coord() 0 7 1
A test_convert_refh() 0 6 1
A test_mlt_nodatetime() 0 6 1
A test_module_invocation() 0 10 1
A test_convert_stdin_stdout() 0 6 1
A test_convert_dates() 0 11 1
A test_invalid_date() 0 7 1
A test_convert_mlt() 0 8 1
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
import pytest
4
from pytest import approx
5
import numpy as np
6
import subprocess
7
import sys
8
import os
9
10
PYEXE = sys.executable
11
TESTFN = 'output.txt'
12
INDIR = os.path.dirname(__file__)
13
INFN1 = os.path.join(INDIR, 'test_convert.txt')
14
INFN2 = os.path.join(INDIR, 'test_convert_single_line.txt')
15
16
def test_module_invocation(tmp_path):
17
    outfile = str(tmp_path / TESTFN)
18
    subprocess.check_call([PYEXE, '-m', 'apexpy', 'geo', 'apex', '2015',
19
                             '--height', '300', '-i', INFN1,
20
                             '-o', outfile])
21
22
    data = np.loadtxt(outfile)
23
    assert data == approx(np.array([[57.469547, 93.639816],
24
                                  [58.522701, 94.044762],
25
                                  [59.571465, 94.477257]]), rel=1e-4)
26
27
28
@pytest.mark.parametrize('dt', ['2015', '201501', '20150101', '20150101000000'])
29
def test_convert_dates(dt, tmp_path):
30
    outfile = str(tmp_path / TESTFN)
31
    subprocess.check_call([PYEXE, '-m', 'apexpy', 'geo', 'apex', dt,
32
                           '--height', '300',
33
                           '-i', INFN1, '-o', outfile])
34
35
    data = np.loadtxt(outfile)
36
    assert data == approx(np.array([[57.469547, 93.639816],
37
                                      [58.522701, 94.044762],
38
                                      [59.571465, 94.477257]]), rel=1e-4)
39
40
41
def test_convert_single_line(tmp_path):
42
    outfile = str(tmp_path / TESTFN)
43
    subprocess.check_call([PYEXE, '-m', 'apexpy', 'geo', 'apex',
44
                             '20150101000000', '--height', '300',
45
                             '-i', INFN2, '-o', outfile])
46
47
    data = np.loadtxt(outfile)
48
    assert data == approx([57.469547, 93.639816], rel=1e-4)
49
50
51
def test_convert_stdin_stdout():
52
    ret = subprocess.check_output(['apexpy', 'geo', 'apex', '2015', '--height', '300'],
53
                          input='60 15',
54
                          universal_newlines=True)
55
56
    assert np.array(ret.split(' '), dtype=float) == approx([57.469547, 93.639816], rel=1e-4)
57
58
59
def test_convert_refh():
60
    ret = subprocess.check_output(['apexpy', 'geo', 'apex', '2000', '--height', '100', '--refh=300'],
61
                          input='60 15',
62
                          universal_newlines=True)
63
64
    assert np.array(ret.split(' '), dtype=float) == approx([55.94841766, 94.1068344], rel=1e-4)
65
66
67
def test_convert_mlt(tmp_path):
68
    outfile = str(tmp_path / TESTFN)
69
    subprocess.check_call([PYEXE, '-m', 'apexpy', 'geo', 'mlt',
70
                             '20150101000000', '--height', '300',
71
                             '-i', INFN2, '-o', outfile])
72
73
    data = np.loadtxt(outfile)
74
    assert data == approx([57.469547, 1.06324], rel=1e-4)
75
76
77
@pytest.mark.parametrize('bad', ['201501010', '2015010100000'])
78
def test_invalid_date(bad):
79
    ret = subprocess.run(['apexpy', 'geo', 'apex', bad],
80
                          input='60 15', stderr=subprocess.PIPE,
81
                          universal_newlines=True)
82
83
    assert 'ValueError' in ret.stderr
84
85
86
def test_mlt_nodatetime():
87
    ret = subprocess.run(['apexpy', 'geo', 'mlt', '20150101'],
88
                          input='60 15', stderr=subprocess.PIPE,
89
                          universal_newlines=True)
90
91
    assert 'ValueError' in ret.stderr
92
93
94
@pytest.mark.parametrize('bad1, bad2', [('foobar', 'apex'), ('geo', 'foobar')])
95
def test_invalid_coord(bad1, bad2):
96
    ret = subprocess.run(['apexpy', bad1, bad2, '2015'],
97
                          input='60 15', stderr=subprocess.PIPE,
98
                          universal_newlines=True)
99
100
    assert 'invalid choice' in ret.stderr
101
102
103
104
if __name__ == '__main__':
105
    pytest.main([__file__])
106