1

I'm written a software as an exercise to improve my skills and I ask my self about how to test code that changes a database.

Let the code in my github: https://github.com/daniloanp/Ensaios/blob/develop/application/backend/pgsql/tables/module.go

What is the best technique to perform tests over, for example, the Create and Update methods?

My first attempt was using the GetByID to test my insert, but considering the fact GetByID wasn't tested yeat, I don't know if that is good way to test.

Currently I'm thinking about use a third party ORM or library to check if the database was tested as expected. I could test even the GetByID comparing the results of my own implementation and the thirdy party ones.

Are there any good material about it? Any recomendation or good framework in Golang or even in any other language?

Update: Well, I think I need something like Java DbUNit in Go

  • the standard way is to use an interfaced repository and inject a mock. dunno how you achieve it in that crazy non microsoft language you are using though – Ewan Jul 12 '16 at 22:47
  • My mistake, I did a refactor – Danilo Jul 13 '16 at 3:58
  • Do you need to test that an INSERT occurs in the database, or are you testing other parts of the application that call methods on your data access module? – Greg Burghardt Jul 13 '16 at 17:08
  • @GregBurghardt I need to test the insert itself. I mean: did my call changes de database state correctly. To test any other part I could use mocks – Danilo Jul 13 '16 at 23:12
1

The language is immaterial. Whether your database changing code is testable is all that matters. In any language you can access the database in a way that makes testing difficult. Is knowledge of your particular database spread throughout your code (or is it located in one place)? Is it difficult to swap in a different database? How hard would it be to go without a database and just let objects stand in the place of the database? Objects that can tell you what you told them and can stand in for others are called mocks. Doing this to a database is called mocking out the database.

Different frameworks and libraries may offer tools that simplify testing this but HOW you code impacts testability far more than what language you code in.

  • My project is still in beginning, but my design intent is to put every code dealing directly with database in a specific place. I want to have model interfaces to that, something like app.Db.Table.* app.Db.View.* Every code that deal with a database in abstract way will be placed in model.Db, every code really dealing with database will be placed in pgsql.Db. – Danilo Jul 13 '16 at 4:11
  • I know how to mock around my entire project to generate tests and thats enough for test most of layers. But I want to test the layer dealing with database directly. I'm considering to use a database just for tests with the same schema of the oficial one – Danilo Jul 13 '16 at 4:11
  • That looks like a good beginning: dbunit.sourceforge.net/intro.html I will check for any similar thing for Go – Danilo Jul 13 '16 at 4:13
  • Again, a dead link – candied_orange Jul 13 '16 at 4:16
  • There's no dead link now. – Danilo Jul 13 '16 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.