Skip to content

Commit c571b51

Browse files
Copilotrickyrombo
andauthored
Add unit tests for trashid.IntId
Agent-Logs-Url: https://github.com/AudiusProject/api/sessions/1e868cb7-009d-437d-a60f-b0b1a123b27c Co-authored-by: rickyrombo <3690498+rickyrombo@users.noreply.github.com>
1 parent e1b3d21 commit c571b51

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

trashid/hashid_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,38 @@ func TestHashId(t *testing.T) {
4646
assert.Equal(t, 0, int(h))
4747
}
4848
}
49+
50+
func TestIntId(t *testing.T) {
51+
52+
// when we serialize... it emits a plain number (not a hash string)
53+
{
54+
i := IntId(44)
55+
j, err := json.Marshal(i)
56+
assert.NoError(t, err)
57+
assert.Equal(t, `44`, string(j))
58+
}
59+
60+
// when we parse a hashid string... it decodes to the numeric value
61+
{
62+
var i IntId
63+
err := json.Unmarshal([]byte(`"eYorL"`), &i)
64+
assert.NoError(t, err)
65+
assert.Equal(t, 44, int(i))
66+
}
67+
68+
// when we parse a raw number... it works as-is
69+
{
70+
var i IntId
71+
err := json.Unmarshal([]byte("33"), &i)
72+
assert.NoError(t, err)
73+
assert.Equal(t, 33, int(i))
74+
}
75+
76+
// errors on bad hashid string
77+
{
78+
var i IntId
79+
err := json.Unmarshal([]byte(`"asdjkfalksdjfaklsdjf"`), &i)
80+
assert.Error(t, err)
81+
assert.Equal(t, 0, int(i))
82+
}
83+
}

0 commit comments

Comments
 (0)