azimuth-client-go/bigint_test.go
Theodore Blackman 4d7ab16549 Initial release of azimuth-client-go library
- Consolidated azimuth client implementations from zenithd and janus
- Type-safe GraphQL client using genqlient code generation
- Comprehensive caching with configurable TTL (1 hour default)
- Full API coverage: authentication keys, ship activity, sponsorship, ownership
- Enhanced functionality combining best features from both original implementations
- Extensive test suite with 16 unit tests covering all functionality
- Complete documentation with examples and usage patterns
- Support for galaxy, star, and planet ship type classification
- BigInt utility for proper GraphQL number handling
- MIT licensed for open source usage

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-30 14:01:03 -04:00

255 lines
4.5 KiB
Go

package azimuth
import (
"encoding/json"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewBigInt(t *testing.T) {
tests := []struct {
name string
value int64
expected string
}{
{
name: "zero",
value: 0,
expected: "0",
},
{
name: "positive",
value: 12345,
expected: "12345",
},
{
name: "negative",
value: -12345,
expected: "-12345",
},
{
name: "large positive",
value: 9223372036854775807, // max int64
expected: "9223372036854775807",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bigInt := NewBigInt(tt.value)
assert.Equal(t, tt.expected, bigInt.String())
})
}
}
func TestNewBigIntFromUint32(t *testing.T) {
tests := []struct {
name string
value uint32
expected string
}{
{
name: "zero",
value: 0,
expected: "0",
},
{
name: "small value",
value: 256,
expected: "256",
},
{
name: "max uint32",
value: 4294967295, // max uint32
expected: "4294967295",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bigInt := NewBigIntFromUint32(tt.value)
assert.Equal(t, tt.expected, bigInt.String())
})
}
}
func TestBigInt_MarshalJSON(t *testing.T) {
tests := []struct {
name string
bigInt BigInt
expected string
}{
{
name: "zero",
bigInt: NewBigInt(0),
expected: "0",
},
{
name: "positive",
bigInt: NewBigInt(12345),
expected: "12345",
},
{
name: "negative",
bigInt: NewBigInt(-12345),
expected: "-12345",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := tt.bigInt.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, tt.expected, string(data))
})
}
}
func TestBigInt_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input string
expected string
expectErr bool
}{
{
name: "zero",
input: `"0"`,
expected: "0",
},
{
name: "positive",
input: `"12345"`,
expected: "12345",
},
{
name: "negative",
input: `"-12345"`,
expected: "-12345",
},
{
name: "large number",
input: `"123456789012345678901234567890"`,
expected: "123456789012345678901234567890",
},
{
name: "null",
input: `null`,
expected: "0", // big.Int zero value
},
{
name: "invalid format",
input: `"not-a-number"`,
expectErr: true,
},
{
name: "invalid json",
input: `invalid-json`,
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var bigInt BigInt
err := bigInt.UnmarshalJSON([]byte(tt.input))
if tt.expectErr {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.expected, bigInt.String())
}
})
}
}
func TestBigInt_ToUint32(t *testing.T) {
tests := []struct {
name string
bigInt BigInt
expected uint32
expectErr bool
}{
{
name: "zero",
bigInt: NewBigInt(0),
expected: 0,
},
{
name: "small positive",
bigInt: NewBigInt(256),
expected: 256,
},
{
name: "max uint32",
bigInt: NewBigIntFromUint32(4294967295),
expected: 4294967295,
},
{
name: "negative",
bigInt: NewBigInt(-1),
expectErr: true,
},
{
name: "too large",
bigInt: BigInt{Int: *big.NewInt(0).SetUint64(4294967296)}, // max uint32 + 1
expectErr: true,
},
{
name: "way too large",
bigInt: func() BigInt {
var b BigInt
b.SetString("123456789012345678901234567890", 10)
return b
}(),
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := tt.bigInt.ToUint32()
if tt.expectErr {
assert.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}
func TestBigInt_JSONRoundTrip(t *testing.T) {
tests := []struct {
name string
value int64
}{
{"zero", 0},
{"positive", 12345},
{"negative", -12345},
{"large", 9223372036854775807},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
original := NewBigInt(tt.value)
// Marshal to JSON
data, err := json.Marshal(original)
require.NoError(t, err)
// Unmarshal from JSON
var unmarshaled BigInt
err = json.Unmarshal(data, &unmarshaled)
require.NoError(t, err)
// Check equality
assert.Equal(t, original.String(), unmarshaled.String())
})
}
}