Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
feat(BUX-222): exposing mock consts for tests (#30)
Browse files Browse the repository at this point in the history
* feat(BUX-222): exposing mock consts for tests

* refactor(BUX-222): name changes
  • Loading branch information
kuba-4chain authored Sep 26, 2023
1 parent a0e339c commit bc5174e
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 57 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,30 @@ result, err := client.GetPolicyQuote(ctx) // client will return a response after

If you pass the context without a timeout, the client will instantly return a successful response (just like from a MockSuccess type).

### Mock Responses Constants

In order to test the Mock Client, you may often need to check if the response from the client is correct. Therefore, some useful constants are exposed from `fixtures` package.

```go
const (
ProviderMain = "https://mocked_arc_api_url.com/arc"
ProviderSecondary = "https://secondary_mocked_arc_api_url.com/arc"
MaxScriptSizePolicy = 100000000
MaxTxSigOpsCountPolicy = 4294967295
MaxTxSizePolicy = 100000000
MaxTxSizePolicySecondary = 220000000
MiningFeeBytes = 1000
SatoshisPerBytes = 1
SatoshisPerBytesSecondary = 2
Timestamp = "2023-09-05T17:03:49.537230128Z"
TimestampSecondary = "2023-09-05T17:05:29.736256927Z"
TxResponseStatus = 200
TxResponseTitle = "OK"
TxStatus = "SEEN_ON_NETWORK"
TxBlockHash = "123123"
TxBlockHashSecondary = "123123"
TxBlockHeight = 123456
TxBlockHeightSecondary = 123456
TxExtraInfo = ""
)
```
23 changes: 23 additions & 0 deletions broadcast/broadcast-client-mock/fixtures/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fixtures

const (
ProviderMain = "https://mocked_arc_api_url.com/arc"
ProviderSecondary = "https://secondary_mocked_arc_api_url.com/arc"
MaxScriptSizePolicy = 100000000
MaxTxSigOpsCountPolicy = 4294967295
MaxTxSizePolicy = 100000000
MaxTxSizePolicySecondary = 220000000
MiningFeeBytes = 1000
SatoshisPerBytes = 1
SatoshisPerBytesSecondary = 2
Timestamp = "2023-09-05T17:03:49.537230128Z"
TimestampSecondary = "2023-09-05T17:05:29.736256927Z"
TxResponseStatus = 200
TxResponseTitle = "OK"
TxStatus = "SEEN_ON_NETWORK"
TxBlockHash = "0000000000000000019a575e0ea4d9bbe251dd24c473a0d8407935973151f282"
TxBlockHashSecondary = "0000000000000000045c969f3acd5db37896aba95f91389f2d191496bf15584b"
TxBlockHeight = 800182
TxBlockHeightSecondary = 799439
TxExtraInfo = ""
)
29 changes: 15 additions & 14 deletions broadcast/internal/arc/mocks/arc_client_mock_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import (
"context"

broadcast_api "github.com/bitcoin-sv/go-broadcast-client/broadcast"
"github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client-mock/fixtures"
)

type ArcClientMock struct{}

// GetFeeQuote returns a successful FeeQuote response.
func (*ArcClientMock) GetFeeQuote(ctx context.Context) ([]*broadcast_api.FeeQuote, error) {
quote1 := &broadcast_api.FeeQuote{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl1},
MiningFee: Policy1.Policy.MiningFee,
Timestamp: Policy1.Timestamp,
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderMain},
MiningFee: policy1.Policy.MiningFee,
Timestamp: policy1.Timestamp,
}

quote2 := &broadcast_api.FeeQuote{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl2},
MiningFee: Policy2.Policy.MiningFee,
Timestamp: Policy2.Timestamp,
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderSecondary},
MiningFee: policy2.Policy.MiningFee,
Timestamp: policy2.Timestamp,
}

