Пример #1
0
def test_load_status_updates(monkeypatch):
    '''
    Test each aspect of load_status_updates
    '''
    def bad_file(self):  # pylint: disable=W0613
        '''
        Simulates an empty file for testing
        '''
        return iter([])  # Empty file

    header = 'header to be eaten'
    bad_data = [
        ('too', 'many', 'params', 'test'),  # Too many parameters
        ('not_enough', 'params'),  # Not enough parameters
        ('', 'one', 'message'),  # Empty status_id
        ('missing2', '', 'message'),  # Empty user_id
        ('missing3', 'three', '')
    ]  # Empty message
    assert main.load_status_updates(
        'status_updates.csv')  # Test sucessful inserts
    assert not main.load_status_updates(
        'status_updates')  # Refuse non csv files
    monkeypatch.setattr(csv, 'reader', bad_file)
    assert not main.load_status_updates(
        'status_updates.csv')  # Refuse empty files
    for test in bad_data:
        status_data = [header] + [test]
        csv.reader = Mock(return_value=iter(status_data))
        assert not main.load_status_updates(
            'status_updates.csv')  # Refuse to use bad data
Пример #2
0
def test_load_status_updates_false(status_collection, status_database):
    filename = 'missing_fields_status.csv'
    status_collection.database = status_database

    result = main.load_status_updates(filename, status_collection)

    assert result is False
Пример #3
0
def baseline():
    '''
    Method needed for memory profiling
    '''
    user_baseline = main.init_user_collection()
    status_baseline = main.init_status_collection()
    main.load_users('accounts.csv', user_baseline)
    main.load_status_updates('status_updates.csv', status_baseline)
    main.add_user('dave03', '*****@*****.**', 'Dave', 'Jones', user_baseline)
    main.add_status('dave03', 'dave03_01', 'Random text', status_baseline)
    main.update_user('dave03', '*****@*****.**', 'Dave', 'Rex', user_baseline)
    main.update_status('dave03', 'dave03_01', 'Whos in charge now',
                       status_baseline)
    main.search_user('dave03', user_baseline)
    main.search_status('dave03_01', status_baseline)
    main.delete_status('dave03_01', status_baseline)
    main.delete_user('dave03', user_baseline)
Пример #4
0
def test_load_status_updates_not_exists(status_collection):
    filename = 'status_updates.csv'
    old_database = copy.copy(status_collection.database)

    result = main.load_status_updates(filename, status_collection)

    assert result is True
    assert status_collection.database != old_database
Пример #5
0
def load_status_updates():
    """
    Loads status updates from a file
    """
    filename = input('Enter filename for status file: ')
    if not main.load_status_updates(filename, status_collection):
        print("An error occurred while trying to load status updates")
    else:
        print("Load status updates was successful")
Пример #6
0
def test_load_status_updates_empty_file(status_file):
    '''
    Test that load_status_updates reports empty files
    '''
    with patch('main.user_status.UserStatusCollection') as mocker:
        csv.reader = Mock(return_value=iter([]))
        assert not main.load_status_updates(status_file, mocker())
        insert = mocker.return_value.table.insert_many
        insert.assert_not_called()
Пример #7
0
def test_load_status_updates_bad_file(status_file):
    '''
    Test that load_status_updates will only work with .csv files
    '''
    csv.reader = Mock(return_value=iter([]))
    with patch('main.user_status.UserStatusCollection') as mocker:
        insert = mocker.return_value.table.insert_many
        assert not main.load_status_updates(status_file[:-4], mocker())
        insert.assert_not_called()
Пример #8
0
def test_load_status_updates_empty_data(status_file):
    '''
    Test that load_status_updates will not transfer empty data
    '''
    bad_data = [('', 'one', 'message'), ('missing2', '', 'message'),
                ('missing3', 'three', '')]
    header = 'header to be eaten'
    with patch('main.user_status.UserStatusCollection') as mocker:
        insert = mocker.return_value.table.insert_many
        for test in bad_data:
            status_data = [header] + [test]
            csv.reader = Mock(return_value=iter(status_data))
            assert not main.load_status_updates(status_file, mocker())
        insert.assert_not_called()
Пример #9
0
def test_load_status_updates_true(status_collection, status_database):
    status_collection.database = status_database

    file = '''STATUS_ID,USER_ID,STATUS_TEXT
evmiles97_00001,evmiles97,"Code is finally compiling"
dave03_00001,dave03,"Sunny in Seattle this morning"
evmiles97_00002,evmiles97,"Perfect weather for a hike"
ted_00002,evmiles97,"Perfect weather for a hike"
ted_moop,ted,"Perfect weather for a hike"'''

    with patch('builtins.open', mock_open(read_data=file)) as mock_file:
        result = main.load_status_updates(mock_file, status_collection)

    assert result is True
