<commit_before># Licensed under a 3-clause BSD style license - see LICENSE.rst
from ... import alfalfa
from astropy import coordinates
import astropy.utils.data as aud
from astropy.tests.helper import pytest
import requests
from contextlib import contextmanager
import os

DATA_FILES = {'catalog':'alfalfa_cat_small.txt',
              'spectrum':'alfalfa_sp.fits'}

class MockResponse(object):

    def __init__(self, content):
        self.content = content

    def iter_lines(self):
        for l in self.content.split("\n"):
            yield l

    def close(self):
        pass

@pytest.fixture
def patch_get(request):
    mp = request.getfuncargvalue("monkeypatch")
    mp.setattr(requests, 'get', get_mockreturn)
    return mp

@pytest.fixture
def patch_get_readable_fileobj(request):
    @contextmanager
    def get_readable_fileobj_mockreturn(filename, **kwargs):
        file_obj = data_path(DATA_FILES['spectrum']) # TODO: add images option
        yield open(file_obj)
    mp = request.getfuncargvalue("monkeypatch")
    mp.setattr(aud, 'get_readable_fileobj', get_readable_fileobj_mockreturn)
    return mp

def get_mockreturn(url, params=None, timeout=10):
    filename = data_path(DATA_FILES['catalog'])
    content = open(filename, 'r').read()
    return MockResponse(content)

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

# Test Case: A Seyfert 1 galaxy
coords = coordinates.ICRS('0h8m05.63s +14d50m23.3s')

ALFALFA = alfalfa.core.Alfalfa()

def test_alfalfa_catalog(patch_get, patch_get_readable_fileobj, coords=coords):
    cat = ALFALFA.get_catalog()<commit_msg>need to read FITS files as 'rb' for py3<commit_after># Licensed under a 3-clause BSD style license - see LICENSE.rst
from ... import alfalfa
from astropy import coordinates
import astropy.utils.data as aud
from astropy.tests.helper import pytest
import requests
from contextlib import contextmanager
import os

DATA_FILES = {'catalog':'alfalfa_cat_small.txt',
              'spectrum':'alfalfa_sp.fits'}

class MockResponse(object):

    def __init__(self, content):
        self.content = content

    def iter_lines(self):
        for l in self.content.split("\n"):
            yield l

    def close(self):
        pass

@pytest.fixture
def patch_get(request):
    mp = request.getfuncargvalue("monkeypatch")
    mp.setattr(requests, 'get', get_mockreturn)
    return mp

@pytest.fixture
def patch_get_readable_fileobj(request):
    @contextmanager
    def get_readable_fileobj_mockreturn(filename, **kwargs):
        file_obj = data_path(DATA_FILES['spectrum']) # TODO: add images option
        yield open(file_obj,'rb') # read as bytes, assuming FITS
    mp = request.getfuncargvalue("monkeypatch")
    mp.setattr(aud, 'get_readable_fileobj', get_readable_fileobj_mockreturn)
    return mp

def get_mockreturn(url, params=None, timeout=10):
    filename = data_path(DATA_FILES['catalog'])
    content = open(filename, 'r').read()
    return MockResponse(content)

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

# Test Case: A Seyfert 1 galaxy
coords = coordinates.ICRS('0h8m05.63s +14d50m23.3s')

ALFALFA = alfalfa.core.Alfalfa()

def test_alfalfa_catalog(patch_get, patch_get_readable_fileobj, coords=coords):
    cat = ALFALFA.get_catalog()
{'fraction_non_alphanumeric': 0.10657662947739284, 'fraction_numerical': 0.010275983558426306, 'mean_word_length': 4.249614791987673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5886983', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1112, 'n_words': 256}