quotes := make([]*broadcast_api.FeeQuote, 2)
Expand All @@ -32,32 +33,32 @@ func (*ArcClientMock) GetFeeQuote(ctx context.Context) ([]*broadcast_api.FeeQuot
// GetPolicyQuote return a successful PolicyQuoteResponse.
func (*ArcClientMock) GetPolicyQuote(ctx context.Context) ([]*broadcast_api.PolicyQuoteResponse, error) {
policies := make([]*broadcast_api.PolicyQuoteResponse, 2)
policies = append(policies, Policy1)
policies = append(policies, Policy2)
policies = append(policies, policy1)
policies = append(policies, policy2)

return policies, nil
}

// QueryTransaction returns a successful QueryTxResponse.
func (*ArcClientMock) QueryTransaction(ctx context.Context, txID string) (*broadcast_api.QueryTxResponse, error) {
return QueryTx(txID), nil
return queryTx(txID), nil
}

// SubmitTransaction returns a successful SubmitTxResponse.
func (*ArcClientMock) SubmitTransaction(ctx context.Context, tx *broadcast_api.Transaction, opts ...broadcast_api.TransactionOptFunc) (*broadcast_api.SubmitTxResponse, error) {
return &broadcast_api.SubmitTxResponse{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl1},
SubmittedTx: SubmittedTx,
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderMain},
SubmittedTx: submittedTx,
}, nil
}

// SubmitBatchTransactions returns a successful SubmitBatchTxResponse.
func (*ArcClientMock) SubmitBatchTransactions(ctx context.Context, tx []*broadcast_api.Transaction, opts ...broadcast_api.TransactionOptFunc) (*broadcast_api.SubmitBatchTxResponse, error) {
return &broadcast_api.SubmitBatchTxResponse{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl1},
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderMain},
Transactions: []*broadcast_api.SubmittedTx{
SubmittedTx,
SubmittedTx,
submittedTx,
submittedTxSecondary,
},
}, nil
}
Expand Down
29 changes: 15 additions & 14 deletions broadcast/internal/arc/mocks/arc_client_mock_timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

broadcast_api "github.com/bitcoin-sv/go-broadcast-client/broadcast"
"github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client-mock/fixtures"
)

type ArcClientMockTimeout struct{}
Expand All @@ -16,15 +17,15 @@ func (*ArcClientMockTimeout) GetFeeQuote(ctx context.Context) ([]*broadcast_api.
}

quote1 := &broadcast_api.FeeQuote{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl1},
MiningFee: Policy1.Policy.MiningFee,
Timestamp: Policy1.Timestamp,
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderMain},
MiningFee: policy1.Policy.MiningFee,
Timestamp: policy1.Timestamp,
}

quote2 := &broadcast_api.FeeQuote{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl2},
MiningFee: Policy2.Policy.MiningFee,
Timestamp: Policy2.Timestamp,
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderSecondary},
MiningFee: policy2.Policy.MiningFee,
Timestamp: policy2.Timestamp,
}

quotes := make([]*broadcast_api.FeeQuote, 2)
Expand All @@ -41,8 +42,8 @@ func (*ArcClientMockTimeout) GetPolicyQuote(ctx context.Context) ([]*broadcast_a
}

policies := make([]*broadcast_api.PolicyQuoteResponse, 2)
policies = append(policies, Policy1)
policies = append(policies, Policy2)
policies = append(policies, policy1)
policies = append(policies, policy2)

return policies, nil
}
Expand All @@ -53,7 +54,7 @@ func (*ArcClientMockTimeout) QueryTransaction(ctx context.Context, txID string)
time.Sleep(time.Until(deadline) + 10*time.Millisecond)
}

return QueryTx(txID), nil
return queryTx(txID), nil
}

// SubmitTransaction returns a successful SubmitTxResponse.
Expand All @@ -63,8 +64,8 @@ func (*ArcClientMockTimeout) SubmitTransaction(ctx context.Context, tx *broadcas
}

return &broadcast_api.SubmitTxResponse{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl1},
SubmittedTx: SubmittedTx,
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderMain},
SubmittedTx: submittedTx,
}, nil
}

Expand All @@ -75,10 +76,10 @@ func (*ArcClientMockTimeout) SubmitBatchTransactions(ctx context.Context, tx []*
}

return &broadcast_api.SubmitBatchTxResponse{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl1},
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderMain},
Transactions: []*broadcast_api.SubmittedTx{
SubmittedTx,
SubmittedTx,
submittedTx,
submittedTxSecondary,
},
}, nil
}
Expand Down
70 changes: 41 additions & 29 deletions broadcast/internal/arc/mocks/consts.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,65 @@
package mocks

