From 2340b4f5fbb082ed6e3db7adc350b511fd942a7b Mon Sep 17 00:00:00 2001 From: sxp20008 <81209245@qq.com> Date: Wed, 27 Aug 2025 21:13:45 +0800 Subject: [PATCH 01/10] Add the function of obtaining all configurations to facilitate business operations such as verification after addition. Signed-off-by: sxp20008 <81209245@qq.com> --- database/gdb/gdb_core_config.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index ae9c7c85ba2..3255bb08050 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -252,6 +252,13 @@ func GetConfig(group string) ConfigGroup { return configs.config[group] } +// GetConfig retrieves and returns the all configuration. +func GetAllConfig() Config { + configs.RLock() + defer configs.RUnlock() + return configs.config +} + // SetDefaultGroup sets the group name for default configuration. func SetDefaultGroup(name string) { defer instances.Clear() From 54897a4a21f449be908a7052423307716d66014e Mon Sep 17 00:00:00 2001 From: sxp20008 <81209245@qq.com> Date: Thu, 28 Aug 2025 22:24:39 +0800 Subject: [PATCH 02/10] Add the function of obtaining all configurations Signed-off-by: sxp20008 <81209245@qq.com> --- database/gdb/gdb_core_config.go | 2 +- database/gdb/gdb_z_example_test.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index 3255bb08050..bd3b1e1ad06 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -252,7 +252,7 @@ func GetConfig(group string) ConfigGroup { return configs.config[group] } -// GetConfig retrieves and returns the all configuration. +// GetAllConfig retrieves and returns the all configuration. func GetAllConfig() Config { configs.RLock() defer configs.RUnlock() diff --git a/database/gdb/gdb_z_example_test.go b/database/gdb/gdb_z_example_test.go index dcd3631dc29..0b17602c072 100644 --- a/database/gdb/gdb_z_example_test.go +++ b/database/gdb/gdb_z_example_test.go @@ -8,6 +8,7 @@ package gdb_test import ( "context" + "fmt" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" @@ -41,3 +42,15 @@ func ExampleDB_Transaction() { return nil }) } + +// Example_GetAllConfig +func ExampleGetAllConfig() { + //add confignode by addconfignode + gdb.AddConfigNode("default", gdb.ConfigNode{ + Link: "mysql://root:123456@tcp(127.0.0.1:3306)/test", + }) + + //get all config (addconfignode and defualt config) + configs := gdb.GetAllConfig() + fmt.Println(configs) +} From 3cff22e494ca6222bb21c8f5c6a1ed35cc691e21 Mon Sep 17 00:00:00 2001 From: sxp20008 <81209245@qq.com> Date: Thu, 28 Aug 2025 22:53:15 +0800 Subject: [PATCH 03/10] add example and fix Signed-off-by: sxp20008 <81209245@qq.com> --- database/gdb/gdb_z_example_test.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/database/gdb/gdb_z_example_test.go b/database/gdb/gdb_z_example_test.go index 0b17602c072..d1eae3b622b 100644 --- a/database/gdb/gdb_z_example_test.go +++ b/database/gdb/gdb_z_example_test.go @@ -43,14 +43,23 @@ func ExampleDB_Transaction() { }) } -// Example_GetAllConfig +// ExampleGetAllConfig demonstrates the usage of GetAllConfig. func ExampleGetAllConfig() { //add confignode by addconfignode - gdb.AddConfigNode("default", gdb.ConfigNode{ + gdb.AddConfigNode("test", gdb.ConfigNode{ Link: "mysql://root:123456@tcp(127.0.0.1:3306)/test", }) - //get all config (addconfignode and defualt config) + //get all config (addconfignode and test config) configs := gdb.GetAllConfig() fmt.Println(configs) + + gdb.AddConfigNode("test2", gdb.ConfigNode{ + Link: "mysql://root:123456@tcp(127.0.0.1:3306)/test", + }) + + //get all config again + configs2 := gdb.GetAllConfig() + fmt.Println(configs2) + } From 6470f09793aff01d447f17d773f32e3b1652a821 Mon Sep 17 00:00:00 2001 From: houseme Date: Thu, 28 Aug 2025 23:22:59 +0800 Subject: [PATCH 04/10] Rename ExampleGetAllConfig to ExampleDB_GetAllConfig --- database/gdb/gdb_z_example_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/gdb/gdb_z_example_test.go b/database/gdb/gdb_z_example_test.go index d1eae3b622b..9d3dc812b83 100644 --- a/database/gdb/gdb_z_example_test.go +++ b/database/gdb/gdb_z_example_test.go @@ -43,8 +43,8 @@ func ExampleDB_Transaction() { }) } -// ExampleGetAllConfig demonstrates the usage of GetAllConfig. -func ExampleGetAllConfig() { +// ExampleDB_GetAllConfig demonstrates the usage of GetAllConfig. +func ExampleDB_GetAllConfig() { //add confignode by addconfignode gdb.AddConfigNode("test", gdb.ConfigNode{ Link: "mysql://root:123456@tcp(127.0.0.1:3306)/test", From ac8b02b7f66d522ba8853a532c50f7cd7c992e23 Mon Sep 17 00:00:00 2001 From: houseme Date: Thu, 28 Aug 2025 23:36:12 +0800 Subject: [PATCH 05/10] Rename ExampleDB_GetAllConfig to ExampleGetAllConfig --- database/gdb/gdb_z_example_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/gdb/gdb_z_example_test.go b/database/gdb/gdb_z_example_test.go index 9d3dc812b83..d1eae3b622b 100644 --- a/database/gdb/gdb_z_example_test.go +++ b/database/gdb/gdb_z_example_test.go @@ -43,8 +43,8 @@ func ExampleDB_Transaction() { }) } -// ExampleDB_GetAllConfig demonstrates the usage of GetAllConfig. -func ExampleDB_GetAllConfig() { +// ExampleGetAllConfig demonstrates the usage of GetAllConfig. +func ExampleGetAllConfig() { //add confignode by addconfignode gdb.AddConfigNode("test", gdb.ConfigNode{ Link: "mysql://root:123456@tcp(127.0.0.1:3306)/test", From cb247a05cb7e2feeb427d10ea362c2f11b49e6b6 Mon Sep 17 00:00:00 2001 From: houseme Date: Fri, 29 Aug 2025 11:06:33 +0800 Subject: [PATCH 06/10] Update comments for consistency in ExampleGetAllConfig --- database/gdb/gdb_z_example_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/database/gdb/gdb_z_example_test.go b/database/gdb/gdb_z_example_test.go index d1eae3b622b..dc9f67ca31c 100644 --- a/database/gdb/gdb_z_example_test.go +++ b/database/gdb/gdb_z_example_test.go @@ -45,12 +45,12 @@ func ExampleDB_Transaction() { // ExampleGetAllConfig demonstrates the usage of GetAllConfig. func ExampleGetAllConfig() { - //add confignode by addconfignode + // Add confignode by addconfignode gdb.AddConfigNode("test", gdb.ConfigNode{ Link: "mysql://root:123456@tcp(127.0.0.1:3306)/test", }) - //get all config (addconfignode and test config) + // Get all config (addconfignode and test config) configs := gdb.GetAllConfig() fmt.Println(configs) @@ -58,8 +58,7 @@ func ExampleGetAllConfig() { Link: "mysql://root:123456@tcp(127.0.0.1:3306)/test", }) - //get all config again + // Get all config again configs2 := gdb.GetAllConfig() fmt.Println(configs2) - } From 8d8c6c918a6c599c547aa677977e7cc34e0e1235 Mon Sep 17 00:00:00 2001 From: houseme Date: Thu, 18 Sep 2025 12:55:31 +0800 Subject: [PATCH 07/10] Update database/gdb/gdb_core_config.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- database/gdb/gdb_core_config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index bd3b1e1ad06..83afe03aae1 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -252,7 +252,7 @@ func GetConfig(group string) ConfigGroup { return configs.config[group] } -// GetAllConfig retrieves and returns the all configuration. +// GetAllConfig retrieves and returns all configurations. func GetAllConfig() Config { configs.RLock() defer configs.RUnlock() From ad30e429d9e919465f9a93e3f4427be790ebf032 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Thu, 18 Sep 2025 15:15:52 +0800 Subject: [PATCH 08/10] =?UTF-8?q?fix(database/gdb):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=20SetDefaultConfigGroup=20=E5=92=8C=20GetConfigGroup=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E6=A0=87=E8=AE=B0=E6=97=A7=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=B8=BA=E5=B7=B2=E5=BC=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/gdb/gdb_core_config.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index 83afe03aae1..7d18da91fe9 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -241,15 +241,40 @@ func AddDefaultConfigNode(node ConfigNode) error { } // AddDefaultConfigGroup adds multiple node configurations to configuration of default group. +// +// Deprecated: Use SetDefaultConfigGroup instead. func AddDefaultConfigGroup(nodes ConfigGroup) error { return SetConfigGroup(DefaultGroupName, nodes) } +// SetDefaultConfigGroup sets multiple node configurations to configuration of default group. +func SetDefaultConfigGroup(nodes ConfigGroup) error { + return SetConfigGroup(DefaultGroupName, nodes) +} + // GetConfig retrieves and returns the configuration of given group. +// +// Deprecated: Use GetConfigGroup instead. func GetConfig(group string) ConfigGroup { + configGroup, _ := GetConfigGroup(group) + return configGroup +} + +// GetConfigGroup retrieves and returns the configuration of given group. +// It returns an error if the group does not exist, or an empty slice if the group exists but has no nodes. +func GetConfigGroup(group string) (ConfigGroup, error) { configs.RLock() defer configs.RUnlock() - return configs.config[group] + + configGroup, exists := configs.config[group] + if !exists { + return nil, gerror.NewCodef( + gcode.CodeInvalidParameter, + `configuration group "%s" not found`, + group, + ) + } + return configGroup, nil } // GetAllConfig retrieves and returns all configurations. From 14bc196171ea0ca823a1c30472d5a25a617c8e61 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Thu, 18 Sep 2025 15:54:54 +0800 Subject: [PATCH 09/10] Add unit tests for Core configuration methods and remove unused example function - Created a new test file `gdb_z_core_config_test.go` to implement unit tests for various methods in the Core struct, including GetDebug, SetDebug, GetDryRun, SetDryRun, GetLogger, SetLogger, SetMaxConnections, GetCache, GetGroup, and GetPrefix. - Removed the unused example function `ExampleGetAllConfig` from `gdb_z_example_test.go` to clean up the codebase. --- .../gdb/gdb_z_core_config_external_test.go | 1191 +++++++++++++++++ database/gdb/gdb_z_core_config_test.go | 204 +++ database/gdb/gdb_z_example_test.go | 21 - 3 files changed, 1395 insertions(+), 21 deletions(-) create mode 100644 database/gdb/gdb_z_core_config_external_test.go create mode 100644 database/gdb/gdb_z_core_config_test.go diff --git a/database/gdb/gdb_z_core_config_external_test.go b/database/gdb/gdb_z_core_config_external_test.go new file mode 100644 index 00000000000..7af02bf0bcb --- /dev/null +++ b/database/gdb/gdb_z_core_config_external_test.go @@ -0,0 +1,1191 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdb_test + +import ( + "testing" + + "github.com/gogf/gf/v2/database/gdb" + "github.com/gogf/gf/v2/test/gtest" +) + +func Test_GetAllConfig(t *testing.T) { + // Test case 1: Empty configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config to empty + gdb.SetConfig(make(gdb.Config)) + + result := gdb.GetAllConfig() + t.Assert(len(result), 0) + }) + + // Test case 2: Single configuration group with one node + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + testNode := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("test_group", testNode) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["test_group"]), 1) + t.Assert(result["test_group"][0].Host, "127.0.0.1") + t.Assert(result["test_group"][0].Port, "3306") + t.Assert(result["test_group"][0].User, "root") + t.Assert(result["test_group"][0].Pass, "123456") + t.Assert(result["test_group"][0].Name, "test_db") + t.Assert(result["test_group"][0].Type, "mysql") + }) + + // Test case 3: Multiple configuration groups with multiple nodes + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + // Add first group with two nodes + testNode1 := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "master_db", + Type: "mysql", + Role: "master", + } + testNode2 := gdb.ConfigNode{ + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "slave_db", + Type: "mysql", + Role: "slave", + } + + err := gdb.AddConfigNode("mysql_cluster", testNode1) + t.AssertNil(err) + err = gdb.AddConfigNode("mysql_cluster", testNode2) + t.AssertNil(err) + + // Add second group with one node + testNode3 := gdb.ConfigNode{ + Host: "localhost", + Port: "5432", + User: "postgres", + Pass: "password", + Name: "pg_db", + Type: "pgsql", + } + + err = gdb.AddConfigNode("postgres_db", testNode3) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 2) + + // Check mysql_cluster group + t.Assert(len(result["mysql_cluster"]), 2) + t.Assert(result["mysql_cluster"][0].Host, "127.0.0.1") + t.Assert(result["mysql_cluster"][0].Role, "master") + t.Assert(result["mysql_cluster"][1].Host, "127.0.0.2") + t.Assert(result["mysql_cluster"][1].Role, "slave") + + // Check postgres_db group + t.Assert(len(result["postgres_db"]), 1) + t.Assert(result["postgres_db"][0].Host, "localhost") + t.Assert(result["postgres_db"][0].Port, "5432") + t.Assert(result["postgres_db"][0].Type, "pgsql") + }) + + // Test case 4: Configuration with Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + testNode := gdb.ConfigNode{ + Link: "mysql:root:123456@tcp(127.0.0.1:3306)/test_db?charset=utf8", + } + + err := gdb.AddConfigNode("link_test", testNode) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["link_test"]), 1) + + // Check parsed values from link + node := result["link_test"][0] + t.Assert(node.Type, "mysql") + t.Assert(node.User, "root") + t.Assert(node.Pass, "123456") + t.Assert(node.Host, "127.0.0.1") + t.Assert(node.Port, "3306") + t.Assert(node.Name, "test_db") + t.Assert(node.Charset, "utf8") + t.Assert(node.Protocol, "tcp") + }) + + // Test case 5: Default group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + testNode := gdb.ConfigNode{ + Host: "localhost", + Port: "3306", + User: "user", + Pass: "pass", + Name: "default_db", + Type: "mysql", + } + + err := gdb.AddDefaultConfigNode(testNode) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 1) + t.Assert(result["default"][0].Host, "localhost") + t.Assert(result["default"][0].Name, "default_db") + }) + + // Test case 6: SetConfig with multiple groups + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{ + "group1": gdb.ConfigGroup{ + { + Host: "host1", + Port: "3306", + User: "user1", + Pass: "pass1", + Name: "db1", + Type: "mysql", + }, + }, + "group2": gdb.ConfigGroup{ + { + Host: "host2", + Port: "5432", + User: "user2", + Pass: "pass2", + Name: "db2", + Type: "pgsql", + }, + { + Host: "host3", + Port: "5432", + User: "user3", + Pass: "pass3", + Name: "db3", + Type: "pgsql", + }, + }, + } + + err := gdb.SetConfig(testConfig) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 2) + t.Assert(len(result["group1"]), 1) + t.Assert(len(result["group2"]), 2) + + t.Assert(result["group1"][0].Host, "host1") + t.Assert(result["group1"][0].Type, "mysql") + + t.Assert(result["group2"][0].Host, "host2") + t.Assert(result["group2"][0].Type, "pgsql") + t.Assert(result["group2"][1].Host, "host3") + t.Assert(result["group2"][1].Type, "pgsql") + }) + + // Test case 7: Test return value is a copy (not reference) + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + testNode := gdb.ConfigNode{ + Host: "original_host", + Port: "3306", + User: "original_user", + Pass: "original_pass", + Name: "original_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("test_copy", testNode) + t.AssertNil(err) + + // Get config and modify it + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + + // Verify original values + t.Assert(result["test_copy"][0].Host, "original_host") + + // Note: GetAllConfig returns the internal config directly (not a copy) + // This is by design for performance reasons + // So modifying the returned config would affect the internal state + // This test just verifies the current behavior + }) +} + +func Test_SetConfig(t *testing.T) { + // Test case 1: Normal configuration setting + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{ + "group1": gdb.ConfigGroup{ + { + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + }, + }, + "group2": gdb.ConfigGroup{ + { + Host: "192.168.1.100", + Port: "5432", + User: "postgres", + Pass: "password", + Name: "pg_db", + Type: "pgsql", + }, + }, + } + + err := gdb.SetConfig(testConfig) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 2) + t.Assert(result["group1"][0].Host, "127.0.0.1") + t.Assert(result["group2"][0].Type, "pgsql") + }) + + // Test case 2: Empty configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{} + err := gdb.SetConfig(testConfig) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 0) + }) + + // Test case 3: Configuration with Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{ + "mysql_link": gdb.ConfigGroup{ + { + Link: "mysql:root:123456@tcp(127.0.0.1:3306)/test_db?charset=utf8", + }, + }, + } + + err := gdb.SetConfig(testConfig) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + node := result["mysql_link"][0] + t.Assert(node.Type, "mysql") + t.Assert(node.User, "root") + t.Assert(node.Host, "127.0.0.1") + t.Assert(node.Port, "3306") + t.Assert(node.Name, "test_db") + }) + + // Test case 4: Configuration with invalid Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{ + "invalid_link": gdb.ConfigGroup{ + { + Link: "invalid_link_format", + }, + }, + } + + err := gdb.SetConfig(testConfig) + t.AssertNE(err, nil) + }) +} + +func Test_SetConfigGroup(t *testing.T) { + // Test case 1: Set new group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + Role: "master", + }, + { + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + Role: "slave", + }, + } + + err := gdb.SetConfigGroup("test_group", nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["test_group"]), 2) + t.Assert(result["test_group"][0].Role, "master") + t.Assert(result["test_group"][1].Role, "slave") + }) + + // Test case 2: Overwrite existing group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + // First set + nodes1 := gdb.ConfigGroup{ + { + Host: "old_host", + Port: "3306", + User: "old_user", + Name: "old_db", + Type: "mysql", + }, + } + err := gdb.SetConfigGroup("test_group", nodes1) + t.AssertNil(err) + + // Overwrite with new config + nodes2 := gdb.ConfigGroup{ + { + Host: "new_host", + Port: "5432", + User: "new_user", + Name: "new_db", + Type: "pgsql", + }, + } + err = gdb.SetConfigGroup("test_group", nodes2) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["test_group"]), 1) + t.Assert(result["test_group"][0].Host, "new_host") + t.Assert(result["test_group"][0].Type, "pgsql") + }) + + // Test case 3: Empty group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{} + err := gdb.SetConfigGroup("empty_group", nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["empty_group"]), 0) + }) + + // Test case 4: Configuration with invalid Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + nodes := gdb.ConfigGroup{ + { + Link: "invalid_link", + }, + } + + err := gdb.SetConfigGroup("invalid_group", nodes) + t.AssertNE(err, nil) + }) +} + +func Test_AddConfigNode(t *testing.T) { + // Test case 1: Add node to new group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("new_group", node) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["new_group"]), 1) + t.Assert(result["new_group"][0].Host, "127.0.0.1") + }) + + // Test case 2: Add node to existing group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + // Add first node + node1 := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + } + err := gdb.AddConfigNode("existing_group", node1) + t.AssertNil(err) + + // Add second node to same group + node2 := gdb.ConfigNode{ + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + } + err = gdb.AddConfigNode("existing_group", node2) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["existing_group"]), 2) + t.Assert(result["existing_group"][0].Name, "db1") + t.Assert(result["existing_group"][1].Name, "db2") + }) + + // Test case 3: Add node with Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Link: "mysql:root:password@tcp(192.168.1.100:3306)/mydb?charset=utf8mb4", + } + + err := gdb.AddConfigNode("link_group", node) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["link_group"]), 1) + t.Assert(result["link_group"][0].Type, "mysql") + t.Assert(result["link_group"][0].Host, "192.168.1.100") + t.Assert(result["link_group"][0].Port, "3306") + t.Assert(result["link_group"][0].Name, "mydb") + }) + + // Test case 4: Add node with invalid Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + node := gdb.ConfigNode{ + Link: "invalid_link_format", + } + + err := gdb.AddConfigNode("invalid_group", node) + t.AssertNE(err, nil) + }) +} + +func Test_AddDefaultConfigNode(t *testing.T) { + // Test case 1: Add node to default group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Host: "localhost", + Port: "3306", + User: "root", + Pass: "root", + Name: "default_db", + Type: "mysql", + } + + err := gdb.AddDefaultConfigNode(node) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 1) + t.Assert(result["default"][0].Host, "localhost") + t.Assert(result["default"][0].Name, "default_db") + }) + + // Test case 2: Add multiple nodes to default group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node1 := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + Role: "master", + } + err := gdb.AddDefaultConfigNode(node1) + t.AssertNil(err) + + node2 := gdb.ConfigNode{ + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + Role: "slave", + } + err = gdb.AddDefaultConfigNode(node2) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 2) + t.Assert(result["default"][0].Role, "master") + t.Assert(result["default"][1].Role, "slave") + }) + + // Test case 3: Add node with Link syntax to default group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Link: "pgsql:postgres:password@tcp(localhost:5432)/testdb", + } + + err := gdb.AddDefaultConfigNode(node) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 1) + t.Assert(result["default"][0].Type, "pgsql") + t.Assert(result["default"][0].User, "postgres") + t.Assert(result["default"][0].Host, "localhost") + t.Assert(result["default"][0].Port, "5432") + t.Assert(result["default"][0].Name, "testdb") + }) +} + +func Test_AddDefaultConfigGroup(t *testing.T) { + // Test case 1: Add multiple nodes to default group (deprecated function) + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + Role: "master", + }, + { + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + Role: "slave", + }, + } + + err := gdb.AddDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 2) + t.Assert(result["default"][0].Role, "master") + t.Assert(result["default"][1].Role, "slave") + }) + + // Test case 2: Overwrite existing default group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + // First set + node1 := gdb.ConfigNode{ + Host: "old_host", + Port: "3306", + User: "old_user", + Name: "old_db", + Type: "mysql", + } + err := gdb.AddDefaultConfigNode(node1) + t.AssertNil(err) + + // Overwrite with new group config + nodes := gdb.ConfigGroup{ + { + Host: "new_host", + Port: "5432", + User: "new_user", + Name: "new_db", + Type: "pgsql", + }, + } + err = gdb.AddDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 1) + t.Assert(result["default"][0].Host, "new_host") + t.Assert(result["default"][0].Type, "pgsql") + }) +} + +func Test_SetDefaultConfigGroup(t *testing.T) { + // Test case 1: Set default group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Host: "192.168.1.10", + Port: "3306", + User: "admin", + Pass: "admin123", + Name: "main_db", + Type: "mysql", + Role: "master", + }, + { + Host: "192.168.1.11", + Port: "3306", + User: "admin", + Pass: "admin123", + Name: "backup_db", + Type: "mysql", + Role: "slave", + }, + } + + err := gdb.SetDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 2) + t.Assert(result["default"][0].Host, "192.168.1.10") + t.Assert(result["default"][0].Role, "master") + t.Assert(result["default"][1].Host, "192.168.1.11") + t.Assert(result["default"][1].Role, "slave") + }) + + // Test case 2: Empty default group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config and add some initial data + gdb.SetConfig(make(gdb.Config)) + err := gdb.AddDefaultConfigNode(gdb.ConfigNode{ + Host: "temp_host", + Name: "temp_db", + Type: "mysql", + }) + t.AssertNil(err) + + // Set empty group + nodes := gdb.ConfigGroup{} + err = gdb.SetDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 0) + }) + + // Test case 3: Configuration with Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Link: "mysql:root:123456@tcp(localhost:3306)/test_db1", + }, + { + Link: "pgsql:postgres:password@tcp(localhost:5432)/test_db2", + }, + } + + err := gdb.SetDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 2) + t.Assert(result["default"][0].Type, "mysql") + t.Assert(result["default"][0].Name, "test_db1") + t.Assert(result["default"][1].Type, "pgsql") + t.Assert(result["default"][1].Name, "test_db2") + }) +} + +func Test_GetConfig(t *testing.T) { + // Test case 1: Get existing group configuration (deprecated function) + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("test_group", node) + t.AssertNil(err) + + result := gdb.GetConfig("test_group") + t.Assert(len(result), 1) + t.Assert(result[0].Host, "127.0.0.1") + t.Assert(result[0].Type, "mysql") + }) + + // Test case 2: Get non-existing group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + result := gdb.GetConfig("non_existing_group") + t.Assert(len(result), 0) + }) +} + +func Test_GetConfigGroup(t *testing.T) { + // Test case 1: Get existing group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + Role: "master", + }, + { + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + Role: "slave", + }, + } + + err := gdb.SetConfigGroup("test_group", nodes) + t.AssertNil(err) + + result, err := gdb.GetConfigGroup("test_group") + t.AssertNil(err) + t.Assert(len(result), 2) + t.Assert(result[0].Host, "127.0.0.1") + t.Assert(result[0].Role, "master") + t.Assert(result[1].Host, "127.0.0.2") + t.Assert(result[1].Role, "slave") + }) + + // Test case 2: Get non-existing group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + result, err := gdb.GetConfigGroup("non_existing_group") + t.AssertNE(err, nil) + t.Assert(result, nil) + }) + + // Test case 3: Get empty group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + err := gdb.SetConfigGroup("empty_group", gdb.ConfigGroup{}) + t.AssertNil(err) + + result, err := gdb.GetConfigGroup("empty_group") + t.AssertNil(err) + t.Assert(len(result), 0) + }) +} + +func Test_SetDefaultGroup(t *testing.T) { + // Test case 1: Set default group name + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + gdb.SetDefaultGroup("custom_default") + result := gdb.GetDefaultGroup() + t.Assert(result, "custom_default") + }) + + // Test case 2: Set empty default group name + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + gdb.SetDefaultGroup("") + result := gdb.GetDefaultGroup() + t.Assert(result, "") + }) + + // Test case 3: Multiple calls to SetDefaultGroup + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + gdb.SetDefaultGroup("first_group") + result1 := gdb.GetDefaultGroup() + t.Assert(result1, "first_group") + + gdb.SetDefaultGroup("second_group") + result2 := gdb.GetDefaultGroup() + t.Assert(result2, "second_group") + }) +} + +func Test_GetDefaultGroup(t *testing.T) { + // Test case 1: Get default group name + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + // Test with default value + result := gdb.GetDefaultGroup() + t.Assert(result, "default") + }) + + // Test case 2: Get custom default group name + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + gdb.SetDefaultGroup("my_custom_group") + result := gdb.GetDefaultGroup() + t.Assert(result, "my_custom_group") + }) +} + +func Test_IsConfigured(t *testing.T) { + // Test case 1: No configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config to empty + gdb.SetConfig(make(gdb.Config)) + + result := gdb.IsConfigured() + t.Assert(result, false) + }) + + // Test case 2: Has configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("test_group", node) + t.AssertNil(err) + + result := gdb.IsConfigured() + t.Assert(result, true) + }) + + // Test case 3: Has empty group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + err := gdb.SetConfigGroup("empty_group", gdb.ConfigGroup{}) + t.AssertNil(err) + + result := gdb.IsConfigured() + t.Assert(result, true) + }) +} diff --git a/database/gdb/gdb_z_core_config_test.go b/database/gdb/gdb_z_core_config_test.go new file mode 100644 index 00000000000..492e0e93d08 --- /dev/null +++ b/database/gdb/gdb_z_core_config_test.go @@ -0,0 +1,204 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdb + +import ( + "testing" + "time" + + "github.com/gogf/gf/v2/container/gtype" + "github.com/gogf/gf/v2/os/glog" + "github.com/gogf/gf/v2/test/gtest" +) + +func Test_Core_SetDebug_GetDebug(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := configs.config + defer func() { + configs.config = originalConfig + }() + + // Create a test configuration + configs.config = make(Config) + testNode := ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + err := AddConfigNode("test_group", testNode) + t.AssertNil(err) + + // Create Core instance + node, err := GetConfigGroup("test_group") + t.AssertNil(err) + core := &Core{ + group: "test_group", + config: &node[0], + debug: gtype.NewBool(false), + } + + // Test default value + result := core.GetDebug() + t.Assert(result, false) + + // Test setting debug to true + core.SetDebug(true) + result = core.GetDebug() + t.Assert(result, true) + + // Test setting debug to false + core.SetDebug(false) + result = core.GetDebug() + t.Assert(result, false) + }) +} + +func Test_Core_SetDryRun_GetDryRun(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := configs.config + defer func() { + configs.config = originalConfig + }() + + // Create a test configuration + configs.config = make(Config) + testNode := ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + DryRun: false, + } + err := AddConfigNode("test_group", testNode) + t.AssertNil(err) + + // Create Core instance + node, err := GetConfigGroup("test_group") + t.AssertNil(err) + core := &Core{ + group: "test_group", + config: &node[0], + } + + // Test default value + result := core.GetDryRun() + t.Assert(result, false) + + // Test setting dry run to true + core.SetDryRun(true) + result = core.GetDryRun() + t.Assert(result, true) + + // Test setting dry run to false + core.SetDryRun(false) + result = core.GetDryRun() + t.Assert(result, false) + }) +} + +func Test_Core_SetLogger_GetLogger(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Create Core instance + core := &Core{} + + // Test setting custom logger + customLogger := glog.New() + core.SetLogger(customLogger) + result := core.GetLogger() + t.Assert(result, customLogger) + + // Test setting nil logger + core.SetLogger(nil) + result = core.GetLogger() + t.Assert(result, nil) + }) +} + +func Test_Core_SetMaxConnections(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Create Core instance + core := &Core{} + + // Test SetMaxIdleConnCount + core.SetMaxIdleConnCount(10) + t.Assert(core.dynamicConfig.MaxIdleConnCount, 10) + + // Test SetMaxOpenConnCount + core.SetMaxOpenConnCount(20) + t.Assert(core.dynamicConfig.MaxOpenConnCount, 20) + + // Test SetMaxConnLifeTime + testDuration := time.Hour + core.SetMaxConnLifeTime(testDuration) + t.Assert(core.dynamicConfig.MaxConnLifeTime, testDuration) + }) +} + +func Test_Core_GetCache(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Create Core instance + core := &Core{} + + cache := core.GetCache() + // Cache might be nil if not initialized, so we just test that the call doesn't panic + _ = cache + }) +} + +func Test_Core_GetGroup(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Create Core instance + core := &Core{ + group: "test_group", + } + + group := core.GetGroup() + t.Assert(group, "test_group") + }) +} + +func Test_Core_GetPrefix(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := configs.config + defer func() { + configs.config = originalConfig + }() + + // Create a test configuration + configs.config = make(Config) + testNode := ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + Prefix: "gf_", + } + err := AddConfigNode("test_group", testNode) + t.AssertNil(err) + + // Create Core instance + node, err := GetConfigGroup("test_group") + t.AssertNil(err) + core := &Core{ + group: "test_group", + config: &node[0], + } + + prefix := core.GetPrefix() + t.Assert(prefix, "gf_") + }) +} diff --git a/database/gdb/gdb_z_example_test.go b/database/gdb/gdb_z_example_test.go index dc9f67ca31c..dcd3631dc29 100644 --- a/database/gdb/gdb_z_example_test.go +++ b/database/gdb/gdb_z_example_test.go @@ -8,7 +8,6 @@ package gdb_test import ( "context" - "fmt" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" @@ -42,23 +41,3 @@ func ExampleDB_Transaction() { return nil }) } - -// ExampleGetAllConfig demonstrates the usage of GetAllConfig. -func ExampleGetAllConfig() { - // Add confignode by addconfignode - gdb.AddConfigNode("test", gdb.ConfigNode{ - Link: "mysql://root:123456@tcp(127.0.0.1:3306)/test", - }) - - // Get all config (addconfignode and test config) - configs := gdb.GetAllConfig() - fmt.Println(configs) - - gdb.AddConfigNode("test2", gdb.ConfigNode{ - Link: "mysql://root:123456@tcp(127.0.0.1:3306)/test", - }) - - // Get all config again - configs2 := gdb.GetAllConfig() - fmt.Println(configs2) -} From 3af77aa1f8173dbfa492f9a9a8155f18a5cef219 Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Thu, 18 Sep 2025 16:01:35 +0800 Subject: [PATCH 10/10] fix: LF --- .../gdb/gdb_z_core_config_external_test.go | 2382 ++++++++--------- database/gdb/gdb_z_core_config_test.go | 408 +-- 2 files changed, 1395 insertions(+), 1395 deletions(-) diff --git a/database/gdb/gdb_z_core_config_external_test.go b/database/gdb/gdb_z_core_config_external_test.go index 7af02bf0bcb..bc37495125a 100644 --- a/database/gdb/gdb_z_core_config_external_test.go +++ b/database/gdb/gdb_z_core_config_external_test.go @@ -1,1191 +1,1191 @@ -// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -package gdb_test - -import ( - "testing" - - "github.com/gogf/gf/v2/database/gdb" - "github.com/gogf/gf/v2/test/gtest" -) - -func Test_GetAllConfig(t *testing.T) { - // Test case 1: Empty configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config to empty - gdb.SetConfig(make(gdb.Config)) - - result := gdb.GetAllConfig() - t.Assert(len(result), 0) - }) - - // Test case 2: Single configuration group with one node - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - testNode := gdb.ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "test_db", - Type: "mysql", - } - - err := gdb.AddConfigNode("test_group", testNode) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["test_group"]), 1) - t.Assert(result["test_group"][0].Host, "127.0.0.1") - t.Assert(result["test_group"][0].Port, "3306") - t.Assert(result["test_group"][0].User, "root") - t.Assert(result["test_group"][0].Pass, "123456") - t.Assert(result["test_group"][0].Name, "test_db") - t.Assert(result["test_group"][0].Type, "mysql") - }) - - // Test case 3: Multiple configuration groups with multiple nodes - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - // Add first group with two nodes - testNode1 := gdb.ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "master_db", - Type: "mysql", - Role: "master", - } - testNode2 := gdb.ConfigNode{ - Host: "127.0.0.2", - Port: "3306", - User: "root", - Pass: "123456", - Name: "slave_db", - Type: "mysql", - Role: "slave", - } - - err := gdb.AddConfigNode("mysql_cluster", testNode1) - t.AssertNil(err) - err = gdb.AddConfigNode("mysql_cluster", testNode2) - t.AssertNil(err) - - // Add second group with one node - testNode3 := gdb.ConfigNode{ - Host: "localhost", - Port: "5432", - User: "postgres", - Pass: "password", - Name: "pg_db", - Type: "pgsql", - } - - err = gdb.AddConfigNode("postgres_db", testNode3) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 2) - - // Check mysql_cluster group - t.Assert(len(result["mysql_cluster"]), 2) - t.Assert(result["mysql_cluster"][0].Host, "127.0.0.1") - t.Assert(result["mysql_cluster"][0].Role, "master") - t.Assert(result["mysql_cluster"][1].Host, "127.0.0.2") - t.Assert(result["mysql_cluster"][1].Role, "slave") - - // Check postgres_db group - t.Assert(len(result["postgres_db"]), 1) - t.Assert(result["postgres_db"][0].Host, "localhost") - t.Assert(result["postgres_db"][0].Port, "5432") - t.Assert(result["postgres_db"][0].Type, "pgsql") - }) - - // Test case 4: Configuration with Link syntax - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - testNode := gdb.ConfigNode{ - Link: "mysql:root:123456@tcp(127.0.0.1:3306)/test_db?charset=utf8", - } - - err := gdb.AddConfigNode("link_test", testNode) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["link_test"]), 1) - - // Check parsed values from link - node := result["link_test"][0] - t.Assert(node.Type, "mysql") - t.Assert(node.User, "root") - t.Assert(node.Pass, "123456") - t.Assert(node.Host, "127.0.0.1") - t.Assert(node.Port, "3306") - t.Assert(node.Name, "test_db") - t.Assert(node.Charset, "utf8") - t.Assert(node.Protocol, "tcp") - }) - - // Test case 5: Default group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - testNode := gdb.ConfigNode{ - Host: "localhost", - Port: "3306", - User: "user", - Pass: "pass", - Name: "default_db", - Type: "mysql", - } - - err := gdb.AddDefaultConfigNode(testNode) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["default"]), 1) - t.Assert(result["default"][0].Host, "localhost") - t.Assert(result["default"][0].Name, "default_db") - }) - - // Test case 6: SetConfig with multiple groups - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - testConfig := gdb.Config{ - "group1": gdb.ConfigGroup{ - { - Host: "host1", - Port: "3306", - User: "user1", - Pass: "pass1", - Name: "db1", - Type: "mysql", - }, - }, - "group2": gdb.ConfigGroup{ - { - Host: "host2", - Port: "5432", - User: "user2", - Pass: "pass2", - Name: "db2", - Type: "pgsql", - }, - { - Host: "host3", - Port: "5432", - User: "user3", - Pass: "pass3", - Name: "db3", - Type: "pgsql", - }, - }, - } - - err := gdb.SetConfig(testConfig) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 2) - t.Assert(len(result["group1"]), 1) - t.Assert(len(result["group2"]), 2) - - t.Assert(result["group1"][0].Host, "host1") - t.Assert(result["group1"][0].Type, "mysql") - - t.Assert(result["group2"][0].Host, "host2") - t.Assert(result["group2"][0].Type, "pgsql") - t.Assert(result["group2"][1].Host, "host3") - t.Assert(result["group2"][1].Type, "pgsql") - }) - - // Test case 7: Test return value is a copy (not reference) - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - testNode := gdb.ConfigNode{ - Host: "original_host", - Port: "3306", - User: "original_user", - Pass: "original_pass", - Name: "original_db", - Type: "mysql", - } - - err := gdb.AddConfigNode("test_copy", testNode) - t.AssertNil(err) - - // Get config and modify it - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - - // Verify original values - t.Assert(result["test_copy"][0].Host, "original_host") - - // Note: GetAllConfig returns the internal config directly (not a copy) - // This is by design for performance reasons - // So modifying the returned config would affect the internal state - // This test just verifies the current behavior - }) -} - -func Test_SetConfig(t *testing.T) { - // Test case 1: Normal configuration setting - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - testConfig := gdb.Config{ - "group1": gdb.ConfigGroup{ - { - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "test_db", - Type: "mysql", - }, - }, - "group2": gdb.ConfigGroup{ - { - Host: "192.168.1.100", - Port: "5432", - User: "postgres", - Pass: "password", - Name: "pg_db", - Type: "pgsql", - }, - }, - } - - err := gdb.SetConfig(testConfig) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 2) - t.Assert(result["group1"][0].Host, "127.0.0.1") - t.Assert(result["group2"][0].Type, "pgsql") - }) - - // Test case 2: Empty configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - testConfig := gdb.Config{} - err := gdb.SetConfig(testConfig) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 0) - }) - - // Test case 3: Configuration with Link syntax - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - testConfig := gdb.Config{ - "mysql_link": gdb.ConfigGroup{ - { - Link: "mysql:root:123456@tcp(127.0.0.1:3306)/test_db?charset=utf8", - }, - }, - } - - err := gdb.SetConfig(testConfig) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - node := result["mysql_link"][0] - t.Assert(node.Type, "mysql") - t.Assert(node.User, "root") - t.Assert(node.Host, "127.0.0.1") - t.Assert(node.Port, "3306") - t.Assert(node.Name, "test_db") - }) - - // Test case 4: Configuration with invalid Link syntax - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - testConfig := gdb.Config{ - "invalid_link": gdb.ConfigGroup{ - { - Link: "invalid_link_format", - }, - }, - } - - err := gdb.SetConfig(testConfig) - t.AssertNE(err, nil) - }) -} - -func Test_SetConfigGroup(t *testing.T) { - // Test case 1: Set new group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - nodes := gdb.ConfigGroup{ - { - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db1", - Type: "mysql", - Role: "master", - }, - { - Host: "127.0.0.2", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db2", - Type: "mysql", - Role: "slave", - }, - } - - err := gdb.SetConfigGroup("test_group", nodes) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["test_group"]), 2) - t.Assert(result["test_group"][0].Role, "master") - t.Assert(result["test_group"][1].Role, "slave") - }) - - // Test case 2: Overwrite existing group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - // First set - nodes1 := gdb.ConfigGroup{ - { - Host: "old_host", - Port: "3306", - User: "old_user", - Name: "old_db", - Type: "mysql", - }, - } - err := gdb.SetConfigGroup("test_group", nodes1) - t.AssertNil(err) - - // Overwrite with new config - nodes2 := gdb.ConfigGroup{ - { - Host: "new_host", - Port: "5432", - User: "new_user", - Name: "new_db", - Type: "pgsql", - }, - } - err = gdb.SetConfigGroup("test_group", nodes2) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["test_group"]), 1) - t.Assert(result["test_group"][0].Host, "new_host") - t.Assert(result["test_group"][0].Type, "pgsql") - }) - - // Test case 3: Empty group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - nodes := gdb.ConfigGroup{} - err := gdb.SetConfigGroup("empty_group", nodes) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["empty_group"]), 0) - }) - - // Test case 4: Configuration with invalid Link syntax - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - nodes := gdb.ConfigGroup{ - { - Link: "invalid_link", - }, - } - - err := gdb.SetConfigGroup("invalid_group", nodes) - t.AssertNE(err, nil) - }) -} - -func Test_AddConfigNode(t *testing.T) { - // Test case 1: Add node to new group - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - node := gdb.ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "test_db", - Type: "mysql", - } - - err := gdb.AddConfigNode("new_group", node) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["new_group"]), 1) - t.Assert(result["new_group"][0].Host, "127.0.0.1") - }) - - // Test case 2: Add node to existing group - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - // Add first node - node1 := gdb.ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db1", - Type: "mysql", - } - err := gdb.AddConfigNode("existing_group", node1) - t.AssertNil(err) - - // Add second node to same group - node2 := gdb.ConfigNode{ - Host: "127.0.0.2", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db2", - Type: "mysql", - } - err = gdb.AddConfigNode("existing_group", node2) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["existing_group"]), 2) - t.Assert(result["existing_group"][0].Name, "db1") - t.Assert(result["existing_group"][1].Name, "db2") - }) - - // Test case 3: Add node with Link syntax - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - node := gdb.ConfigNode{ - Link: "mysql:root:password@tcp(192.168.1.100:3306)/mydb?charset=utf8mb4", - } - - err := gdb.AddConfigNode("link_group", node) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["link_group"]), 1) - t.Assert(result["link_group"][0].Type, "mysql") - t.Assert(result["link_group"][0].Host, "192.168.1.100") - t.Assert(result["link_group"][0].Port, "3306") - t.Assert(result["link_group"][0].Name, "mydb") - }) - - // Test case 4: Add node with invalid Link syntax - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - node := gdb.ConfigNode{ - Link: "invalid_link_format", - } - - err := gdb.AddConfigNode("invalid_group", node) - t.AssertNE(err, nil) - }) -} - -func Test_AddDefaultConfigNode(t *testing.T) { - // Test case 1: Add node to default group - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - node := gdb.ConfigNode{ - Host: "localhost", - Port: "3306", - User: "root", - Pass: "root", - Name: "default_db", - Type: "mysql", - } - - err := gdb.AddDefaultConfigNode(node) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["default"]), 1) - t.Assert(result["default"][0].Host, "localhost") - t.Assert(result["default"][0].Name, "default_db") - }) - - // Test case 2: Add multiple nodes to default group - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - node1 := gdb.ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db1", - Type: "mysql", - Role: "master", - } - err := gdb.AddDefaultConfigNode(node1) - t.AssertNil(err) - - node2 := gdb.ConfigNode{ - Host: "127.0.0.2", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db2", - Type: "mysql", - Role: "slave", - } - err = gdb.AddDefaultConfigNode(node2) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["default"]), 2) - t.Assert(result["default"][0].Role, "master") - t.Assert(result["default"][1].Role, "slave") - }) - - // Test case 3: Add node with Link syntax to default group - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - node := gdb.ConfigNode{ - Link: "pgsql:postgres:password@tcp(localhost:5432)/testdb", - } - - err := gdb.AddDefaultConfigNode(node) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["default"]), 1) - t.Assert(result["default"][0].Type, "pgsql") - t.Assert(result["default"][0].User, "postgres") - t.Assert(result["default"][0].Host, "localhost") - t.Assert(result["default"][0].Port, "5432") - t.Assert(result["default"][0].Name, "testdb") - }) -} - -func Test_AddDefaultConfigGroup(t *testing.T) { - // Test case 1: Add multiple nodes to default group (deprecated function) - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - nodes := gdb.ConfigGroup{ - { - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db1", - Type: "mysql", - Role: "master", - }, - { - Host: "127.0.0.2", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db2", - Type: "mysql", - Role: "slave", - }, - } - - err := gdb.AddDefaultConfigGroup(nodes) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["default"]), 2) - t.Assert(result["default"][0].Role, "master") - t.Assert(result["default"][1].Role, "slave") - }) - - // Test case 2: Overwrite existing default group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - // First set - node1 := gdb.ConfigNode{ - Host: "old_host", - Port: "3306", - User: "old_user", - Name: "old_db", - Type: "mysql", - } - err := gdb.AddDefaultConfigNode(node1) - t.AssertNil(err) - - // Overwrite with new group config - nodes := gdb.ConfigGroup{ - { - Host: "new_host", - Port: "5432", - User: "new_user", - Name: "new_db", - Type: "pgsql", - }, - } - err = gdb.AddDefaultConfigGroup(nodes) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["default"]), 1) - t.Assert(result["default"][0].Host, "new_host") - t.Assert(result["default"][0].Type, "pgsql") - }) -} - -func Test_SetDefaultConfigGroup(t *testing.T) { - // Test case 1: Set default group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - nodes := gdb.ConfigGroup{ - { - Host: "192.168.1.10", - Port: "3306", - User: "admin", - Pass: "admin123", - Name: "main_db", - Type: "mysql", - Role: "master", - }, - { - Host: "192.168.1.11", - Port: "3306", - User: "admin", - Pass: "admin123", - Name: "backup_db", - Type: "mysql", - Role: "slave", - }, - } - - err := gdb.SetDefaultConfigGroup(nodes) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["default"]), 2) - t.Assert(result["default"][0].Host, "192.168.1.10") - t.Assert(result["default"][0].Role, "master") - t.Assert(result["default"][1].Host, "192.168.1.11") - t.Assert(result["default"][1].Role, "slave") - }) - - // Test case 2: Empty default group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config and add some initial data - gdb.SetConfig(make(gdb.Config)) - err := gdb.AddDefaultConfigNode(gdb.ConfigNode{ - Host: "temp_host", - Name: "temp_db", - Type: "mysql", - }) - t.AssertNil(err) - - // Set empty group - nodes := gdb.ConfigGroup{} - err = gdb.SetDefaultConfigGroup(nodes) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["default"]), 0) - }) - - // Test case 3: Configuration with Link syntax - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - nodes := gdb.ConfigGroup{ - { - Link: "mysql:root:123456@tcp(localhost:3306)/test_db1", - }, - { - Link: "pgsql:postgres:password@tcp(localhost:5432)/test_db2", - }, - } - - err := gdb.SetDefaultConfigGroup(nodes) - t.AssertNil(err) - - result := gdb.GetAllConfig() - t.Assert(len(result), 1) - t.Assert(len(result["default"]), 2) - t.Assert(result["default"][0].Type, "mysql") - t.Assert(result["default"][0].Name, "test_db1") - t.Assert(result["default"][1].Type, "pgsql") - t.Assert(result["default"][1].Name, "test_db2") - }) -} - -func Test_GetConfig(t *testing.T) { - // Test case 1: Get existing group configuration (deprecated function) - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - node := gdb.ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "test_db", - Type: "mysql", - } - - err := gdb.AddConfigNode("test_group", node) - t.AssertNil(err) - - result := gdb.GetConfig("test_group") - t.Assert(len(result), 1) - t.Assert(result[0].Host, "127.0.0.1") - t.Assert(result[0].Type, "mysql") - }) - - // Test case 2: Get non-existing group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - result := gdb.GetConfig("non_existing_group") - t.Assert(len(result), 0) - }) -} - -func Test_GetConfigGroup(t *testing.T) { - // Test case 1: Get existing group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - nodes := gdb.ConfigGroup{ - { - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db1", - Type: "mysql", - Role: "master", - }, - { - Host: "127.0.0.2", - Port: "3306", - User: "root", - Pass: "123456", - Name: "db2", - Type: "mysql", - Role: "slave", - }, - } - - err := gdb.SetConfigGroup("test_group", nodes) - t.AssertNil(err) - - result, err := gdb.GetConfigGroup("test_group") - t.AssertNil(err) - t.Assert(len(result), 2) - t.Assert(result[0].Host, "127.0.0.1") - t.Assert(result[0].Role, "master") - t.Assert(result[1].Host, "127.0.0.2") - t.Assert(result[1].Role, "slave") - }) - - // Test case 2: Get non-existing group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - result, err := gdb.GetConfigGroup("non_existing_group") - t.AssertNE(err, nil) - t.Assert(result, nil) - }) - - // Test case 3: Get empty group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - err := gdb.SetConfigGroup("empty_group", gdb.ConfigGroup{}) - t.AssertNil(err) - - result, err := gdb.GetConfigGroup("empty_group") - t.AssertNil(err) - t.Assert(len(result), 0) - }) -} - -func Test_SetDefaultGroup(t *testing.T) { - // Test case 1: Set default group name - gtest.C(t, func(t *gtest.T) { - // Save original group and restore after test - originalGroup := gdb.GetDefaultGroup() - defer func() { - gdb.SetDefaultGroup(originalGroup) - }() - - gdb.SetDefaultGroup("custom_default") - result := gdb.GetDefaultGroup() - t.Assert(result, "custom_default") - }) - - // Test case 2: Set empty default group name - gtest.C(t, func(t *gtest.T) { - // Save original group and restore after test - originalGroup := gdb.GetDefaultGroup() - defer func() { - gdb.SetDefaultGroup(originalGroup) - }() - - gdb.SetDefaultGroup("") - result := gdb.GetDefaultGroup() - t.Assert(result, "") - }) - - // Test case 3: Multiple calls to SetDefaultGroup - gtest.C(t, func(t *gtest.T) { - // Save original group and restore after test - originalGroup := gdb.GetDefaultGroup() - defer func() { - gdb.SetDefaultGroup(originalGroup) - }() - - gdb.SetDefaultGroup("first_group") - result1 := gdb.GetDefaultGroup() - t.Assert(result1, "first_group") - - gdb.SetDefaultGroup("second_group") - result2 := gdb.GetDefaultGroup() - t.Assert(result2, "second_group") - }) -} - -func Test_GetDefaultGroup(t *testing.T) { - // Test case 1: Get default group name - gtest.C(t, func(t *gtest.T) { - // Save original group and restore after test - originalGroup := gdb.GetDefaultGroup() - defer func() { - gdb.SetDefaultGroup(originalGroup) - }() - - // Test with default value - result := gdb.GetDefaultGroup() - t.Assert(result, "default") - }) - - // Test case 2: Get custom default group name - gtest.C(t, func(t *gtest.T) { - // Save original group and restore after test - originalGroup := gdb.GetDefaultGroup() - defer func() { - gdb.SetDefaultGroup(originalGroup) - }() - - gdb.SetDefaultGroup("my_custom_group") - result := gdb.GetDefaultGroup() - t.Assert(result, "my_custom_group") - }) -} - -func Test_IsConfigured(t *testing.T) { - // Test case 1: No configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config to empty - gdb.SetConfig(make(gdb.Config)) - - result := gdb.IsConfigured() - t.Assert(result, false) - }) - - // Test case 2: Has configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - node := gdb.ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "test_db", - Type: "mysql", - } - - err := gdb.AddConfigNode("test_group", node) - t.AssertNil(err) - - result := gdb.IsConfigured() - t.Assert(result, true) - }) - - // Test case 3: Has empty group configuration - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := gdb.GetAllConfig() - defer func() { - gdb.SetConfig(originalConfig) - }() - - // Reset config - gdb.SetConfig(make(gdb.Config)) - - err := gdb.SetConfigGroup("empty_group", gdb.ConfigGroup{}) - t.AssertNil(err) - - result := gdb.IsConfigured() - t.Assert(result, true) - }) -} +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdb_test + +import ( + "testing" + + "github.com/gogf/gf/v2/database/gdb" + "github.com/gogf/gf/v2/test/gtest" +) + +func Test_GetAllConfig(t *testing.T) { + // Test case 1: Empty configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config to empty + gdb.SetConfig(make(gdb.Config)) + + result := gdb.GetAllConfig() + t.Assert(len(result), 0) + }) + + // Test case 2: Single configuration group with one node + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + testNode := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("test_group", testNode) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["test_group"]), 1) + t.Assert(result["test_group"][0].Host, "127.0.0.1") + t.Assert(result["test_group"][0].Port, "3306") + t.Assert(result["test_group"][0].User, "root") + t.Assert(result["test_group"][0].Pass, "123456") + t.Assert(result["test_group"][0].Name, "test_db") + t.Assert(result["test_group"][0].Type, "mysql") + }) + + // Test case 3: Multiple configuration groups with multiple nodes + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + // Add first group with two nodes + testNode1 := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "master_db", + Type: "mysql", + Role: "master", + } + testNode2 := gdb.ConfigNode{ + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "slave_db", + Type: "mysql", + Role: "slave", + } + + err := gdb.AddConfigNode("mysql_cluster", testNode1) + t.AssertNil(err) + err = gdb.AddConfigNode("mysql_cluster", testNode2) + t.AssertNil(err) + + // Add second group with one node + testNode3 := gdb.ConfigNode{ + Host: "localhost", + Port: "5432", + User: "postgres", + Pass: "password", + Name: "pg_db", + Type: "pgsql", + } + + err = gdb.AddConfigNode("postgres_db", testNode3) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 2) + + // Check mysql_cluster group + t.Assert(len(result["mysql_cluster"]), 2) + t.Assert(result["mysql_cluster"][0].Host, "127.0.0.1") + t.Assert(result["mysql_cluster"][0].Role, "master") + t.Assert(result["mysql_cluster"][1].Host, "127.0.0.2") + t.Assert(result["mysql_cluster"][1].Role, "slave") + + // Check postgres_db group + t.Assert(len(result["postgres_db"]), 1) + t.Assert(result["postgres_db"][0].Host, "localhost") + t.Assert(result["postgres_db"][0].Port, "5432") + t.Assert(result["postgres_db"][0].Type, "pgsql") + }) + + // Test case 4: Configuration with Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + testNode := gdb.ConfigNode{ + Link: "mysql:root:123456@tcp(127.0.0.1:3306)/test_db?charset=utf8", + } + + err := gdb.AddConfigNode("link_test", testNode) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["link_test"]), 1) + + // Check parsed values from link + node := result["link_test"][0] + t.Assert(node.Type, "mysql") + t.Assert(node.User, "root") + t.Assert(node.Pass, "123456") + t.Assert(node.Host, "127.0.0.1") + t.Assert(node.Port, "3306") + t.Assert(node.Name, "test_db") + t.Assert(node.Charset, "utf8") + t.Assert(node.Protocol, "tcp") + }) + + // Test case 5: Default group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + testNode := gdb.ConfigNode{ + Host: "localhost", + Port: "3306", + User: "user", + Pass: "pass", + Name: "default_db", + Type: "mysql", + } + + err := gdb.AddDefaultConfigNode(testNode) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 1) + t.Assert(result["default"][0].Host, "localhost") + t.Assert(result["default"][0].Name, "default_db") + }) + + // Test case 6: SetConfig with multiple groups + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{ + "group1": gdb.ConfigGroup{ + { + Host: "host1", + Port: "3306", + User: "user1", + Pass: "pass1", + Name: "db1", + Type: "mysql", + }, + }, + "group2": gdb.ConfigGroup{ + { + Host: "host2", + Port: "5432", + User: "user2", + Pass: "pass2", + Name: "db2", + Type: "pgsql", + }, + { + Host: "host3", + Port: "5432", + User: "user3", + Pass: "pass3", + Name: "db3", + Type: "pgsql", + }, + }, + } + + err := gdb.SetConfig(testConfig) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 2) + t.Assert(len(result["group1"]), 1) + t.Assert(len(result["group2"]), 2) + + t.Assert(result["group1"][0].Host, "host1") + t.Assert(result["group1"][0].Type, "mysql") + + t.Assert(result["group2"][0].Host, "host2") + t.Assert(result["group2"][0].Type, "pgsql") + t.Assert(result["group2"][1].Host, "host3") + t.Assert(result["group2"][1].Type, "pgsql") + }) + + // Test case 7: Test return value is a copy (not reference) + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + testNode := gdb.ConfigNode{ + Host: "original_host", + Port: "3306", + User: "original_user", + Pass: "original_pass", + Name: "original_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("test_copy", testNode) + t.AssertNil(err) + + // Get config and modify it + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + + // Verify original values + t.Assert(result["test_copy"][0].Host, "original_host") + + // Note: GetAllConfig returns the internal config directly (not a copy) + // This is by design for performance reasons + // So modifying the returned config would affect the internal state + // This test just verifies the current behavior + }) +} + +func Test_SetConfig(t *testing.T) { + // Test case 1: Normal configuration setting + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{ + "group1": gdb.ConfigGroup{ + { + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + }, + }, + "group2": gdb.ConfigGroup{ + { + Host: "192.168.1.100", + Port: "5432", + User: "postgres", + Pass: "password", + Name: "pg_db", + Type: "pgsql", + }, + }, + } + + err := gdb.SetConfig(testConfig) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 2) + t.Assert(result["group1"][0].Host, "127.0.0.1") + t.Assert(result["group2"][0].Type, "pgsql") + }) + + // Test case 2: Empty configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{} + err := gdb.SetConfig(testConfig) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 0) + }) + + // Test case 3: Configuration with Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{ + "mysql_link": gdb.ConfigGroup{ + { + Link: "mysql:root:123456@tcp(127.0.0.1:3306)/test_db?charset=utf8", + }, + }, + } + + err := gdb.SetConfig(testConfig) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + node := result["mysql_link"][0] + t.Assert(node.Type, "mysql") + t.Assert(node.User, "root") + t.Assert(node.Host, "127.0.0.1") + t.Assert(node.Port, "3306") + t.Assert(node.Name, "test_db") + }) + + // Test case 4: Configuration with invalid Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + testConfig := gdb.Config{ + "invalid_link": gdb.ConfigGroup{ + { + Link: "invalid_link_format", + }, + }, + } + + err := gdb.SetConfig(testConfig) + t.AssertNE(err, nil) + }) +} + +func Test_SetConfigGroup(t *testing.T) { + // Test case 1: Set new group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + Role: "master", + }, + { + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + Role: "slave", + }, + } + + err := gdb.SetConfigGroup("test_group", nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["test_group"]), 2) + t.Assert(result["test_group"][0].Role, "master") + t.Assert(result["test_group"][1].Role, "slave") + }) + + // Test case 2: Overwrite existing group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + // First set + nodes1 := gdb.ConfigGroup{ + { + Host: "old_host", + Port: "3306", + User: "old_user", + Name: "old_db", + Type: "mysql", + }, + } + err := gdb.SetConfigGroup("test_group", nodes1) + t.AssertNil(err) + + // Overwrite with new config + nodes2 := gdb.ConfigGroup{ + { + Host: "new_host", + Port: "5432", + User: "new_user", + Name: "new_db", + Type: "pgsql", + }, + } + err = gdb.SetConfigGroup("test_group", nodes2) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["test_group"]), 1) + t.Assert(result["test_group"][0].Host, "new_host") + t.Assert(result["test_group"][0].Type, "pgsql") + }) + + // Test case 3: Empty group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{} + err := gdb.SetConfigGroup("empty_group", nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["empty_group"]), 0) + }) + + // Test case 4: Configuration with invalid Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + nodes := gdb.ConfigGroup{ + { + Link: "invalid_link", + }, + } + + err := gdb.SetConfigGroup("invalid_group", nodes) + t.AssertNE(err, nil) + }) +} + +func Test_AddConfigNode(t *testing.T) { + // Test case 1: Add node to new group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("new_group", node) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["new_group"]), 1) + t.Assert(result["new_group"][0].Host, "127.0.0.1") + }) + + // Test case 2: Add node to existing group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + // Add first node + node1 := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + } + err := gdb.AddConfigNode("existing_group", node1) + t.AssertNil(err) + + // Add second node to same group + node2 := gdb.ConfigNode{ + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + } + err = gdb.AddConfigNode("existing_group", node2) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["existing_group"]), 2) + t.Assert(result["existing_group"][0].Name, "db1") + t.Assert(result["existing_group"][1].Name, "db2") + }) + + // Test case 3: Add node with Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Link: "mysql:root:password@tcp(192.168.1.100:3306)/mydb?charset=utf8mb4", + } + + err := gdb.AddConfigNode("link_group", node) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["link_group"]), 1) + t.Assert(result["link_group"][0].Type, "mysql") + t.Assert(result["link_group"][0].Host, "192.168.1.100") + t.Assert(result["link_group"][0].Port, "3306") + t.Assert(result["link_group"][0].Name, "mydb") + }) + + // Test case 4: Add node with invalid Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + node := gdb.ConfigNode{ + Link: "invalid_link_format", + } + + err := gdb.AddConfigNode("invalid_group", node) + t.AssertNE(err, nil) + }) +} + +func Test_AddDefaultConfigNode(t *testing.T) { + // Test case 1: Add node to default group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Host: "localhost", + Port: "3306", + User: "root", + Pass: "root", + Name: "default_db", + Type: "mysql", + } + + err := gdb.AddDefaultConfigNode(node) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 1) + t.Assert(result["default"][0].Host, "localhost") + t.Assert(result["default"][0].Name, "default_db") + }) + + // Test case 2: Add multiple nodes to default group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node1 := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + Role: "master", + } + err := gdb.AddDefaultConfigNode(node1) + t.AssertNil(err) + + node2 := gdb.ConfigNode{ + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + Role: "slave", + } + err = gdb.AddDefaultConfigNode(node2) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 2) + t.Assert(result["default"][0].Role, "master") + t.Assert(result["default"][1].Role, "slave") + }) + + // Test case 3: Add node with Link syntax to default group + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Link: "pgsql:postgres:password@tcp(localhost:5432)/testdb", + } + + err := gdb.AddDefaultConfigNode(node) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 1) + t.Assert(result["default"][0].Type, "pgsql") + t.Assert(result["default"][0].User, "postgres") + t.Assert(result["default"][0].Host, "localhost") + t.Assert(result["default"][0].Port, "5432") + t.Assert(result["default"][0].Name, "testdb") + }) +} + +func Test_AddDefaultConfigGroup(t *testing.T) { + // Test case 1: Add multiple nodes to default group (deprecated function) + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + Role: "master", + }, + { + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + Role: "slave", + }, + } + + err := gdb.AddDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 2) + t.Assert(result["default"][0].Role, "master") + t.Assert(result["default"][1].Role, "slave") + }) + + // Test case 2: Overwrite existing default group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + // First set + node1 := gdb.ConfigNode{ + Host: "old_host", + Port: "3306", + User: "old_user", + Name: "old_db", + Type: "mysql", + } + err := gdb.AddDefaultConfigNode(node1) + t.AssertNil(err) + + // Overwrite with new group config + nodes := gdb.ConfigGroup{ + { + Host: "new_host", + Port: "5432", + User: "new_user", + Name: "new_db", + Type: "pgsql", + }, + } + err = gdb.AddDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 1) + t.Assert(result["default"][0].Host, "new_host") + t.Assert(result["default"][0].Type, "pgsql") + }) +} + +func Test_SetDefaultConfigGroup(t *testing.T) { + // Test case 1: Set default group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Host: "192.168.1.10", + Port: "3306", + User: "admin", + Pass: "admin123", + Name: "main_db", + Type: "mysql", + Role: "master", + }, + { + Host: "192.168.1.11", + Port: "3306", + User: "admin", + Pass: "admin123", + Name: "backup_db", + Type: "mysql", + Role: "slave", + }, + } + + err := gdb.SetDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 2) + t.Assert(result["default"][0].Host, "192.168.1.10") + t.Assert(result["default"][0].Role, "master") + t.Assert(result["default"][1].Host, "192.168.1.11") + t.Assert(result["default"][1].Role, "slave") + }) + + // Test case 2: Empty default group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config and add some initial data + gdb.SetConfig(make(gdb.Config)) + err := gdb.AddDefaultConfigNode(gdb.ConfigNode{ + Host: "temp_host", + Name: "temp_db", + Type: "mysql", + }) + t.AssertNil(err) + + // Set empty group + nodes := gdb.ConfigGroup{} + err = gdb.SetDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 0) + }) + + // Test case 3: Configuration with Link syntax + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Link: "mysql:root:123456@tcp(localhost:3306)/test_db1", + }, + { + Link: "pgsql:postgres:password@tcp(localhost:5432)/test_db2", + }, + } + + err := gdb.SetDefaultConfigGroup(nodes) + t.AssertNil(err) + + result := gdb.GetAllConfig() + t.Assert(len(result), 1) + t.Assert(len(result["default"]), 2) + t.Assert(result["default"][0].Type, "mysql") + t.Assert(result["default"][0].Name, "test_db1") + t.Assert(result["default"][1].Type, "pgsql") + t.Assert(result["default"][1].Name, "test_db2") + }) +} + +func Test_GetConfig(t *testing.T) { + // Test case 1: Get existing group configuration (deprecated function) + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("test_group", node) + t.AssertNil(err) + + result := gdb.GetConfig("test_group") + t.Assert(len(result), 1) + t.Assert(result[0].Host, "127.0.0.1") + t.Assert(result[0].Type, "mysql") + }) + + // Test case 2: Get non-existing group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + result := gdb.GetConfig("non_existing_group") + t.Assert(len(result), 0) + }) +} + +func Test_GetConfigGroup(t *testing.T) { + // Test case 1: Get existing group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + nodes := gdb.ConfigGroup{ + { + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db1", + Type: "mysql", + Role: "master", + }, + { + Host: "127.0.0.2", + Port: "3306", + User: "root", + Pass: "123456", + Name: "db2", + Type: "mysql", + Role: "slave", + }, + } + + err := gdb.SetConfigGroup("test_group", nodes) + t.AssertNil(err) + + result, err := gdb.GetConfigGroup("test_group") + t.AssertNil(err) + t.Assert(len(result), 2) + t.Assert(result[0].Host, "127.0.0.1") + t.Assert(result[0].Role, "master") + t.Assert(result[1].Host, "127.0.0.2") + t.Assert(result[1].Role, "slave") + }) + + // Test case 2: Get non-existing group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + result, err := gdb.GetConfigGroup("non_existing_group") + t.AssertNE(err, nil) + t.Assert(result, nil) + }) + + // Test case 3: Get empty group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + err := gdb.SetConfigGroup("empty_group", gdb.ConfigGroup{}) + t.AssertNil(err) + + result, err := gdb.GetConfigGroup("empty_group") + t.AssertNil(err) + t.Assert(len(result), 0) + }) +} + +func Test_SetDefaultGroup(t *testing.T) { + // Test case 1: Set default group name + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + gdb.SetDefaultGroup("custom_default") + result := gdb.GetDefaultGroup() + t.Assert(result, "custom_default") + }) + + // Test case 2: Set empty default group name + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + gdb.SetDefaultGroup("") + result := gdb.GetDefaultGroup() + t.Assert(result, "") + }) + + // Test case 3: Multiple calls to SetDefaultGroup + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + gdb.SetDefaultGroup("first_group") + result1 := gdb.GetDefaultGroup() + t.Assert(result1, "first_group") + + gdb.SetDefaultGroup("second_group") + result2 := gdb.GetDefaultGroup() + t.Assert(result2, "second_group") + }) +} + +func Test_GetDefaultGroup(t *testing.T) { + // Test case 1: Get default group name + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + // Test with default value + result := gdb.GetDefaultGroup() + t.Assert(result, "default") + }) + + // Test case 2: Get custom default group name + gtest.C(t, func(t *gtest.T) { + // Save original group and restore after test + originalGroup := gdb.GetDefaultGroup() + defer func() { + gdb.SetDefaultGroup(originalGroup) + }() + + gdb.SetDefaultGroup("my_custom_group") + result := gdb.GetDefaultGroup() + t.Assert(result, "my_custom_group") + }) +} + +func Test_IsConfigured(t *testing.T) { + // Test case 1: No configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config to empty + gdb.SetConfig(make(gdb.Config)) + + result := gdb.IsConfigured() + t.Assert(result, false) + }) + + // Test case 2: Has configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + node := gdb.ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + + err := gdb.AddConfigNode("test_group", node) + t.AssertNil(err) + + result := gdb.IsConfigured() + t.Assert(result, true) + }) + + // Test case 3: Has empty group configuration + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := gdb.GetAllConfig() + defer func() { + gdb.SetConfig(originalConfig) + }() + + // Reset config + gdb.SetConfig(make(gdb.Config)) + + err := gdb.SetConfigGroup("empty_group", gdb.ConfigGroup{}) + t.AssertNil(err) + + result := gdb.IsConfigured() + t.Assert(result, true) + }) +} diff --git a/database/gdb/gdb_z_core_config_test.go b/database/gdb/gdb_z_core_config_test.go index 492e0e93d08..d0e876192ad 100644 --- a/database/gdb/gdb_z_core_config_test.go +++ b/database/gdb/gdb_z_core_config_test.go @@ -1,204 +1,204 @@ -// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -package gdb - -import ( - "testing" - "time" - - "github.com/gogf/gf/v2/container/gtype" - "github.com/gogf/gf/v2/os/glog" - "github.com/gogf/gf/v2/test/gtest" -) - -func Test_Core_SetDebug_GetDebug(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := configs.config - defer func() { - configs.config = originalConfig - }() - - // Create a test configuration - configs.config = make(Config) - testNode := ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "test_db", - Type: "mysql", - } - err := AddConfigNode("test_group", testNode) - t.AssertNil(err) - - // Create Core instance - node, err := GetConfigGroup("test_group") - t.AssertNil(err) - core := &Core{ - group: "test_group", - config: &node[0], - debug: gtype.NewBool(false), - } - - // Test default value - result := core.GetDebug() - t.Assert(result, false) - - // Test setting debug to true - core.SetDebug(true) - result = core.GetDebug() - t.Assert(result, true) - - // Test setting debug to false - core.SetDebug(false) - result = core.GetDebug() - t.Assert(result, false) - }) -} - -func Test_Core_SetDryRun_GetDryRun(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := configs.config - defer func() { - configs.config = originalConfig - }() - - // Create a test configuration - configs.config = make(Config) - testNode := ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "test_db", - Type: "mysql", - DryRun: false, - } - err := AddConfigNode("test_group", testNode) - t.AssertNil(err) - - // Create Core instance - node, err := GetConfigGroup("test_group") - t.AssertNil(err) - core := &Core{ - group: "test_group", - config: &node[0], - } - - // Test default value - result := core.GetDryRun() - t.Assert(result, false) - - // Test setting dry run to true - core.SetDryRun(true) - result = core.GetDryRun() - t.Assert(result, true) - - // Test setting dry run to false - core.SetDryRun(false) - result = core.GetDryRun() - t.Assert(result, false) - }) -} - -func Test_Core_SetLogger_GetLogger(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - // Create Core instance - core := &Core{} - - // Test setting custom logger - customLogger := glog.New() - core.SetLogger(customLogger) - result := core.GetLogger() - t.Assert(result, customLogger) - - // Test setting nil logger - core.SetLogger(nil) - result = core.GetLogger() - t.Assert(result, nil) - }) -} - -func Test_Core_SetMaxConnections(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - // Create Core instance - core := &Core{} - - // Test SetMaxIdleConnCount - core.SetMaxIdleConnCount(10) - t.Assert(core.dynamicConfig.MaxIdleConnCount, 10) - - // Test SetMaxOpenConnCount - core.SetMaxOpenConnCount(20) - t.Assert(core.dynamicConfig.MaxOpenConnCount, 20) - - // Test SetMaxConnLifeTime - testDuration := time.Hour - core.SetMaxConnLifeTime(testDuration) - t.Assert(core.dynamicConfig.MaxConnLifeTime, testDuration) - }) -} - -func Test_Core_GetCache(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - // Create Core instance - core := &Core{} - - cache := core.GetCache() - // Cache might be nil if not initialized, so we just test that the call doesn't panic - _ = cache - }) -} - -func Test_Core_GetGroup(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - // Create Core instance - core := &Core{ - group: "test_group", - } - - group := core.GetGroup() - t.Assert(group, "test_group") - }) -} - -func Test_Core_GetPrefix(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - // Save original config and restore after test - originalConfig := configs.config - defer func() { - configs.config = originalConfig - }() - - // Create a test configuration - configs.config = make(Config) - testNode := ConfigNode{ - Host: "127.0.0.1", - Port: "3306", - User: "root", - Pass: "123456", - Name: "test_db", - Type: "mysql", - Prefix: "gf_", - } - err := AddConfigNode("test_group", testNode) - t.AssertNil(err) - - // Create Core instance - node, err := GetConfigGroup("test_group") - t.AssertNil(err) - core := &Core{ - group: "test_group", - config: &node[0], - } - - prefix := core.GetPrefix() - t.Assert(prefix, "gf_") - }) -} +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdb + +import ( + "testing" + "time" + + "github.com/gogf/gf/v2/container/gtype" + "github.com/gogf/gf/v2/os/glog" + "github.com/gogf/gf/v2/test/gtest" +) + +func Test_Core_SetDebug_GetDebug(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := configs.config + defer func() { + configs.config = originalConfig + }() + + // Create a test configuration + configs.config = make(Config) + testNode := ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + } + err := AddConfigNode("test_group", testNode) + t.AssertNil(err) + + // Create Core instance + node, err := GetConfigGroup("test_group") + t.AssertNil(err) + core := &Core{ + group: "test_group", + config: &node[0], + debug: gtype.NewBool(false), + } + + // Test default value + result := core.GetDebug() + t.Assert(result, false) + + // Test setting debug to true + core.SetDebug(true) + result = core.GetDebug() + t.Assert(result, true) + + // Test setting debug to false + core.SetDebug(false) + result = core.GetDebug() + t.Assert(result, false) + }) +} + +func Test_Core_SetDryRun_GetDryRun(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := configs.config + defer func() { + configs.config = originalConfig + }() + + // Create a test configuration + configs.config = make(Config) + testNode := ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + DryRun: false, + } + err := AddConfigNode("test_group", testNode) + t.AssertNil(err) + + // Create Core instance + node, err := GetConfigGroup("test_group") + t.AssertNil(err) + core := &Core{ + group: "test_group", + config: &node[0], + } + + // Test default value + result := core.GetDryRun() + t.Assert(result, false) + + // Test setting dry run to true + core.SetDryRun(true) + result = core.GetDryRun() + t.Assert(result, true) + + // Test setting dry run to false + core.SetDryRun(false) + result = core.GetDryRun() + t.Assert(result, false) + }) +} + +func Test_Core_SetLogger_GetLogger(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Create Core instance + core := &Core{} + + // Test setting custom logger + customLogger := glog.New() + core.SetLogger(customLogger) + result := core.GetLogger() + t.Assert(result, customLogger) + + // Test setting nil logger + core.SetLogger(nil) + result = core.GetLogger() + t.Assert(result, nil) + }) +} + +func Test_Core_SetMaxConnections(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Create Core instance + core := &Core{} + + // Test SetMaxIdleConnCount + core.SetMaxIdleConnCount(10) + t.Assert(core.dynamicConfig.MaxIdleConnCount, 10) + + // Test SetMaxOpenConnCount + core.SetMaxOpenConnCount(20) + t.Assert(core.dynamicConfig.MaxOpenConnCount, 20) + + // Test SetMaxConnLifeTime + testDuration := time.Hour + core.SetMaxConnLifeTime(testDuration) + t.Assert(core.dynamicConfig.MaxConnLifeTime, testDuration) + }) +} + +func Test_Core_GetCache(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Create Core instance + core := &Core{} + + cache := core.GetCache() + // Cache might be nil if not initialized, so we just test that the call doesn't panic + _ = cache + }) +} + +func Test_Core_GetGroup(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Create Core instance + core := &Core{ + group: "test_group", + } + + group := core.GetGroup() + t.Assert(group, "test_group") + }) +} + +func Test_Core_GetPrefix(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + // Save original config and restore after test + originalConfig := configs.config + defer func() { + configs.config = originalConfig + }() + + // Create a test configuration + configs.config = make(Config) + testNode := ConfigNode{ + Host: "127.0.0.1", + Port: "3306", + User: "root", + Pass: "123456", + Name: "test_db", + Type: "mysql", + Prefix: "gf_", + } + err := AddConfigNode("test_group", testNode) + t.AssertNil(err) + + // Create Core instance + node, err := GetConfigGroup("test_group") + t.AssertNil(err) + core := &Core{ + group: "test_group", + config: &node[0], + } + + prefix := core.GetPrefix() + t.Assert(prefix, "gf_") + }) +}