예제 #1
0
def test_pull():
    base_test.reinit_table()
    # create mock user
    _create_mock_user(1)
    user_id = 1
    # set last_sync_time
    current_time = int(time.time())
    last_sync_time = current_time - 1000
    # generate new data[create, update, delete] since last_sync_time
    uuid1 = _get_uuid()
    db_diary.create_diary(user_id, uuid1, _get_title(), _get_content(),
                          current_time)

    uuid2 = _get_uuid()
    db_diary.create_diary(user_id, uuid2, _get_title(), _get_content(),
                          current_time)
    db_diary.update_diary(user_id, uuid2, 'updated', 'updated',
                          current_time + 100)

    uuid3 = _get_uuid()
    db_diary.create_diary(user_id, uuid3, _get_title(), _get_content(),
                          current_time)
    db_diary.delete_diary(user_id, uuid3, current_time + 150)
    # assert uuid1 created, uuid2 created, uuid3 unknown
    changed_diary_list = db_diary.get_diary_list_since_last_sync(
        user_id, last_sync_time)
    assert len(changed_diary_list) == 3
예제 #2
0
def test_delete():
    base_test.reinit_table()
    _create_mock_user(3)
    uuid1 = _get_uuid()
    uuid2 = _get_uuid()
    uuid3 = _get_uuid()
    diary_id_1 = db_diary.create_diary(1, uuid1, _get_title(), _get_content())
    diary_id_2 = db_diary.create_diary(2, uuid2, _get_title(), _get_content())
    diary_id_3 = db_diary.create_diary(3, uuid3, _get_title(), _get_content())

    assert db_diary.get_diary_by_id(diary_id_1) is not None
    assert db_diary.get_diary_by_uuid(uuid1, 1) is not None
    assert db_diary.get_diary_by_id(diary_id_2) is not None
    assert db_diary.get_diary_by_uuid(uuid2, 2) is not None
    assert db_diary.get_diary_by_id(diary_id_3) is not None
    assert db_diary.get_diary_by_uuid(uuid3, 3) is not None

    db_diary.delete_diary(1, uuid1)
    assert db_diary.get_diary_by_id(diary_id_1) is None
    assert db_diary.get_diary_by_uuid(uuid1, 1) is None
    db_diary.delete_diary(2, uuid2)
    assert db_diary.get_diary_by_id(diary_id_1) is None
    assert db_diary.get_diary_by_uuid(uuid2, 2) is None
    db_diary.delete_diary(3, uuid3)
    assert db_diary.get_diary_by_id(diary_id_3) is None
    assert db_diary.get_diary_by_uuid(uuid3, 3) is None
예제 #3
0
def test_sync():
    base_test.reinit_table()
    # create mock user
    _create_mock_user(1)
    user_id = 1
    current_time = int(time.time())
    # user 1 create two diary long long ago
    uuid_1 = _get_uuid()
    uuid_2 = _get_uuid()
    db_diary.create_diary(user_id, uuid_1, "first", "first",
                          current_time - 10000)
    db_diary.create_diary(user_id, uuid_2, "second", "second",
                          current_time - 9000)
    assert db_diary.get_diary_by_uuid(uuid_1, user_id) is not None
    assert db_diary.get_diary_by_uuid(uuid_2, user_id) is not None

    # until now, he update first diary, delete second diary, created one new diary. he starts sync
    sync_token = None
    uuid_3 = "2F69DEB5-B631-40DD-A65E-AFE9A0882275"
    sync_items = [{
        'Diary': {
            'update': {
                'uuid': uuid_1,
                'time': current_time - 2000,
                'title': 'I update first diary',
                'content': 'I update first diary'
            }
        }
    }, {
        'Diary': {
            'delete': {
                'uuid': uuid_2,
                'time': current_time - 1000,
            }
        }
    }, {
        'Diary': {
            'create': {
                'uuid': uuid_3,
                'time': current_time - 3000,
                'title': 'this is third diary',
                'content': 'this is third diary',
            }
        }
    }]
    pull_result = logic_sync.sync_data(user_id, sync_token, sync_items, True)
    # if sync success, user 1 currently will only own first & third diary
    first_diary = db_diary.get_diary_by_uuid(uuid_1, user_id)
    assert first_diary is not None
    assert first_diary['title'] == 'I update first diary'
    assert first_diary['content'] == 'I update first diary'
    assert first_diary['time_modified'] == current_time - 2000
    second_diary = db_diary.get_diary_by_uuid(uuid_2, user_id)
    assert second_diary is None
    third_diary = db_diary.get_diary_by_uuid(uuid_3, user_id)
    assert third_diary is not None
    assert third_diary['time_created'] == current_time - 3000
    assert third_diary['time_modified'] == current_time - 3000
