Esempio n. 1
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
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 4
0
def create_diary(user_id, uuid, title, content):
    """Create new diary with title & content for user_id.

    Return:
        new_diary_id
    """
    user = logic_user.get_user_by_id(user_id)
    if user is None:
        raise errors.UserNotFound()
    return db_diary.create_diary(user_id, uuid, title, content)
Esempio n. 5
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'
Esempio n. 6
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