Skip to content
This repository was archived by the owner on May 31, 2023. It is now read-only.

Commit ce92eaa

Browse files
author
Richard Patel
committed
more unit tests
1 parent 6ca04b7 commit ce92eaa

File tree

2 files changed

+117
-32
lines changed

2 files changed

+117
-32
lines changed

accounts_test.go

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,40 @@ var (
3232
caseMappingAccount []byte
3333
)
3434

35-
func TestProductAccount(t *testing.T) {
36-
expected := ProductAccount{
37-
AccountHeader: AccountHeader{
38-
Magic: Magic,
39-
Version: V2,
40-
AccountType: AccountTypeProduct,
41-
Size: 161,
42-
},
43-
FirstPrice: solana.MustPublicKeyFromBase58("E36MyBbavhYKHVLWR79GiReNNnBDiHj6nWA7htbkNZbh"),
44-
AttrsData: [464]byte{
45-
0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x0a,
46-
0x46, 0x58, 0x2e, 0x45, 0x55, 0x52, 0x2f, 0x55,
47-
0x53, 0x44, 0x0a, 0x61, 0x73, 0x73, 0x65, 0x74,
48-
0x5f, 0x74, 0x79, 0x70, 0x65, 0x02, 0x46, 0x58,
49-
0x0e, 0x71, 0x75, 0x6f, 0x74, 0x65, 0x5f, 0x63,
50-
0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x03,
51-
0x55, 0x53, 0x44, 0x0b, 0x64, 0x65, 0x73, 0x63,
52-
0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x07,
53-
0x45, 0x55, 0x52, 0x2f, 0x55, 0x53, 0x44, 0x0e,
54-
0x67, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x5f,
55-
0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x06, 0x45,
56-
0x55, 0x52, 0x55, 0x53, 0x44, 0x04, 0x62, 0x61,
57-
0x73, 0x65, 0x03, 0x45, 0x55, 0x52, 0x05, 0x74,
58-
0x65, 0x6e, 0x6f, 0x72, 0x04, 0x53, 0x70, 0x6f,
59-
0x74, 0x53, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
60-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63-
},
64-
}
35+
var productAccount_EWxGfxoPQSNA2744AYdAKmsQZ8F9o9M7oKkvL3VM1dko = ProductAccount{
36+
AccountHeader: AccountHeader{
37+
Magic: Magic,
38+
Version: V2,
39+
AccountType: AccountTypeProduct,
40+
Size: 161,
41+
},
42+
FirstPrice: solana.MustPublicKeyFromBase58("E36MyBbavhYKHVLWR79GiReNNnBDiHj6nWA7htbkNZbh"),
43+
AttrsData: [464]byte{
44+
0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x0a,
45+
0x46, 0x58, 0x2e, 0x45, 0x55, 0x52, 0x2f, 0x55,
46+
0x53, 0x44, 0x0a, 0x61, 0x73, 0x73, 0x65, 0x74,
47+
0x5f, 0x74, 0x79, 0x70, 0x65, 0x02, 0x46, 0x58,
48+
0x0e, 0x71, 0x75, 0x6f, 0x74, 0x65, 0x5f, 0x63,
49+
0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x03,
50+
0x55, 0x53, 0x44, 0x0b, 0x64, 0x65, 0x73, 0x63,
51+
0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x07,
52+
0x45, 0x55, 0x52, 0x2f, 0x55, 0x53, 0x44, 0x0e,
53+
0x67, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x5f,
54+
0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x06, 0x45,
55+
0x55, 0x52, 0x55, 0x53, 0x44, 0x04, 0x62, 0x61,
56+
0x73, 0x65, 0x03, 0x45, 0x55, 0x52, 0x05, 0x74,
57+
0x65, 0x6e, 0x6f, 0x72, 0x04, 0x53, 0x70, 0x6f,
58+
0x74, 0x53, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
59+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62+
},
63+
}
6564

65+
func TestProductAccount(t *testing.T) {
6666
var actual ProductAccount
6767
require.NoError(t, actual.UnmarshalBinary(caseProductAccount))
68-
assert.Equal(t, &expected, &actual)
68+
assert.Equal(t, &productAccount_EWxGfxoPQSNA2744AYdAKmsQZ8F9o9M7oKkvL3VM1dko, &actual)
6969

7070
t.Run("GetAttrsMap", func(t *testing.T) {
7171
expected := map[string]string{
@@ -193,8 +193,20 @@ var priceAccount_E36MyBbavhYKHVLWR79GiReNNnBDiHj6nWA7htbkNZbh = PriceAccount{
193193
func TestPriceAccount(t *testing.T) {
194194
var actual PriceAccount
195195
require.NoError(t, actual.UnmarshalBinary(casePriceAccount))
196-
197196
assert.Equal(t, &priceAccount_E36MyBbavhYKHVLWR79GiReNNnBDiHj6nWA7htbkNZbh, &actual)
197+
198+
t.Run("GetComponent_Exists", func(t *testing.T) {
199+
pubkey := solana.MustPublicKeyFromBase58("EevTjv14eGHqsxKvgpastHsuLr9FNPfzkP23wG61pT2U")
200+
comp := actual.GetComponent(&pubkey)
201+
assert.NotNil(t, comp)
202+
assert.Equal(t, pubkey, comp.Publisher)
203+
})
204+
205+
t.Run("GetComponent_NotExists", func(t *testing.T) {
206+
pubkey := solana.StakeProgramID
207+
comp := actual.GetComponent(&pubkey)
208+
assert.Nil(t, comp)
209+
})
198210
}
199211

200212
func TestMappingAccount(t *testing.T) {

products_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2022 Blockdaemon Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pyth
16+
17+
import (
18+
"context"
19+
"encoding/base64"
20+
"io"
21+
"net/http"
22+
"net/http/httptest"
23+
"testing"
24+
25+
"github.com/gagliardetto/solana-go"
26+
"github.com/stretchr/testify/assert"
27+
"github.com/stretchr/testify/require"
28+
)
29+
30+
func TestClient_GetProductAccount(t *testing.T) {
31+
server := httptest.NewServer(http.HandlerFunc(func(wr http.ResponseWriter, req *http.Request) {
32+
buf, err := io.ReadAll(req.Body)
33+
require.NoError(t, err)
34+
assert.JSONEq(t, `{
35+
"jsonrpc": "2.0",
36+
"id": 0,
37+
"method": "getAccountInfo",
38+
"params": [
39+
"EWxGfxoPQSNA2744AYdAKmsQZ8F9o9M7oKkvL3VM1dko",
40+
{"encoding": "base64"}
41+
]
42+
}`, string(buf))
43+
44+
_, err = wr.Write([]byte(`{
45+
"jsonrpc": "2.0",
46+
"id": 0,
47+
"result": {
48+
"context": {
49+
"slot": 118773287
50+
},
51+
"value": {
52+
"data": [
53+
"` + base64.StdEncoding.EncodeToString(caseProductAccount) + `",
54+
"base64"
55+
],
56+
"executable": false,
57+
"lamports": 23942400,
58+
"owner": "gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s",
59+
"rentEpoch": 274
60+
}
61+
}
62+
}`))
63+
require.NoError(t, err)
64+
}))
65+
defer server.Close()
66+
67+
c := NewClient(ProgramIDDevnet, server.URL, server.URL)
68+
acc, err := c.GetProductAccount(context.Background(), solana.MustPublicKeyFromBase58("EWxGfxoPQSNA2744AYdAKmsQZ8F9o9M7oKkvL3VM1dko"))
69+
require.NoError(t, err)
70+
require.NotNil(t, acc)
71+
72+
assert.Equal(t, &productAccount_EWxGfxoPQSNA2744AYdAKmsQZ8F9o9M7oKkvL3VM1dko, acc)
73+
}

0 commit comments

Comments
 (0)