Skip to content

Commit

Permalink
SonarCloud: add tests to increase code coverage (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 authored and Daniel Adam committed Sep 21, 2022
1 parent deb63e0 commit d0094bb
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 2 deletions.
79 changes: 79 additions & 0 deletions schema/device/device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package device_test

import (
"testing"

"github.com/plgd-dev/device/schema/device"
"github.com/stretchr/testify/require"
)

func TestDeviceGetManufacturerName(t *testing.T) {
type fields struct {
ManufacturerName []device.LocalizedString
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Empty",
fields: fields{
ManufacturerName: nil,
},
want: "",
},
{
name: "Slovak",
fields: fields{
ManufacturerName: []device.LocalizedString{
{
Language: "sk",
Value: "zariadenie",
},
},
},
want: "",
},
{
name: "English",
fields: fields{
ManufacturerName: []device.LocalizedString{
{
Language: "en",
Value: "device",
},
},
},
want: "device",
},
{
name: "Multiple",
fields: fields{
ManufacturerName: []device.LocalizedString{
{
Language: "sk",
Value: "zariadenie",
},
{
Language: "en",
Value: "device",
},
{
Language: "de",
Value: "Gerät",
},
},
},
want: "device",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := device.Device{
ManufacturerName: tt.fields.ManufacturerName,
}
require.Equal(t, d.GetManufacturerName(), tt.want)
})
}
}
198 changes: 198 additions & 0 deletions schema/link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package schema_test

import (
"testing"

"github.com/plgd-dev/device/schema"
"github.com/plgd-dev/device/schema/acl"
"github.com/plgd-dev/device/schema/ael"
"github.com/plgd-dev/device/schema/credential"
kitNet "github.com/plgd-dev/kit/v2/net"
"github.com/stretchr/testify/require"
)

func TestBitMaskHas(t *testing.T) {
b := schema.BitMask(0)
require.False(t, b.Has(schema.Discoverable))
require.False(t, b.Has(schema.Observable))

b = schema.Discoverable
require.True(t, b.Has(schema.Discoverable))
require.False(t, b.Has(schema.Observable))

b = schema.Discoverable | schema.Observable
require.True(t, b.Has(schema.Discoverable))
require.True(t, b.Has(schema.Observable))
}

func TestResourceLinkHasType(t *testing.T) {
type fields struct {
resourceTypes []string
}
type args struct {
resourceType string
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "Empty",
fields: fields{
resourceTypes: []string{credential.ResourceType, acl.ResourceType},
},
args: args{
resourceType: "",
},
want: false,
},
{
name: "Mismatch",
fields: fields{
resourceTypes: []string{credential.ResourceType, acl.ResourceType},
},
args: args{
resourceType: ael.ResourceType,
},
want: false,
},
{
name: "Match",
fields: fields{
resourceTypes: []string{credential.ResourceType, acl.ResourceType},
},
args: args{
resourceType: acl.ResourceType,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := schema.ResourceLink{
ResourceTypes: tt.fields.resourceTypes,
}
require.Equal(t, tt.want, r.HasType(tt.args.resourceType))
})
}
}

func TestResourceLinkPatchEndpoint(t *testing.T) {
type args struct {
addr kitNet.Addr
}
tests := []struct {
name string
args args
want schema.Endpoints
}{
{
name: "IPv4",
args: args{
addr: kitNet.MakeAddr("coap", "127.0.0.1", 5683),
},
want: schema.Endpoints{
schema.Endpoint{URI: "coap://127.0.0.1:5683"},
schema.Endpoint{URI: "coap+tcp://127.0.0.1:5683"},
schema.Endpoint{URI: "coaps+tcp://127.0.0.1:5684"},
},
},
{
name: "IPv6",
args: args{
addr: kitNet.MakeAddr("coap", "fe80::1", 5683),
},
want: schema.Endpoints{
schema.Endpoint{URI: "coap://[fe80::1]:5683"},
schema.Endpoint{URI: "coap+tcp://[fe80::1]:5683"},
schema.Endpoint{URI: "coaps+tcp://[fe80::1]:5684"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := schema.ResourceLink{
Policy: &schema.Policy{
UDPPort: 5683,
TCPPort: 5683,
TCPTLSPort: 5684,
},
}
got := r.PatchEndpoint(tt.args.addr, nil)
require.Len(t, got.Endpoints, len(tt.want))
if len(tt.want) > 0 {
got.Endpoints = got.Endpoints.Sort()
tt.want = tt.want.Sort()
for i := range got.Endpoints {
require.Equal(t, got.Endpoints[i], tt.want[i])
}
}
})
}
}

func TestResourceLinkPatchEndpointLinkLocal(t *testing.T) {
type fields struct {
Endpoints schema.Endpoints
}
type args struct {
addr kitNet.Addr
}
tests := []struct {
name string
fields fields
args args
want schema.Endpoints
}{
{
name: "IPv4",
fields: fields{
Endpoints: schema.Endpoints{
schema.Endpoint{
URI: "coap://169.254.0.1:5683",
},
},
},
args: args{
addr: kitNet.MakeAddr("coap", "169.254.0.1", 5683),
},
want: schema.Endpoints{
schema.Endpoint{URI: "coap://169.254.0.1:5683"},
},
},
{
name: "IPv6",
fields: fields{
Endpoints: schema.Endpoints{
schema.Endpoint{
URI: "coap://[fe80::1]:5683",
},
},
},
args: args{
addr: kitNet.MakeAddr("coap", "fe80::1%eth0", 0),
},
want: schema.Endpoints{
schema.Endpoint{URI: "coap://[fe80::1%eth0]:5683"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := schema.ResourceLink{
Endpoints: tt.fields.Endpoints,
}
got := r.PatchEndpoint(tt.args.addr, nil)
require.Len(t, got.Endpoints, len(tt.want))
if len(tt.want) > 0 {
got.Endpoints = got.Endpoints.Sort()
tt.want = tt.want.Sort()
for i := range got.Endpoints {
require.Equal(t, got.Endpoints[i], tt.want[i])
}
}
})
}
}
2 changes: 1 addition & 1 deletion schema/pstat/pstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s OperationalState) String() string {
case OperationalState_RFNOP:
return "RFNOP"
case OperationalState_SRESET:
return "OperationalState_SRESET"
return "SRESET"
default:
return fmt.Sprintf("unknown %v", int(s))
}
Expand Down
2 changes: 1 addition & 1 deletion schema/pstat/pstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestOperationalStateString(t *testing.T) {
pstat.OperationalState_RFOTM: "RFOTM",
pstat.OperationalState_RFPRO: "RFPRO",
pstat.OperationalState_RFNOP: "RFNOP",
pstat.OperationalState_SRESET: "OperationalState_SRESET",
pstat.OperationalState_SRESET: "SRESET",
}

for k, v := range states {
Expand Down

0 comments on commit d0094bb

Please sign in to comment.