Reputation: 254
I am hoping to get some advice on a unit test I am writing for to test some db entries.
The function I am testing seeds the database if no records are found.
func Seed(db *gorm.DB) {
var data []Data
db.Find(&data)
if len(data) == 0 {
// do seed default data
}
}
What I can't quite seem to get going is the test for that if len test. I am using a test db so I can nuke it whenever so it is not an issue if I just need to force an empty DB on the function.
The function itself works and I just want to make sure I get that covered.
Any advice would be great.
Thanks!
Upvotes: 0
Views: 1468
Reputation: 55962
It really depends, there are so many ways of addressing this based on your risk level and the amount of time you want to invest to mitigate those risks.
gorm
library you could:
Seed
with no users in the DB, after calling it your test could select from Users
and make sure there are the expected entries created from len(users) == 0
conditionalSeed
, after which asserting that underlying tables are empty.It can get more complicated. If Seed
is selecting a subset of data than your test could insert 2 users, one that qualifies and one that doesnt', and make sure that no new users are Seed
ed.
Upvotes: 1