def test_add_bookmark_with_new_tag_and_existing_tag(self): t1 = Tag(id=uuid.uuid4().hex, tag='existing_1') db.session.add(t1) db.session.commit() data = {'url': 'google.com', 'tags': '#' + t1.tag + '#new2'} res = api_utils.post(url_for('library.add_ele'), data=data, headers={'Authentication-Token': self.token}) bookmark = BookMark.query.first() self.assertIsNotNone(bookmark) self.assertIsNotNone(bookmark.img) self.assertEqual(2, len(bookmark.tags)) tags = Tag.query.all() self.assertEqual(2, len(tags)) for tag in bookmark.tags: assert tag in tags self.assertEqual(bookmark, self.current_user.bookmarks[0]) result = api_utils.get(url_for('library.urls'), headers={'Authentication-Token': self.token}) content = api_utils.loads(result.data) self.assertEqual(2, len(content['tags'])) assert t1.as_dict() in content['tags'] assert Tag.query.filter_by( tag='new2').first().as_dict() in content['tags'] self.assertEqual('Google', content['bookmarks'][0]['name'])
def test_register_bookmark_and_tag(self): u1 = User() b1 = BookMark(url='google.com') t1 = Tag(tag='tag1') t2 = Tag(tag='tag2') contract.register_bookmark_and_tag(u1, b1, t1, t2) self.assertEqual(b1, u1.bookmarks[0]) self.assertEqual(2, len(u1.bookmarks[0].tags))
def test_search_thumbnail_by_tag(self): from bookMarkLibrary.database import db user = User.query.first() b1 = BookMark(url='http://localhost', name='test_thumbnail') b2 = BookMark(url='http://localhost2', name='test_thumbnail2') tag = Tag(tag='test_tag') b1.tags.append(tag) b2.tags.append(tag) user.bookmarks.extend([b1, b2]) db.session.add_all([user, tag]) db.session.commit() result = api_utils.get(url_for('library.urls', tag='test_tag'), headers={'Authentication-Token': self.token}) content = api_utils.loads(result.data.decode('utf-8')) self.assertEqual(1, len(content['tags'])) self.assertEqual([{ 'id': tag.id.__str__(), 'tag': tag.tag }], content['tags']) self.assertEqual([b1.as_dict(), b2.as_dict()], content['bookmarks'])
def add_ele(): code = const.NO_CONTENT try: bookmark = BookMark( url=get_http_format_url(request.json.get('url', ''))) tag_inputs = Tag.conv_tag_str_to_list(request.json.get('tags', '')) tags = [Tag.find_or_make(tag) for idx, tag in enumerate(tag_inputs)] bookmark.makeup() contract.register_bookmark_and_tag(current_user, bookmark, *tags) except InvalidURLException: code = const.SERVER_ERROR finally: return json.dumps({}), code, {'ContentType': 'application/json'}
def test_register_bookmark_and_tag_rollback_on_error(self): with patch('library.contract.db', wraps=db) as mock_db: # mock_db.session.rollback.return_value = None mock_db.session.commit.side_effect = IntegrityError u1 = User() b1 = BookMark(url='google.com') t = Tag(tag='tag2') t2 = Tag() contract.register_bookmark_and_tag(u1, b1, t, t2) mock_db.session.rollback.assert_called_once() self.assertIsNone(u1.bookmarks[0].id, u1.bookmarks[0].id) self.assertEqual(0, Tag.query.count()) self.assertEqual(0, BookMark.query.count())
def test_tag_bookmark_relation(self): u1 = User() b1 = BookMark(url='google.com') b2 = BookMark(url='python.org') u1.create_bookmarks(b1, b2) t1 = Tag(tag='tag1') t2 = Tag(tag='tag2') b1.tags.extend([t1, t2]) t2.bookmarks.extend([b1, b2]) db.session.add_all([u1, b1, b2, t1, t2]) db.session.commit() self.assertEqual(2, len(u1.bookmarks.all())) self.assertEqual(u1, b1.holder) self.assertEqual(u1, b1.holder) self.assertEqual(2, len(b1.tags)) self.assertEqual(2, len(t2.bookmarks)) self.assertEqual(1, len(b2.tags)) self.assertEqual(1, len(t1.bookmarks))
def urls(tag=None): book_marks = current_user.bookmarks ids = [bookmark.id for bookmark in book_marks] tags = Tag.get_lists(ids) if tag is not None: book_marks = book_marks.join(Tag.bookmarks).filter(Tag.tag == tag) book_marks = book_marks.order_by(BookMark.id).all() tag_json = [tag.as_dict() for tag in tags] bookmarks_json = [bookmark.as_dict() for bookmark in book_marks] return jsonify({'tags': tag_json, 'bookmarks': bookmarks_json})
def setup_tags(): """ Instatiate tags from TAG_LIST into database. Does not clear previous tags to prevent video tag reference loss. TODO: remove tags that are not listed in TAG_LIST """ print(f"[{script}]: Setting up tags...") if verbosity >= 1 else None # get current tags in database to avoid duplication tags_current = [x.name for x in Tag.objects.all()] for tag in TAG_LIST: if tag in tags_current: print( f'[{script}]: WARNING: "{tag}" already in database. Ignoring.' ) if verbosity >= 3 else None else: Tag(name=tag).save() print(f'[{script}]: Added "{tag}".') if verbosity >= 3 else None print(f"[{script}]: Tag creation complete.") if verbosity >= 2 else None