#1 修改测试用例中book_id生成方式,避免book_id重复

Açık
李云帆 10195501416/Project2:master içindeki 5 işlemeyi master ile birleştirmek istiyor
李云帆 3 yıl önce yorum yaptı

根据文档设计可知,"商家添加书籍信息"接口要求对Book ID的唯一性作判断,如果book_id已存在则视为错误并返回对应的错误码

(见be/doc/seller.md)

描述
200 添加图书信息成功
5XX 卖家用户ID不存在
5XX 商铺ID不存在
5XX 图书ID已存在

但在该接口的测试用例中,每次测试所使用的图书ID都是一样的,可以看到测试用例中生成输入数据(图书信息)的代码:

def get_book_info(self, start, size) -> [Book]:
        books = []
        conn = sqlite.connect(self.book_db)
        cursor = conn.execute(
            "SELECT id, title, author, "
            "publisher, original_title, "
            "translator, pub_year, pages, "
            "price, currency_unit, binding, "
            "isbn, author_intro, book_intro, "
            "content, tags, picture FROM book ORDER BY id "
            "LIMIT ? OFFSET ?", (size, start))
        for row in cursor:
            book = Book()
            book.id = row[0]
            book.title = row[1]
            book.author = row[2]
            book.publisher = row[3]
            book.original_title = row[4]
            book.translator = row[5]
            book.pub_year = row[6]
            book.pages = row[7]
            book.price = row[8]
            book.currency_unit = row[9]
            book.binding = row[10]
            book.isbn = row[11]
            book.author_intro = row[12]
            book.book_intro = row[13]
            book.content = row[14]
            tags = row[15]

            picture = row[16]

            for tag in tags.split("\\n"):
                if tag.strip() != "":
                    book.tags.append(tag)
            for i in range(0, random.randint(0, 9)):
                if picture is not None:
                    encode_str = base64.b64encode(picture).decode('utf-8')
                    book.pictures.append(encode_str)
            books.append(book)
            # print(tags.decode('utf-8'))

            # print(book.tags, len(book.picture))
            # print(book)
            # print(tags)

        return books

book_db = book.BookDB()
self.books = book_db.get_book_info(0, 2)

可以发现每次使用的都是book.db中id最小的两种书,即id分别为1000067和1000134的《美丽心灵》和《三毛流浪记全集》。

这样一来,由于每次测试用的是相同的图书ID,必须在每次测试前都清空数据库才能保证没有重复的图书id,否则所有测试都将返回错误(book_id已存在)。但每次测试前都清空数据库是不太现实的,这会大大增加测试的代价。

因此我认为正确的测试方法时,每次给输入图书ID也加上一个通过uuid随机生成的后缀,保证每次测试使用的图书id不一样,例如修改上述代码中生成book_id的部分,即修改access/book.py

# book.id = row[0]
book.id = row[0] + '_' + str(str(uuid.uuid1()))

经过测试,按照该方法修改后,参考代码仍然能通过全部测试用例。

另外,参考代码能够通过原测试用例的原因是:其检查book_id是否存在的逻辑是要求同一组store_id和book_id重复出现

def book_id_exist(self, store_id, book_id):
        cursor = self.conn.execute("SELECT book_id FROM store WHERE store_id = ? AND book_id = ?;", (store_id, book_id))
        row = cursor.fetchone()
        if row is None:
            return False
        else:
            return True

即同时对store_id和book_id同时进行检查,只有该store_id和book_id同时存在才会返回错误。

这样确实可以通过所有的测试用例。但这与文档描述不符,根据文档说明,即使两次添加的store_id不同,只要book_id存在(某一个book_id曾被添加过)就应该返回错误。

