11 Database and Integration Testing

 

This chapter covers

  • Discovering and extending the sql package.
  • Structuring code with external dependencies for improved maintainability.
  • Testing approaches for a reliable and stable codebase.
  • Designing idiomatic interfaces to achieve modularity and composability.

In the previous chapters, our engineering team at Bite built a link server. However, there are flaws: The server stores links in memory. The product owner, Lina, wants the links preserved in a database long-term, even after the link server is restarted.

Figure 11.1 shows that the team picked the SQLite database for its practicality and lightweightness. Instead of temporarily storing links in memory, Store stores them permanently in SQLite using the sqlx and sql packages.

Figure 11.1 The link server uses Store to store links permanently. Store uses sqlx, which uses Go's database/sql to interact with SQLite.

Go's sql package can communicate with any SQL (or SQL-like, row-oriented) database. It enables our programs to work independently of the underlying SQL database technology. We'll also create the helper sqlx package to work with the sql package (which can help us augment sql with additional functionality, such as logging, tracing, etc.).

11.1 The database/sql package

11.1.1 Downloading and registering a driver

11.1.2 Opening up a connection pool

11.1.3 Dialing the database

11.1.4 Testing the connection

11.1.5 Applying the schema

11.1.6 Wrap Up

11.2 Storage service

11.2.1 Persisting links

11.2.2 Retrieving links

11.2.3 Wrap up

11.3 Extending the sql package

11.3.1 Satisfying Valuer and Scanner

11.3.2 Encoding and decoding

11.3.3 Wrap up

11.4 Updating the daemon

11.5 Testing

11.5.1 Testing against the real thing

11.5.2 Testing with interfaces

11.5.3 Wrap up

11.6 Designing idiomatic interfaces

11.6.1 Where should we declare interfaces?

11.6.2 How to design better interfaces?

11.6.3 Wrap up

11.7 Exercises

11.8 Summary