<commit_before>#!/usr/bin/python3

"""

APP: Trumpatron
DESC: Bot.py Unit Test
CREATION_DATE: 2017-03-01

"""

# MODULES
# | Native

# | Third-Party
import pytest
import argparse
import tweepy

# | Custom
import lib.trump
import trumpatron


class DummyArgParse:
    """
    A dummy object that mimics the object returned from ArgParse
    """

    log_file = ''
    log_level = ''
    config_file = ''
    num_clauses = 2
    assume_yes = False
    config_check = False
    test_run = False
    daemon_mode = False
    sleep_delay = 5
    random_sleep = False


class DummyArgParse_MISSING_PARAMS:
    """
    A dummy object that mimics the object returned from ArgParse. Purposely doesn't include random variables for testing
    """

    log_file = ''
    config_file = ''
    assume_yes = False
    config_check = False
    test_run = False
    daemon_mode = False
    random_sleep = False


# FIXTURES
@pytest.fixture()
def validCliParser():
    # create dummy argparse obj
    return DummyArgParse()


@pytest.fixture()
def invalidCliParser():
    # create a purposefully invalid dummy argparse obj
    DAP = DummyArgParse_MISSING_PARAMS()

    return DAP


@pytest.fixture()
def validCfgParams():
    # generate dummy config file data
    cfg = {
        'AUTH': {
            'consumerkey': '',
            'consumersecret': '',
            'accesstoken': '',
            'accesstokensecret': ''
        },
        'GENERAL': {
            'numclauses': 2,
            'sleepdelay': 5
        },
        'LOGGING': {
            'logfile': '/var/log/trumpatron/app_test.log',
            'debuglevel': 'INFO'
        }
    }

    return cfg


@pytest.fixture()
def roCfgParams(validCfgParams):
    # generate dummy config file data w/ RO twitter API creds
    cfg = {
        'AUTH': {
            'consumerkey': '<KEY>',
            'consumersecret': '<KEY>',
            'accesstoken': '<KEY>',
            'accesstokensecret': '<KEY>'
        },
        'GENERAL': {
            'numclauses': 2,
            'sleepdelay': 5
        },
        'LOGGING': {
            'logfile': '/var/log/trumpatron/app_test.log',
            'debuglevel': 'INFO'
        }
    }

    return cfg


