Testfixtures: could not clean table "access": no such table: access

Bonjour,

The following test file

package foobar

import (
	"path/filepath"
	"testing"

	//	"code.gitea.io/gitea/models"
	"code.gitea.io/gitea/models/unittest"
	"github.com/stretchr/testify/assert"
)

//var _ models.AccessMode

func TestMain(m *testing.M) {
	unittest.MainTest(m, filepath.Join("..", ".."))
}

func TestFoobar(t *testing.T) {
	assert.NoError(t, unittest.PrepareTestDatabase())
}

fails with:

=== RUN   TestFoobar
LoadFixtures failed after retries: testfixtures: could not clean table "access": no such table: access
    foobar_test.go:19: 
        	Error Trace:	foobar_test.go:19
        	Error:      	Received unexpected error:
        	            	testfixtures: could not clean table "access": no such table: access
        	Test:       	TestFoobar
--- FAIL: TestFoobar (4.02s)
FAIL
FAIL	code.gitea.io/gitea/modules/foobar	4.110s

But it works when models is imported (uncomment in the above).

=== RUN   TestFoobar
--- PASS: TestFoobar (0.01s)
PASS
ok  	code.gitea.io/gitea/modules/foobar	0.116s

Why is that?

Yes, because of recent refactor, some model files momved into sub packages of models. So that, in sub packages, Access haven’t RegisterModel but testfixtures want to load it. Then the error occupied. I think you can import _ "code.gitea.io/gitea/models" to force Access invoked RegisterModel.

1 Like

Nice trick, I’ll try that, thanks!

I confirm that the following workaround works:

package foobar

import (
	"path/filepath"
	"testing"

	_ "code.gitea.io/gitea/models"  // workaround testfixtures: could not clean table "access": no such table: access
	"code.gitea.io/gitea/models/unittest"
	"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
	unittest.MainTest(m, filepath.Join("..", ".."))
}

func TestFoobar(t *testing.T) {
	assert.NoError(t, unittest.PrepareTestDatabase())
}