当前位置: 代码迷 >> 综合 >> 简单数据库实现——Part4 - 初次测试
  详细解决方案

简单数据库实现——Part4 - 初次测试

热度:77   发布时间:2023-12-06 14:07:21.0

简单数据库实现——Part4 - 初次测试

我们现在已经能够插入行和打印所有行了,让我们花点时间来测试一下我们得到了什么。

我将使用rspec来编写我的测试,因为我对它很熟悉(我不熟。。。),而且它的语法相当容易读。

这里提一下rspec安装
基本上按照官网来就行了,先要安装一个bundler。

sudo apt install bundler

然后创建一个文件Gemfile

vim Gemfile

在Gemfile中写入:

// 注意此处替换为国内源
source "https://gems.ruby-china.com"
gem 'rspec', '~> 3.0'

安装rspec+初始化

bundle install --binstubs
bin/rspec --init

我将定义一个helper来向数据库程序发送命令,然后对输出进行断言(assertion)处理。
在/spec目录下添加测试文件

describe 'database' dodef run_script(commands)raw_output = nilIO.popen("./db", "r+") do |pipe|commands.each do |command|pipe.puts commandendpipe.close_write# Read entire outputraw_output = pipe.gets(nil)endraw_output.split("\n")end// 大概功能是执行一系列语句it 'inserts and retrieves a row' doresult = run_script(["insert 1 user1 person1@example.com","select",".exit",])expect(result).to match_array(["db > Executed.","db > (1, user1, person1@example.com)","Executed.","db > ",])end
end

这个简单的测试可以确保我们得到我们输入的东西。
在根目录下进行测试:

bundle exec rspec
.Finished in 0.00871 seconds (files took 0.09506 seconds to load)
1 example, 0 failures

接下来测试向数据库插入大量行。

it 'prints error message when table is full' doscript = (1..1401).map do |i|"insert #{
      i} user#{
      i} person#{
      i}@example.com"endscript << ".exit"result = run_script(script)expect(result[-2]).to eq('db > Error: Table full.')
end

测试结果,这个我一直有问题,最后写了个C测试了一下。正常测试结果如下:

bundle exec rspec
..Finished in 0.01553 seconds (files took 0.08156 seconds to load)
2 examples, 0 failures

我们的数据库现在可以容纳1400行,因为我们将页面的最大数量设置为100,一个页面可以容纳14行。应次测试的是1401行。

这个测试实在是不会用,后面我都是手动测试的。他测试完后发现一个问题,支持的用户名和电子邮件长度最长时会出现问题,原因是字符串结尾的空字符,解决方法是将长度+1。如下:

 const uint32_t COLUMN_EMAIL_SIZE = 255;typedef struct {
    uint32_t id;
-  char username[COLUMN_USERNAME_SIZE];
-  char email[COLUMN_EMAIL_SIZE];
+  char username[COLUMN_USERNAME_SIZE + 1];
+  char email[COLUMN_EMAIL_SIZE + 1];} Row;

我们不应该允许插入用户名或电子邮件的长度超过列的大小。为了做到这一点,我们需要升级我们的解析器。不再使用scanf(),而使用strtok()。

