diff --git a/types/address_test.go b/types/address_test.go new file mode 100644 index 00000000..2657980b --- /dev/null +++ b/types/address_test.go @@ -0,0 +1,1222 @@ +package types + +import ( + "reflect" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func TestNodeAddressFromBech32(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want NodeAddress + wantErr bool + }{ + { + "empty", + args{ + s: "", + }, + NodeAddress{}, + true, + }, + { + "invalid", + args{ + s: "invalid", + }, + nil, + true, + }, + { + "invalid prefix", + args{ + s: "sent1qypqxpq9qcrsszgszyfpx9q4zct3sxfq0fzduj", + }, + nil, + true, + }, + { + "10 bytes", + args{ + s: "sentnode1qypqxpq9qcrsszgse4wwrm", + }, + nil, + true, + }, + { + "20 bytes", + args{ + s: "sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqelr5ey", + }, + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + false, + }, + { + "30 bytes", + args{ + s: "sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsxqglcv", + }, + nil, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := NodeAddressFromBech32(tt.args.s) + if (err != nil) != tt.wantErr { + t.Errorf("NodeAddressFromBech32() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("NodeAddressFromBech32() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNodeAddress_Bytes(t *testing.T) { + tests := []struct { + name string + n NodeAddress + want []byte + }{ + { + "nil", + nil, + nil, + }, + { + "empty", + NodeAddress{}, + []byte{}, + }, + { + "10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + }, + { + "20 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + }, + { + "30 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.n.Bytes(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Bytes() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNodeAddress_Empty(t *testing.T) { + tests := []struct { + name string + n NodeAddress + want bool + }{ + { + "nil", + nil, + true}, + { + "empty", + NodeAddress{}, + true, + }, + { + "10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + false, + }, + { + "20 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + false, + }, + { + "30 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.n.Empty(); got != tt.want { + t.Errorf("Empty() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNodeAddress_Equals(t *testing.T) { + type args struct { + address sdk.Address + } + tests := []struct { + name string + n NodeAddress + args args + want bool + }{ + { + "nil", + nil, + args{ + address: nil, + }, + true, + }, + { + "equal type with 0 bytes", + NodeAddress{}, + args{ + address: NodeAddress{}, + }, + true, + }, + { + "unequal type with 0 bytes", + NodeAddress{}, + args{ + address: ProvAddress{}, + }, + true, + }, + { + "unequal type with unequal10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + args{ + address: ProvAddress{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + }, + false, + }, + { + "unequal type with equal 10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + args{ + address: ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + }, + true, + }, + { + "equal type with unequal 10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + args{ + address: NodeAddress{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + }, + false, + }, + { + "equal type with equal 10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + args{ + address: NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.n.Equals(tt.args.address); got != tt.want { + t.Errorf("Equals() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNodeAddress_Marshal(t *testing.T) { + tests := []struct { + name string + n NodeAddress + want []byte + wantErr bool + }{ + { + "nil", + nil, + nil, + false, + }, + { + "empty", + NodeAddress{}, + []byte{}, + false, + }, + { + "10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + false, + }, + { + "20 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + false, + }, + { + "30 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.n.Marshal() + if (err != nil) != tt.wantErr { + t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Marshal() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNodeAddress_MarshalJSON(t *testing.T) { + tests := []struct { + name string + n NodeAddress + want []byte + wantErr bool + }{ + { + "nil", + nil, + []byte(`""`), + false, + }, + { + "empty", + NodeAddress{}, + []byte(`""`), + false, + }, + { + "10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + []byte(`"sentnode1qypqxpq9qcrsszgse4wwrm"`), + false, + }, + { + "20 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + []byte(`"sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqelr5ey"`), + false, + }, + { + "30 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + []byte(`"sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsxqglcv"`), + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.n.MarshalJSON() + if (err != nil) != tt.wantErr { + t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNodeAddress_MarshalYAML(t *testing.T) { + tests := []struct { + name string + n NodeAddress + want interface{} + wantErr bool + }{ + { + "nil", + nil, + "", + false, + }, + { + "empty", + NodeAddress{}, + "", + false, + }, + { + "10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + "sentnode1qypqxpq9qcrsszgse4wwrm", + false, + }, + { + "20 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + "sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqelr5ey", + false, + }, + { + "30 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + "sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsxqglcv", + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.n.MarshalYAML() + if (err != nil) != tt.wantErr { + t.Errorf("MarshalYAML() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MarshalYAML() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNodeAddress_String(t *testing.T) { + tests := []struct { + name string + n NodeAddress + want string + }{ + { + "nil", + nil, + "", + }, + { + "empty", + NodeAddress{}, + "", + }, + { + "10 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + "sentnode1qypqxpq9qcrsszgse4wwrm", + }, + { + "20 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + "sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqelr5ey", + }, + { + "30 bytes", + NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + "sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsxqglcv", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.n.String(); got != tt.want { + t.Errorf("String() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNodeAddress_Unmarshal(t *testing.T) { + type args struct { + data []byte + } + tests := []struct { + name string + n NodeAddress + args args + wantErr bool + }{ + { + "nil", + nil, + args{ + data: nil, + }, + false, + }, + { + "empty", + NodeAddress{}, + args{ + data: []byte{}, + }, + false, + }, + { + "10 bytes", + NodeAddress{}, + args{ + data: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + }, + false, + }, + { + "20 bytes", + NodeAddress{}, + args{ + data: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + }, + false, + }, + { + "30 bytes", + NodeAddress{}, + args{ + data: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.n.Unmarshal(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestNodeAddress_UnmarshalJSON(t *testing.T) { + type args struct { + data []byte + } + tests := []struct { + name string + n NodeAddress + args args + wantErr bool + }{ + { + "nil", + nil, + args{ + data: []byte(`""`), + }, + true, + }, + { + "empty", + NodeAddress{}, + args{ + data: []byte(`""`), + }, + true, + }, + { + "10 bytes", + NodeAddress{}, + args{ + data: []byte(`"sentnode1qypqxpq9qcrsszgse4wwrm"`), + }, + true, + }, + { + "20 bytes", + NodeAddress{}, + args{ + data: []byte(`"sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqelr5ey"`), + }, + false, + }, + { + "30 bytes", + NodeAddress{}, + args{ + data: []byte(`"sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsxqglcv"`), + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.n.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestNodeAddress_UnmarshalYAML(t *testing.T) { + type args struct { + data []byte + } + tests := []struct { + name string + n NodeAddress + args args + wantErr bool + }{ + { + "nil", + nil, + args{ + data: nil, + }, + true, + }, + { + "empty", + NodeAddress{}, + args{ + data: []byte(""), + }, + true, + }, + { + "10 bytes", + NodeAddress{}, + args{ + data: []byte("sentnode1qypqxpq9qcrsszgse4wwrm"), + }, + true, + }, + { + "20 bytes", + NodeAddress{}, + args{ + data: []byte("sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqelr5ey"), + }, + false, + }, + { + "30 bytes", + NodeAddress{}, + args{ + data: []byte("sentnode1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsxqglcv"), + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.n.UnmarshalYAML(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalYAML() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestProvAddressFromBech32(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want ProvAddress + wantErr bool + }{ + { + "empty", + args{ + s: "", + }, + ProvAddress{}, + true, + }, + { + "invalid", + args{ + s: "invalid", + }, + nil, + true, + }, + { + "invalid prefix", + args{s: "sent1qypqxpq9qcrsszgszyfpx9q4zct3sxfq0fzduj", + }, + nil, + true, + }, + { + "10 bytes", + args{ + s: "sentprov1qypqxpq9qcrsszgsutj8xr", + }, + nil, + true, + }, + { + "20 bytes", + args{ + s: "sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfq877k82", + }, + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + false, + }, + { + "30 bytes", + args{ + s: "sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsh33zgx", + }, + nil, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := ProvAddressFromBech32(tt.args.s) + if (err != nil) != tt.wantErr { + t.Errorf("ProvAddressFromBech32() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("ProvAddressFromBech32() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProvAddress_Bytes(t *testing.T) { + tests := []struct { + name string + p ProvAddress + want []byte + }{ + { + "nil", + nil, + nil, + }, + { + "empty", + ProvAddress{}, + []byte{}, + }, + { + "10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + }, + { + "20 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + }, + { + "30 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.p.Bytes(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Bytes() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProvAddress_Empty(t *testing.T) { + tests := []struct { + name string + p ProvAddress + want bool + }{ + { + "nil", + nil, + true, + }, + { + "empty", + ProvAddress{}, + true, + }, + { + "10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + false, + }, + { + "20 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + false, + }, + { + "30 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.p.Empty(); got != tt.want { + t.Errorf("Empty() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProvAddress_Equals(t *testing.T) { + type args struct { + address sdk.Address + } + tests := []struct { + name string + p ProvAddress + args args + want bool + }{ + { + "nil", + nil, + args{ + address: nil, + }, + true, + }, + { + "equal type with 0 bytes", + ProvAddress{}, + args{ + address: ProvAddress{}, + }, + true, + }, + { + "unequal type with 0 bytes", + ProvAddress{}, + args{ + address: NodeAddress{}, + }, + true, + }, + { + "unequal type with unequal10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + args{ + address: NodeAddress{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + }, + false, + }, + { + "unequal type with equal 10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + args{ + address: NodeAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + }, + true, + }, + { + "equal type with unequal 10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + args{ + address: ProvAddress{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + }, + false, + }, + { + "equal type with equal 10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + args{ + address: ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.p.Equals(tt.args.address); got != tt.want { + t.Errorf("Equals() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProvAddress_Marshal(t *testing.T) { + tests := []struct { + name string + p ProvAddress + want []byte + wantErr bool + }{ + { + "nil", + nil, + nil, + false, + }, + { + "empty", + ProvAddress{}, + []byte{}, + false, + }, + { + "10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + false, + }, + { + "20 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + false, + }, + { + "30 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.p.Marshal() + if (err != nil) != tt.wantErr { + t.Errorf("Marshal() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Marshal() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProvAddress_MarshalJSON(t *testing.T) { + tests := []struct { + name string + p ProvAddress + want []byte + wantErr bool + }{ + { + "nil", + nil, + []byte(`""`), + false, + }, + { + "empty", + ProvAddress{}, + []byte(`""`), + false, + }, + { + "10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + []byte(`"sentprov1qypqxpq9qcrsszgsutj8xr"`), + false, + }, + { + "20 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + []byte(`"sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfq877k82"`), + false, + }, + { + "30 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + []byte(`"sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsh33zgx"`), + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.p.MarshalJSON() + if (err != nil) != tt.wantErr { + t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProvAddress_MarshalYAML(t *testing.T) { + tests := []struct { + name string + p ProvAddress + want interface{} + wantErr bool + }{ + { + "nil", + nil, + "", + false, + }, + { + "empty", + ProvAddress{}, + "", + false, + }, + { + "10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + "sentprov1qypqxpq9qcrsszgsutj8xr", + false, + }, + { + "20 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + "sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfq877k82", + false, + }, + { + "30 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + "sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsh33zgx", + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.p.MarshalYAML() + if (err != nil) != tt.wantErr { + t.Errorf("MarshalYAML() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("MarshalYAML() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProvAddress_String(t *testing.T) { + tests := []struct { + name string + p ProvAddress + want string + }{ + { + "nil", + nil, + "", + }, + { + "empty", + ProvAddress{}, + "", + }, + { + "10 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + "sentprov1qypqxpq9qcrsszgsutj8xr", + }, + { + "20 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + "sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfq877k82", + }, + { + "30 bytes", + ProvAddress{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + "sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsh33zgx", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.p.String(); got != tt.want { + t.Errorf("String() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProvAddress_Unmarshal(t *testing.T) { + type args struct { + data []byte + } + tests := []struct { + name string + p ProvAddress + args args + wantErr bool + }{ + { + "nil", + nil, + args{ + data: nil, + }, + false, + }, + { + "empty", + ProvAddress{}, + args{ + data: []byte{}, + }, + false, + }, + { + "10 bytes", + ProvAddress{}, + args{ + data: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, + }, + false, + }, + { + "20 bytes", + ProvAddress{}, + args{ + data: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20}, + }, + false, + }, + { + "30 bytes", + ProvAddress{}, + args{ + data: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30}, + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.p.Unmarshal(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestProvAddress_UnmarshalJSON(t *testing.T) { + type args struct { + data []byte + } + tests := []struct { + name string + p ProvAddress + args args + wantErr bool + }{ + { + "nil", + nil, + args{ + data: []byte(`""`), + }, + true, + }, + { + "empty", + ProvAddress{}, + args{ + data: []byte(`""`), + }, + true, + }, + { + "10 bytes", + ProvAddress{}, + args{ + data: []byte(`"sentprov1qypqxpq9qcrsszgsutj8xr"`), + }, + true, + }, + { + "20 bytes", + ProvAddress{}, + args{ + data: []byte(`"sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfq877k82"`), + }, + false, + }, + { + "30 bytes", + ProvAddress{}, + args{ + data: []byte(`"sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsh33zgx"`), + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.p.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestProvAddress_UnmarshalYAML(t *testing.T) { + type args struct { + data []byte + } + tests := []struct { + name string + p ProvAddress + args args + wantErr bool + }{ + { + "nil", + nil, + args{ + data: nil, + }, + true, + }, + { + "empty", + ProvAddress{}, + args{ + data: []byte(""), + }, + true, + }, + { + "10 bytes", + ProvAddress{}, + args{ + data: []byte("sentprov1qypqxpq9qcrsszgsutj8xr"), + }, + true, + }, + { + "20 bytes", + ProvAddress{}, + args{ + data: []byte("sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfq877k82"), + }, + false, + }, + { + "30 bytes", + ProvAddress{}, + args{ + data: []byte("sentprov1qypqxpq9qcrsszgszyfpx9q4zct3sxfqyy3zxfp9ycnjs2fsh33zgx"), + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.p.UnmarshalYAML(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalYAML() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/types/bandwidth_test.go b/types/bandwidth_test.go new file mode 100644 index 00000000..6cb3f2f8 --- /dev/null +++ b/types/bandwidth_test.go @@ -0,0 +1,1067 @@ +package types + +import ( + "reflect" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func TestBandwidth_Add(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + type args struct { + v Bandwidth + } + tests := []struct { + name string + fields fields + args args + want Bandwidth + }{ + { + "negative upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(-1000)}, + }, + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(-1000)}, + }, + { + "negative upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(0)}, + }, + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(0)}, + }, + { + "negative upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(1000)}, + }, + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(1000)}, + }, + { + "zero upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(-1000)}, + }, + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(-1000)}, + }, + { + "zero upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(0)}, + }, + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(0)}, + }, + { + "zero upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(1000)}, + }, + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(1000)}, + }, + { + "positive upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(-1000)}, + }, + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(-1000)}, + }, + { + "positive upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(0)}, + }, + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(0)}, + }, + { + "positive upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(1000)}, + }, + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(1000)}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.Add(tt.args.v); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Add() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBandwidth_CeilTo(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + type args struct { + precision sdk.Int + } + tests := []struct { + name string + fields fields + args args + want Bandwidth + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.CeilTo(tt.args.precision); !reflect.DeepEqual(got, tt.want) { + t.Errorf("CeilTo() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBandwidth_IsAllLTE(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + type args struct { + v Bandwidth + } + tests := []struct { + name string + fields fields + args args + want bool + }{ + { + "negative upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(-1000)}, + }, + false, + }, + { + "negative upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(0)}, + }, + false, + }, + { + "negative upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(1000)}, + }, + false, + }, + { + "zero upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(-1000)}, + }, + false, + }, + { + "zero upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(0)}, + }, + true, + }, + { + "zero upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(1000)}, + }, + true, + }, + { + "positive upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(-1000)}, + }, + false, + }, + { + "positive upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(0)}, + }, + true, + }, + { + "positive upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(1000)}, + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.IsAllLTE(tt.args.v); got != tt.want { + t.Errorf("IsAllLTE() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBandwidth_IsAllPositive(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + tests := []struct { + name string + fields fields + want bool + }{ + { + "negative upload and negative download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(-1000), + }, + false, + }, + { + "negative upload and zero download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(0), + }, + false, + }, + { + "negative upload and positive download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(1000), + }, + false, + }, + { + "zero upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(-1000), + }, + false, + }, + { + "zero upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + false, + }, + { + "zero upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(1000), + }, + false, + }, + { + "positive upload and negative download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(-1000), + }, + false, + }, + { + "positive upload and zero download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(0), + }, + false, + }, + { + "positive upload and positive download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(1000), + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.IsAllPositive(); got != tt.want { + t.Errorf("IsAllPositive() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBandwidth_IsAllZero(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + tests := []struct { + name string + fields fields + want bool + }{ + { + "negative upload and negative download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(-1000), + }, + false, + }, + { + "negative upload and zero download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(0), + }, + false, + }, + { + "negative upload and positive download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(1000), + }, + false, + }, + { + "zero upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(-1000), + }, + false, + }, + { + "zero upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + true, + }, + { + "zero upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(1000), + }, + false, + }, + { + "positive upload and negative download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(-1000), + }, + false, + }, + { + "positive upload and zero download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(0), + }, + false, + }, + { + "positive upload and positive download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(1000), + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.IsAllZero(); got != tt.want { + t.Errorf("IsAllZero() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBandwidth_IsAnyGT(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + type args struct { + v Bandwidth + } + tests := []struct { + name string + fields fields + args args + want bool + }{ + { + "negative upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(-1000)}, + }, + true, + }, + { + "negative upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(0)}, + }, + true, + }, + { + "negative upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(1000)}, + }, + true, + }, + { + "zero upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(-1000)}, + }, + true, + }, + { + "zero upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(0)}, + }, + false, + }, + { + "zero upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(1000)}, + }, + false, + }, + { + "positive upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(-1000)}, + }, + true, + }, + { + "positive upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(0)}, + }, + false, + }, + { + "positive upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(1000)}, + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.IsAnyGT(tt.args.v); got != tt.want { + t.Errorf("IsAnyGT() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBandwidth_IsAnyNegative(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + tests := []struct { + name string + fields fields + want bool + }{ + { + "negative upload and negative download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(-1000), + }, + true, + }, + { + "negative upload and zero download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(0), + }, + true, + }, + { + "negative upload and positive download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(1000), + }, + true, + }, + { + "zero upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(-1000), + }, + true, + }, + { + "zero upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + false, + }, + { + "zero upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(1000), + }, + false, + }, + { + "positive upload and negative download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(-1000), + }, + true, + }, + { + "positive upload and zero download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(0), + }, + false, + }, + { + "positive upload and positive download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(1000), + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.IsAnyNegative(); got != tt.want { + t.Errorf("IsAnyNegative() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBandwidth_IsAnyZero(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + tests := []struct { + name string + fields fields + want bool + }{ + { + "negative upload and negative download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(-1000), + }, + false, + }, + { + "negative upload and zero download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(0), + }, + true, + }, + { + "negative upload and positive download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(1000), + }, + false, + }, + { + "zero upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(-1000), + }, + true, + }, + { + "zero upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + true, + }, + { + "zero upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(1000), + }, + true, + }, + { + "positive upload and negative download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(-1000), + }, + false, + }, + { + "positive upload and zero download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(0), + }, + true, + }, + { + "positive upload and positive download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(1000), + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.IsAnyZero(); got != tt.want { + t.Errorf("IsAnyZero() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBandwidth_Sub(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + type args struct { + v Bandwidth + } + tests := []struct { + name string + fields fields + args args + want Bandwidth + }{ + { + "negative upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(-1000)}, + }, + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(1000)}, + }, + { + "negative upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(0)}, + }, + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(0)}, + }, + { + "negative upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(1000)}, + }, + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(-1000)}, + }, + { + "zero upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(-1000)}, + }, + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(1000)}, + }, + { + "zero upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(0)}, + }, + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(0)}, + }, + { + "zero upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(1000)}, + }, + Bandwidth{Upload: sdk.NewInt(0), Download: sdk.NewInt(-1000)}, + }, + { + "positive upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(-1000)}, + }, + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(1000)}, + }, + { + "positive upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(0)}, + }, + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(0)}, + }, + { + "positive upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + args{ + Bandwidth{Upload: sdk.NewInt(1000), Download: sdk.NewInt(1000)}, + }, + Bandwidth{Upload: sdk.NewInt(-1000), Download: sdk.NewInt(-1000)}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.Sub(tt.args.v); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Sub() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestBandwidth_Sum(t *testing.T) { + type fields struct { + Upload sdk.Int + Download sdk.Int + } + tests := []struct { + name string + fields fields + want sdk.Int + }{ + { + "negative upload and negative download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(-1000), + }, + sdk.NewInt(-2000), + }, + { + "negative upload and zero download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(0), + }, + sdk.NewInt(-1000), + }, + { + "negative upload and positive download", + fields{ + Upload: sdk.NewInt(-1000), + Download: sdk.NewInt(1000), + }, + sdk.NewInt(0), + }, + { + "zero upload and negative download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(-1000), + }, + sdk.NewInt(-1000), + }, + { + "zero upload and zero download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(0), + }, + sdk.NewInt(0), + }, + { + "zero upload and positive download", + fields{ + Upload: sdk.NewInt(0), + Download: sdk.NewInt(1000), + }, + sdk.NewInt(1000), + }, + { + "positive upload and negative download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(-1000), + }, + sdk.NewInt(0), + }, + { + "positive upload and zero download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(0), + }, + sdk.NewInt(1000), + }, + { + "positive upload and positive download", + fields{ + Upload: sdk.NewInt(1000), + Download: sdk.NewInt(1000), + }, + sdk.NewInt(2000), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := Bandwidth{ + Upload: tt.fields.Upload, + Download: tt.fields.Download, + } + if got := b.Sum(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Sum() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNewBandwidth(t *testing.T) { + type args struct { + upload sdk.Int + download sdk.Int + } + tests := []struct { + name string + args args + want Bandwidth + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewBandwidth(tt.args.upload, tt.args.download); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewBandwidth() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNewBandwidthFromInt64(t *testing.T) { + type args struct { + upload int64 + download int64 + } + tests := []struct { + name string + args args + want Bandwidth + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewBandwidthFromInt64(tt.args.upload, tt.args.download); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewBandwidthFromInt64() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/types/config_test.go b/types/config_test.go new file mode 100644 index 00000000..d75b78ed --- /dev/null +++ b/types/config_test.go @@ -0,0 +1,200 @@ +package types + +import ( + "sync" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func TestConfig_GetBech32NodeAddrPrefix(t *testing.T) { + type fields struct { + Config *sdk.Config + prefixes map[string]string + sealed bool + mtx sync.Mutex + } + tests := []struct { + name string + fields fields + want string + }{ + { + "invalid prefix", + fields{ + Config: sdk.GetConfig(), + prefixes: map[string]string{"node_addr": "sent"}, + sealed: false, + mtx: sync.Mutex{}, + }, + "sent", + }, + { + "valid prefix", + fields{ + Config: sdk.GetConfig(), + prefixes: map[string]string{"node_addr": "sentnode"}, + sealed: false, + mtx: sync.Mutex{}, + }, + "sentnode", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Config{ + Config: tt.fields.Config, + prefixes: tt.fields.prefixes, + sealed: tt.fields.sealed, + mtx: tt.fields.mtx, + } + if got := c.GetBech32NodeAddrPrefix(); got != tt.want { + t.Errorf("GetBech32NodeAddrPrefix() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestConfig_GetBech32NodePubPrefix(t *testing.T) { + type fields struct { + Config *sdk.Config + prefixes map[string]string + sealed bool + mtx sync.Mutex + } + tests := []struct { + name string + fields fields + want string + }{ + { + "invalid prefix", + fields{ + Config: sdk.GetConfig(), + prefixes: map[string]string{"node_pub": "sentpub"}, + sealed: false, + mtx: sync.Mutex{}, + }, + "sentpub", + }, + { + "valid prefix", + fields{ + Config: sdk.GetConfig(), + prefixes: map[string]string{"node_pub": "sentnodepub"}, + sealed: false, + mtx: sync.Mutex{}, + }, + "sentnodepub", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Config{ + Config: tt.fields.Config, + prefixes: tt.fields.prefixes, + sealed: tt.fields.sealed, + mtx: tt.fields.mtx, + } + if got := c.GetBech32NodePubPrefix(); got != tt.want { + t.Errorf("GetBech32NodePubPrefix() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestConfig_GetBech32ProviderAddrPrefix(t *testing.T) { + type fields struct { + Config *sdk.Config + prefixes map[string]string + sealed bool + mtx sync.Mutex + } + tests := []struct { + name string + fields fields + want string + }{ + { + "invalid prefix", + fields{ + Config: sdk.GetConfig(), + prefixes: map[string]string{"provider_addr": "sent"}, + sealed: false, + mtx: sync.Mutex{}, + }, + "sent", + }, + { + "valid prefix", + fields{ + Config: sdk.GetConfig(), + prefixes: map[string]string{"provider_addr": "sentprov"}, + sealed: false, + mtx: sync.Mutex{}, + }, + "sentprov", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Config{ + Config: tt.fields.Config, + prefixes: tt.fields.prefixes, + sealed: tt.fields.sealed, + mtx: tt.fields.mtx, + } + if got := c.GetBech32ProviderAddrPrefix(); got != tt.want { + t.Errorf("GetBech32ProviderAddrPrefix() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestConfig_GetBech32ProviderPubPrefix(t *testing.T) { + type fields struct { + Config *sdk.Config + prefixes map[string]string + sealed bool + mtx sync.Mutex + } + tests := []struct { + name string + fields fields + want string + }{ + { + "invalid prefix", + fields{ + Config: sdk.GetConfig(), + prefixes: map[string]string{"provider_pub": "sentpub"}, + sealed: false, + mtx: sync.Mutex{}, + }, + "sentpub", + }, + { + "valid prefix", + fields{ + Config: sdk.GetConfig(), + prefixes: map[string]string{"provider_pub": "sentprovpub"}, + sealed: false, + mtx: sync.Mutex{}, + }, + "sentprovpub", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Config{ + Config: tt.fields.Config, + prefixes: tt.fields.prefixes, + sealed: tt.fields.sealed, + mtx: tt.fields.mtx, + } + if got := c.GetBech32ProviderPubPrefix(); got != tt.want { + t.Errorf("GetBech32ProviderPubPrefix() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/types/status_test.go b/types/status_test.go new file mode 100644 index 00000000..fd8bb275 --- /dev/null +++ b/types/status_test.go @@ -0,0 +1,250 @@ +package types + +import ( + "testing" +) + +func TestStatusFromString(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want Status + }{ + { + "empty", + args{ + s: "", + }, + StatusUnknown, + }, + { + "invalid", + args{ + s: "invalid", + }, + StatusUnknown, + }, + { + "unknown", + args{ + s: "unknown", + }, + StatusUnknown, + }, + { + "active", + args{ + s: "Active", + }, + StatusActive, + }, + { + "inactive pending", + args{ + s: "InactivePending", + }, + StatusInactivePending, + }, + { + "inactive", + args{ + s: "Inactive", + }, + StatusInactive, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := StatusFromString(tt.args.s); got != tt.want { + t.Errorf("StatusFromString() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestStatus_Equal(t *testing.T) { + type args struct { + v Status + } + tests := []struct { + name string + s Status + args args + want bool + }{ + { + "unknown and unknown", + StatusUnknown, + args{ + v: StatusUnknown, + }, + true, + }, + { + "unknown and active", + StatusUnknown, + args{ + v: StatusActive, + }, + false, + }, + { + "unknown and inactive pending", + StatusUnknown, + args{ + v: StatusInactivePending, + }, + false, + }, + { + "unknown and inactive", + StatusUnknown, + args{ + v: StatusInactive, + }, + false, + }, + { + "active and unknown", + StatusActive, + args{ + v: StatusUnknown, + }, + false, + }, + { + "active and active", + StatusActive, + args{ + v: StatusActive, + }, + true, + }, + { + "active and inactive pending", + StatusActive, + args{ + v: StatusInactivePending, + }, + false, + }, + { + "active and inactive", + StatusActive, + args{ + v: StatusInactive, + }, + false, + }, + { + "inactive pending and unknown", + StatusInactivePending, + args{ + v: StatusUnknown, + }, + false, + }, + { + "inactive pending and active", + StatusInactivePending, + args{ + v: StatusActive, + }, + false, + }, + { + "inactive pending and inactive pending", + StatusInactivePending, + args{ + v: StatusInactivePending, + }, + true, + }, + { + "inactive pending and inactive", + StatusInactivePending, + args{ + v: StatusInactive, + }, + false, + }, + { + "inactive and unknown", + StatusInactive, + args{ + v: StatusUnknown, + }, + false, + }, + { + "inactive and active", + StatusInactive, + args{ + v: StatusActive, + }, + false, + }, + { + "inactive and inactive pending", + StatusInactive, + args{ + v: StatusInactivePending, + }, + false, + }, + { + "inactive and inactive", + StatusInactive, + args{ + v: StatusInactive, + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.s.Equal(tt.args.v); got != tt.want { + t.Errorf("Equal() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestStatus_IsValid(t *testing.T) { + tests := []struct { + name string + s Status + want bool + }{ + { + "unknown", + StatusUnknown, + false, + }, + { + "active", + StatusActive, + true, + }, + { + "inactive pending", + StatusInactive, + true, + }, + { + "inactive", + StatusInactive, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.s.IsValid(); got != tt.want { + t.Errorf("IsValid() = %v, want %v", got, tt.want) + } + }) + } +}