File: test_nvas.py

package info (click to toggle)
astroquery 0.4.6%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,420 kB
  • sloc: xml: 56,574; python: 43,303; makefile: 145; ansic: 69
file content (125 lines) | stat: -rw-r--r-- 4,058 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Licensed under a 3-clause BSD style license - see LICENSE.rst


import os
import re
from contextlib import contextmanager

import numpy.testing as npt
import astropy.units as u
import pytest

from ...import nvas
from astroquery.utils.mocks import MockResponse
from ...utils import commons

COORDS_GAL = commons.GalacticCoordGenerator(
    l=49.489, b=-0.37, unit=(u.deg, u.deg))  # ARM 2000
COORDS_ICRS = commons.ICRSCoordGenerator(
    "12h29m06.69512s +2d03m08.66276s")  # 3C 273

DATA_FILES = {'image': 'image.imfits',
              'image_search': 'image_results.html'}


def data_path(filename):
    data_dir = os.path.join(os.path.dirname(__file__), 'data')
    return os.path.join(data_dir, filename)


@pytest.fixture
def patch_post(request):
    mp = request.getfixturevalue("monkeypatch")

    mp.setattr(nvas.Nvas, '_request', post_mockreturn)
    return mp


@pytest.fixture
def patch_parse_coordinates(request):
    def parse_coordinates_mock_return(c):
        return c

    mp = request.getfixturevalue("monkeypatch")

    mp.setattr(commons, 'parse_coordinates', parse_coordinates_mock_return)
    return mp


def post_mockreturn(method, url, data, timeout, **kwargs):
    filename = data_path(DATA_FILES['image_search'])
    content = open(filename, 'rb').read()
    response = MockResponse(content, **kwargs)
    return response


@pytest.fixture
def patch_get_readable_fileobj(request):
    @contextmanager
    def get_readable_fileobj_mockreturn(filename, **kwargs):
        encoding = kwargs.get('encoding', None)
        if encoding == 'binary':
            file_obj = open(data_path(DATA_FILES["image"]), 'rb')
        else:
            file_obj = open(data_path(DATA_FILES["image"]),
                            "r", encoding=encoding)
        yield file_obj

    mp = request.getfixturevalue("monkeypatch")

    mp.setattr(commons, 'get_readable_fileobj',
               get_readable_fileobj_mockreturn)
    return mp


def deparse_coordinates(cstr):
    """
    '19 23 40.001395 +14 31 01.550347' -> '19:23:40.001395 +14:31:01.550347'
    """
    return re.sub(r" ([\+-])", r",\1", cstr).replace(" ", ":").replace(",", " ")


@pytest.mark.parametrize(('coordinates'), [COORDS_GAL, COORDS_ICRS])
def test_parse_coordinates(coordinates):
    out_str = nvas.core._parse_coordinates(coordinates)
    new_coords = commons.ICRSCoordGenerator(
        deparse_coordinates(out_str), unit=(u.hour, u.deg))
    # if all goes well new_coords and coordinates have same ra and dec
    npt.assert_approx_equal(new_coords.ra.degree,
                            coordinates.transform_to('fk5').ra.degree,
                            significant=3)
    npt.assert_approx_equal(new_coords.dec.degree,
                            coordinates.transform_to('fk5').dec.degree,
                            significant=3)


def test_extract_image_urls():
    html_in = open(data_path(DATA_FILES['image_search']), 'r').read()
    image_list = nvas.core.Nvas.extract_image_urls(html_in)
    assert len(image_list) == 2


def test_get_images_async(patch_post, patch_parse_coordinates):
    image_list = nvas.core.Nvas.get_images_async(COORDS_ICRS, band='K',
                                                 radius=2 * u.arcsec,
                                                 max_rms=100)
    assert len(image_list) == 2


#def test_get_images(patch_post, patch_parse_coordinates,
#                    patch_get_readable_fileobj):
#    images = nvas.core.Nvas.get_images(COORDS_GAL, radius='5d0m0s', band='all')
#    assert images is not None


def test_get_image_list(patch_post, patch_parse_coordinates):
    image_list = nvas.core.Nvas.get_image_list(
        COORDS_GAL, radius=15 * u.arcsec,
        max_rms=500, band="all", get_query_payload=True)
    npt.assert_approx_equal(image_list["nvas_rad"], 0.25, significant=2)
    assert image_list["nvas_bnd"] == ""
    assert image_list["nvas_rms"] == 500
    image_list = nvas.core.Nvas.get_image_list(
        COORDS_GAL, radius=15 * u.arcsec, max_rms=500, band="all")

    assert len(image_list) == 2