Answer the question
In order to leave comments, you need to log in
How to organize tests in adapter type packages?
The application has a repo package that contains several interfaces - all the methods necessary for the application are described there. Then there are various packages (adapters) that implement `repo` for different databases: repo/mongodb , repo/redis , repo/sqlite .
The fs structure is like this:
Answer the question
In order to leave comments, you need to log in
I have in some projects the scheme is very similar to yours.
I move the tests and initialization to the repo folder.
The folder structure looks like this (I take the users package as an example)
users/repo.go - описание интерфейса Repo
users/repo/repo.go - тут инициализация любого типа реализации в зависимости от конфига
users/repo/repo_test.go - тут тесты
(создаю все типы репозиториев (mongo, redis...)) и одними и теми же тестами прогоняю каждый тип.
Тут и инициализация репозиториев для тестов и зачистка после тестов.)
users/repo/mongodb - реализация (импортирует пакет users при необходимости)
users/repo/redis
users/repo/sqlite
The problem is that these adapters have the same methods, do the same thing (but in their own way),
type Repository interface{
MethodA() error
MethodB() error
}
func TestRedis(t *testing.T) {
testRepo(t, NewRedis(...))
}
func TestMongo(t *testing.T) {
testRepo(t, NewMongo(...))
}
func testRepo(t *testing.T, repo Repository){
err := repo.MethodA()
if err != nil {
t.Errorf("methodA: %s", err)
}
err = repo.MethodB()
if err != nil {
t.Errorf("methodB: %s", err)
}
}
... but there is a problem with cyclic imports (all adapters import components from repo).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question