Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在django unittest中从/到json字符串加载/转储夹具_Django_Django Unittest_Django Fixtures - Fatal编程技术网

在django unittest中从/到json字符串加载/转储夹具

在django unittest中从/到json字符串加载/转储夹具,django,django-unittest,django-fixtures,Django,Django Unittest,Django Fixtures,我在django中有一个自定义模型,它重写为_python()和get _db_prep_save()方法。我发现了一个bug:当转储和重新加载数据时,它是不一致的。这个bug已经修复,但是我想用简单的json字符串对它进行联合测试 我的问题是:如何在unittest内部调用loaddata/dumpdata 我想创建以下场景: from django.test import TestCase class CustomModelTest(TestCase): def test_load_

我在django中有一个自定义模型,它重写为_python()和get _db_prep_save()方法。我发现了一个bug:当转储和重新加载数据时,它是不一致的。这个bug已经修复,但是我想用简单的json字符串对它进行联合测试

我的问题是:如何在unittest内部调用loaddata/dumpdata

我想创建以下场景:

from django.test import TestCase
class CustomModelTest(TestCase):
    def test_load_fixture(self):
        mydata = '[{"model": "some_app.custommodel", "pk": 1, "fields": {"custom_field": "some correct value"}}]'
        django.some_interface_to_load_fixture.loaddata(mydata) // data could be as json string, file, stream
        make some assertions on database model custommodel

    def test_dump_fixture(self):
        mymodel = create model object with some data
        result = django.some_interface_to_dump_fixture.dumpdata()
        make some assertions on result
来自django.test的
导入测试用例
类CustomModelTest(TestCase):
def测试负载夹具(自身):
mydata='[{“model”:“some_app.custommodel”,“pk”:1,“fields”:{“custom_field”:“some correct value”}]'
django.some_interface_to_load_fixture.loaddata(mydata)//数据可以是json字符串、文件和流
对数据库模型custommodel做一些断言
def测试卸载夹具(自身):
mymodel=使用一些数据创建模型对象
result=django.some_interface_to_dump_fixture.dumpdata()
对结果做出一些断言

我知道有一个
fixture=[]
字段可以在django单元测试中使用,它可以解决加载fixture的场景。但是,如果有人能给我指一些接口来按需加载或转储夹具数据,那就太好了。

多亏了@YugandharChaudhari,我想出了使用django.core.serializers的解决方案:

import json
from django.core import serializers
from django.test import TestCase
from some_app.models import CustomModel

class CustomModelTest(TestCase):
    def test_deserializing(self):
        test_data = [
            {"model": "some_app.custommodel",
             "pk": 1,
             "fields":
                 {
                     "custom_field": "some correct value"}
             }
        ]
        result = list(serializers.deserialize('json', json.dumps(test_data)))
        self.assertEqual(result[0].object.custom_field, 'some data after deserialization')

    def test_serializing(self):
        custom_model_obj = CustomModel(id=1, custom_field='some data')
        result_json = json.loads(serializers.serialize('json', [custom_model_obj]))
        self.assertEqual('some data after serialization', result_json[0]['fields']['custom_field'])

你能发布你的卸载和重新加载方法吗?还有,为什么不简单地使用
json
模块呢?我想问的是,djago中是否已经有一些方法可以做到这一点。代码只是一个概念。如果有接口,我可以根据现有接口调整数据。我想是的。如果我可以使用
python manage.py loaddata data.json
加载数据,那么应该有一些接口可以直接在unittests中的django模块中使用。django使用来自django.core.serializers.json导入序列化程序、反序列化程序的
这个
接口来实现这一点,使用管理命令
loaddata
怎么样?您也可以在代码中调用它。您可以实例化
命令
,然后使用管理命令将采用的参数运行
execute
。查看源代码以了解需要哪些参数(如果不是从命令行调用,则必须传递默认值)很高兴我提供了帮助:)