Skip to content

Commit

Permalink
Merge pull request #193
Browse files Browse the repository at this point in the history
feat: impl simple mock server
  • Loading branch information
lihuacai168 authored Mar 10, 2024
2 parents 1ece791 + 8878298 commit 089abfd
Show file tree
Hide file tree
Showing 30 changed files with 1,697 additions and 124 deletions.
8 changes: 8 additions & 0 deletions FasterRunner/customer_swagger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from drf_yasg.inspectors import SwaggerAutoSchema


class CustomSwaggerAutoSchema(SwaggerAutoSchema):
def get_tags(self, operation_keys=None):
if hasattr(self.view, 'swagger_tag'):
return [self.view.swagger_tag]
return super().get_tags(operation_keys)
6 changes: 6 additions & 0 deletions FasterRunner/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"drf_yasg",
"system",
"django_auth_ldap",
"mock",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -314,6 +315,11 @@
"level": "INFO",
"propagate": True,
},
"mock": {
"handlers": ["default", "console", "error", "db"],
"level": "INFO",
"propagate": True,
},
"django_auth_ldap": {
"handlers": ["default", "console", "error", "db"],
"level": "INFO",
Expand Down
6 changes: 4 additions & 2 deletions FasterRunner/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

DEBUG = True

# LOGGING["loggers"]["mock"]["level"] = "DEBUG"

logger.remove()

logger.add(
Expand All @@ -22,7 +24,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "fast_dev", # 新建数据库
"NAME": "fast", # 新建数据库
# 'NAME': 'fast_mb4', # 新建数据库名
"HOST": "127.0.0.1",
"USER": "root", # 数据库登录名
Expand All @@ -39,7 +41,7 @@
BROKER_URL = "amqp://username:password@localhost:5672//"
# 需要先在RabbitMQ上创建fast_dev这个vhost

broker_url = 'amqp://admin:[email protected]:5672/fast_dev'
broker_url = "amqp://admin:[email protected]:5672/fast_dev"


BASE_REPORT_URL = "http://localhost:8000/api/fastrunner/reports"
Expand Down
11 changes: 11 additions & 0 deletions FasterRunner/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from rest_framework_jwt.views import obtain_jwt_token

from fastrunner.views import run_all_auto_case
from mock.views import MockAPIView, MockAPIViewset, MockProjectViewSet
from system import views as system_views

schema_view = get_schema_view(
Expand All @@ -40,7 +41,16 @@
system_router = DefaultRouter()
system_router.register(r"log_records", system_views.LogRecordViewSet)

mock_api_router = DefaultRouter()
mock_api_router.register(r"mock_api", MockAPIViewset)


mock_project_router = DefaultRouter()
mock_project_router.register(r"mock_project", MockProjectViewSet)

urlpatterns = [
path("api/mock/", include(mock_project_router.urls)),
path("api/mock/", include(mock_api_router.urls)),
path(r"login", obtain_jwt_token),
path("admin/", admin.site.urls),
# re_path(r'^docs/', schema_view, name="docs"),
Expand All @@ -62,4 +72,5 @@
re_path(r"^swagger(?P<format>\.json|\.yaml)$", schema_view.without_ui(cache_timeout=0), name="schema-json"),
path("swagger/", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),
path("redoc/", schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc"),
re_path(r'^mock/(?P<project_id>\w+)(?P<path>/.*)$', MockAPIView.as_view())
]
79 changes: 79 additions & 0 deletions db/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,89 @@
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

DROP TABLE IF EXISTS `mock_api_log`;
create table mock_api_log
(
request_obj longtext collate utf8mb4_bin not null
check (json_valid(`request_obj`)),
response_obj longtext collate utf8mb4_bin null
check (json_valid(`response_obj`)),
api_id varchar(32) not null,
project_id varchar(100) null,
request_id varchar(100) null,
id bigint auto_increment
primary key,
create_time datetime(6) not null,
update_time datetime(6) not null,
creator varchar(20) null,
updater varchar(20) null
);

create index mock_api_log_api_id_d69785e2
on mock_api_log (api_id);

create index mock_api_log_project_id_e3b47bfd
on mock_api_log (project_id);

create index mock_api_log_request_id_86906b7c
on mock_api_log (request_id);

DROP TABLE IF EXISTS `mock_api_tab`;

create table mock_api_tab
(
id bigint auto_increment
primary key,
request_path varchar(100) not null,
request_method varchar(10) not null,
request_body longtext collate utf8mb4_bin default json_object() null,
response_text longtext not null,
is_active tinyint(1) not null,
project_id varchar(100) null,
api_desc varchar(100) null,
api_id varchar(32) not null,
api_name varchar(100) null,
enabled tinyint(1) not null,
create_time datetime(6) not null,
update_time datetime(6) not null,
creator varchar(20) null,
updater varchar(20) null,
constraint api_id
unique (api_id),
constraint mock_api_tab_project_id_request_path__eeea3f07_uniq
unique (project_id, request_path, request_method)
);

create index mock_api_tab_project_id_9b708a91
on mock_api_tab (project_id);

DROP TABLE IF EXISTS `mock_project_tab`;

create table mock_project_tab
(
id bigint auto_increment
primary key,
project_id varchar(100) not null,
project_name varchar(100) not null,
project_desc varchar(100) not null,
is_active tinyint(1) not null,
create_time datetime(6) not null,
update_time datetime(6) not null,
creator varchar(20) null,
updater varchar(20) null,
constraint mock_project_tab_project_id_446f3335_uniq
unique (project_id),
constraint project_id
unique (project_id)
);



--
-- Table structure for table `api`
--


DROP TABLE IF EXISTS `api`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
Expand Down
Empty file added mock/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions mock/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions mock/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class MockConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "mock"
114 changes: 114 additions & 0 deletions mock/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Generated by Django 4.1.13 on 2024-02-25 21:47

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="MockProject",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"create_time",
models.DateTimeField(auto_now_add=True, verbose_name="创建时间"),
),
(
"update_time",
models.DateTimeField(auto_now=True, verbose_name="更新时间"),
),
(
"creator",
models.CharField(max_length=20, null=True, verbose_name="创建人"),
),
(
"updater",
models.CharField(max_length=20, null=True, verbose_name="更新人"),
),
("project_id", models.CharField(max_length=100, unique=True)),
("project_name", models.CharField(max_length=100)),
("project_desc", models.CharField(max_length=100)),
("is_active", models.BooleanField(default=True)),
],
options={
"verbose_name": "mock项目表",
"db_table": "mock_project_tab",
"unique_together": {("project_id",)},
},
),
migrations.CreateModel(
name="MockAPI",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"create_time",
models.DateTimeField(auto_now_add=True, verbose_name="创建时间"),
),
(
"update_time",
models.DateTimeField(auto_now=True, verbose_name="更新时间"),
),
(
"creator",
models.CharField(max_length=20, null=True, verbose_name="创建人"),
),
(
"updater",
models.CharField(max_length=20, null=True, verbose_name="更新人"),
),
("request_path", models.CharField(max_length=100)),
(
"request_method",
models.CharField(
choices=[
("GET", "GET"),
("POST", "POST"),
("PUT", "PUT"),
("DELETE", "DELETE"),
("PATCH", "PATCH"),
],
default="GET",
max_length=10,
),
),
("response_text", models.TextField()),
("is_active", models.BooleanField(default=True)),
(
"project",
models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
to="mock.mockproject",
),
),
],
options={
"verbose_name": "mock接口表",
"db_table": "mock_api_tab",
"unique_together": {("project", "request_path", "request_method")},
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.1.13 on 2024-02-27 22:10

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("mock", "0001_initial"),
]

operations = [
migrations.AlterModelOptions(
name="mockapi",
options={"ordering": ["-create_time"], "verbose_name": "mock接口表"},
),
migrations.AlterField(
model_name="mockapi",
name="project",
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
to="mock.mockproject",
to_field="project_id",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 4.1.13 on 2024-02-27 22:49

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("mock", "0002_alter_mockapi_options_alter_mockapi_project"),
]

operations = [
migrations.AddField(
model_name="mockapi",
name="api_desc",
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name="mockapi",
name="api_id",
field=models.CharField(default="4e9eb9a68bd8441d9c503f1347f156ff", max_length=32, unique=True),
),
migrations.AddField(
model_name="mockapi",
name="api_name",
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name="mockapi",
name="followers",
field=models.JSONField(blank=True, default=list, null=True, verbose_name="关注者"),
),
migrations.AlterField(
model_name="mockapi",
name="project",
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="mock_apis",
to="mock.mockproject",
to_field="project_id",
),
),
]
Loading

0 comments on commit 089abfd

Please sign in to comment.