Skip to content

Commit

Permalink
Fix tests and null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jklein24 committed Nov 9, 2023
1 parent 06ddcda commit 26eb908
Show file tree
Hide file tree
Showing 25 changed files with 74 additions and 36 deletions.
10 changes: 4 additions & 6 deletions lightspark/__tests__/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@

class TestSerialization:
def test_serialize_deserialize_invoice_data(self):
serialized = '{"__typename": "InvoiceData", "invoice_data_encoded_payment_request":"lnbcrt34170n1pj5vdn4pp56jhw0672v566u4rvl333v8hwwuvavvu9gx4a2mqag4pkrvm0hwkqhp5xaz278y6cejcvpqnndl4wfq3slgthjduwlfksg778aevn23v2pdscqzpgxqyz5vqsp5ee5jezfvjqvvz7hfwta3ekk8hs6dq36szkgp40qh7twa8upquxlq9qyyssqjg2slc95falxf2t67y0wu2w43qwfcvfflwl8tn4ppqw9tumwqxk36qkfct9p2w8c3yy2ld7c6nacy4ssv2gl6qyqfpmhl4jmarnjf8cpvjlxek","invoice_data_bitcoin_network":"REGTEST","invoice_data_payment_hash":"d4aee7ebca6535ae546cfc63161eee7719d6338541abd56c1d454361b36fbbac","invoice_data_amount":{"currency_amount_original_value":3417,"currency_amount_original_unit":"SATOSHI","currency_amount_preferred_currency_unit":"USD","currency_amount_preferred_currency_value_rounded":118,"currency_amount_preferred_currency_value_approx":118.89352818371607},"invoice_data_created_at":"2023-11-04T12:17:57Z","invoice_data_expires_at":"2023-11-05T12:17:57Z","invoice_data_memo":null,"invoice_data_destination":{"graph_node_id":"GraphNode:0189a572-6dba-cf00-0000-ac0908d34ea6","graph_node_created_at":"2023-07-30T06:18:07.162759Z","graph_node_updated_at":"2023-11-04T12:01:04.015414Z","graph_node_alias":"ls_test_vSViIQitob_SE","graph_node_bitcoin_network":"REGTEST","graph_node_color":"#3399ff","graph_node_conductivity":null,"graph_node_display_name":"ls_test_vSViIQitob_SE","graph_node_public_key":"02253935a5703a6f0429081e08d2defce0faa15f4d75305302284751d53a4e0608", "__typename":"GraphNode"}}'
serialized = '{"__typename": "InvoiceData", "invoice_data_encoded_payment_request":"lnbcrt34170n1pj5vdn4pp56jhw0672v566u4rvl333v8hwwuvavvu9gx4a2mqag4pkrvm0hwkqhp5xaz278y6cejcvpqnndl4wfq3slgthjduwlfksg778aevn23v2pdscqzpgxqyz5vqsp5ee5jezfvjqvvz7hfwta3ekk8hs6dq36szkgp40qh7twa8upquxlq9qyyssqjg2slc95falxf2t67y0wu2w43qwfcvfflwl8tn4ppqw9tumwqxk36qkfct9p2w8c3yy2ld7c6nacy4ssv2gl6qyqfpmhl4jmarnjf8cpvjlxek","invoice_data_bitcoin_network":"REGTEST","invoice_data_payment_hash":"d4aee7ebca6535ae546cfc63161eee7719d6338541abd56c1d454361b36fbbac","invoice_data_amount":{"currency_amount_original_value":3417,"currency_amount_original_unit":"SATOSHI","currency_amount_preferred_currency_unit":"USD","currency_amount_preferred_currency_value_rounded":118,"currency_amount_preferred_currency_value_approx":118.89352818371607},"invoice_data_created_at":"2023-11-04T12:17:57+00:00","invoice_data_expires_at":"2023-11-05T12:17:57+00:00","invoice_data_memo":null,"invoice_data_destination":{"graph_node_id":"GraphNode:0189a572-6dba-cf00-0000-ac0908d34ea6","graph_node_created_at":"2023-07-30T06:18:07.162759+00:00","graph_node_updated_at":"2023-11-04T12:01:04.015414+00:00","graph_node_alias":"ls_test_vSViIQitob_SE","graph_node_bitcoin_network":"REGTEST","graph_node_color":"#3399ff","graph_node_conductivity":null,"graph_node_display_name":"ls_test_vSViIQitob_SE","graph_node_public_key":"02253935a5703a6f0429081e08d2defce0faa15f4d75305302284751d53a4e0608", "__typename":"GraphNode"}}'
deserialized = InvoiceData_from_json(None, json.loads(serialized))
reserialized = deserialized.to_json()
# TODO(Jeremy): Fix this assertion by making iso format consistent w.r.t. trailing Z.
# assert reserialized == json.loads(serialized)
assert reserialized == json.loads(serialized)

