示例#1
0
def test_get_prev_tweet_id():
    session = boto3.Session()
    client = session.client('dynamodb')
    test_table_name = 'bills_test'
    client.create_table(
        TableName=test_table_name,
        AttributeDefinitions=AttributeDefinitions,
        KeySchema=KeySchema,
        ProvisionedThroughput=ProvisionedThroughput,
        GlobalSecondaryIndexes=GlobalSecondaryIndexes,
    )
    print('Waiting for', test_table_name, '...')
    waiter = client.get_waiter('table_exists')
    waiter.wait(TableName=test_table_name)

    introductions = [
        Bill('1', 'text', ['tag'], 'ocd-id', '10/28/18', '789'),
        Bill('2', 'text', ['tag'], 'ocd-id', '10/28/18', '123'),
        Bill('3', 'text', ['tag'], 'ocd-id', '10/28/18', '10000'),
        Bill('4', 'text', ['tag'], 'ocd-id', '10/28/18', '1'),
        Bill('5', 'text', ['tag'], 'ocd-id', '10/29/18', '10000000')
    ]
    bills = Bills(boto3.Session(), test_table_name)
    for introduction in introductions:
        bills.insert(introduction)

    prev_tweet_id = bills.get_prev_tweet_id('10/29/18')
    assert prev_tweet_id == '10000000'
    prev_tweet_id = bills.get_prev_tweet_id('10/28/18')
    assert prev_tweet_id == '10000'
    client.delete_table(TableName=test_table_name)
    waiter = client.get_waiter('table_not_exists')
    waiter.wait(TableName=test_table_name)
示例#2
0
def test_no_tweet(bills_table):
    no_tweet_id_bills = [
        Bill(str(i), None, None, None, '10/25/18') for i in range(4)
    ]
    have_tweet_id_bills = [
        Bill(str(len(no_tweet_id_bills) + i), None, None, None, '10/25/18',
             str(i)) for i in range(4)
    ]
    bills = Bills(boto3.Session())
    combined_bills = no_tweet_id_bills + have_tweet_id_bills
    for bill in combined_bills:
        bills.insert(bill)
    assert sorted(bills.missing_tweet_id(),
                  key=lambda x: x.identifier) == no_tweet_id_bills
示例#3
0
def test_shorten_shortening_required():
    bot = TwitterBot(TwitterCredentials('', '', '', ''))
    bill_identifier = 'O2099-1111'
    tweet_with_shortened_url = tweet_after_url_shortening(
        Bill(bill_identifier, '', [], ''))
    allowed_chars_excluding_url = (TWITTER_MAX_CHARS -
                                   len(tweet_with_shortened_url))
    too_long_title = 'x' * (allowed_chars_excluding_url + 1)
    bill = Bill(bill_identifier, too_long_title, '', '')
    too_many_chars_tweet = tweet_after_url_shortening(bill)

    shortened_bill = bot.shorten(bill)

    assert len(too_many_chars_tweet) == TWITTER_MAX_CHARS + 1
    assert len(tweet_after_url_shortening(shortened_bill)) == TWITTER_MAX_CHARS
示例#4
0
 def parse_bills(bills_json):
     bills = []
     for bill in bills_json['results']:
         b = Bill(bill['identifier'], bill['title'].strip(),
                  bill['classification'], bill['id'])
         bills.append(b)
     return bills
示例#5
0
def test_get_prev_tweet_id_no_db_call(app):
    bill_date = '1/1/2018'
    bill = Bill('1', 'title', [], '1', bill_date)
    prev_tweets = {bill_date: '1000'}

    prev_tweet_id = get_prev_tweet_id(bill, app.bills, prev_tweets)

    assert app.bills.get_prev_tweet_id.call_count == 0
    assert prev_tweet_id == '1000'
示例#6
0
def test_shorten_shortening_not_required():
    bot = TwitterBot(TwitterCredentials('', '', '', ''))
    bill_identifier = 'O2099-1111'
    bill_title = 'title'
    bill = Bill(bill_identifier, bill_title, '', '')
    shortened_bill = bot.shorten(bill)

    assert len(tweet_after_url_shortening(bill)) <= TWITTER_MAX_CHARS
    assert shortened_bill == bill
