Ejemplo n.º 1
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 = model.Tag()
    tag.names = [model.TagName('alias1', 0), model.TagName('alias2', 1)]
    tag.suggestions = []
    tag.implications = []
    tag.category = model.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(model.Tag).count() == 4
    assert db.session.query(model.TagName).count() == 4
    assert db.session.query(model.TagImplication).count() == 0
    assert db.session.query(model.TagSuggestion).count() == 0
Ejemplo n.º 2
0
def update_tag_names(tag: model.Tag, names: List[str]) -> None:
    # sanitize
    assert tag
    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)

    # check for existing tags
    expr = sa.sql.false()
    for name in names:
        expr = expr | (sa.func.lower(model.TagName.name) == name.lower())
    if tag.tag_id:
        expr = expr & (model.TagName.tag_id != tag.tag_id)
    existing_tags = db.session.query(model.TagName).filter(expr).all()
    if len(existing_tags):
        raise TagAlreadyExistsError(
            "One of names is already used by another tag.")

    # remove unwanted items
    for tag_name in tag.names[:]:
        if not _check_name_intersection([tag_name.name], names, True):
            tag.names.remove(tag_name)
    # add wanted items
    for name in names:
        if not _check_name_intersection(_get_names(tag), [name], True):
            tag.names.append(model.TagName(name, -1))

    # set alias order to match the request
    for i, name in enumerate(names):
        for tag_name in tag.names:
            if tag_name.name.lower() == name.lower():
                tag_name.order = i
Ejemplo n.º 3
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 = model.Tag()
    tag.names = [model.TagName("alias1", 0), model.TagName("alias2", 1)]
    tag.suggestions = []
    tag.implications = []
    tag.category = model.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(model.Tag)
        .join(model.TagName)
        .filter(model.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.º 4
0
 def factory(names=None, category=None):
     if not category:
         category = model.TagCategory(get_unique_name())
         db.session.add(category)
     tag = model.Tag()
     tag.names = []
     for i, name in enumerate(names or [get_unique_name()]):
         tag.names.append(model.TagName(name, i))
     tag.category = category
     tag.creation_time = datetime(1996, 1, 1)
     return tag
Ejemplo n.º 5
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 = model.Tag()
    tag.names = [model.TagName('alias1', 0), model.TagName('alias2', 1)]
    tag.suggestions = []
    tag.implications = []
    tag.category = model.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(model.Tag)
        .join(model.TagName)
        .filter(model.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']