实现一个简单的Database4(译文)

前文回顾

实现一个简单的Database1(译文)

实现一个简单的Database2(译文)

实现一个简单的Database3(译文)


译注:cstsck在github维护了一个简单的、类似SQLite的数据库实现,通过这个简单的项目,可以很好的理解数据库是如何运行的。本文是第四篇,主要是使用rspec对目前实现的功能进行测试并解决测试出现BUG

译注:cstsck在github维护了一个简单的、类似sqlite的数据库实现,通过这个简单的项目,可以很好的理解数据库是如何运行的。本文是第四篇,主要是使用rspec对目前实现的功能进行测试并解决测试出现BUG

Part 4 我们的第一个测试(和BUG)

我们已经获得插入数据到数据库并打印所有数据的能力。现在来测试一下目前microseconds已有的功能。

我要使用rspec来写我的测试,因为我对rspec很熟悉,它的语法也相当易读。
译注:rsepec 是一个基于Ruby的测试框架,语法非常简单,可以很方便的测试各种可执行程序,判断输出

我定义一个短小的help来发送一个帮助命令列表到数据库,然后对输出进行断言。

describe 'database' do def run_script(commands) raw_output = nil IO.popen("./db", "r+") do |pipe| commands.each do |command| pipe.puts command end pipe.close_write 1. Read entire output raw_output = pipe.gets(nil) end raw_output.split("\n") end it 'inserts and retrieves a row' do result = 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