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
def create_tag(names: List[str], category_name: str, suggestions: List[str], implications: List[str]) -> model.Tag: tag = model.Tag() tag.creation_time = datetime.utcnow() update_tag_names(tag, names) update_tag_category_name(tag, category_name) update_tag_suggestions(tag, suggestions) update_tag_implications(tag, implications) return tag
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
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", ]
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']