This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new daa7d21148 Cdn folder unit test coverage (#7522)
daa7d21148 is described below

commit daa7d211486817e91a4a58034c2d9e4d8d98357e
Author: Kurtis Michie <[email protected]>
AuthorDate: Thu May 25 10:34:39 2023 -0600

    Cdn folder unit test coverage (#7522)
    
    * Working progress on TestTOCDNUpdate in cdn_test.go
    
    * Finished functionality for unbit test TestTOCDNUpdate in cdns_test.go
    
    * Reordered import
---
 traffic_ops/traffic_ops_golang/cdn/cdns_test.go | 113 +++++++++++++++++++++++-
 1 file changed, 112 insertions(+), 1 deletion(-)

diff --git a/traffic_ops/traffic_ops_golang/cdn/cdns_test.go 
b/traffic_ops/traffic_ops_golang/cdn/cdns_test.go
index e4a3296251..a19fc2ee99 100644
--- a/traffic_ops/traffic_ops_golang/cdn/cdns_test.go
+++ b/traffic_ops/traffic_ops_golang/cdn/cdns_test.go
@@ -20,6 +20,7 @@ package cdn
  */
 
 import (
+       "database/sql"
        "errors"
        "reflect"
        "strings"
@@ -29,8 +30,10 @@ import (
        "github.com/apache/trafficcontrol/lib/go-tc"
        "github.com/apache/trafficcontrol/lib/go-util"
        "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
        "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
        "github.com/jmoiron/sqlx"
+
        sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
 )
 
@@ -112,8 +115,8 @@ func TestFuncs(t *testing.T) {
        if strings.Index(deleteQuery(), "DELETE") != 0 {
                t.Errorf("expected deleteQuery to start with DELETE")
        }
-
 }
+
 func TestInterfaces(t *testing.T) {
        var i interface{}
        i = &TOCDN{}
@@ -161,3 +164,111 @@ func TestValidate(t *testing.T) {
                t.Errorf("expected nil, got %s", err)
        }
 }
+
+func TestTOCDNUpdate(t *testing.T) {
+       // Create a new mock database and retrieve a mock connection
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       // Create a new sqlx database object using the mock connection
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       // Define the columns for the database rows
+       cols := []string{"name"}
+
+       // Create a new sqlmock.Rows object with the defined columns
+       rows := sqlmock.NewRows(cols)
+       rows.AddRow("testcdn")
+
+       // Expect a transaction begin
+       mock.ExpectBegin()
+
+       // Expect a query to select a name from the database with an argument 
of 1
+       mock.ExpectQuery("SELECT name").WithArgs(1).WillReturnRows(rows)
+
+       // Redefine the columns for the database rows
+       cols = []string{"username", "soft", "shared_usernames"}
+
+       // Create a new sqlmock.Rows object with the new columns
+       rows = sqlmock.NewRows(cols)
+
+       // Expect a query to select username from the database with an argument 
of "testcdn"
+       // and return an error of "sql.ErrNoRows"
+       mock.ExpectQuery("SELECT 
c.username").WithArgs("testcdn").WillReturnError(sql.ErrNoRows)
+
+       // Redefine the columns for the database rows
+       cols = []string{"last_updated"}
+
+       // Create a new sqlmock.Rows object with the new columns
+       rows = sqlmock.NewRows(cols)
+       rows.AddRow(time.Now())
+
+       // Expect a query to select last_updated from the database with an 
argument of 1
+       mock.ExpectQuery("select last_updated").WithArgs(1).WillReturnRows(rows)
+
+       // Create a new APIInfo object with required information
+       reqInfo := api.APIInfo{
+               Tx:     db.MustBegin(),
+               Params: map[string]string{"dsId": "1"},
+               User: &auth.CurrentUser{
+                       UserName: "admin",
+               },
+               Version: &api.Version{Major: 4, Minor: 1},
+       }
+
+       // Define variables for the CDN update
+       domainName := "example.com"
+       id := 1
+       lastUpdated := tc.TimeNoMod{Time: time.Now()}
+       dnsSecEnabled := false
+       name := "testcdn"
+       ttlOverride := 0
+
+       // Create a new TOCDN object with the defined variables
+       cdn := &TOCDN{
+               api.APIInfoImpl{ReqInfo: &reqInfo},
+               tc.CDNNullable{
+                       DNSSECEnabled: &dnsSecEnabled,
+                       DomainName:    &domainName,
+                       Name:          &name,
+                       ID:            &id,
+                       TTLOverride:   &ttlOverride,
+                       LastUpdated:   &lastUpdated,
+               },
+       }
+
+       // Redefine the columns for the database rows
+       cols = []string{"last_updated"}
+
+       // Create a new sqlmock.Rows object with the new columns
+       rows = sqlmock.NewRows(cols)
+       rows.AddRow(time.Now())
+
+       // Expect a query to update the CDN in the database with the CDN object 
values
+       mock.ExpectQuery("UPDATE cdn SET").WithArgs(*cdn.DNSSECEnabled, 
*cdn.DomainName, *cdn.Name, *cdn.TTLOverride, *cdn.ID).WillReturnRows(rows)
+
+       // Expect a transaction commit
+       mock.ExpectCommit()
+
+       // Call the update method on the CDN object and retrieve the error 
values
+       userErr, sysErr, errCode := cdn.Update(nil)
+
+       // Check if there are any unexpected errors
+       if userErr != nil || sysErr != nil {
+               t.Errorf("Unexpected error: userErr=%v, sysErr=%v, errCode=%d", 
userErr, sysErr, errCode)
+       }
+
+       // Check if the domain name is updated correctly
+       if *cdn.DomainName != "example.com" {
+               t.Errorf("Unexpected domain name: %s", *cdn.DomainName)
+       }
+
+       // Check if the TTL override value is set correctly
+       if cdn.TTLOverride == nil {
+               t.Errorf("Unexpected TTL override value: %d", *cdn.TTLOverride)
+       }
+}

Reply via email to