<commit_before>import pytest
from graphapi.schema import schema
from opencivicdata.legislative.models import Bill
from opencivicdata.core.models import Person
from .utils import populate_db


@pytest.mark.django_db
def setup():
    populate_db()


@pytest.mark.django_db
def test_bill_by_id(django_assert_num_queries):
    with django_assert_num_queries(17):
        result = schema.execute(''' {
            bill(id:"ocd-bill/1") {
                title
                classification
                subject
                abstracts {
                    abstract
                }
                otherTitles {
                    title
                }
                otherIdentifiers {
                    identifier
                }
                actions {
                    description
                    organization {
                        name
                        classification
                    }
                    relatedEntities {
                        name
                        entityType
                        organization { name }
                        person { name }
                    }
                }
                sponsorships {
                    name
                    classification
                }
                documents {
                    note
                    links { url }
                }
                versions {
                    note
                    links { url }
                }
                relatedBills {
                    legislativeSession
                    identifier
                    relationType
                    relatedBill {
                        title
                    }
                }
                sources { url }
                votes {
                    edges {
                        node {
                            votes {
                                option
                                voterName
                                voter { id name }
                            }
                            counts {
                                option
                                value
                            }
                        }
                    }
                }
            }
        }''')

    assert result.errors is None
    assert result.data['bill']['title'] == 'Moose Freedom Act'
    assert result.data['bill']['classification'] == ['bill', 'constitutional amendment']
    assert result.data['bill']['subject'] == ['nature']
    assert len(result.data['bill']['abstracts']) == 2
    assert len(result.data['bill']['otherTitles']) == 3
    assert len(result.data['bill']['actions']) == 3
    assert result.data['bill']['actions'][0]['organization']['classification'] == 'lower'
    assert result.data['bill']['actions'][0]['relatedEntities'][0]['person']['name'] != ''
    assert result.data['bill']['actions'][0]['relatedEntities'][0]['organization'] is None
    assert len(result.data['bill']['sponsorships']) == 2
    assert len(result.data['bill']['documents'][0]['links']) == 1
    assert len(result.data['bill']['versions'][0]['links']) == 2
    assert len(result.data['bill']['sources']) == 3
    assert len(result.data['bill']['relatedBills']) == 1
    assert 'Alces' in result.data['bill']['relatedBills'][0]['relatedBill']['title']
    assert len(result.data['bill']['votes']['edges']) == 1

    for vote in result.data['bill']['votes']['edges'][0]['node']['votes']:
        if vote['voterName'] == '<NAME>':
            assert vote['voter']['name'] == '<NAME>'
            break
    else:
        assert False, 'never found amanda'


@pytest.mark.django_db
def test_bill_by_jurisdiction_id_session_identifier(django_assert_num_queries):
    with django_assert_num_queries(1):
        result = schema.execute(''' {
            bill(jurisdiction:"ocd-jurisdiction/country:us/state:ak/government",
                 session:"2018",
                 identifier:"HB 1") {
                title
            }
        }''')
        assert result.errors is None
        assert result.data['bill']['title'] == 'Moose Freedom Act'


@pytest.mark.django_db
def test_bill_openstates_url(django_assert_num_queries):
    with django_assert_num_queries(1):
        result = schema.execute(''' {
            bill(jurisdiction:"ocd-jurisdiction/country:us/state:ak/government",
                 session:"2018",
                 identifier:"HB 1") {
            openstatesUrl
            }
        }''')
        assert result.errors is None
        assert result.data['bill']['openstatesUrl'] == 'https://openstates.org/ak/bills/2018/HB1'


@pytest.mark.django_db
def test_bill_by_jurisdiction_name_session_identifier(django_assert_num_queries):
    with django_assert_num_queries(1):
        result = schema.execute(''' {
            bill(jurisdiction:"Alaska", session:"2018", identifier:"HB 1") {
                title
            }
        }''')
        assert result.errors is None
        assert result.data['bill']['title'] == 'Moose Freedom Act'


