示例#1
0
def test_get_user():
    '''Tests if a user's score persists & is retrievable'''

    print '\nTesting get user- ',
    # reset data on redis database for testing
    clear_data()
    sample_data = [
        {
            "name": "abcd",
            "text": "abcdeedcba"
        },
        {
            "name": "abcd",
            "text": "abcdeedcba"
        },
        {
            "name": "efgh",
            "text": "abcddcba"
        },
        {
            "name": "ijkl",
            "text": "abcdeffedcba"
        },
        {
            "name": "mnop",
            "text": "abba"
        },
        {
            "name": "qrst",
            "text": "123456789987654321"
        },
        {
            "name": "uvwx",
            "text": "a"
        },
    ]
    user_names = {item['name'] for item in sample_data}
    expected_get_user = {}
    for item in sample_data:
        expected_get_user[item["name"]] = expected_get_user.get(
            item["name"], 0) + get_score(item["text"])
    #print 'expected_get_user- ',expected_get_user

    # submit data
    for item in sample_data:
        client.play(item)

    # retrieve user data
    recieved_get_user = {
        user: float(client.get_user(user)['data']['score'])
        for user in user_names
    }
    #print 'recieved_get_user- ',recieved_get_user
    if recieved_get_user == expected_get_user:
        print 'PASSED!'
    else:
        print 'FAILED!'
示例#2
0
def test_halloffame():
    '''Tests if halloffame correctly returns top5 users'''
    print '\nTesting halloffame- ',
    # reset data on redis database for testing
    clear_data()
    sample_data = [
        {
            "name": "abcd",
            "text": "abcdeedcba"
        },
        {
            "name": "efgh",
            "text": "abcddcba"
        },
        {
            "name": "ijkl",
            "text": "abcdeffedcba"
        },
        {
            "name": "mnop",
            "text": "abba"
        },
        {
            "name": "qrst",
            "text": "123456789987654321"
        },
        {
            "name": "uvwx",
            "text": "a"
        },
    ]
    user_names = [item['name'] for item in sample_data]
    scores_dict = {
        item["name"]: len(item["text"]) / 2.0
        for item in sample_data
    }
    expected_halloffame = sorted(user_names, key=scores_dict.get,
                                 reverse=True)[:5]

    #print 'scores_dict- ', scores_dict
    #print 'expected_halloffame- ', expected_halloffame

    # submit data
    for item in sample_data:
        client.play(item)

    # retrieve halloffame data
    recieved_halloffame = [
        item['name'] for item in client.hall_of_fame()['data']
    ]

    #print 'recieved_halloffame- ', recieved_halloffame
    if recieved_halloffame == expected_halloffame:
        print 'PASSED!'
    else:
        print 'FAILED!'
示例#3
0
def test_play():
    '''Tests if a user's score is incremented properly'''
    print '\nTesting play- ',
    # reset data on redis database for testing
    clear_data()

    sample_data = [
        {
            "name": "abcd",
            "text": "abcdeedcba"
        },
        {
            "name": "abcd",
            "text": "abcdeedcba"
        },
        {
            "name": "ijkl",
            "text": "abcdeffedcba"
        },
        {
            "name": "mnop",
            "text": "abba"
        },
        {
            "name": "qrst",
            "text": "123456789987654321"
        },
        {
            "name": "qrst",
            "text": "123456789987654321"
        },
    ]

    expected_scores_dict = {}
    for item in sample_data:
        expected_scores_dict[item["name"]] = expected_scores_dict.get(
            item["name"], 0) + get_score(item["text"])

    #print expected_scores_dict

    # submit data
    for item in sample_data:
        client.play(item)

    # retrieve halloffame data
    recieved_scores_dict = {
        item["name"]: client.get_user(item["name"])["data"]["score"]
        for item in sample_data
    }

    #print 'recieved_scores_dict- ', recieved_scores_dict
    if recieved_scores_dict == recieved_scores_dict:
        print 'PASSED!'
    else:
        print 'FAILED!'
示例#4
0
def test_get_all():
    '''Tests get all fnctionality'''

    print '\nTesting get all- ',
    # reset data on redis database for testing
    clear_data()
    sample_data = [
        {
            "name": "abcd",
            "text": "abcdeedcba"
        },
        {
            "name": "efgh",
            "text": "abcddcba"
        },
        {
            "name": "ijkl",
            "text": "abcdeffedcba"
        },
        {
            "name": "mnop",
            "text": "abba"
        },
        {
            "name": "qrst",
            "text": "123456789987654321"
        },
        {
            "name": "uvwx",
            "text": "a"
        },
    ]
    expected_get_all = {}
    for item in sample_data:
        expected_get_all[item["name"]] = expected_get_all.get(
            item["name"], 0) + get_score(item["text"])
    #print 'expected_get_all- ',expected_get_all

    # submit data
    for item in sample_data:
        client.play(item)

    # retrieve all user data
    recieved_get_all = {
        item['name']: float(item['score'])
        for item in client.get_all()['data']
    }
    #print 'recieved_get_all- ',recieved_get_all
    if recieved_get_all == expected_get_all:
        print 'PASSED!'
    else:
        print 'FAILED!'
示例#5
0
def play_tictactoe(host, port):
    sock = connect(host, port, 'tictactoe')
    return play(sock, get_move)
示例#6
0
def play_checkers(host, port):
    sock = connect(host, port, 'checkers')
    return play(sock, get_move)
示例#7
0
def play_connect_four(host, port):
    sock = connect(host, port, 'connectfour')
    return play(sock, get_move)