def testCreateTwitterAPIConn(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    tBot.createTwitterAPIConn()

    assert tBot.apiConn != None


def testGetTweets_INVALID_TWITTER_API_CREDS(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    # get tweets from specified username
    # should cause a Tweepy exception
    with pytest.raises(tweepy.TweepError):
        tBot.getTweets('realdonaldtrump')


def testGetTweets_VALID_TWITTER_USERNAME(validCliParser, roCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, roCfgParams)

    # get tweets from specified username
    tBot.getTweets('realdonaldtrump')

    assert tBot.tweetSet != []


def testGetTweets_INVALID_TWITTER_USERNAME(validCliParser, roCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, roCfgParams)

    # get tweets from specified username
    # should cause a Tweepy exception
    with pytest.raises(tweepy.TweepError):
        tBot.getTweets('aiojwrrwtnnnnnaisdjfoiajsdif88892lnl132323')


def testSpliceTweets(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    # create test set of Status objs
    status1 = 'This is a test. Testing.'
    status2 = 'Test #2. Unit. Test.'
    testTweetSet = [status1, status2]
    tBot.tweetSet = testTweetSet

    # splice tweets
    tBot.spliceTweets()

    assert tBot.tweetClauses == ['This is a test', 'Testing.', 'Test #2', 'Unit', 'Test.']


def testFormatTweet_BLANK_TWEET(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    with pytest.raises(ValueError):
        tBot.formatTweet('')


def testFormatTweet_PLAIN_TWEET(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    # set tweet
    tweet = 'Test of normal tweet!'

    assert tweet == tBot.formatTweet(tweet)


def testFormatTweet_FORMATTED_TWEET(validCliParser, validCfgParams):<commit_msg>Add Unit Test for Trump.flushTweetData()<commit_after>#!/usr/bin/python3

"""

APP: Trumpatron
DESC: Bot.py Unit Test
CREATION_DATE: 2017-03-01

"""

# MODULES
# | Native

# | Third-Party
import pytest
import argparse
import tweepy

# | Custom
import lib.trump
import trumpatron


class DummyArgParse:
    """
    A dummy object that mimics the object returned from ArgParse
    """

    log_file = ''
    log_level = ''
    config_file = ''
    num_clauses = 2
    assume_yes = False
    config_check = False
    test_run = False
    daemon_mode = False
    sleep_delay = 5
    random_sleep = False


class DummyArgParse_MISSING_PARAMS:
    """
    A dummy object that mimics the object returned from ArgParse. Purposely doesn't include random variables for testing
    """

    log_file = ''
    config_file = ''
    assume_yes = False
    config_check = False
    test_run = False
    daemon_mode = False
    random_sleep = False


# FIXTURES
@pytest.fixture()
def validCliParser():
    # create dummy argparse obj
    return DummyArgParse()


@pytest.fixture()
def invalidCliParser():
    # create a purposefully invalid dummy argparse obj
    DAP = DummyArgParse_MISSING_PARAMS()

    return DAP


@pytest.fixture()
def validCfgParams():
    # generate dummy config file data
    cfg = {
        'AUTH': {
            'consumerkey': '',
            'consumersecret': '',
            'accesstoken': '',
            'accesstokensecret': ''
        },
        'GENERAL': {
            'numclauses': 2,
            'sleepdelay': 5
        },
        'LOGGING': {
            'logfile': '/var/log/trumpatron/app_test.log',
            'debuglevel': 'INFO'
        }
    }

    return cfg


@pytest.fixture()
def roCfgParams(validCfgParams):
    # generate dummy config file data w/ RO twitter API creds
    cfg = {
        'AUTH': {
            'consumerkey': '965vXNOAJBIQQK71ggCTGTyfU',
            'consumersecret': '<KEY>',
            'accesstoken': '<KEY>',
            'accesstokensecret': '<KEY>'
        },
        'GENERAL': {
            'numclauses': 2,
            'sleepdelay': 5
        },
        'LOGGING': {
            'logfile': '/var/log/trumpatron/app_test.log',
            'debuglevel': 'INFO'
        }
    }

    return cfg


def testCreateTwitterAPIConn(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    tBot.createTwitterAPIConn()

    assert tBot.apiConn != None


def testGetTweets_INVALID_TWITTER_API_CREDS(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    # get tweets from specified username
    # should cause a Tweepy exception
    with pytest.raises(tweepy.TweepError):
        tBot.getTweets('realdonaldtrump')


def testGetTweets_VALID_TWITTER_USERNAME(validCliParser, roCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, roCfgParams)

    # get tweets from specified username
    tBot.getTweets('realdonaldtrump')

    assert tBot.tweetSet != []


def testGetTweets_INVALID_TWITTER_USERNAME(validCliParser, roCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, roCfgParams)

    # get tweets from specified username
    # should cause a Tweepy exception
    with pytest.raises(tweepy.TweepError):
        tBot.getTweets('aiojwrrwtnnnnnaisdjfoiajsdif88892lnl132323')


def testFlushTweetData_SUCCESSFUL(validCliParser, roCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, roCfgParams)

    # set tweets, tweet clauses, and generated tweet
    # create test set of Status objs
    status1 = 'This is a test. Testing.'
    status2 = 'Test #2. Unit. Test.'
    testTweetSet = [status1, status2]
    tBot.tweetSet = testTweetSet
    tBot.tweetClauses = ['This is a test', 'Testing.', 'Test #2', 'Unit', 'Test.']
    tBot.generatedTweet = 'Test tweet!'

    # flush tweet data
    tBot.flushTweetData()

    assert tBot.tweetSet == []
    assert tBot.tweetClauses == []
    assert tBot.generatedTweet == ''


def testSpliceTweets(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    # create test set of Status objs
    status1 = 'This is a test. Testing.'
    status2 = 'Test #2. Unit. Test.'
    testTweetSet = [status1, status2]
    tBot.tweetSet = testTweetSet

    # splice tweets
    tBot.spliceTweets()

    assert tBot.tweetClauses == ['This is a test', 'Testing.', 'Test #2', 'Unit', 'Test.']


def testFormatTweet_BLANK_TWEET(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    with pytest.raises(ValueError):
        tBot.formatTweet('')


def testFormatTweet_PLAIN_TWEET(validCliParser, validCfgParams):
    # create Trump instance
    tBot = lib.trump.Trump(validCliParser, validCfgParams)

    # set tweet
    tweet = 'Test of normal tweet!'

    assert tweet == tBot.formatTweet(tweet)


def testFormatTweet_FORMATTED_TWEET(validCliParser, validCfgParams):
{'fraction_non_alphanumeric': 0.10059109140806417, 'fraction_numerical': 0.007916402786573781, 'mean_word_length': 2.903996703749485, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '7117068', 'n_tokens_mistral': 3260, 'n_tokens_neox': 3062, 'n_words': 706}