import broadcast_api "github.com/bitcoin-sv/go-broadcast-client/broadcast"

const (
MockedApiUrl1 = "https://mocked_api_url.com/arc"
MockedApiUrl2 = "https://second_mocked_api_url.com/arc"
import (
broadcast_api "github.com/bitcoin-sv/go-broadcast-client/broadcast"
"github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client-mock/fixtures"
)

var (
Policy1 = &broadcast_api.PolicyQuoteResponse{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl1},
policy1 = &broadcast_api.PolicyQuoteResponse{
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderMain},
Policy: broadcast_api.PolicyResponse{
MaxScriptSizePolicy: 100000000,
MaxTxSigOpsCountPolicy: 4294967295,
MaxTxSizePolicy: 100000000,
MaxScriptSizePolicy: fixtures.MaxScriptSizePolicy,
MaxTxSigOpsCountPolicy: fixtures.MaxTxSigOpsCountPolicy,
MaxTxSizePolicy: fixtures.MaxTxSizePolicy,
MiningFee: broadcast_api.MiningFeeResponse{
Bytes: 1000,
Satoshis: 1,
Bytes: fixtures.MiningFeeBytes,
Satoshis: fixtures.SatoshisPerBytes,
},
},
Timestamp: "2023-09-05T17:03:49.537230128Z",
Timestamp: fixtures.Timestamp,
}

Policy2 = &broadcast_api.PolicyQuoteResponse{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl2},
policy2 = &broadcast_api.PolicyQuoteResponse{
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderSecondary},
Policy: broadcast_api.PolicyResponse{
MaxScriptSizePolicy: 100000000,
MaxTxSigOpsCountPolicy: 4294967295,
MaxTxSizePolicy: 220000000,
MaxScriptSizePolicy: fixtures.MaxScriptSizePolicy,
MaxTxSigOpsCountPolicy: fixtures.MaxTxSigOpsCountPolicy,
MaxTxSizePolicy: fixtures.MaxTxSizePolicySecondary,
MiningFee: broadcast_api.MiningFeeResponse{
Bytes: 1000,
Satoshis: 2,
Bytes: fixtures.MiningFeeBytes,
Satoshis: fixtures.SatoshisPerBytesSecondary,
},
},
Timestamp: "2023-09-05T17:05:29.736256927Z",
Timestamp: fixtures.TimestampSecondary,
}

submittedTx = &broadcast_api.SubmittedTx{
Status: fixtures.TxResponseStatus,
Title: fixtures.TxResponseTitle,
TxStatus: fixtures.TxStatus,
BlockHash: fixtures.TxBlockHash,
BlockHeight: fixtures.TxBlockHeight,
ExtraInfo: fixtures.TxExtraInfo,
}

SubmittedTx = &broadcast_api.SubmittedTx{
Status: 200,
Title: "OK",
TxStatus: "SENT_TO_NETWORK",
submittedTxSecondary = &broadcast_api.SubmittedTx{
Status: fixtures.TxResponseStatus,
Title: fixtures.TxResponseTitle,
TxStatus: fixtures.TxStatus,
BlockHash: fixtures.TxBlockHashSecondary,
BlockHeight: fixtures.TxBlockHeightSecondary,
ExtraInfo: fixtures.TxExtraInfo,
}
)

func QueryTx(txID string) *broadcast_api.QueryTxResponse {
func queryTx(txID string) *broadcast_api.QueryTxResponse {
return &broadcast_api.QueryTxResponse{
BaseResponse: broadcast_api.BaseResponse{Miner: MockedApiUrl1},
Timestamp: "2023-09-05T17:05:29.736256927Z",
BaseResponse: broadcast_api.BaseResponse{Miner: fixtures.ProviderMain},
Timestamp: fixtures.Timestamp,
TxStatus: fixtures.TxStatus,
BlockHash: fixtures.TxBlockHash,
BlockHeight: fixtures.TxBlockHeight,
TxID: txID,
TxStatus: "SEEN_ON_NETWORK",
}
}

0 comments on commit bc5174e

Please sign in to comment.