Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: velocity limits #254

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bats/gql/velocity-control-add-limit.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mutation VelocityControlAddLimit($input: VelocityControlAddLimitInput!) {
velocityControlAddLimit(input: $input) {
velocityControl {
limits {
velocityLimitId
}
}
}
}
7 changes: 7 additions & 0 deletions bats/gql/velocity-control-attach.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation VelocityControlAttach($input: VelocityControlAttachInput!) {
velocityControlAttach(input: $input) {
velocityControl {
velocityControlId
}
}
}
7 changes: 7 additions & 0 deletions bats/gql/velocity-control-create.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation VelocityControlCreate($input: VelocityControlCreateInput!) {
velocityControlCreate(input: $input) {
velocityControl {
velocityControlId
}
}
}
7 changes: 7 additions & 0 deletions bats/gql/velocity-limit-create.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation VelocityLimitCreate($input: VelocityLimitCreateInput!) {
velocityLimitCreate(input: $input) {
velocityLimit {
velocityLimitId
}
}
}
64 changes: 64 additions & 0 deletions bats/gql/velocity-tx-template-create.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
mutation VelocityTxTemplateCreate(
$templateId: UUID!
$templateCode: String!
$journalId: Expression!
) {
velocityTemplate: txTemplateCreate(
input: {
txTemplateId: $templateId
code: $templateCode
description: "Velocity transaction template."
params: [
{ name: "recipient", type: UUID, description: "Recipient account ID." }
{ name: "sender", type: UUID, description: "Sender account ID." }
{ name: "journal_id", type: UUID, description: "Journal ID." }
{
name: "amount"
type: DECIMAL
description: "Amount with decimal, e.g. `1.23`."
}
{
name: "currency"
type: STRING
description: "Currency used in the transaction."
default: "'USD'"
}
{
name: "layer"
type: STRING
description: "Layer for the transaction."
default: "'SETTLED'"
}
{
name: "effective"
type: DATE
description: "Effective date for transaction."
default: "date()"
}
]
transaction: { journalId: $journalId, effective: "params.effective" }
entries: [
{
accountId: "params.sender"
units: "params.amount"
currency: "params.currency"
entryType: "'TEST_DR'"
direction: "DEBIT"
layer: "params.layer"
}
{
accountId: "params.recipient"
units: "params.amount"
currency: "params.currency"
entryType: "'TEST_CR'"
direction: "CREDIT"
layer: "params.layer"
}
]
}
) {
txTemplate {
txTemplateId
}
}
}
194 changes: 194 additions & 0 deletions bats/velocity.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#!/usr/bin/env bats

load "helpers"

setup_file() {
start_server
}

teardown_file() {
stop_server
}

@test "cala: create velocity control and post transaction with limits" {
journal_id=$(random_uuid)
variables=$(
jq -n \
--arg journal_id "$journal_id" \
'{
"input": {
"journalId": $journal_id,
"name": "General Ledger",
}
}'
)
exec_graphql 'journal-create' "$variables"
output=$(graphql_output '.data.journalCreate.journal.journalId')
[[ "$output" != "null" ]] || exit 1

# create accounts

sender_account_id=$(random_uuid)
variables=$(
jq -n \
--arg sender_account_id "$sender_account_id" \
'{
"input": {
"accountId": $sender_account_id,
"name": "Sender - Checking",
"code": ("SENDER.CHECKING-" + $sender_account_id),
"normalBalanceType": "CREDIT"
}
}'
)
exec_graphql 'account-create' "$variables"
output=$(graphql_output '.data.accountCreate.account.accountId')
[[ "$output" != "null" ]] || exit 1

recipient_account_id=$(random_uuid)
variables=$(
jq -n \
--arg recipient_account_id "$recipient_account_id" \
'{
"input": {
"accountId": $recipient_account_id,
"name": "Recipient - Checking",
"code": ("RECIPIENT.CHECKING-" + $recipient_account_id),
"normalBalanceType": "DEBIT"
}
}'
)
exec_graphql 'account-create' "$variables"
output=$(graphql_output '.data.accountCreate.account.accountId')
[[ "$output" != "null" ]] || exit 1