deserialized_again = InvoiceData_from_json(None, reserialized)
assert deserialized_again == deserialized

def test_serialize_deserialize_graph_node(self):
serialized = '{"graph_node_id":"GraphNode:0189a572-6dba-cf00-0000-ac0908d34ea6","graph_node_created_at":"2023-07-30T06:18:07.162759Z","graph_node_updated_at":"2023-11-04T12:01:04.015414Z","graph_node_alias":"ls_test_vSViIQitob_SE","graph_node_bitcoin_network":"REGTEST","graph_node_color":"#3399ff","graph_node_conductivity":null,"graph_node_display_name":"ls_test_vSViIQitob_SE","graph_node_public_key":"02253935a5703a6f0429081e08d2defce0faa15f4d75305302284751d53a4e0608", "__typename":"GraphNode"}'
serialized = '{"graph_node_id":"GraphNode:0189a572-6dba-cf00-0000-ac0908d34ea6","graph_node_created_at":"2023-07-30T06:18:07.162759+00:00","graph_node_updated_at":"2023-11-04T12:01:04.015414+00:00","graph_node_alias":"ls_test_vSViIQitob_SE","graph_node_bitcoin_network":"REGTEST","graph_node_color":"#3399ff","graph_node_conductivity":null,"graph_node_display_name":"ls_test_vSViIQitob_SE","graph_node_public_key":"02253935a5703a6f0429081e08d2defce0faa15f4d75305302284751d53a4e0608", "__typename":"GraphNode"}'
deserialized = Node_from_json(None, json.loads(serialized))
reserialized = deserialized.to_json()
# TODO(Jeremy): Fix this assertion by making iso format consistent w.r.t. trailing Z.
# assert reserialized == json.loads(serialized)
assert reserialized == json.loads(serialized)

deserialized_again = Node_from_json(None, reserialized)
assert deserialized_again == deserialized
2 changes: 1 addition & 1 deletion lightspark/objects/ApiToken.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def to_json(self) -> Mapping[str, Any]:
"api_token_updated_at": self.updated_at.isoformat(),
"api_token_client_id": self.client_id,
"api_token_name": self.name,
"api_token_permissions": [e.to_json() for e in self.permissions],
"api_token_permissions": [e.value for e in self.permissions],
}


