简体   繁体   English

测试从数据库查询的结果集中返回唯一值的方法

[英]Testing a method to return unique values from a result set of a db query

Let's say there is a database table named "Students". 假设有一个名为“ Students”的数据库表。

         Students Table
|---------------|---------------|
|     Name      |      Age      |
|---------------|---------------|
|     John      |       9       |
|---------------|---------------|
|     Jane      |       7       |
|---------------|---------------|
|     Dana      |       8       |
|---------------|---------------|
|     John      |       6       |
|---------------|---------------|

I make a request to the database to return all names using: 我使用以下命令向数据库返回所有名称:

SELECT Name FROM Students

So the result set would be: 因此结果集将是:

ResultSet rs = {"John" "Jane" "Dana" "John"}

I then want to return all unique values so I write a method called populateSet() to populate a HashSet setOfNames with rs . 然后,我想返回所有唯一值,因此我编写了一个名为populateSet()的方法,以用rs填充HashSet setOfNames

After executing populateSet(ResultSet rs) , only unique names appear: 执行populateSet(ResultSet rs) ,仅出现唯一的名称:

"John" "Jane" "Dana"

Here is the validation test: 这是验证测试:

public void testValidation() {
    // Skipping the test data / db connection / query part
    ResultSet rs = ResultSet received back from DB
    Set<String> expected = {"John", "Jane", "Dana"};
    Set<String> actual = WhateverClass.populateSet(rs);
    assertEquals(expected, actual);
}

What are some of the possible unit tests that I could write? 我可以编写哪些可能的单元测试?

I know I should write a null, empty, and negative tests. 我知道我应该写一个空,空和否定的测试。 What are some corner cases? 什么是极端情况?

Potential corner cases: 潜在的极端情况:

  • Very large table (millions of rows) 很大的桌子(几百万行)
  • Transactionality - eg, what happens if a name is inserted/deleted/updated after you start the table scan? 事务性-例如,如果在启动表扫描后插入/删除/更新名称会发生​​什么?
  • Data ordering 数据排序
  • Case sensitivity/insensitivity of names 名称区分大小写/不区分大小写
  • non-ASCII characters in names 名称中的非ASCII字符

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

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