Пример #10
0
def test_load_status_updates_param_check(status_file):
    '''
    Test that load_status_updates will only transfer data with three parameters
    '''
    bad_data = [('header to be eaten'),
                ('way', 'too', 'many', 'params', 'test'), ('one', 'message'),
                ('missing2', 'message'), ('missing3', 'three')]
    header = 'header to be eaten'
    with patch('main.user_status.UserStatusCollection') as mocker:
        insert = mocker.return_value.table.insert_many
        for test in bad_data:
            status_data = [header] + [test]
            csv.reader = Mock(return_value=iter(status_data))
            assert not main.load_status_updates(status_file, mocker())
        insert.assert_not_called()
Пример #11
0
def test_load_status_updates(status_file):
    '''
    Test that status data is tranfered from the CSV file to the database
    '''
    db_fields = ('status_id', 'user_id', 'status_text')
    # Last item must have be a repeaded user_id
    status_data = [('header to be eaten'),
                   ('evmiles97_00001', 'evmiles97', 'Compiling'),
                   ('dave03_00001', 'dave03', 'Sunny in Seattle'),
                   ('evmiles97_00002', 'evmiles97', 'Hike'),
                   ('dave03_00001', 'dave03', 'Sunny in Seattle')]
    csv.reader = Mock(return_value=iter(status_data))
    with patch('main.user_status.UserStatusCollection') as mocker:
        insert = mocker.return_value.table.insert_many
        assert main.load_status_updates(status_file, mocker())
        insert.assert_called_once_with(status_data[1:-1], fields=db_fields)
Пример #12
0
def test_load_status_updates(status_file, status_keys):
    '''
    Test that status data is tranfered from the CSV file to the database
    '''
    # Last item must have be a repeaded user_id
    status_data = [('header to be eaten'),
                   ('evmiles97_00001', 'evmiles97', 'Compiling'),
                   ('dave03_00001', 'dave03', 'Sunny in Seattle'),
                   ('evmiles97_00002', 'evmiles97', 'Hike'),
                   ('dave03_00001', 'dave03', 'Sunny in Seattle')]
    call_data = []
    for data in status_data[1:-1]:
        call_data.append(dict(zip(status_keys, data)))
    csv.reader = Mock(return_value=iter(status_data))
    with patch('main.user_status.UserStatusCollection') as mocker:
        assert main.load_status_updates(status_file, mocker())
        insert = mocker.return_value.table.insert_many
        insert.assert_called_once_with(call_data)
Пример #13
0
 def test_load_status_updates(self):
     self.assertEqual(
         main.load_status_updates('status_updates.csv',
                                  self.status_collection), True)
Пример #14
0
def load_status_updates():
    '''
    Loads status updates from a file
    '''
    filename = input('Enter filename for status file: ').strip()
    main.load_status_updates(filename, status_collection)
Пример #15
0
'''
Simple test for loading data into database
'''
from multiprocessing import Process, Queue
import main

def fun(queue):
    '''
    Example demenstrating queues
    '''
    queue.put([42, None, 'hello'])

if __name__ == '__main__':
    que = Queue()
    pro = Process(target=fun, args=(que,))
    pro.start()
    print(que.get())    # prints "[42, None, 'hello']"
    pro.join()
    # test main now
    accounts = main.init_user_collection()
    statuses = main.init_status_collection()
    accounts.table.drop()
    statuses.table.drop()
    print(main.load_users('accounts.csv', accounts))
    print(main.load_status_updates('status_updates.csv', statuses))
Пример #16
0
'''
Import into ipython for easier testing
'''
import peewee as pw  # pylint: disable = W0611
import main

user = main.init_user_collection()
status = main.init_status_collection()
main.load_users('accounts.csv', user)
main.load_status_updates('status_updates.csv', status)
Пример #17
0
def load_status_updates():
    '''
    Loads status updates from a file
    '''
    filename = input('Enter filename for status file: ')
    main.load_status_updates(filename)
Пример #18
0
def test_load_status_updates():
    '''
    Test each aspect of load_status_updates
    '''
    assert main.load_status_updates('status_updates.csv')
Пример #19
0
 def test_save_status_updates(self):
     main.load_status_updates('status_updates.csv', self.status_collection)
     self.assertEqual(
         main.save_status_updates('status_updates_saved.csv',
                                  self.status_collection), True)