def test_problem_filter_with_tag(): cnt = 10 random_tag = [secrets.token_hex(4) for _ in range(5)] for t in random_tag: Tag.add(t, engine.Tag.Category.NORMAL_PROBLEM) for _ in range(cnt): selected_tags = random.sample(random_tag, 2) course = utils.course.lazy_add( normal_problem_tags=selected_tags, auto_insert_tags=True, ) utils.problem.lazy_add( tags=selected_tags, course=course, ) target_tag = [secrets.token_hex(6) for _ in range(3)] course = utils.course.lazy_add( normal_problem_tags=target_tag, auto_insert_tags=True, ) for _ in range(cnt): utils.problem.lazy_add( tags=target_tag, course=course, ) assert len(Problem.filter(tags=target_tag)) == cnt
def test_get_all_tag(forge_client: Callable[[str], FlaskClient]): tags = [random_tag_str() for _ in range(10)] for tag in tags: Tag.add(tag, engine.Tag.Category.NORMAL_PROBLEM) teacher = utils.user.Factory.teacher() client = forge_client(teacher.username) rv = client.get('/tag') assert rv.status_code == 200 rv_tags = rv.get_json()['data'] sys_tags = [t.value for t in Tag.engine.objects] assert sorted(rv_tags) == sorted(sys_tags)
def test_delete_tag(forge_client: Callable[[str], FlaskClient]): t = Tag.add(random_tag_str(), engine.Tag.Category.NORMAL_PROBLEM) user = utils.user.Factory.teacher() client = forge_client(user.username) rv = client.delete( '/tag', json={ 'tags': [str(t.pk)], 'category': engine.Tag.Category.NORMAL_PROBLEM }, ) assert rv.status_code == 200, rv.data assert not Tag.is_tag(t.pk, engine.Tag.Category.NORMAL_PROBLEM)
def test_cannot_delete_tag_used_by_attachment( forge_client: Callable[[str], FlaskClient]): tag = Tag.add(random_tag_str(), engine.Tag.Category.ATTACHMENT) user = utils.user.Factory.admin() # Create attachment with tag client = forge_client(user.username) rv = client.post( '/attachment', data={ 'filename': 'test', 'description': 'test', 'fileObj': (io.BytesIO(b'test'), 'test'), 'patchNote': '', 'tags': str(tag.pk), }, ) assert rv.status_code == 200, rv.data rv = client.delete( '/tag', json={ 'tags': [str(tag.pk)], 'category': engine.Tag.Category.ATTACHMENT }, ) assert rv.status_code == 400 rv_data = rv.get_json()['data'] fail = rv_data['fail'] assert isinstance(fail, list) and len(fail) == 1 assert 'used' in fail[0]['msg'] assert fail[0]['value'] == str(tag.pk)
def test_tag_used_courses_count(): t = Tag.add('tag', engine.Tag.Category.COURSE) assert t.used_count(engine.Tag.Category.COURSE) == 0 c = utils.course.lazy_add(tags=['tag']) assert t.used_count(engine.Tag.Category.COURSE) == 1 c.patch_tag([], ['tag'], engine.Tag.Category.COURSE) assert t.used_count(engine.Tag.Category.COURSE) == 0
def test_cannot_delete_tag_used_by_course(): tag = Tag.add(random_tag_str(), engine.Tag.Category.COURSE) course = utils.course.lazy_add( tags=[str(tag.pk)], auto_insert_tags=True, ) with pytest.raises( PermissionError, match=r'.*used.*', ): tag.delete(engine.Tag.Category.COURSE)
def test_add_tag(forge_client: Callable[[str], FlaskClient]): assert len(Tag.engine.objects) == 0 tag_str = random_tag_str() teacher = utils.user.Factory.teacher() client = forge_client(teacher.username) rv = client.post( '/tag', json={'tags': [tag_str]}, ) assert rv.status_code == 200, rv.get_json() assert len(Tag.engine.objects) == 1 assert Tag(tag_str)
def test_tag_delete(): t = Tag.add('tag', engine.Tag.Category.NORMAL_PROBLEM) assert len(Tag.engine.objects(value='tag')) == 1 t.delete(engine.Tag.Category.NORMAL_PROBLEM) assert len(Tag.engine.objects(value='tag')) == 0
def test_tag_add(): assert len(Tag.engine.objects(value='tag')) == 0 t = Tag.add('tag', engine.Tag.Category.NORMAL_PROBLEM) assert Tag.is_tag('tag', engine.Tag.Category.NORMAL_PROBLEM) assert len(Tag.engine.objects(value='tag')) == 1, Tag.engine( value='tag', categories=engine.Tag.Category.NORMAL_PROBLEM).categories