Ejemplo n.º 1
0
def test_merging_deletion_to_modification_or_creation(fake_datetime,
                                                      tag_factory,
                                                      user_factory,
                                                      initial_operation):
    tag = tag_factory(names=['dummy'], category_name='dummy')
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        initial_operation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('14:00:01'):
        snapshots.save_entity_deletion(tag, user)
    assert db.session.query(db.Snapshot).count() == 2
    results = db.session \
        .query(db.Snapshot) \
        .order_by(db.Snapshot.snapshot_id.asc()) \
        .all()
    assert results[1].operation == db.Snapshot.OPERATION_DELETED
    assert results[1].data == {
        'names': ['changed again'],
        'category': 'dummy',
        'suggestions': [],
        'implications': [],
    }
Ejemplo n.º 2
0
def test_cascade_deletions(tag_factory):
    sug1 = tag_factory(names=['sug1'])
    sug2 = tag_factory(names=['sug2'])
    imp1 = tag_factory(names=['imp1'])
    imp2 = tag_factory(names=['imp2'])
    tag = db.Tag()
    tag.names = [db.TagName('alias1'), db.TagName('alias2')]
    tag.suggestions = []
    tag.implications = []
    tag.category = db.TagCategory('category')
    tag.creation_time = datetime(1997, 1, 1)
    tag.last_edit_time = datetime(1998, 1, 1)
    tag.post_count = 1
    db.session.add_all([tag, sug1, sug2, imp1, imp2])
    db.session.commit()

    assert tag.tag_id is not None
    assert sug1.tag_id is not None
    assert sug2.tag_id is not None
    assert imp1.tag_id is not None
    assert imp2.tag_id is not None
    tag.suggestions.append(sug1)
    tag.suggestions.append(sug2)
    tag.implications.append(imp1)
    tag.implications.append(imp2)
    db.session.commit()

    db.session.delete(tag)
    db.session.commit()
    assert db.session.query(db.Tag).count() == 4
    assert db.session.query(db.TagName).count() == 4
    assert db.session.query(db.TagImplication).count() == 0
    assert db.session.query(db.TagSuggestion).count() == 0
Ejemplo n.º 3
0
def test_merging_deletion_all_the_way_deletes_all_snapshots(
        fake_datetime, tag_factory, user_factory, expected_operation):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:00:01'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:00:02'):
        snapshots.save_entity_deletion(tag, user)
    assert db.session.query(db.Snapshot).count() == 0
Ejemplo n.º 4
0
def update_tag_names(tag, names):
    names = util.icase_unique([name for name in names if name])
    if not len(names):
        raise InvalidTagNameError('At least one name must be specified.')
    for name in names:
        _verify_name_validity(name)
    expr = sqlalchemy.sql.false()
    for name in names:
        if util.value_exceeds_column_size(name, db.TagName.name):
            raise InvalidTagNameError('Name is too long.')
        expr = expr | db.TagName.name.ilike(name)
    if tag.tag_id:
        expr = expr & (db.TagName.tag_id != tag.tag_id)
    existing_tags = db.session.query(db.TagName).filter(expr).all()
    if len(existing_tags):
        raise TagAlreadyExistsError(
            'One of names is already used by another tag.')
    tag_names_to_remove = []
    for tag_name in tag.names:
        if not _check_name_intersection([tag_name.name], names):
            tag_names_to_remove.append(tag_name)
    for tag_name in tag_names_to_remove:
        tag.names.remove(tag_name)
    for name in names:
        if not _check_name_intersection(_get_plain_names(tag), [name]):
            tag.names.append(db.TagName(name))
Ejemplo n.º 5
0
def test_merging_resets_merging_time_window(fake_datetime, tag_factory,
                                            user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:09:59'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:19:59'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].data['names'] == ['changed again']
Ejemplo n.º 6
0
 def factory(names=None, category=None, category_name='dummy'):
     if not category:
         category = db.TagCategory(category_name)
         db.session.add(category)
     tag = db.Tag()
     tag.names = [db.TagName(name) for name in (names or [get_unique_name()])]
     tag.category = category
     tag.creation_time = datetime.datetime(1996, 1, 1)
     return tag
Ejemplo n.º 7
0
def test_merging_modifications(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('14:00:01'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 2
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[1].operation == db.Snapshot.OPERATION_MODIFIED
    assert results[0].data['names'] == ['dummy']
    assert results[1].data['names'] == ['changed again']
Ejemplo n.º 8
0
def test_not_merging_operations_by_different_users(fake_datetime, tag_factory,
                                                   user_factory):
    tag = tag_factory(names=['dummy'])
    user1, user2 = [user_factory(), user_factory()]
    db.session.add_all([tag, user1, user2])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user1)
        tag.names = [db.TagName('changed')]
        snapshots.save_entity_modification(tag, user2)
    assert db.session.query(db.Snapshot).count() == 2
Ejemplo n.º 9
0
def test_not_merging_due_to_time_difference(fake_datetime, tag_factory,
                                            user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert db.session.query(db.Snapshot).count() == 2
Ejemplo n.º 10
0
def test_merging_modification_to_creation(tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[0].data['names'] == ['changed']
Ejemplo n.º 11
0
def test_saving_tag(tag_factory):
    sug1 = tag_factory(names=['sug1'])
    sug2 = tag_factory(names=['sug2'])
    imp1 = tag_factory(names=['imp1'])
    imp2 = tag_factory(names=['imp2'])
    tag = db.Tag()
    tag.names = [db.TagName('alias1'), db.TagName('alias2')]
    tag.suggestions = []
    tag.implications = []
    tag.category = db.TagCategory('category')
    tag.creation_time = datetime(1997, 1, 1)
    tag.last_edit_time = datetime(1998, 1, 1)
    db.session.add_all([tag, sug1, sug2, imp1, imp2])
    db.session.commit()

    assert tag.tag_id is not None
    assert sug1.tag_id is not None
    assert sug2.tag_id is not None
    assert imp1.tag_id is not None
    assert imp2.tag_id is not None
    tag.suggestions.append(sug1)
    tag.suggestions.append(sug2)
    tag.implications.append(imp1)
    tag.implications.append(imp2)
    db.session.commit()

    tag = db.session \
        .query(db.Tag) \
        .join(db.TagName) \
        .filter(db.TagName.name=='alias1') \
        .one()
    assert [tag_name.name for tag_name in tag.names] == ['alias1', 'alias2']
    assert tag.category.name == 'category'
    assert tag.creation_time == datetime(1997, 1, 1)
    assert tag.last_edit_time == datetime(1998, 1, 1)
    assert [relation.names[0].name for relation in tag.suggestions] \
        == ['sug1', 'sug2']
    assert [relation.names[0].name for relation in tag.implications] \
        == ['imp1', 'imp2']
Ejemplo n.º 12
0
def test_get_serialized_history(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory(name='the-user')
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('2016-04-19 13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    db.session.flush()
    with fake_datetime('2016-04-19 13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert snapshots.get_serialized_history(tag) == [
        {
            'operation': 'modified',
            'time': datetime.datetime(2016, 4, 19, 13, 10, 1),
            'type': 'tag',
            'id': 'changed',
            'user': '******',
            'data': {
                'names': ['changed'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
        },
        {
            'operation': 'created',
            'time': datetime.datetime(2016, 4, 19, 13, 0, 0),
            'type': 'tag',
            'id': 'dummy',
            'user': '******',
            'data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': None,
        },
    ]