|
1 | 1 | # Licensed under a 3-clause BSD style license - see LICENSE.rst |
2 | 2 | from __future__ import print_function |
3 | 3 |
|
4 | | -# astroquery uses the pytest framework for testing |
5 | | -# this is already available in astropy and does |
6 | | -# not require a separate install. Import it using: |
7 | | -from astropy.tests.helper import pytest |
8 | | - |
9 | | -# It would be best if tests are separated in two |
10 | | -# modules. This module performs tests on local data |
11 | | -# by mocking HTTP requests and responses. To test the |
12 | | -# same functions on the remote server, put the relevant |
13 | | -# tests in the 'test_module_remote.py' |
14 | | - |
15 | | -# Now import other commonly used modules for tests |
16 | 4 | import os |
17 | | -import requests |
18 | | - |
19 | | -from numpy import testing as npt |
20 | | -from astropy.table import Table |
21 | | -import astropy.coordinates as coord |
22 | | -import astropy.units as u |
23 | 5 |
|
24 | | -from ...utils.testing_tools import MockResponse |
25 | | - |
26 | | -# finally import the module which is to be tested |
27 | | -# and the various configuration items created |
28 | 6 | from ... import vamdc |
29 | | -from ...vamdc import conf |
30 | | - |
31 | | -# Local tests should have the corresponding data stored |
32 | | -# in the ./data folder. This is the actual HTTP response |
33 | | -# one would expect from the server when a valid query is made. |
34 | | -# Its best to keep the data file small, so that testing is |
35 | | -# quicker. When running tests locally the stored data is used |
36 | | -# to mock the HTTP requests and response part of the query |
37 | | -# thereby saving time and bypassing unreliability for |
38 | | -# an actual remote network query. |
39 | 7 |
|
40 | | -DATA_FILES = {'GET': |
41 | | - # You might have a different response for each URL you're |
42 | | - # querying: |
43 | | - {'http://dummy_server_mirror_1': |
44 | | - 'dummy.dat'}} |
45 | 8 |
|
46 | | - |
47 | | -# ./setup_package.py helps the test function locate the data file |
48 | | -# define a function that can construct the full path to the file in the |
49 | | -# ./data directory: |
50 | 9 | def data_path(filename): |
51 | 10 | data_dir = os.path.join(os.path.dirname(__file__), 'data') |
52 | 11 | return os.path.join(data_dir, filename) |
53 | 12 |
|
54 | 13 |
|
55 | | -# define a monkeypatch replacement request function that returns the |
56 | | -# dummy HTTP response for the dummy 'get' function, by |
57 | | -# reading in data from some data file: |
58 | | -def nonremote_request(self, request_type, url, **kwargs): |
59 | | - # kwargs are ignored in this case, but they don't have to be |
60 | | - # (you could use them to define which data file to read) |
61 | | - with open(data_path(DATA_FILES[request_type][url]), 'rb') as f: |
62 | | - response = MockResponse(content=f.read(), url=url) |
63 | | - return response |
64 | | - |
65 | | - |
66 | | -# use a pytest fixture to create a dummy 'requests.get' function, |
67 | | -# that mocks(monkeypatches) the actual 'requests.get' function: |
68 | | -@pytest.fixture |
69 | | -def patch_request(request): |
70 | | - mp = request.getfuncargvalue("monkeypatch") |
71 | | - mp.setattr(vamdc.core.VamdcClass, '_request', |
72 | | - nonremote_request) |
73 | | - return mp |
74 | | - |
75 | | - |
76 | 14 | # finally test the methods using the mock HTTP response |
77 | 15 | #def test_query_molecule(patch_request): |
78 | 16 | # ch3oh = vamdc.core.VamdcClass().query_molecule('CH3OH') |
|
0 commit comments