Expand Down
2 changes: 1 addition & 1 deletion lightspark/objects/Channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def to_json(self) -> Mapping[str, Any]:
"channel_total_balance": self.total_balance.to_json()
if self.total_balance
else None,
"channel_status": self.status.value,
"channel_status": self.status.value if self.status else None,
"channel_estimated_force_closure_wait_minutes": self.estimated_force_closure_wait_minutes,
"channel_commit_fee": self.commit_fee.to_json()
if self.commit_fee
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/ChannelClosingTransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def to_json(self) -> Mapping[str, Any]:
"channel_closing_transaction_created_at": self.created_at.isoformat(),
"channel_closing_transaction_updated_at": self.updated_at.isoformat(),
"channel_closing_transaction_status": self.status.value,
"channel_closing_transaction_resolved_at": self.resolved_at.isoformat(),
"channel_closing_transaction_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"channel_closing_transaction_amount": self.amount.to_json(),
"channel_closing_transaction_transaction_hash": self.transaction_hash,
"channel_closing_transaction_fees": self.fees.to_json()
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/ChannelOpeningTransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def to_json(self) -> Mapping[str, Any]:
"channel_opening_transaction_created_at": self.created_at.isoformat(),
"channel_opening_transaction_updated_at": self.updated_at.isoformat(),
"channel_opening_transaction_status": self.status.value,
"channel_opening_transaction_resolved_at": self.resolved_at.isoformat(),
"channel_opening_transaction_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"channel_opening_transaction_amount": self.amount.to_json(),
"channel_opening_transaction_transaction_hash": self.transaction_hash,
"channel_opening_transaction_fees": self.fees.to_json()
Expand Down
4 changes: 1 addition & 3 deletions lightspark/objects/CreateApiTokenInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class CreateApiTokenInput:
def to_json(self) -> Mapping[str, Any]:
return {
"create_api_token_input_name": self.name,
"create_api_token_input_permissions": [
e.to_json() for e in self.permissions
],
"create_api_token_input_permissions": [e.value for e in self.permissions],
}


Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/CreateInvoiceInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def to_json(self) -> Mapping[str, Any]:
"create_invoice_input_node_id": self.node_id,
"create_invoice_input_amount_msats": self.amount_msats,
"create_invoice_input_memo": self.memo,
"create_invoice_input_invoice_type": self.invoice_type.value,
"create_invoice_input_invoice_type": self.invoice_type.value
if self.invoice_type
else None,
"create_invoice_input_expiry_secs": self.expiry_secs,
}

Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/CreateTestModeInvoiceInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def to_json(self) -> Mapping[str, Any]:
"create_test_mode_invoice_input_local_node_id": self.local_node_id,
"create_test_mode_invoice_input_amount_msats": self.amount_msats,
"create_test_mode_invoice_input_memo": self.memo,
"create_test_mode_invoice_input_invoice_type": self.invoice_type.value,
"create_test_mode_invoice_input_invoice_type": self.invoice_type.value
if self.invoice_type
else None,
}


Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/Deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def to_json(self) -> Mapping[str, Any]:
"deposit_created_at": self.created_at.isoformat(),
"deposit_updated_at": self.updated_at.isoformat(),
"deposit_status": self.status.value,
"deposit_resolved_at": self.resolved_at.isoformat(),
"deposit_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"deposit_amount": self.amount.to_json(),
"deposit_transaction_hash": self.transaction_hash,
"deposit_fees": self.fees.to_json() if self.fees else None,
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/IncomingPayment.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def to_json(self) -> Mapping[str, Any]:
"incoming_payment_created_at": self.created_at.isoformat(),
"incoming_payment_updated_at": self.updated_at.isoformat(),
"incoming_payment_status": self.status.value,
"incoming_payment_resolved_at": self.resolved_at.isoformat(),
"incoming_payment_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"incoming_payment_amount": self.amount.to_json(),
"incoming_payment_transaction_hash": self.transaction_hash,
"incoming_payment_destination": {"id": self.destination_id},
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/IncomingPaymentAttempt.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def to_json(self) -> Mapping[str, Any]:
"incoming_payment_attempt_created_at": self.created_at.isoformat(),
"incoming_payment_attempt_updated_at": self.updated_at.isoformat(),
"incoming_payment_attempt_status": self.status.value,
"incoming_payment_attempt_resolved_at": self.resolved_at.isoformat(),
"incoming_payment_attempt_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"incoming_payment_attempt_amount": self.amount.to_json(),
"incoming_payment_attempt_channel": {"id": self.channel_id},
}
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/LightningTransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def to_json(self) -> Mapping[str, Any]:
"lightning_transaction_created_at": self.created_at.isoformat(),
"lightning_transaction_updated_at": self.updated_at.isoformat(),
"lightning_transaction_status": self.status.value,
"lightning_transaction_resolved_at": self.resolved_at.isoformat(),
"lightning_transaction_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"lightning_transaction_amount": self.amount.to_json(),
"lightning_transaction_transaction_hash": self.transaction_hash,
}
Expand Down
2 changes: 1 addition & 1 deletion lightspark/objects/LightsparkNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def to_json(self) -> Mapping[str, Any]:
"lightspark_node_display_name": self.display_name,
"lightspark_node_public_key": self.public_key,
"lightspark_node_owner": {"id": self.owner_id},
"lightspark_node_status": self.status.value,
"lightspark_node_status": self.status.value if self.status else None,
"lightspark_node_total_balance": self.total_balance.to_json()
if self.total_balance
else None,
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/LightsparkNodeWithOSK.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ def to_json(self) -> Mapping[str, Any]:
"lightspark_node_with_o_s_k_display_name": self.display_name,
"lightspark_node_with_o_s_k_public_key": self.public_key,
"lightspark_node_with_o_s_k_owner": {"id": self.owner_id},
"lightspark_node_with_o_s_k_status": self.status.value,
"lightspark_node_with_o_s_k_status": self.status.value
if self.status
else None,
"lightspark_node_with_o_s_k_total_balance": self.total_balance.to_json()
if self.total_balance
else None,
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/LightsparkNodeWithRemoteSigning.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ def to_json(self) -> Mapping[str, Any]:
"lightspark_node_with_remote_signing_display_name": self.display_name,
"lightspark_node_with_remote_signing_public_key": self.public_key,
"lightspark_node_with_remote_signing_owner": {"id": self.owner_id},
"lightspark_node_with_remote_signing_status": self.status.value,
"lightspark_node_with_remote_signing_status": self.status.value
if self.status
else None,
"lightspark_node_with_remote_signing_total_balance": self.total_balance.to_json()
if self.total_balance
else None,
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/OnChainTransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def to_json(self) -> Mapping[str, Any]:
"on_chain_transaction_created_at": self.created_at.isoformat(),
"on_chain_transaction_updated_at": self.updated_at.isoformat(),
"on_chain_transaction_status": self.status.value,
"on_chain_transaction_resolved_at": self.resolved_at.isoformat(),
"on_chain_transaction_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"on_chain_transaction_amount": self.amount.to_json(),
"on_chain_transaction_transaction_hash": self.transaction_hash,
"on_chain_transaction_fees": self.fees.to_json() if self.fees else None,
Expand Down
8 changes: 6 additions & 2 deletions lightspark/objects/OutgoingPayment.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ def to_json(self) -> Mapping[str, Any]:
"outgoing_payment_created_at": self.created_at.isoformat(),
"outgoing_payment_updated_at": self.updated_at.isoformat(),
"outgoing_payment_status": self.status.value,
"outgoing_payment_resolved_at": self.resolved_at.isoformat(),
"outgoing_payment_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"outgoing_payment_amount": self.amount.to_json(),
"outgoing_payment_transaction_hash": self.transaction_hash,
"outgoing_payment_origin": {"id": self.origin_id},
Expand All @@ -202,7 +204,9 @@ def to_json(self) -> Mapping[str, Any]:
"outgoing_payment_payment_request_data": self.payment_request_data.to_json()
if self.payment_request_data
else None,
"outgoing_payment_failure_reason": self.failure_reason.value,
"outgoing_payment_failure_reason": self.failure_reason.value
if self.failure_reason
else None,
"outgoing_payment_failure_message": self.failure_message.to_json()
if self.failure_message
else None,
Expand Down
8 changes: 6 additions & 2 deletions lightspark/objects/OutgoingPaymentAttempt.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,13 @@ def to_json(self) -> Mapping[str, Any]:
"outgoing_payment_attempt_created_at": self.created_at.isoformat(),
"outgoing_payment_attempt_updated_at": self.updated_at.isoformat(),
"outgoing_payment_attempt_status": self.status.value,
"outgoing_payment_attempt_failure_code": self.failure_code.value,
"outgoing_payment_attempt_failure_code": self.failure_code.value
if self.failure_code
else None,
"outgoing_payment_attempt_failure_source_index": self.failure_source_index,
"outgoing_payment_attempt_resolved_at": self.resolved_at.isoformat(),
"outgoing_payment_attempt_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"outgoing_payment_attempt_amount": self.amount.to_json()
if self.amount
else None,
Expand Down
2 changes: 1 addition & 1 deletion lightspark/objects/OutgoingPaymentsForInvoiceQueryInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def to_json(self) -> Mapping[str, Any]:
return {
"outgoing_payments_for_invoice_query_input_encoded_invoice": self.encoded_invoice,
"outgoing_payments_for_invoice_query_input_statuses": [
e.to_json() for e in self.statuses
e.value for e in self.statuses
]
if self.statuses
else None,
Expand Down
8 changes: 6 additions & 2 deletions lightspark/objects/RoutingTransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def to_json(self) -> Mapping[str, Any]:
"routing_transaction_created_at": self.created_at.isoformat(),
"routing_transaction_updated_at": self.updated_at.isoformat(),
"routing_transaction_status": self.status.value,
"routing_transaction_resolved_at": self.resolved_at.isoformat(),
"routing_transaction_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"routing_transaction_amount": self.amount.to_json(),
"routing_transaction_transaction_hash": self.transaction_hash,
"routing_transaction_incoming_channel": {"id": self.incoming_channel_id}
Expand All @@ -85,7 +87,9 @@ def to_json(self) -> Mapping[str, Any]:
"routing_transaction_failure_message": self.failure_message.to_json()
if self.failure_message
else None,
"routing_transaction_failure_reason": self.failure_reason.value,
"routing_transaction_failure_reason": self.failure_reason.value
if self.failure_reason
else None,
}


Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/Transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def to_json(self) -> Mapping[str, Any]:
"transaction_created_at": self.created_at.isoformat(),
"transaction_updated_at": self.updated_at.isoformat(),
"transaction_status": self.status.value,
"transaction_resolved_at": self.resolved_at.isoformat(),
"transaction_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"transaction_amount": self.amount.to_json(),
"transaction_transaction_hash": self.transaction_hash,
}
Expand Down
4 changes: 2 additions & 2 deletions lightspark/objects/TransactionFailures.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class TransactionFailures:
def to_json(self) -> Mapping[str, Any]:
return {
"transaction_failures_payment_failures": [
e.to_json() for e in self.payment_failures
e.value for e in self.payment_failures
]
if self.payment_failures
else None,
"transaction_failures_routing_transaction_failures": [
e.to_json() for e in self.routing_transaction_failures
e.value for e in self.routing_transaction_failures
]
if self.routing_transaction_failures
else None,
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/Wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,9 @@ def to_json(self) -> Mapping[str, Any]:
"wallet_id": self.id,
"wallet_created_at": self.created_at.isoformat(),
"wallet_updated_at": self.updated_at.isoformat(),
"wallet_last_login_at": self.last_login_at.isoformat(),
"wallet_last_login_at": self.last_login_at.isoformat()
if self.last_login_at
else None,
"wallet_balances": self.balances.to_json() if self.balances else None,
"wallet_third_party_identifier": self.third_party_identifier,
"wallet_account": {"id": self.account_id} if self.account_id else None,
Expand Down
4 changes: 3 additions & 1 deletion lightspark/objects/Withdrawal.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def to_json(self) -> Mapping[str, Any]:
"withdrawal_created_at": self.created_at.isoformat(),
"withdrawal_updated_at": self.updated_at.isoformat(),
"withdrawal_status": self.status.value,
"withdrawal_resolved_at": self.resolved_at.isoformat(),
"withdrawal_resolved_at": self.resolved_at.isoformat()
if self.resolved_at
else None,
"withdrawal_amount": self.amount.to_json(),
"withdrawal_transaction_hash": self.transaction_hash,
"withdrawal_fees": self.fees.to_json() if self.fees else None,
Expand Down
Loading

0 comments on commit 26eb908

Please sign in to comment.