+PrepareResult prepare_insert(InputBuffer* input_buffer, Statement* statement) {
    
+  statement->type = STATEMENT_INSERT;
+
+  char* keyword = strtok(input_buffer->buffer, " ");
+  char* id_string = strtok(NULL, " ");
+  char* username = strtok(NULL, " ");
+  char* email = strtok(NULL, " ");
+
+  if (id_string == NULL || username == NULL || email == NULL) {
    
+    return PREPARE_SYNTAX_ERROR;
+  }
+
+  int id = atoi(id_string);
+  if (strlen(username) > COLUMN_USERNAME_SIZE) {
    
+    return PREPARE_STRING_TOO_LONG;
+  }
+  if (strlen(email) > COLUMN_EMAIL_SIZE) {
    
+    return PREPARE_STRING_TOO_LONG;
+  }
+
+  statement->row_to_insert.id = id;
+  strcpy(statement->row_to_insert.username, username);
+  strcpy(statement->row_to_insert.email, email);
+
+  return PREPARE_SUCCESS;
+}
+PrepareResult prepare_statement(InputBuffer* input_buffer,Statement* statement) {
    if (strncmp(input_buffer->buffer, "insert", 6) == 0) {
    
+    return prepare_insert(input_buffer, statement);
-    statement->type = STATEMENT_INSERT;
-    int args_assigned = sscanf(
-        input_buffer->buffer, "insert %d %s %s", &(statement->row_to_insert.id),
-        statement->row_to_insert.username, statement->row_to_insert.email);
-    if (args_assigned < 3) {
    
-      return PREPARE_SYNTAX_ERROR;
-    }
-    return PREPARE_SUCCESS;}

我们可以对每个文本值调用strlen()来查看它是否太长。
我们可以像处理其他错误代码一样处理错误

 enum PrepareResult_t {
    PREPARE_SUCCESS,
+  PREPARE_STRING_TOO_LONG,PREPARE_SYNTAX_ERROR,PREPARE_UNRECOGNIZED_STATEMENT};
 switch (prepare_statement(input_buffer, &statement)) {
    case (PREPARE_SUCCESS):break;
+  case (PREPARE_STRING_TOO_LONG):
+    printf("String is too long.\n");
+    continue;case (PREPARE_SYNTAX_ERROR):printf("Syntax error. Could not parse statement.\n");continue;

我们不妨再处理一个错误情况——ID为负。

 enum PrepareResult_t {
    PREPARE_SUCCESS,
+  PREPARE_NEGATIVE_ID,PREPARE_STRING_TOO_LONG,PREPARE_SYNTAX_ERROR,PREPARE_UNRECOGNIZED_STATEMENT
@@ -148,9 +147,6 @@ PrepareResult prepare_insert(InputBuffer* input_buffer, Statement* statement) {
    }int id = atoi(id_string);
+  if (id < 0) {
    
+    return PREPARE_NEGATIVE_ID;
+  }if (strlen(username) > COLUMN_USERNAME_SIZE) {
    return PREPARE_STRING_TOO_LONG;}
@@ -230,9 +226,6 @@ int main(int argc, char* argv[]) {
    switch (prepare_statement(input_buffer, &statement)) {
    case (PREPARE_SUCCESS):break;
+      case (PREPARE_NEGATIVE_ID):
+        printf("ID must be positive.\n");
+        continue;case (PREPARE_STRING_TOO_LONG):printf("String is too long.\n");continue;

测试到此为止,接下来是一个非常重要的功能:持久性!我们将数据库保存到文件中,然后再次读出。

修改的部分

@@ -22,6 +22,8 @@enum PrepareResult_t {
    PREPARE_SUCCESS,
+  PREPARE_NEGATIVE_ID,
+  PREPARE_STRING_TOO_LONG,PREPARE_SYNTAX_ERROR,PREPARE_UNRECOGNIZED_STATEMENT};
@@ -34,8 +36,8 @@#define COLUMN_EMAIL_SIZE 255typedef struct {
    uint32_t id;
-  char username[COLUMN_USERNAME_SIZE];
-  char email[COLUMN_EMAIL_SIZE];
+  char username[COLUMN_USERNAME_SIZE + 1];
+  char email[COLUMN_EMAIL_SIZE + 1];} Row;@@ -150,18 +152,40 @@ MetaCommandResult do_meta_command(InputBuffer* input_buffer, Table *table) {
    }}-PrepareResult prepare_statement(InputBuffer* input_buffer,
-                                Statement* statement) {
    
-  if (strncmp(input_buffer->buffer, "insert", 6) == 0) {
    
+PrepareResult prepare_insert(InputBuffer* input_buffer, Statement* statement) {
    statement->type = STATEMENT_INSERT;
-  int args_assigned = sscanf(
-     input_buffer->buffer, "insert %d %s %s", &(statement->row_to_insert.id),
-     statement->row_to_insert.username, statement->row_to_insert.email
-     );
-  if (args_assigned < 3) {
    
+
+  char* keyword = strtok(input_buffer->buffer, " ");
+  char* id_string = strtok(NULL, " ");
+  char* username = strtok(NULL, " ");
+  char* email = strtok(NULL, " ");
+
+  if (id_string == NULL || username == NULL || email == NULL) {
    return PREPARE_SYNTAX_ERROR;}
+
+  int id = atoi(id_string);
+  if (id < 0) {
    
+     return PREPARE_NEGATIVE_ID;
+  }
+  if (strlen(username) > COLUMN_USERNAME_SIZE) {
    
+     return PREPARE_STRING_TOO_LONG;
+  }
+  if (strlen(email) > COLUMN_EMAIL_SIZE) {
    
+     return PREPARE_STRING_TOO_LONG;
+  }
+
+  statement->row_to_insert.id = id;
+  strcpy(statement->row_to_insert.username, username);
+  strcpy(statement->row_to_insert.email, email);
+return PREPARE_SUCCESS;
+
+}
+PrepareResult prepare_statement(InputBuffer* input_buffer,
+                                Statement* statement) {
    
+  if (strncmp(input_buffer->buffer, "insert", 6) == 0) {
    
+      return prepare_insert(input_buffer, statement);}if (strcmp(input_buffer->buffer, "select") == 0) {
    statement->type = STATEMENT_SELECT;
@@ -223,6 +247,12 @@ int main(int argc, char* argv[]) {
    switch (prepare_statement(input_buffer, &statement)) {
    case (PREPARE_SUCCESS):break;
+      case (PREPARE_NEGATIVE_ID):
+	printf("ID must be positive.\n");
+	continue;
+      case (PREPARE_STRING_TOO_LONG):
+	printf("String is too long.\n");
+	continue;case (PREPARE_SYNTAX_ERROR):printf("Syntax error. Could not parse statement.\n");continue;