Skip to content

Commit

Permalink
utils UT coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
KshitijaKakde committed Jan 27, 2025
1 parent 61757cd commit 8506521
Showing 1 changed file with 223 additions and 1 deletion.
224 changes: 223 additions & 1 deletion openapi/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,53 @@ func TestPtrInt(t *testing.T) {
}
}

// Add similar tests for PtrInt32, PtrInt64, PtrFloat32, PtrFloat64, PtrString, PtrTime
func TestPtrInt32(t *testing.T) {
v := int32(42)
ptr := PtrInt32(v)
if *ptr != v {
t.Errorf("PtrInt32(%d) = %d; want %d", v, *ptr, v)
}
}

func TestPtrInt64(t *testing.T) {
v := int64(42)
ptr := PtrInt64(v)
if *ptr != v {
t.Errorf("PtrInt64(%d) = %d; want %d", v, *ptr, v)
}
}

func TestPtrFloat32(t *testing.T) {
v := float32(42.0)
ptr := PtrFloat32(v)
if *ptr != v {
t.Errorf("PtrFloat32(%f) = %f; want %f", v, *ptr, v)
}
}

func TestPtrFloat64(t *testing.T) {
v := float64(42.0)
ptr := PtrFloat64(v)
if *ptr != v {
t.Errorf("PtrFloat64(%f) = %f; want %f", v, *ptr, v)
}
}

func TestPtrString(t *testing.T) {
v := "hello"
ptr := PtrString(v)
if *ptr != v {
t.Errorf("PtrString(%s) = %s; want %s", v, *ptr, v)
}
}

func TestPtrTime(t *testing.T) {
v := time.Now()
ptr := PtrTime(v)
if *ptr != v {
t.Errorf("PtrTime(%v) = %v; want %v", v, *ptr, v)
}
}

func TestNullableBool(t *testing.T) {
val := true
Expand Down Expand Up @@ -103,6 +149,38 @@ func TestNullableInt(t *testing.T) {
}
}

func TestNullableBool_Set(t *testing.T) {
var nb NullableBool

// Test setting a true value
trueVal := true
nb.Set(&trueVal)
if nb.value == nil || *nb.value != trueVal {
t.Errorf("Expected value to be %v, got %v", trueVal, nb.value)
}
if !nb.isSet {
t.Errorf("Expected isSet to be true, got %v", nb.isSet)
}

// Test setting a false value
falseVal := false
nb.Set(&falseVal)
if nb.value == nil || *nb.value != falseVal {
t.Errorf("Expected value to be %v, got %v", falseVal, nb.value)
}
if !nb.isSet {
t.Errorf("Expected isSet to be true, got %v", nb.isSet)
}

// Test setting a nil value
nb.Set(nil)
if nb.value != nil {
t.Errorf("Expected value to be nil, got %v", nb.value)
}
if !nb.isSet {
t.Errorf("Expected isSet to be true, got %v", nb.isSet)
}
}
func TestNullableInt32(t *testing.T) {
val := int32(42)
ni32 := NullableInt32{}
Expand Down Expand Up @@ -741,3 +819,147 @@ func TestIsNil(t *testing.T) {
t.Fatal("Expected IsNil(nonEmptySlice) to return false")
}
}

func TestNullableString_Set(t *testing.T) {
var ns NullableString

// Test setting a non-nil value
str := "hello"
ns.Set(&str)
if ns.value == nil || *ns.value != str {
t.Errorf("Expected value to be %v, got %v", str, ns.value)
}
if !ns.isSet {
t.Errorf("Expected isSet to be true, got %v", ns.isSet)
}

// Test setting a nil value
ns.Set(nil)
if ns.value != nil {
t.Errorf("Expected value to be nil, got %v", ns.value)
}
if !ns.isSet {
t.Errorf("Expected isSet to be true, got %v", ns.isSet)
}
}

func TestNullableString_Get(t *testing.T) {
var ns NullableString

// Test getting a non-nil value
str := "hello"
ns.Set(&str)
if ns.Get() == nil || *ns.Get() != str {
t.Errorf("Expected Get() to return %v, got %v", str, ns.Get())
}

// Test getting a nil value
ns.Unset()
if ns.Get() != nil {
t.Errorf("Expected Get() to return nil, got %v", ns.Get())
}
}

func TestNullableString_IsSet(t *testing.T) {
var ns NullableString

// Test IsSet when value is set
str := "hello"
ns.Set(&str)
if !ns.IsSet() {
t.Errorf("Expected IsSet() to be true, got %v", ns.IsSet())
}

// Test IsSet when value is unset
ns.Unset()
if ns.IsSet() {
t.Errorf("Expected IsSet() to be false, got %v", ns.IsSet())
}
}

func TestNullableString_Unset(t *testing.T) {
var ns NullableString

// Test Unset
str := "hello"
ns.Set(&str)
ns.Unset()
if ns.value != nil {
t.Errorf("Expected value to be nil, got %v", ns.value)
}
if ns.isSet {
t.Errorf("Expected isSet to be false, got %v", ns.isSet)
}
}

func TestNullableTime_Set(t *testing.T) {
var nt NullableTime

// Test setting a non-nil value
now := time.Now()
nt.Set(&now)
if nt.value == nil || *nt.value != now {
t.Errorf("Expected value to be %v, got %v", now, nt.value)
}
if !nt.isSet {
t.Errorf("Expected isSet to be true, got %v", nt.isSet)
}

// Test setting a nil value
nt.Set(nil)
if nt.value != nil {
t.Errorf("Expected value to be nil, got %v", nt.value)
}
if !nt.isSet {
t.Errorf("Expected isSet to be true, got %v", nt.isSet)
}
}

func TestNullableTime_Get(t *testing.T) {
var nt NullableTime

// Test getting a non-nil value
now := time.Now()
nt.Set(&now)
if nt.Get() == nil || *nt.Get() != now {
t.Errorf("Expected Get() to return %v, got %v", now, nt.Get())
}

// Test getting a nil value
nt.Unset()
if nt.Get() != nil {
t.Errorf("Expected Get() to return nil, got %v", nt.Get())
}
}

func TestNullableTime_IsSet(t *testing.T) {
var nt NullableTime

// Test IsSet when value is set
now := time.Now()
nt.Set(&now)
if !nt.IsSet() {
t.Errorf("Expected IsSet() to be true, got %v", nt.IsSet())
}

// Test IsSet when value is unset
nt.Unset()
if nt.IsSet() {
t.Errorf("Expected IsSet() to be false, got %v", nt.IsSet())
}
}

func TestNullableTime_Unset(t *testing.T) {
var nt NullableTime

// Test Unset
now := time.Now()
nt.Set(&now)
nt.Unset()
if nt.value != nil {
t.Errorf("Expected value to be nil, got %v", nt.value)
}
if nt.isSet {
t.Errorf("Expected isSet to be false, got %v", nt.isSet)
}
}

0 comments on commit 8506521

Please sign in to comment.