-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
增加<数据源增改查部分>单元测试 #1224
Closed
Canway-shiisa
wants to merge
3
commits into
TencentBlueKing:ft_tenant
from
Canway-shiisa:ft_tenant_test
Closed
增加<数据源增改查部分>单元测试 #1224
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/bk-user/tests/apis/web/data_source_organization/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" |
377 changes: 377 additions & 0 deletions
377
src/bk-user/tests/apis/web/data_source_organization/test_data_source_organization.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
import pytest | ||
from bkuser.apps.data_source.constants import DataSourcePluginEnum | ||
from bkuser.apps.data_source.models import DataSource | ||
|
||
from tests.test_utils.helpers import generate_random_string | ||
|
||
|
||
@pytest.fixture() | ||
def local_data_source(default_tenant: str) -> DataSource: | ||
""" | ||
生成测试数据源 | ||
""" | ||
|
||
local_data_source, _ = DataSource.objects.get_or_create( | ||
name=generate_random_string(), | ||
defaults={"owner_tenant_id": default_tenant, "plugin_id": DataSourcePluginEnum.LOCAL.value}, | ||
) | ||
return local_data_source |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. | ||
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
import random | ||
from typing import List | ||
|
||
import pytest | ||
from bkuser.apps.data_source.models import ( | ||
DataSourceDepartment, | ||
DataSourceDepartmentUserRelation, | ||
DataSourceUser, | ||
DataSourceUserLeaderRelation, | ||
) | ||
from bkuser.biz.data_source_organization import ( | ||
DataSourceOrganizationHandler, | ||
DataSourceUserBaseInfo, | ||
DataSourceUserDepartmentInfo, | ||
DataSourceUserEditableBaseInfo, | ||
DataSourceUserLeaderInfo, | ||
DataSourceUserRelationInfo, | ||
) | ||
from django.db import transaction | ||
|
||
from tests.test_utils.data_source_organization import ( | ||
create_data_source_departments, | ||
create_data_source_users, | ||
generate_data_source_username, | ||
) | ||
from tests.test_utils.helpers import generate_random_string | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
@pytest.fixture() | ||
def base_user_info() -> DataSourceUserBaseInfo: | ||
return DataSourceUserBaseInfo( | ||
username=generate_data_source_username(), | ||
full_name=generate_random_string(), | ||
email="[email protected]", | ||
phone="13000000000", | ||
phone_country_code="86", | ||
) | ||
|
||
|
||
@pytest.fixture() | ||
def editable_base_user_info() -> DataSourceUserEditableBaseInfo: | ||
return DataSourceUserEditableBaseInfo( | ||
full_name=generate_random_string(), | ||
email="[email protected]", | ||
phone="13000000000", | ||
phone_country_code="86", | ||
logo="", | ||
) | ||
|
||
|
||
@pytest.fixture() | ||
def relation_info() -> DataSourceUserRelationInfo: | ||
return DataSourceUserRelationInfo(department_ids=[11, 22], leader_ids=[33, 44]) | ||
|
||
|
||
@pytest.fixture() | ||
def data_source_departments(local_data_source) -> List[DataSourceDepartment]: | ||
return create_data_source_departments(data_source=local_data_source) | ||
|
||
|
||
@pytest.fixture() | ||
def data_source_users(local_data_source, data_source_departments) -> List[DataSourceUser]: | ||
return create_data_source_users(data_source=local_data_source, departments=data_source_departments) | ||
|
||
|
||
class TestDataSourceOrganizationHandler: | ||
def test_create_user(self, local_data_source, base_user_info, relation_info): | ||
# 创建用户 | ||
with transaction.atomic(): | ||
user_id = DataSourceOrganizationHandler.create_user(local_data_source, base_user_info, relation_info) | ||
|
||
# 验证用户是否创建成功 | ||
user = DataSourceUser.objects.get(id=user_id) | ||
assert user.data_source == local_data_source | ||
assert user.username == base_user_info.username | ||
|
||
# 验证用户-部门关系是否创建成功 | ||
department_ids = relation_info.department_ids | ||
relations = DataSourceDepartmentUserRelation.objects.filter(user=user) | ||
assert set(relations.values_list("department_id", flat=True)) == set(department_ids) | ||
|
||
# 验证用户-上级关系是否创建成功 | ||
leader_ids = relation_info.leader_ids | ||
relations = DataSourceUserLeaderRelation.objects.filter(user=user) | ||
assert set(relations.values_list("leader_id", flat=True)) == set(leader_ids) | ||
|
||
def test_update_user_department_relations(self, data_source_users): | ||
# 创建用户 | ||
user = random.choice(data_source_users) | ||
|
||
# 创建用户-部门关系 | ||
department_ids = [1, 2, 3] | ||
relations = [ | ||
DataSourceDepartmentUserRelation(department_id=department_id, user=user) | ||
for department_id in department_ids | ||
] | ||
DataSourceDepartmentUserRelation.objects.bulk_create(relations) | ||
|
||
# 更新用户-部门关系 | ||
new_department_ids = [2, 3, 4] | ||
DataSourceOrganizationHandler.update_user_department_relations(user, new_department_ids) | ||
|
||
# 验证用户-部门关系是否更新成功 | ||
department_ids = DataSourceDepartmentUserRelation.objects.filter(user=user).values_list( | ||
"department_id", flat=True | ||
) | ||
assert set(department_ids) == set(new_department_ids) | ||
|
||
def test_update_user_leader_relations(self, data_source_users): | ||
user = random.choice(data_source_users) | ||
# 创建用户-上级关系 | ||
leader_ids = [1, 2, 3] | ||
relations = [DataSourceUserLeaderRelation(leader_id=leader_id, user=user) for leader_id in leader_ids] | ||
DataSourceUserLeaderRelation.objects.bulk_create(relations) | ||
|
||
# 更新用户-上级关系 | ||
new_leader_ids = [2, 3, 4] | ||
DataSourceOrganizationHandler.update_user_leader_relations(user, new_leader_ids) | ||
|
||
# 验证用户-上级关系是否更新成功 | ||
leaders = DataSourceUserLeaderRelation.objects.filter(user=user).values_list("leader_id", flat=True) | ||
assert set(leaders) == set(new_leader_ids) | ||
|
||
def test_update_user(self, data_source_users, editable_base_user_info, relation_info): | ||
user = random.choice(data_source_users) | ||
|
||
# 更新用户 | ||
with transaction.atomic(): | ||
DataSourceOrganizationHandler.update_user(user, editable_base_user_info, relation_info) | ||
|
||
# 验证用户基础信息是否更新成功 | ||
assert user.full_name == editable_base_user_info.full_name | ||
assert user.email == editable_base_user_info.email | ||
assert user.phone == editable_base_user_info.phone | ||
assert user.phone_country_code == editable_base_user_info.phone_country_code | ||
assert user.logo == editable_base_user_info.logo | ||
|
||
# 验证用户-部门关系是否更新成功 | ||
department_ids = relation_info.department_ids | ||
relations = DataSourceDepartmentUserRelation.objects.filter(user=user) | ||
assert set(relations.values_list("department_id", flat=True)) == set(department_ids) | ||
|
||
# 验证用户-上级关系是否更新成功 | ||
leader_ids = relation_info.leader_ids | ||
relations = DataSourceUserLeaderRelation.objects.filter(user=user) | ||
assert set(relations.values_list("leader_id", flat=True)) == set(leader_ids) | ||
|
||
def test_list_department_info_by_id(self, data_source_departments): | ||
# 获取部门信息 | ||
department_ids = [dept.id for dept in data_source_departments] | ||
department_infos = DataSourceOrganizationHandler.list_department_info_by_id(department_ids) | ||
|
||
# 验证部门信息是否正确 | ||
assert len(department_infos) == len(department_ids) | ||
for department_info in department_infos: | ||
assert department_info.id in department_ids | ||
assert department_info.name == DataSourceDepartment.objects.get(id=department_info.id).name | ||
|
||
def test_get_user_department_ids_map(self, data_source_users): | ||
# 获取用户-部门id映射 | ||
user_ids = [user.id for user in data_source_users] | ||
user_department_ids_map = DataSourceOrganizationHandler.get_user_department_ids_map(user_ids) | ||
|
||
# 验证用户-部门id映射是否正确 | ||
assert set(user_department_ids_map.keys()) == set(user_ids) | ||
for user_id, department_ids in user_department_ids_map.items(): | ||
assert set(department_ids) == set( | ||
DataSourceDepartmentUserRelation.objects.filter(user_id=user_id).values_list( | ||
"department_id", flat=True | ||
) | ||
) | ||
|
||
def test_get_user_departments_map_by_user_id(self, data_source_users): | ||
# 获取用户-所有归属部门信息 | ||
user_ids = [user.id for user in data_source_users] | ||
user_departments_map = DataSourceOrganizationHandler.get_user_departments_map_by_user_id(user_ids) | ||
|
||
# 验证用户-所有归属部门信息是否正确 | ||
assert set(user_departments_map.keys()) == set(user_ids) | ||
for user_id, department_infos in user_departments_map.items(): | ||
assert list(department_infos) == [ | ||
DataSourceUserDepartmentInfo(id=dept["department_id"], name=dept["department__name"]) | ||
for dept in DataSourceDepartmentUserRelation.objects.filter(user_id=user_id).values( | ||
"department_id", "department__name" | ||
) | ||
] | ||
|
||
def test_get_user_leader_ids_map(self, data_source_users): | ||
# 获取用户-所有上级id关系映射 | ||
# 由于首个用户为其余用户的上级,因此需要将该用户剔除 | ||
user_ids = [user.id for user in data_source_users][1:] | ||
user_leader_ids_map = DataSourceOrganizationHandler.get_user_leader_ids_map(user_ids) | ||
|
||
# 验证用户-所有上级id关系映射是否正确 | ||
assert set(user_leader_ids_map.keys()) == set(user_ids) | ||
for user_id, leader_ids in user_leader_ids_map.items(): | ||
assert set(leader_ids) == set( | ||
DataSourceUserLeaderRelation.objects.filter(user_id=user_id).values_list("leader_id", flat=True) | ||
) | ||
|
||
def test_get_user_leaders_map_by_user_id(self, data_source_users): | ||
# 获取用户-所有上级信息数据 | ||
users = data_source_users[1:] | ||
user_ids = [user.id for user in users] | ||
user_leaders_map = DataSourceOrganizationHandler.get_user_leaders_map_by_user_id(user_ids) | ||
# 验证用户-所有上级信息数据是否正确 | ||
assert set(user_leaders_map.keys()) == set(user_ids) | ||
|
||
for user_id, leader_infos in user_leaders_map.items(): | ||
assert list(leader_infos) == [ | ||
DataSourceUserLeaderInfo(id=leader["leader_id"], username=leader["leader__username"]) | ||
for leader in DataSourceUserLeaderRelation.objects.filter(user_id=user_id).values( | ||
"leader_id", "leader__username" | ||
) | ||
] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create_user里已经开启事务,这里开启的作用是?