Django使用存储过程测试MS-SQL遗留数据库

时间:2014-08-07 10:56:51

标签: python django sql-server-2008 unit-testing stored-procedures

在Django中,我想使用Unit Test来测试MS-SQL Server遗留数据库。数据库正在使用存储过程来添加数据。情况如下:

  • MS-SQL数据库在Django中具有以下设置:

    DATABASES['vadain_import'] = {
      'ENGINE': 'sql_server.pyodbc',
      'USER': 'xx',
      'PASSWORD': 'xxx',
      'HOST': '192.168.103.102',
      'PORT': '',
      'NAME': 'Vadain_import',
      'OPTIONS': {
        'driver': 'SQL Server Native Client 11.0',
        'MARS_Connection': True,
      }
    }
    
  • 数据库的模型使用inspectdb,例如:

    class WaOrders(models.Model):
        order_id = models.IntegerField(primary_key=True, db_column='intOrderId')
        type = models.TextField(db_column='chvType', blank=True)
    
    class Meta:
        db_table = 'WA_ORDERS'
        database_name = 'vadain_import'
        managed = False
    
    # (There's a lot more of properties and models)
    
  • 在模型中正在执行存储过程。我不能使用保存 Django的功能,如WAOrders.save(),因为在MS-SQL中 数据库主键是在存储过程中生成的。

    @classmethod
    def export(cls, **kwargs):
        # Stored procedure for adding data into the db
        sql = "declare @id int \
            set @id=0\
            declare @error varchar(1000)\
            set @error=''\
            exec UspWA_OrderImport\
            @intOrderId=@id out\
            ,@chvType=N'%s'" % kwargs['type'] + " \
            ,@chvErrorMsg=@error output\
            select @id as id, @error as 'error' \
        "
        # Connection Vadain db
        cursor = connections['vadain_import'].cursor()
        # Execute sql stored procedure, no_count is needed otherwise it's returning an error
        # Return value primary key of WAOrders
        try:
            cursor.execute(no_count + sql)
            result = cursor.fetchone()
            # Check if primary key is set and if there are no errors:
            if result[0] > 1 and result[1] == '':
                # Commit SP
                cursor.execute('COMMIT')
                return result[0]
    
  • 有一个用于创建模型的映射,因为MS-SQL 数据库期望不同的数据然后是正常的对象,比如'order'。

    def adding_data_into_vadain(self, order):
        for curtain in order.curtains.all():
            order_id = WaOrders.export(
                type=format(curtain.type)
            )
            # relation with default and vadain db.
            order.exported_id = order_id
            order.save()
    

该功能通过运行程序正常工作,但通过运行'manage.py test'将创建一个测试数据库。这给出了以下问题:

  • 通过创建测试数据库缺少南表(遗留数据库中也不需要这样)
  • 通过将SOUTH_TESTS_MIGRATE更改为False,我收到的错误消息是表已存在默认数据库。

我的测试如下:

class AddDataServiceTest(TestCase):
  fixtures = ['order']
  def test_data_service(self):
    # add data into vadain_import data
    for order in Order.objects.all():
      AddingDataService.adding_data_into_vadain(order)
    # test the exported values 
    for order in Order.objects.all():
      exported_order = WaOrders.objects.get(order_exported_id=order.exported_id)
    self.assertEqual(exported_order.type, 'Pleat curtain')

有人可以告诉我如何测试这种情况吗?

1 个答案:

答案 0 :(得分:0)

也许在你的WaOrders里面你可以扩展save()方法并调用那里的导出函数。