根据文档设计可知,"商家添加书籍信息"接口要求对Book ID的唯一性作判断,如果book_id已存在则视为错误并返回对应的错误码 (见be/doc/seller.md) 码 | 描述 --- | --- 200 | 添加图书信息成功 5XX | 卖家用户ID不存在 5XX | 商铺ID不存在 5XX | 图书ID已存在 但在该接口的测试用例中,每次测试所使用的图书ID都是一样的,可以看到测试用例中生成输入数据(图书信息)的代码: ```python def get_book_info(self, start, size) -> [Book]: books = [] conn = sqlite.connect(self.book_db) cursor = conn.execute( "SELECT id, title, author, " "publisher, original_title, " "translator, pub_year, pages, " "price, currency_unit, binding, " "isbn, author_intro, book_intro, " "content, tags, picture FROM book ORDER BY id " "LIMIT ? OFFSET ?", (size, start)) for row in cursor: book = Book() book.id = row[0] book.title = row[1] book.author = row[2] book.publisher = row[3] book.original_title = row[4] book.translator = row[5] book.pub_year = row[6] book.pages = row[7] book.price = row[8] book.currency_unit = row[9] book.binding = row[10] book.isbn = row[11] book.author_intro = row[12] book.book_intro = row[13] book.content = row[14] tags = row[15] picture = row[16] for tag in tags.split("\\n"): if tag.strip() != "": book.tags.append(tag) for i in range(0, random.randint(0, 9)): if picture is not None: encode_str = base64.b64encode(picture).decode('utf-8') book.pictures.append(encode_str) books.append(book) # print(tags.decode('utf-8')) # print(book.tags, len(book.picture)) # print(book) # print(tags) return books book_db = book.BookDB() self.books = book_db.get_book_info(0, 2) ``` 可以发现每次使用的都是book.db中id最小的两种书,即id分别为1000067和1000134的《美丽心灵》和《三毛流浪记全集》。 这样一来,由于每次测试用的是相同的图书ID,必须在每次测试前都清空数据库才能保证没有重复的图书id,否则所有测试都将返回错误(book_id已存在)。但每次测试前都清空数据库是不太现实的,这会大大增加测试的代价。 因此我认为正确的测试方法时,每次给输入图书ID也加上一个通过uuid随机生成的后缀,**保证每次测试使用的图书id不一样**,例如修改上述代码中生成book_id的部分,即修改access/book.py ```python # book.id = row[0] book.id = row[0] + '_' + str(str(uuid.uuid1())) ``` 经过测试,按照该方法修改后,参考代码仍然能通过全部测试用例。 另外,参考代码能够通过原测试用例的原因是:其检查book_id是否存在的逻辑是要求同一组store_id和book_id重复出现 ```python def book_id_exist(self, store_id, book_id): cursor = self.conn.execute("SELECT book_id FROM store WHERE store_id = ? AND book_id = ?;", (store_id, book_id)) row = cursor.fetchone() if row is None: return False else: return True ``` 即同时对store_id和book_id同时进行检查,只有该store_id和book_id同时存在才会返回错误。 这样确实可以通过所有的测试用例。但这与文档描述不符,根据文档说明,即使两次添加的store_id不同,只要book_id存在(某一个book_id曾被添加过)就应该返回错误。
李云帆 1 3 yıl önce işlemesi eklendi
李云帆 1 3 yıl önce işlemesi eklendi
李云帆 1 3 yıl önce işlemesi eklendi
李云帆 1 3 yıl önce işlemesi eklendi
李云帆 1 3 yıl önce işlemesi eklendi
Bu değişiklik isteği otomatik olarak birleştirilebilir.
Bu dal, temel dal ile güncel değil
Bu değişiklik isteğini birleştirme yetkiniz yok.
Bu konuşmaya katılmak için oturum aç.
Değerlendirici yok
Etiket Yok
Kilometre Taşı Yok
Atanan Kişi Yok
1 Katılımcı
Bildirimler
Bitiş Tarihi

Bitiş tarihi atanmadı.

Bağımlılıklar

Bu çekme isteği henüz bir bağımlılık içermiyor.

Yükleniyor…
Henüz bir içerik yok.