# create velocity limits
withdrawal_limit_id=$(random_uuid)
variables=$(
jq -n \
--arg velocityLimitId "$withdrawal_limit_id" \
'{
"input": {
"velocityLimitId": $velocityLimitId,
"name": ("Withdrawal-" + $velocityLimitId),
"description": "Test withdrawal limit",
"window": [],
"currency": null,
"limit": {
"timestampSource": null,
"balance": [{
"limitType": "AVAILABLE",
"layer": "SETTLED",
"amount": "params.withdrawal_limit",
"normalBalanceType": "DEBIT",
}]
},
"params": [{
"name": "withdrawal_limit",
"type": "DECIMAL",
"default": null,
"description": null
}]
}
}'
)
exec_graphql 'velocity-limit-create' "$variables"
velocity_limit_id=$(graphql_output '.data.velocityLimitCreate.velocityLimit.velocityLimitId')
[[ "$velocity_limit_id" == "$withdrawal_limit_id" ]] || exit 1

# create velocity control
control_id=$(random_uuid)
variables=$(
jq -n \
--arg control_id "$control_id" \
'{
"input": {
"velocityControlId": $control_id,
"name": ("Velocity Control-" + $control_id),
"description": "Test velocity control",
"enforcement": {
"velocityEnforcementAction": "REJECT"
}
}
}'
)
exec_graphql 'velocity-control-create' "$variables"
velocity_control_id=$(graphql_output '.data.velocityControlCreate.velocityControl.velocityControlId')
[[ "$velocity_control_id" == "$control_id" ]] || exit 1

# attach limits to control
variables=$(
jq -n \
--arg velocity_control_id "$control_id" \
--arg velocity_limit_id "$withdrawal_limit_id" \
'{
"input": {
"velocityControlId": $velocity_control_id,
"velocityLimitId": $velocity_limit_id
}
}'
)
exec_graphql 'velocity-control-add-limit' "$variables"
n_limits=$(graphql_output '.data.velocityControlAddLimit.velocityControl.limits' | jq length)
[[ $n_limits == 1 ]] || exit 1

# attach control to sender account
limit="100.0"
variables=$(
jq -n \
--arg control_id "$control_id" \
--arg sender_account_id "$sender_account_id" \
--arg limit "$limit" \
'{
"input": {
"velocityControlId": $control_id,
"accountId": $sender_account_id,
"params": {
"withdrawal_limit": $limit,
}
}
}'
)
exec_graphql 'velocity-control-attach' "$variables"
velocity_control_id=$(graphql_output '.data.velocityControlAttach.velocityControl.velocityControlId')
[[ "$velocity_control_id" != "null" ]] || exit 1

# create tx templates

velocity_template_id=$(random_uuid)
variables=$(jq -n \
--arg velocityTemplateId "$velocity_template_id" \
--arg journalId "$journal_id" \
'{
"templateId": $velocityTemplateId,
"templateCode": ("VELOCITY-" + $velocityTemplateId),
"journalId": ("uuid(\u0027" + $journalId + "\u0027)")
}')
exec_graphql 'velocity-tx-template-create' "$variables"

# # post transaction
overflow_limit="101.10"
transaction_id=$(random_uuid)
variables=$(
jq -n \
--arg transaction_id "$transaction_id" \
--arg sender "$sender_account_id" \
--arg recipient "$recipient_account_id" \
--arg templateId "$velocity_template_id" \
--arg overflowLimit "$overflow_limit" \
'{
"input": {
"transactionId": $transaction_id,
"txTemplateCode": ("VELOCITY-" + $templateId),
"params": {
"sender": $sender,
"recipient": $recipient,
"amount": $overflowLimit,
}
}
}'
)
exec_graphql 'transaction-post' "$variables"
error=$(graphql_output '.errors[0].message')
[[ "$error" == *"Enforcement: Velocity limit"* ]] || exit 1
}
2 changes: 1 addition & 1 deletion cala-server/src/graphql/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ impl<E: MutationExtensionMarker> CoreMutation<E> {
.velocities()
.add_limit_to_control_in_op(
&mut op,
input.velocity_limit_id.into(),
input.velocity_control_id.into(),
input.velocity_limit_id.into(),
)
.await?;
Expand Down
Loading