oschwald / maxminddb-golang

MaxMind DB Reader for Go
ISC License
576 stars 99 forks source link

Mock test MMDB #81

Closed zombozo12 closed 2 years ago

zombozo12 commented 2 years ago

Hi, I was working with something using this library. When I came to unit test, we had a problem where mmdb files cannot be found. Is there a way to mock mmdb.Open() function?

oschwald commented 2 years ago

For unit tests, I would recommend using the test databases provided by MaxMind.

If that doesn't work, I would suggest passing the reader into the code to be tested and using an interface so that you can easily mock it out.

zombozo12 commented 2 years ago

first suggestion doesn't work for some reason I can't tell.

I did interfacing the reader, but in my case I need to initiate mmdb in main.go so it doesn't open every time I want to lookup. I tried to monkey patch the Open() function, but it still return file not found even when I set the function to return nil.