Skip to content

Commit

Permalink
test: add DataSourceDepartmentSyncer unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux committed Sep 17, 2023
1 parent 4278011 commit e9fde69
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 51 deletions.
34 changes: 34 additions & 0 deletions src/bk-user/bkuser/apps/sync/migrations/0003_auto_20230917_2026.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.2.20 on 2023-09-17 12:26

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sync', '0002_auto_20230913_1626'),
]

operations = [
migrations.AlterField(
model_name='datasourcesynctask',
name='duration',
field=models.DurationField(default=datetime.timedelta(0), verbose_name='任务持续时间'),
),
migrations.AlterField(
model_name='datasourcesynctask',
name='start_time',
field=models.DateTimeField(auto_now_add=True, verbose_name='任务开始时间'),
),
migrations.AlterField(
model_name='tenantsynctask',
name='duration',
field=models.DurationField(default=datetime.timedelta(0), verbose_name='任务持续时间'),
),
migrations.AlterField(
model_name='tenantsynctask',
name='start_time',
field=models.DateTimeField(auto_now_add=True, verbose_name='任务开始时间'),
),
]
9 changes: 5 additions & 4 deletions src/bk-user/bkuser/apps/sync/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
specific language governing permissions and limitations under the License.
"""
import uuid
from datetime import timedelta

from django.db import models

Expand All @@ -31,8 +32,8 @@ class DataSourceSyncTask(TimestampedModel):
status = models.CharField("任务总状态", choices=SyncTaskStatus.get_choices(), max_length=32)
trigger = models.CharField("触发方式", choices=SyncTaskTrigger.get_choices(), max_length=32)
operator = models.CharField("操作人", null=True, blank=True, default="", max_length=128)
start_time = models.DateTimeField("任务开始时间")
duration = models.DurationField("任务持续时间")
start_time = models.DateTimeField("任务开始时间", auto_now_add=True)
duration = models.DurationField("任务持续时间", default=timedelta(seconds=0))
extra = models.JSONField("扩展信息", default=dict)

@property
Expand Down Expand Up @@ -91,8 +92,8 @@ class TenantSyncTask(TimestampedModel):
status = models.CharField("任务总状态", choices=SyncTaskStatus.get_choices(), max_length=32)
trigger = models.CharField("触发方式", choices=SyncTaskTrigger.get_choices(), max_length=32)
operator = models.CharField("操作人", null=True, blank=True, default="", max_length=128)
start_time = models.DateTimeField("任务开始时间")
duration = models.DurationField("任务持续时间")
start_time = models.DateTimeField("任务开始时间", auto_now_add=True)
duration = models.DurationField("任务持续时间", default=timedelta(seconds=0))
extra = models.JSONField("扩展信息", default=dict)

@property
Expand Down
13 changes: 12 additions & 1 deletion src/bk-user/bkuser/apps/sync/syncers.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,25 @@ def _sync_department_relations(self):
parent = dept_code_rel_map.get(parent_code)

dept_code_rel_map[node.id] = DataSourceDepartmentRelation(
data_source=self.data_source, department=dept_code_map[node.id], parent=parent
data_source=self.data_source,
department=dept_code_map[node.id],
parent=parent,
# FIXME (su) 不同树不可共用 ID,需要根据 root_idx, data_source_id 生成唯一的 tree_id
tree_id=self.data_source.id,
# NOTE:初始化时 lft, rght, level 均不能为空,因此先赋零值,后面 rebuild 会修改
lft=0,
rght=0,
level=0,
)

# 最后再全部批量创建
DataSourceDepartmentRelation.objects.bulk_create(
list(dept_code_rel_map.values()), batch_size=DATA_SOURCE_SYNC_BATCH_SIZE
)

# 对当前数据源的树进行重建
DataSourceDepartmentRelation.objects.partial_rebuild(self.data_source.id)


class DataSourceUserSyncer:
"""数据源用户同步器,支持覆盖更新,日志记录等"""
Expand Down
2 changes: 1 addition & 1 deletion src/bk-user/tests/apis/web/data_source/test_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def data_source(request, local_ds_plugin, local_ds_plugin_config):
# 支持检查是否使用 random_tenant fixture 以生成不属于默认租户的数据源
tenant_id = DEFAULT_TENANT
if "random_tenant" in request.fixturenames:
tenant_id = request.getfixturevalue("random_tenant")
tenant_id = request.getfixturevalue("random_tenant").id

return DataSource.objects.create(
name=generate_random_string(),
Expand Down
10 changes: 0 additions & 10 deletions src/bk-user/tests/apps/data_source/__init__.py

This file was deleted.

20 changes: 0 additions & 20 deletions src/bk-user/tests/apps/data_source/test_exporter.py

This file was deleted.

221 changes: 221 additions & 0 deletions src/bk-user/tests/apps/sync/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
# -*- 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.
"""
from typing import List

import pytest
from bkuser.apps.sync.constants import SyncTaskStatus, SyncTaskTrigger
from bkuser.apps.sync.models import DataSourceSyncTask, TenantSyncTask
from bkuser.plugins.models import RawDataSourceDepartment, RawDataSourceUser
from django.utils import timezone