示例#7
0
def test_get_prev_tweet_id_db_call(app):
    bill_date = '1/1/2018'
    missing_bill_date = '2/1/2018'
    bill = Bill('1', 'title', [], '1', missing_bill_date)
    prev_tweets = {bill_date: '1000'}
    app.bills.get_prev_tweet_id.return_value = '500'

    prev_tweet_id = get_prev_tweet_id(bill, app.bills, prev_tweets)

    assert app.bills.get_prev_tweet_id.call_count == 1
    assert prev_tweet_id == '500'
示例#8
0
 def parse_bills(self, bills_json, person_id):
     bills = []
     for bill in bills_json['results']:
         if self.is_person_primary_sponsor(bill, person_id):
             b = Bill(
                 bill['identifier'],
                 bill['title'].strip(),
                 bill['classification'],
                 bill['id'],
             )
             bills.append(b)
     return bills
示例#9
0
def load_archive():
    """Load all bills from a CSV file"""
    app = setup(app_config=APP_CONFIG)
    log.setLevel(logging.INFO)
    with open(sys.argv[1], 'r') as f:
        reader = csv.DictReader(f)
        rows = [
            Bill(
                r['identifier'],
                r['title'].strip(),
                r['classification'],
                r['ocd_id'] or 'NA',
                r['date'],
            ) for r in reader
        ]

    log.info(f'loading all: {len(rows)} introductions')
    log.setLevel(logging.WARNING)
    save_introductions(app.bills, rows)
示例#10
0
def test_shorten_with_reply():
    # seems like @username should not count against 280 chars, based on twitter api docs
    # however when trying to update status starting with @username it was being counted
    # against total.
    bot = TwitterBot(TwitterCredentials('', '', '', ''))
    user_name = 'username'
    bill = Bill(
        'O2018-6573',
        'Restructuring of debt to approve settlement payment from original owner NHS Redevelopment Corp., '
        'and allow multiple property transfers, restructuring of City loans, affordability restrictions and '
        'project rehabilitation agreements with new owner, Villa Capital Partners LLC and Villa Capital '
        'Managers LLC', ['ordinance'],
        'ocd-bill/c08ea55e-4017-4dfa-bfca-604b0eba0e85', '2018-07-25', -1)
    shortened_bill = bot.shorten(bill, user_name)

    assert len(tweet_after_url_shortening(shortened_bill,
                                          user_name)) == TWITTER_MAX_CHARS
    assert tweet_after_url_shortening(shortened_bill, user_name) == \
           '@username Restructuring of debt to approve settlement payment from original owner NHS Redevelopment ' \
           'Corp., and allow multiple property transfers, restructuring of City loans, affordability restrictions and ' \
           'project rehabilitation agreements wi... O2018-6573 xxxxxxxxxxxxxxxxxxxxxxx'
示例#11
0
import pytest

from tweet.bills import Bill

EXPECTED_IDENTIFIER = 'O2018-6138'
EXPECTED_TITLE = 'Fifty-fifth amending agreement with SomerCor 504, Inc. regarding Small Business Improvement Fund ' \
                 'program increases within Jefferson Park, Lawrence/Pulaski and Lincoln Avenue areas'
EXPECTED_CLASSIFICATION = ['ordinance']
EXPECTED_OCD_ID = 'ocd-bill/fdff8130-549a-45ed-9517-01419fdbeb54'
EXPECTED_DATE = '2018-07-25'
EXPECTED_BILL = Bill(EXPECTED_IDENTIFIER, EXPECTED_TITLE, EXPECTED_CLASSIFICATION, EXPECTED_OCD_ID, EXPECTED_DATE, None)
EXAMPLE_INTRODUCTIONS = [
    Bill('O2099-1111', 'Make it illegal to put ketchup on hotdogs', ['ordinance'], 'ocd-bill/hash1', '10/28/18', '123'),
    Bill('O2098-1112', 'Dye the lake green everyday', ['ordinance'], 'ocd-bill/hash2', '10/27/18', '456'),
]


def pytest_addoption(parser):
    parser.addoption(
        "--runslow", action="store_true", default=False, help="run slow tests"
    )


def pytest_collection_modifyitems(config, items):
    if config.getoption("--runslow"):
        # --runslow given in cli: do not skip slow tests
        return
    skip_slow = pytest.mark.skip(reason="need --runslow option to run")
    for item in items:
        if "slow" in item.keywords:
            item.add_marker(skip_slow)