exceptionhandler/
manage.py
apis/ -- add dispatch here
sample_apis.py
applications/ -- add user case here, it will handle multi aggregate root cooperation
sample_user_case.py
domains/
rootcases/ -- aggregate
sample.py -- aggregate root
algorithm.py -- entity
sample_domain_service.py -- domain service
infrastructures/ -- infrastructure
samples/
edoc.py -- add upload/download specific implimentation
sample_modules.py -- add db CRUD specific implimentation
exceptionhandler/ -- main root
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
- 使用独立的测试setting文件
# 初始化数据库
python3 manage.py migrate --settings=exceptionhandler.test_settings
# 跑测试
python3 manage.py test --settings=exceptionhandler.test_settings
- 使用sqlite替代掉原有数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db-test.sqlite3'),
}
}
def setUp(self) -> None:
SampleData.objects.create(id_name="id", mark_result=False).save()
def tearDown(self) -> None:
SampleData.objects.all().delete()
- 使用mock替换掉数据
@patch('domain.rootcause.algorithm.analysis')
def test_mock_analysis(self, mock_analysis):
mock_analysis.return_value = True
request = HttpRequest()
request._body = '{"id": "id2"}'
value = create_sample(request)
self.assertEqual(value.getvalue(), b'{"id": "id2", "mark_result": false}')
- 使用stub server
def setUp(self) -> None:
self.server = StubServer(8000)
self.server.run()
def tearDown(self) -> None:
self.server.stop()
@patch('domain.rootcause.algorithm.analysis')
def test_mock_analysis(self, mock_analysis):
self.server.expect(method="GET", url="/").and_return(content="id2")
...