@pytest.fixture()
def data_source_sync_task(bare_local_data_source) -> DataSourceSyncTask:
"""数据源同步任务"""
return DataSourceSyncTask.objects.create(
data_source_id=bare_local_data_source.id,
status=SyncTaskStatus.PENDING,
trigger=SyncTaskTrigger.MANUAL,
operator="admin",
start_time=timezone.now(),
extra={"overwrite": True, "async_run": False},
)


@pytest.fixture()
def tenant_sync_task(bare_local_data_source, default_tenant) -> TenantSyncTask:
"""租户数据同步任务"""
return TenantSyncTask(
tenant_id=default_tenant.id,
data_source_id=bare_local_data_source.id,
status=SyncTaskStatus.PENDING,
trigger=SyncTaskTrigger.MANUAL,
operator="admin",
start_time=timezone.now(),
extra={"async_run": False},
)


@pytest.fixture()
def raw_departments() -> List[RawDataSourceDepartment]:
"""数据源插件提供的原始部门信息"""
return [
RawDataSourceDepartment(code="company", name="公司", parent=None),
RawDataSourceDepartment(code="dept_a", name="部门A", parent="company"),
RawDataSourceDepartment(code="dept_b", name="部门B", parent="company"),
RawDataSourceDepartment(code="center_aa", name="中心AA", parent="dept_a"),
RawDataSourceDepartment(code="center_ab", name="中心AB", parent="dept_a"),
RawDataSourceDepartment(code="center_ba", name="中心BA", parent="dept_b"),
RawDataSourceDepartment(code="group_aaa", name="小组AAA", parent="center_aa"),
RawDataSourceDepartment(code="group_aba", name="小组ABA", parent="center_ab"),
RawDataSourceDepartment(code="group_baa", name="小组BAA", parent="center_ba"),
RawDataSourceDepartment(code="v", name="V", parent=None),
]


def raw_users() -> List[RawDataSourceUser]:
"""数据源插件提供的原始用户信息"""
return [
RawDataSourceUser(
code="Employee-3",
properties={
"username": "zhangsan",
"full_name": "张三",
"email": "[email protected]",
"phone": "13512345671",
"age": "18",
"gender": "male",
"region": "beijing",
},
leaders=[],
departments=["company"],
),
RawDataSourceUser(
code="Employee-4",
properties={
"username": "lisi",
"full_name": "李四",
"email": "[email protected]",
"phone": "13512345672",
"age": "28",
"gender": "female",
"region": "shanghai",
},
leaders=["zhangsan"],
departments=["dept_a", "center_aa"],
),
RawDataSourceUser(
code="Employee-5",
properties={
"username": "wangwu",
"full_name": "王五",
"email": "[email protected]",
"phone": "13512345673",
"age": "38",
"gender": "male",
"region": "shenzhen",
},
leaders=["zhangsan"],
departments=["dept_a", "dept_b"],
),
RawDataSourceUser(
code="Employee-6",
properties={
"username": "zhaoliu",
"full_name": "赵六",
"email": "[email protected]",
"phone": "13512345674",
"age": "33",
"gender": "female",
"region": "tianjin",
},
leaders=["lisi"],
departments=["center_aa"],
),
RawDataSourceUser(
code="Employee-7",
properties={
"username": "liuqi",
"full_name": "柳七",
"email": "[email protected]",
"phone": "13512345675",
"age": "25",
"gender": "female",
"region": "jiangxi",
},
leaders=["zhaoliu"],
departments=["group_aaa"],
),
RawDataSourceUser(
code="Employee-8",
properties={
"username": "maiba",
"full_name": "麦八",
"email": "[email protected]",
"phone": "13512345676",
"age": "35",
"gender": "male",
"region": "xinjiang",
},
leaders=["lisi", "wangwu"],
departments=["center_ab"],
),
RawDataSourceUser(
code="Employee-9",
properties={
"username": "yangjiu",
"full_name": "杨九",
"email": "[email protected]",
"phone": "13512345677",
"age": "40",
"gender": "male",
"region": "guangdong",
},
leaders=["wangwu"],
departments=["center_ab"],
),
RawDataSourceUser(
code="Employee-10",
properties={
"username": "lushi",
"full_name": "鲁十",
"email": "[email protected]",
"phone": "13512345678",
"age": "50",
"gender": "male",
"region": "jiangsu",
},
leaders=["maiba", "wangwu"],
departments=["group_aba", "center_ba"],
),
RawDataSourceUser(
code="Employee-11",
properties={
"username": "linshiyi",
"full_name": "林十一",
"email": "[email protected]",
"phone": "13512345679",
"age": "31",
"gender": "male",
"region": "hunan",
},
leaders=["lushi"],
departments=["group_aba"],
),
RawDataSourceUser(
code="Employee-12",
properties={
"username": "baishier",
"full_name": "白十二",
"email": "[email protected]",
"phone": "13512345670",
"age": "30",
"gender": "female",
"region": "guangdong",
},
leaders=["lushi"],
departments=["group_baa"],
),
RawDataSourceUser(
code="Employee-666",
properties={
"username": "freedom",
"full_name": "自由人",
"email": "[email protected]",
"phone": "1351234567X",
"age": "999",
"gender": "other",
"region": "solar system",
},
leaders=[],
departments=[],
),
]
Loading

0 comments on commit e9fde69

Please sign in to comment.