@pytest.mark.django_db
def test_bill_by_jurisdiction_session_identifier_incomplete():
    result = schema.execute(''' {
        bill(jurisdiction:"Alaska", identifier:"HB 1") {
            title
        }<commit_msg>fix query count on test<commit_after>import pytest
from graphapi.schema import schema
from opencivicdata.legislative.models import Bill
from opencivicdata.core.models import Person
from .utils import populate_db


@pytest.mark.django_db
def setup():
    populate_db()


@pytest.mark.django_db
def test_bill_by_id(django_assert_num_queries):
    with django_assert_num_queries(17):
        result = schema.execute(''' {
            bill(id:"ocd-bill/1") {
                title
                classification
                subject
                abstracts {
                    abstract
                }
                otherTitles {
                    title
                }
                otherIdentifiers {
                    identifier
                }
                actions {
                    description
                    organization {
                        name
                        classification
                    }
                    relatedEntities {
                        name
                        entityType
                        organization { name }
                        person { name }
                    }
                }
                sponsorships {
                    name
                    classification
                }
                documents {
                    note
                    links { url }
                }
                versions {
                    note
                    links { url }
                }
                relatedBills {
                    legislativeSession
                    identifier
                    relationType
                    relatedBill {
                        title
                    }
                }
                sources { url }
                votes {
                    edges {
                        node {
                            votes {
                                option
                                voterName
                                voter { id name }
                            }
                            counts {
                                option
                                value
                            }
                        }
                    }
                }
            }
        }''')

    assert result.errors is None
    assert result.data['bill']['title'] == 'Moose Freedom Act'
    assert result.data['bill']['classification'] == ['bill', 'constitutional amendment']
    assert result.data['bill']['subject'] == ['nature']
    assert len(result.data['bill']['abstracts']) == 2
    assert len(result.data['bill']['otherTitles']) == 3
    assert len(result.data['bill']['actions']) == 3
    assert result.data['bill']['actions'][0]['organization']['classification'] == 'lower'
    assert result.data['bill']['actions'][0]['relatedEntities'][0]['person']['name'] != ''
    assert result.data['bill']['actions'][0]['relatedEntities'][0]['organization'] is None
    assert len(result.data['bill']['sponsorships']) == 2
    assert len(result.data['bill']['documents'][0]['links']) == 1
    assert len(result.data['bill']['versions'][0]['links']) == 2
    assert len(result.data['bill']['sources']) == 3
    assert len(result.data['bill']['relatedBills']) == 1
    assert 'Alces' in result.data['bill']['relatedBills'][0]['relatedBill']['title']
    assert len(result.data['bill']['votes']['edges']) == 1

    for vote in result.data['bill']['votes']['edges'][0]['node']['votes']:
        if vote['voterName'] == '<NAME>':
            assert vote['voter']['name'] == '<NAME>'
            break
    else:
        assert False, 'never found amanda'


@pytest.mark.django_db
def test_bill_by_jurisdiction_id_session_identifier(django_assert_num_queries):
    with django_assert_num_queries(1):
        result = schema.execute(''' {
            bill(jurisdiction:"ocd-jurisdiction/country:us/state:ak/government",
                 session:"2018",
                 identifier:"HB 1") {
                title
            }
        }''')
        assert result.errors is None
        assert result.data['bill']['title'] == 'Moose Freedom Act'


@pytest.mark.django_db
def test_bill_openstates_url(django_assert_num_queries):
    with django_assert_num_queries(2):
        result = schema.execute(''' {
            bill(jurisdiction:"ocd-jurisdiction/country:us/state:ak/government",
                 session:"2018",
                 identifier:"HB 1") {
            openstatesUrl
            }
        }''')
        assert result.errors is None
        assert result.data['bill']['openstatesUrl'] == 'https://openstates.org/ak/bills/2018/HB1'


@pytest.mark.django_db
def test_bill_by_jurisdiction_name_session_identifier(django_assert_num_queries):
    with django_assert_num_queries(1):
        result = schema.execute(''' {
            bill(jurisdiction:"Alaska", session:"2018", identifier:"HB 1") {
                title
            }
        }''')
        assert result.errors is None
        assert result.data['bill']['title'] == 'Moose Freedom Act'


@pytest.mark.django_db
def test_bill_by_jurisdiction_session_identifier_incomplete():
    result = schema.execute(''' {
        bill(jurisdiction:"Alaska", identifier:"HB 1") {
            title
        }
{'fraction_non_alphanumeric': 0.11668255481410868, 'fraction_numerical': 0.008579599618684462, 'mean_word_length': 1.449451319168807, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '2995556', 'n_tokens_mistral': 2783, 'n_tokens_neox': 2519, 'n_words': 456}