<commit_before># coding=utf-8

import pytest
from flask import Response, json
from flask.testing import FlaskClient

from skylines.model import User, Club, Airport


@pytest.fixture(autouse=True)
def fixtures(db_session):
    db_session.add(User(first_name=u'John', last_name=u'Doe', password='<PASSWORD>'))
    db_session.add(User(first_name=u'Jane', last_name=u'Doe', password='<PASSWORD>'))
    db_session.add(Club(name=u'LV Aachen', website='https://www.lv-aachen.de'))
    db_session.add(Club(name=u'Sportflug Niederberg'))
    db_session.add(Airport(name=u'Aachen-Merzbrück', country_code='DE', icao='EDKA', frequency='122.875'))
    db_session.add(Airport(name=u'Meiersberg', country_code='DE'))
    db_session.commit()


def test_search(client, default_headers):
    assert isinstance(client, FlaskClient)

    response = client.get('/search?q=aachen', headers=default_headers)
    assert isinstance(response, Response)
    assert response.status_code == 200

    data = json.loads(response.data)
    assert data == [{
        'type': 'airport',
        'id': 1,
        'name': u'Aachen-Merzbrück',
        'icao': 'EDKA',
        'frequency': '122.875',
    }, {
        'type': 'club',
        'id': 1,
        'name': u'LV Aachen',
        'website': 'https://www.lv-aachen.de',
    }]


def test_search2(client, default_headers):
    assert isinstance(client, FlaskClient)

    response = client.get('/search?q=doe', headers=default_headers)
    assert isinstance(response, Response)
    assert response.status_code == 200

    data = json.loads(response.data)
    assert data == [{
        'type': 'user',
        'id': 4,
        'name': u'<NAME>',
    }, {
        'type': 'user',
        'id': 3,
        'name': u'<NAME>',
    }]


def test_missing_search_text(client, default_headers):
    assert isinstance(client, FlaskClient)

    response = client.get('/search', headers=default_headers)
    assert isinstance(response, Response)
    assert response.status_code == 422

    data = json.loads(response.data)
    assert data == {'messages': {'q': ['Missing data for required field.']}}<commit_msg>tests/api/views/search: Fix broken assertions<commit_after># coding=utf-8

import pytest
from flask import Response, json
from flask.testing import FlaskClient

from skylines.model import User, Club, Airport


@pytest.fixture(autouse=True)
def fixtures(db_session):
    data = {
        'john': User(first_name=u'John', last_name=u'Doe', password='<PASSWORD>'),
        'jane': User(first_name=u'Jane', last_name=u'Doe', password='<PASSWORD>'),
        'lva': Club(name=u'LV Aachen', website='https://www.lv-aachen.de'),
        'sfn': Club(name=u'Sportflug Niederberg'),
        'edka': Airport(name=u'Aachen-Merzbrück', country_code='DE', icao='EDKA', frequency='122.875'),
        'mbg': Airport(name=u'Meiersberg', country_code='DE'),
    }

    for v in data.itervalues():
        db_session.add(v)

    db_session.commit()
    return data


def test_search(client, default_headers, fixtures):
    assert isinstance(client, FlaskClient)

    response = client.get('/search?q=aachen', headers=default_headers)
    assert isinstance(response, Response)
    assert response.status_code == 200

    data = json.loads(response.data)
    assert data == [{
        'type': 'airport',
        'id': fixtures['edka'].id,
        'name': u'Aachen-Merzbrück',
        'icao': 'EDKA',
        'frequency': '122.875',
    }, {
        'type': 'club',
        'id': fixtures['lva'].id,
        'name': u'<NAME>',
        'website': 'https://www.lv-aachen.de',
    }]


def test_search2(client, default_headers, fixtures):
    assert isinstance(client, FlaskClient)

    response = client.get('/search?q=doe', headers=default_headers)
    assert isinstance(response, Response)
    assert response.status_code == 200

    data = json.loads(response.data)
    assert data == [{
        'type': 'user',
        'id': fixtures['jane'].id,
        'name': u'<NAME>',
    }, {
        'type': 'user',
        'id': fixtures['john'].id,
        'name': u'<NAME>',
    }]


def test_missing_search_text(client, default_headers):
    assert isinstance(client, FlaskClient)

    response = client.get('/search', headers=default_headers)
    assert isinstance(response, Response)
    assert response.status_code == 422

    data = json.loads(response.data)
    assert data == {'messages': {'q': ['Missing data for required field.']}}
{'fraction_non_alphanumeric': 0.1598194130925508, 'fraction_numerical': 0.011286681715575621, 'mean_word_length': 3.5727554179566563, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, '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': '7034526', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1445, 'n_words': 287}