简体   繁体   English

单元测试数据库访问层

[英]Unit testing database access layer

I know this question has been asked many times before, but I have some concrete code examples and I wanted to know whether it makes sense to unit test them: 我知道这个问题已经被问过很多次了,但是我有一些具体的代码示例,我想知道对它们进行单元测试是否有意义:

class FooAPI(object):

    def create(self, prop1, prop2, prop3, prop4, prop5):
        sql = "INSERT INTO foo (prop1, prop2, prop3) VALUES (?, ?, ?)"
        self.connection.execute(sql, (prop1, prop2, prop3))

        foo_id = self.connection.insert_id()

        sql = "INSERT INTO foo_settings (foo_id, prop4, prop5) VALUES (?, ?, ?)"
        self.connection.execute(sql, (foo_id, prop4, prop5))

        return foo_id

    def update(self, foo_id, prop1, prop2, prop3, prop4, prop5):
        "Update code similar to above"

    def delete(self, foo_id):
        sql = "DELETE FROM foo WHERE foo_id = ?"
        self.connection.execute(sql, (foo_id,))

    def find(self, foo_id=None, prop1=None):
        "Find objects by ID or by prop1"

Does it make sense to unit-test the above code, and how would one go about this. 对上述代码进行单元测试是否有意义,以及如何进行此测试。 There are two complicating factors here: 这里有两个复杂的因素:

  • The database itself is not trivial and I currently have no easy way to create a database with all test data 数据库本身并不简单,我目前没有简单的方法来创建包含所有测试数据的数据库
  • The schema is evolving, meaning that it's impossible to create a test database once and for all. 模式在不断发展,这意味着不可能一劳永逸地创建测试数据库。

Depending on who you ask, it always makes sense to test code. 根据您询问的人,测试代码总是有意义的。 That said, what you're doing here is pretty straight forward and might not need testing. 也就是说,您在这里所做的工作非常简单,可能不需要测试。 However, I would argue that even if it's difficult to set up a test database, you will most likely need it later anyway, so you might as well do it now when you're not trying to do something complicated. 但是,我认为即使建立测试数据库很困难,您仍然很可能以后需要它,因此,当您不打算做复杂的事情时,最好现在就做。 Also, it's find that the schema is evolving. 此外,还发现该模式正在发展。 It's almost inevitable that there will be major database changes over time and you just have to roll with it. 随着时间的推移,数据库将发生重大变化几乎是不可避免的,而您只需要顺其自然即可。

The only good reason I can think of not to test is if you expect to totally change the database soon. 我能想到不进行测试的唯一好理由是,如果您希望很快就完全更改数据库。

Unit tests (ie without a database), not IMO. 单元测试(即没有数据库),而不是IMO。 Integration tests generally I think it is a good idea. 一般来说,集成测试我认为这是一个好主意。

The database itself is not trivial and I currently have no easy way to create a database with all test data 数据库本身并不简单,我目前没有简单的方法来创建包含所有测试数据的数据库

How do you maintain the schema? 您如何维护架构? You need to get this right before you go forward. 您需要正确执行此操作,然后再继续。 Can you go from a blank database to a one with at least the schema in a single automated step? 您是否可以在一个自动化步骤中将空白数据库从至少一个包含架构的数据库迁移到另一个数据库? I'm guessing not so Liquibase can help you if you don't have a good system to keep the db up to date. 我猜不是,如果您没有一个使数据库保持最新状态的好的系统,那么Liquibase可以为您提供帮助。

Now the test data - Keep the data required small. 现在,测试数据-将所需数据保持较小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM