-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from bigboss2063/master
ref(database): improve database initialization and testing
- Loading branch information
Showing
10 changed files
with
183 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package constant | ||
|
||
import "time" | ||
|
||
// all the databases share this settings | ||
const ( | ||
DatabaseHost = "0.0.0.0" | ||
DatabaseName = "cdc-observer" | ||
DatabaseUsername = "root" | ||
DatabasePassword = "cdc-observer-password" | ||
) | ||
|
||
// retry times and interval for the database connection | ||
const ( | ||
RetryTimes = 10 | ||
RetryInterval = 1 * time.Second | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package constant | ||
|
||
// the prefix of the name of the cdc-observer containers, like /cdc-observer-mysql, /cdc-observer-pgsql | ||
const ContainerNamePrefix = "cdc-observer-" | ||
|
||
// the image name of the database instance in the container | ||
const MysqlImageName = "mysql" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,56 @@ | ||
package database | ||
|
||
import "testing" | ||
import ( | ||
"cdc-observer/constant" | ||
dockerapi "cdc-observer/docker_api" | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewDatabaseAndAddNewTable(t *testing.T) { | ||
db, err := NewDatabase("elliot_test_database", "127.0.0.1", 3307, "root", "123456") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
table, err := NewTableBuilder("test_table", db.dbClient).AddFieldInt("test_field_int").AddFieldVarchar("test_field_string").Submit() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// Create a new Docker client | ||
dockerClient, err := dockerapi.NewDockerClient() | ||
assert.NoError(t, err, "Failed to create Docker client") | ||
|
||
// Start the MySQL container and get the assigned port | ||
ctx := context.Background() | ||
err = dockerClient.StartMySQLContainer(ctx) | ||
assert.NoError(t, err, "Failed to start MySQL container") | ||
defer func() { | ||
dockerClient.StopAllContainers(ctx) | ||
dockerClient.RemoveAllContainers(ctx) | ||
}() | ||
|
||
containerName := dockerClient.ContainerName(constant.MysqlImageName) | ||
port, err := dockerClient.ContainerPort(ctx, containerName) | ||
assert.NoError(t, err, "Failed to get MySQL container port") | ||
|
||
// Initialize the database | ||
db, err := NewDatabase(port) | ||
assert.NoError(t, err, "Failed to create new database") | ||
|
||
// Create a new table | ||
table, err := NewTableBuilder("test_table", db.dbClient). | ||
AddFieldInt("test_field_int"). | ||
AddFieldVarchar("test_field_string"). | ||
Submit() | ||
assert.NoError(t, err, "Failed to create new table") | ||
|
||
// Add the table to the database | ||
err = db.AddTable(table) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.NoError(t, err, "Failed to add table to database") | ||
|
||
// Apply changes | ||
err = db.Apply() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
r := NewRowBuilder().AddField("test_field_int", int64(1)).AddField("test_field_string", "test string").Submit() | ||
assert.NoError(t, err, "Failed to apply changes") | ||
|
||
// Add a row to the table | ||
r := NewRowBuilder(). | ||
AddField("test_field_int", int64(1)). | ||
AddField("test_field_string", "test string"). | ||
Submit() | ||
table.AddRow(r) | ||
assert.NoError(t, err, "Failed to add row to table") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dockerapi | ||
|
||
import ( | ||
"cdc-observer/constant" | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStartMySQLContainer(t *testing.T) { | ||
dockerClient, err := NewDockerClient() | ||
assert.NoError(t, err, "Failed to create Docker client") | ||
|
||
ctx := context.Background() | ||
err = dockerClient.StartMySQLContainer(ctx) | ||
assert.NoError(t, err, "Failed to start MySQL container") | ||
|
||
defer func() { | ||
dockerClient.StopAllContainers(ctx) | ||
dockerClient.RemoveAllContainers(ctx) | ||
}() | ||
|
||
containerName := dockerClient.ContainerName(constant.MysqlImageName) | ||
cj, err := dockerClient.ContainerInfo(ctx, containerName) | ||
assert.NoError(t, err, "Failed to get MySQL container info") | ||
assert.True(t, cj.State.Running, "MySQL container should be running") | ||
|
||
dockerClient.StopAllContainers(ctx) | ||
cj, err = dockerClient.ContainerInfo(ctx, containerName) | ||
assert.NoError(t, err, "Failed to get MySQL container info") | ||
assert.False(t, cj.State.Running, "MySQL container should be stopped") | ||
} |
Oops, something went wrong.