|
| 1 | +package migration |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/suite" |
| 7 | + |
| 8 | + "github.com/goravel/framework/contracts/database" |
| 9 | + "github.com/goravel/framework/database/gorm" |
| 10 | + mocksorm "github.com/goravel/framework/mocks/database/orm" |
| 11 | + "github.com/goravel/framework/support/docker" |
| 12 | + "github.com/goravel/framework/support/env" |
| 13 | +) |
| 14 | + |
| 15 | +type RepositoryTestSuite struct { |
| 16 | + suite.Suite |
| 17 | + driverToTestQuery map[database.Driver]*gorm.TestQuery |
| 18 | +} |
| 19 | + |
| 20 | +func TestRepositoryTestSuite(t *testing.T) { |
| 21 | + if env.IsWindows() { |
| 22 | + t.Skip("Skipping tests of using docker") |
| 23 | + } |
| 24 | + |
| 25 | + suite.Run(t, &RepositoryTestSuite{}) |
| 26 | +} |
| 27 | + |
| 28 | +func (s *RepositoryTestSuite) SetupTest() { |
| 29 | + postgresDocker := docker.Postgres() |
| 30 | + postgresQuery := gorm.NewTestQuery(postgresDocker, true) |
| 31 | + s.driverToTestQuery = map[database.Driver]*gorm.TestQuery{ |
| 32 | + database.DriverPostgres: postgresQuery, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (s *RepositoryTestSuite) TestCreate_Delete_Exists() { |
| 37 | + for driver, testQuery := range s.driverToTestQuery { |
| 38 | + s.Run(driver.String(), func() { |
| 39 | + repository, mockOrm := s.initRepository(testQuery) |
| 40 | + |
| 41 | + mockOrm.EXPECT().Connection(driver.String()).Return(mockOrm).Once() |
| 42 | + mockOrm.EXPECT().Query().Return(repository.query).Once() |
| 43 | + |
| 44 | + err := repository.CreateRepository() |
| 45 | + s.NoError(err) |
| 46 | + |
| 47 | + mockOrm.EXPECT().Query().Return(repository.query).Once() |
| 48 | + |
| 49 | + s.True(repository.RepositoryExists()) |
| 50 | + |
| 51 | + mockOrm.EXPECT().Connection(driver.String()).Return(mockOrm).Once() |
| 52 | + mockOrm.EXPECT().Query().Return(repository.query).Once() |
| 53 | + |
| 54 | + err = repository.DeleteRepository() |
| 55 | + s.NoError(err) |
| 56 | + |
| 57 | + mockOrm.EXPECT().Query().Return(repository.query).Once() |
| 58 | + |
| 59 | + s.False(repository.RepositoryExists()) |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (s *RepositoryTestSuite) TestRecord() { |
| 65 | + for driver, testQuery := range s.driverToTestQuery { |
| 66 | + s.Run(driver.String(), func() { |
| 67 | + repository, mockOrm := s.initRepository(testQuery) |
| 68 | + |
| 69 | + mockOrm.EXPECT().Query().Return(repository.query).Once() |
| 70 | + |
| 71 | + if !repository.RepositoryExists() { |
| 72 | + mockOrm.EXPECT().Connection(driver.String()).Return(mockOrm).Once() |
| 73 | + mockOrm.EXPECT().Query().Return(repository.query).Once() |
| 74 | + |
| 75 | + s.NoError(repository.CreateRepository()) |
| 76 | + } |
| 77 | + |
| 78 | + err := repository.Log("migration1", 1) |
| 79 | + s.NoError(err) |
| 80 | + |
| 81 | + err = repository.Log("migration2", 1) |
| 82 | + s.NoError(err) |
| 83 | + |
| 84 | + err = repository.Log("migration3", 2) |
| 85 | + s.NoError(err) |
| 86 | + |
| 87 | + lastBatchNumber, err := repository.getLastBatchNumber() |
| 88 | + s.NoError(err) |
| 89 | + s.Equal(2, lastBatchNumber) |
| 90 | + |
| 91 | + nextBatchNumber, err := repository.GetNextBatchNumber() |
| 92 | + s.NoError(err) |
| 93 | + s.Equal(3, nextBatchNumber) |
| 94 | + |
| 95 | + ranMigrations, err := repository.GetRan() |
| 96 | + s.NoError(err) |
| 97 | + s.ElementsMatch([]string{"migration1", "migration2", "migration3"}, ranMigrations) |
| 98 | + |
| 99 | + migrations, err := repository.GetMigrations(2) |
| 100 | + s.NoError(err) |
| 101 | + s.Len(migrations, 2) |
| 102 | + s.Equal("migration3", migrations[0].Migration) |
| 103 | + s.Equal(2, migrations[0].Batch) |
| 104 | + s.Equal("migration2", migrations[1].Migration) |
| 105 | + s.Equal(1, migrations[1].Batch) |
| 106 | + |
| 107 | + migrations, err = repository.GetMigrationsByBatch(1) |
| 108 | + s.NoError(err) |
| 109 | + s.Len(migrations, 2) |
| 110 | + s.Equal("migration2", migrations[0].Migration) |
| 111 | + s.Equal(1, migrations[0].Batch) |
| 112 | + s.Equal("migration1", migrations[1].Migration) |
| 113 | + s.Equal(1, migrations[1].Batch) |
| 114 | + |
| 115 | + migrations, err = repository.GetLast() |
| 116 | + s.NoError(err) |
| 117 | + s.Len(migrations, 1) |
| 118 | + s.Equal("migration3", migrations[0].Migration) |
| 119 | + s.Equal(2, migrations[0].Batch) |
| 120 | + |
| 121 | + err = repository.Delete("migration1") |
| 122 | + s.NoError(err) |
| 123 | + |
| 124 | + ranMigrations, err = repository.GetRan() |
| 125 | + s.NoError(err) |
| 126 | + s.ElementsMatch([]string{"migration2", "migration3"}, ranMigrations) |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func (s *RepositoryTestSuite) initRepository(testQuery *gorm.TestQuery) (*Repository, *mocksorm.Orm) { |
| 132 | + schema, mockOrm := initSchema(s.T(), testQuery) |
| 133 | + |
| 134 | + return NewRepository(testQuery.Query(), schema, "migrations"), mockOrm |
| 135 | +} |
0 commit comments