예제 #4
0
def test_token():
    base_test.reinit_table()
    email = "*****@*****.**"
    password = "******"
    user = logic_user.signup(email, password)
    assert user is not None
    login_result, login_user = logic_user.login(email, password)
    assert login_result
    assert login_user is not None
    auth_token = login_user["encrypted_token"]
    is_valid, user_id_from_token = logic_user.is_token_valid(auth_token)
    assert is_valid
    assert user_id_from_token == login_user["id"]
예제 #5
0
def test_create():
    base_test.reinit_table()
    _create_mock_user(3)
    for user_id in [1, 2, 3]:
        title = _get_title()
        content = _get_content()
        print 'create diary for user_id %s' % user_id
        db_diary.create_diary(user_id, _get_uuid(), title, content)

    for user_id in [1, 2, 3]:
        print 'user_id = %s' % user_id
        result = db_diary.get_diary_list_since_last_sync(user_id, 0)
        assert len(result) == 1
예제 #6
0
def test_token():
    base_test.reinit_table()
    email = '*****@*****.**'
    password = '******'
    user = logic_user.signup(email, password)
    assert user is not None
    login_result, login_user = logic_user.login(email, password)
    assert login_result
    assert login_user is not None
    auth_token = login_user['encrypted_token']
    is_valid, user_id_from_token = logic_user.is_token_valid(auth_token)
    assert is_valid
    assert user_id_from_token == login_user['id']
예제 #7
0
def test_signup_login():
    base_test.reinit_table()
    email = "*****@*****.**"
    password = "******"
    user = logic_user.signup(email, password)
    assert user is not None
    user_id = user["id"]
    user_from_db = logic_user.get_user_by_id(user_id)
    assert user_from_db is not None
    assert user_from_db["email"] == user["email"]
    login_result, login_user = logic_user.login(email, password)
    assert login_result
    assert login_user is not None
    assert login_user["email"] == user["email"]
예제 #8
0
def test_signup_login():
    base_test.reinit_table()
    email = '*****@*****.**'
    password = '******'
    user = logic_user.signup(email, password)
    assert user is not None
    user_id = user['id']
    user_from_db = logic_user.get_user_by_id(user_id)
    assert user_from_db is not None
    assert user_from_db['email'] == user['email']
    login_result, login_user = logic_user.login(email, password)
    assert login_result
    assert login_user is not None
    assert login_user['email'] == user['email']
예제 #9
0
def test_update():
    base_test.reinit_table()
    _create_mock_user(1)
    user_id = 1
    _uuid = _get_uuid()
    print 'uuid generated: ', _uuid
    db_diary.create_diary(user_id, _uuid, _get_title(), _get_content())

    result = db_diary.get_diary_by_uuid(_uuid, user_id)
    print result['uuid']
    print _uuid
    assert result['uuid'] == _uuid

    db_diary.update_diary(user_id, _uuid, 'updated title', 'updated content')
    result = db_diary.get_diary_by_uuid(_uuid, user_id)
    print result
    assert result['user_id'] == user_id
    assert result['uuid'] == _uuid
    assert result['title'] == 'updated title'
    assert result['content'] == 'updated content'
예제 #10
0
def test_push():
    base_test.reinit_table()
    # create mock user
    _create_mock_user(1)
    # create a diary
    data = {
        'title': 'first diary',
        'content': 'first diary',
        'time': 11111111,
    }
    _uuid = _get_uuid()
    user_id = 1

    assert db_diary.get_diary_by_uuid(_uuid, user_id) is None
    db_diary.upsert_diary(user_id, _uuid, data)
    diary = db_diary.get_diary_by_uuid(_uuid, user_id)
    assert diary is not None
    assert diary.get('time_created') == 11111111
    assert diary.get('time_modified') == 11111111

    # change this diary
    data = {
        'title': 'changed diary',
        'content': 'changed diary',
        'time': 2222222,
    }
    db_diary.upsert_diary(user_id, _uuid, data)
    diary = db_diary.get_diary_by_uuid(_uuid, user_id)
    assert diary is not None
    assert diary.get('title') == 'changed diary'
    assert diary.get('content') == 'changed diary'
    assert diary.get('time_created') == 11111111
    assert diary.get('time_modified') == 2222222

    # delete this diary
    time_delete = 333333333
    db_diary.delete_diary(user_id, _uuid, time_delete)
    diary = db_diary.get_diary_by_uuid(_uuid, user_id)
    assert diary is None