ERBS41132YUGCITY1KKT Check and Correct TestNodesCount and TestNodesNames used for…
- User
ERBS_41132_YUGCITY_1_KKT Check and Correct TestNodes_Count and TestNodes_Names used for Count() and Names() testing
codepackage model import ( "testing" "github.com/stretchr/testify/assert" ) // TestNodes_Count tests *model.Node.Count func TestNodes_Count(t *testing.T) { nodes := TestNodes(t) nodes.Count() assert.Equal(t, 2, len(*nodes)) } // TestNodes_Names tests *model.Node.Count func TestNodes_Names(t *testing.T) { nodes := TestNodes(t) nodes.Names() assert.Equal(t, []string{"ERBS_41001_BCUKGU_2_KK1", "ERBS_41001_BCUKGU_2_KKT"}, *nodes) } package model import ( validation "github.com/go-ozzo/ozzo-validation/v4" ) // Node is custom type that represents *model.ENodeB and *model.EUtranCellData // of cosite Node for Elastic Ran type Node struct { ENodeB `json:"ENodeB"` EUtranCellData `json:"EUtranCellData"` } // Nodes is custom type that represents map of *model.Node type Nodes map[string]*Node // Count counts number of Cosite nodes func (nodes *Nodes) Count() int { count := 0 for _, node := range *nodes { if len(node.EUtranCellData.CellsGroups) > 0 { count++ } } return count } // Count counts number of Cosite nodes func (nodes *Nodes) Names() []string { names := make([]string, len(*nodes)) i := 0 for node := range *nodes { names[i] = node i++ } return names } - Assistant
Here are the corrected functions TestNodes_Count and TestNodes_Names:
code// TestNodes_Count tests *model.Node.Count func TestNodes_Count(t *testing.T) { nodes := TestNodes(t) count := nodes.Count() assert.Equal(t, 2, count) } // TestNodes_Names tests *model.Node.Names func TestNodes_Names(t *testing.T) { nodes := TestNodes(t) names := nodes.Names() assert.Equal(t, []string{"ERBS_41001_BCUKGU_2_KK1", "ERBS_41001_BCUKGU_2_KKT"}, names) }In the above code, I have made the following changes:
- In TestNodes_Count, I corrected the assertion by assigning the result of nodes.Count() to a variable "count" and then comparing it with 2.
- In TestNodes_Names, I corrected the assertion by assigning the result of nodes.Names() to a variable "names" and then comparing it with the expected list of names.
- User
add test for
// CleanEmpty deletes Cosite node if lte cell datya is empty func (nodes *Nodes) CleanEmpty() { for name, node := range *nodes { if len(node.EUtranCellData.CellsGroups) == 0 { delete(*nodes, name) } } } - Assistant
Sure! Here's a test for the
CleanEmpty()function:go// TestNodes_CleanEmpty tests *model.Node.CleanEmpty func TestNodes_CleanEmpty(t *testing.T) { nodes := TestNodes(t) // Add a node with empty LTE cell data emptyNode := &Node{ ENodeB: ENodeB{}, EUtranCellData: EUtranCellData{}, } nodes["EmptyNode"] = emptyNode // Call the CleanEmpty function nodes.CleanEmpty() // Assert that the empty node is deleted _, exists := nodes["EmptyNode"] assert.False(t, exists) }In the above test, I added a node with empty LTE cell data to the
Nodesmap. Then I called theCleanEmpty()function to delete any nodes with empty LTE cell data. Finally, I checked whether the empty node was deleted by asserting that it does not exist in the map. - User
invalid operation: cannot index nodes (variable of type *Nodes)
- Assistant
I apologize for the mistake. Since
Nodesis a pointer type, you'll need to dereference it to access the underlying map. Here's the corrected test:go// TestNodes_CleanEmpty tests *model.Node.CleanEmpty func TestNodes_CleanEmpty(t *testing.T) { nodes := TestNodes(t) // Add a node with empty LTE cell data emptyNode := &Node{ ENodeB: ENodeB{}, EUtranCellData: EUtranCellData{}, } nodes["EmptyNode"] = emptyNode // Call the CleanEmpty function (*nodes).CleanEmpty() // Assert that the empty node is deleted _, exists := (*nodes)["EmptyNode"] assert.False(t, exists) }In the
CleanEmpty()function test, I added the dereference operator(*nodes)to access the underlying map. This allows you to perform thedeleteoperation correctly. - User
- Assistant
Here's the
TestEran_Portfunction for testing theport()method in theEranConnectionstruct:go// TestEran_Port tests *model.EranConnection.port func TestEran_Port(t *testing.T) { // Create a sample EranConnection instance connection := &EranConnection{ ENodeB: ENodeB{ Cellular: Cellular{ Name: "ERBS_12345_NAME", }, }, SubNetwork: SubNetwork{ Name: "Subnet1", }, Nodes: Nodes{ "Node1": &Node{ ENodeB: ENodeB{ Cellular: Cellular{ Name: "GRBS_54321_NAME", }, }, }, }, } // Call the port() method connection.port() // Assert the values of TnPort and TnPort2 based on the provided conditions assert.Equal(t, "TN_IDL_C", connection.TnPort) assert.Equal(t, "", connection.TnPort2) } - User
Correct TestEran_Port1
func - Assistant
Here's the corrected
TestEran_Port1function:gofunc TestEran_Port1(t *testing.T) { // Create a sample EranConnection instance connection := &EranConnection{ ENodeB: ENodeB{ Site: Site{ Cellular: Cellular{ Name: "ERBS_41001_BCUKGU_1_KKT", }, }, }, SubNetwork: SubNetwork{ Name: "Shymkent", }, Nodes: Nodes{ "GRBS_41001_BCUKGU_1_KKT": &Node{ ENodeB: ENodeB{ Site: Site{ Cellular: Cellular{ Name: "GRBS_41001_BCUKGU_1_KKT", }, }, }, EUtranCellData: EUtranCellData{ CellsGroups: EUtranCellsGroups{ "Kcell": &EUtranCells{ "41511A1L-D4L": &EUtranCell{ Cellname: "41511A1L-D4L", }, }, }, }, }, }, } // Call the port() method connection.port() // Assert the values of TnPort and TnPort2 based on the provided conditions assert.Equal(t, "TN_IDL_D", connection.TnPort) assert.Equal(t, "", connection.TnPort2) }