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

Fix divide by zero error #138

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# dbt_quickbooks v0.14.1
[PR #138](https://github.com/fivetran/dbt_quickbooks/pull/138) introduces the following updates:

## Bug Fixes
- Added `nullif` logic to account for "divide by zero" errors in `int_quickbooks__deposit_double_entry` and `int_quickbooks__deposit_transactions` for when `total_amount` values from the `deposit` source table
are zero.

## Under the Hood
- Added integrity test within integration tests to compare deposit amounts between `int_quickbooks__deposit_double_entry` and deposit `transaction_type` values in `quickbooks__general_ledger`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also just drop the name of the actual integrity test to make it more straightforward

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed!

- Modified seed files in `integration_tests` to reproduce issue and confirm fixes.

# dbt_quickbooks v0.14.0
New major feature alert! Multicurrency is here!

Expand Down
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
config-version: 2
name: 'quickbooks'

version: '0.14.0'
version: '0.14.1'

require-dbt-version: [">=1.3.0", "<2.0.0"]

Expand Down
2 changes: 1 addition & 1 deletion docs/catalog.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/manifest.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/run_results.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions integration_tests/ci/sample.profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ integration_tests:
pass: "{{ env_var('CI_REDSHIFT_DBT_PASS') }}"
dbname: "{{ env_var('CI_REDSHIFT_DBT_DBNAME') }}"
port: 5439
schema: quickbooks_integration_tests_12
schema: quickbooks_integration_tests_13
threads: 8
bigquery:
type: bigquery
method: service-account-json
project: 'dbt-package-testing'
schema: quickbooks_integration_tests_12
schema: quickbooks_integration_tests_13
threads: 8
keyfile_json: "{{ env_var('GCLOUD_SERVICE_KEY') | as_native }}"
snowflake:
Expand All @@ -33,7 +33,7 @@ integration_tests:
role: "{{ env_var('CI_SNOWFLAKE_DBT_ROLE') }}"
database: "{{ env_var('CI_SNOWFLAKE_DBT_DATABASE') }}"
warehouse: "{{ env_var('CI_SNOWFLAKE_DBT_WAREHOUSE') }}"
schema: quickbooks_integration_tests_12
schema: quickbooks_integration_tests_13
threads: 8
postgres:
type: postgres
Expand All @@ -42,13 +42,13 @@ integration_tests:
pass: "{{ env_var('CI_POSTGRES_DBT_PASS') }}"
dbname: "{{ env_var('CI_POSTGRES_DBT_DBNAME') }}"
port: 5432
schema: quickbooks_integration_tests_12
schema: quickbooks_integration_tests_13
threads: 8
databricks:
catalog: "{{ env_var('CI_DATABRICKS_DBT_CATALOG') }}"
host: "{{ env_var('CI_DATABRICKS_DBT_HOST') }}"
http_path: "{{ env_var('CI_DATABRICKS_DBT_HTTP_PATH') }}"
schema: quickbooks_integration_tests_12
schema: quickbooks_integration_tests_13
threads: 8
token: "{{ env_var('CI_DATABRICKS_DBT_TOKEN') }}"
type: databricks
4 changes: 2 additions & 2 deletions integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: 'quickbooks_integration_tests'

version: '0.14.0'
version: '0.14.1'

profile: 'integration_tests'
config-version: 2
Expand All @@ -21,7 +21,7 @@ models:

vars:
quickbooks_source:
quickbooks_schema: quickbooks_integration_tests_12
quickbooks_schema: quickbooks_integration_tests_13
quickbooks_account_identifier: "account_data"
quickbooks_address_identifier: "address_data"
quickbooks_bill_line_identifier: "bill_line_data"
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/seeds/deposit_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ id,_fivetran_deleted,_fivetran_synced,account_id,cash_back_account_id,cash_back_
2,FALSE,2020-06-23 3:58:58,28,,,,2020-05-08 14:33:20,USD,,,96766,7705f28cac4ed281fca93f2d6047ac67,0,96766,2020-03-27,,,2020-05-08 14:33:20
31,FALSE,2020-06-23 3:58:58,28,,,,2020-05-28 13:09:23,USD,,,23039,906b6741066b841342224242068cf2dc,0,23039,2020-04-20,,,2020-05-28 13:09:23
79,FALSE,2020-08-06 18:31:59,28,,,,2020-08-06 18:28:45,USD,,,62931,3ef789b0719133953ab39ea39c75ee7e,0,62931,2020-07-23,,,2020-08-06 18:28:45
137,FALSE,2020-11-10 21:02:04,28,,,,2020-11-03 16:57:25,USD,,,24943,714c8fcbb71513414ad57c0895031129,1,24943,2020-10-28,,,2020-11-10 20:52:40
137,FALSE,2020-11-10 21:02:04,28,,,,2020-11-03 16:57:25,USD,,,24943,714c8fcbb71513414ad57c0895031129,1,24943,2020-10-28,,,2020-11-10 20:52:40
125,FALSE,2020-11-10 21:02:04,75,,,,2020-11-03 16:57:25,USD,,,24943,714c8fcbb71513414ad57c0895031129,1,24943,2020-10-28,,,2020-11-10 20:52:40
3 changes: 2 additions & 1 deletion integration_tests/seeds/deposit_line_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ deposit_id,index,_fivetran_synced,amount,deposit_account_id,deposit_check_number
79,0,2020-08-06 18:31:59,43904,39,,,10,,,,,3ef789b0719133953ab39ea39c75ee7e,4a1ce41b08324458de7d55135d7c76d1,,,,,,,,,,
31,0,2020-06-23 3:58:58,37680,39,,,10,,,,,906b6741066b841342224242068cf2dc,4a1ce41b08324458de7d55135d7c76d1,,,,,,,,,,
66,0,2020-07-15 20:17:26,93599,37,,,18,,,,,2a223879a93f26644c14892df42f3d1d,4a1ce41b08324458de7d55135d7c76d1,,,,,,,,,,
65,0,2020-07-15 20:17:26,43456,37,,,18,,,,,4136eb6f5a9a0820d9d4e0a10b6c88db,4a1ce41b08324458de7d55135d7c76d1,,,,,,,,,,
65,0,2020-07-15 20:17:26,43456,37,,,18,,,,,4136eb6f5a9a0820d9d4e0a10b6c88db,4a1ce41b08324458de7d55135d7c76d1,,,,,,,,,,
125,0,2020-07-15 20:17:26,43456,44,,,28,,,,,4136eb6f5a9a0820d9d4e0a10b6c88dc,4a1ce41b08324458de7d55135d7c76d2,,,,,,,,,,
85 changes: 85 additions & 0 deletions integration_tests/tests/integrity/deposit_ledger_amounts_match.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

{{ config(
tags="fivetran_validations",
enabled=var('fivetran_validation_tests_enabled', false)
) }}

with deposits as (

select *
from {{ ref('int_quickbooks__deposit_double_entry') }}
),


accounts as (

select *
from {{ ref('int_quickbooks__account_classifications') }}
),

deposits_ledger_source as (

select
deposits.transaction_id,
deposits.source_relation,
deposits.index as transaction_index,
deposits.account_id,
deposits.transaction_type,
deposits.transaction_source,
sum(case when accounts.transaction_type = deposits.transaction_type
then deposits.amount
else deposits.amount * -1 end) as adjusted_amount_source,
sum(case when accounts.transaction_type = deposits.transaction_type
then deposits.converted_amount
else deposits.converted_amount * -1 end) as adjusted_converted_amount_source
from deposits

left join accounts
on deposits.account_id = accounts.account_id
and deposits.source_relation = accounts.source_relation

group by 1, 2, 3, 4, 5, 6
),

deposits_ledger_end as (

select
transaction_id,
source_relation,
transaction_index,
account_id,
transaction_type,
transaction_source,
adjusted_amount as adjusted_amount_end,
adjusted_converted_amount as adjusted_converted_amount_end
from {{ ref('quickbooks__general_ledger') }}
where transaction_type = 'deposit'
),

match_check as (

select
deposits_ledger_source.transaction_id,
deposits_ledger_source.source_relation,
deposits_ledger_source.transaction_index,
deposits_ledger_source.account_id,
deposits_ledger_source.transaction_type,
deposits_ledger_source.transaction_source,
deposits_ledger_source.adjusted_amount_source,
deposits_ledger_source.adjusted_converted_amount_source,
deposits_ledger_end.adjusted_amount_end,
deposits_ledger_end.adjusted_converted_amount_end
from deposits_ledger_source
full outer join deposits_ledger_end
on deposits_ledger_source.transaction_id = deposits_ledger_end.transaction_id
and deposits_ledger_source.source_relation = deposits_ledger_end.source_relation
and deposits_ledger_source.transaction_index = deposits_ledger_end.transaction_index
and deposits_ledger_source.account_id = deposits_ledger_end.account_id
and deposits_ledger_source.transaction_type = deposits_ledger_end.transaction_type
and deposits_ledger_source.transaction_source = deposits_ledger_end.transaction_source
)

select *
from match_check
where abs(adjusted_amount_source - adjusted_amount_end) >= 0.01
or abs(adjusted_converted_amount_source - adjusted_converted_amount_end) >= 0.01
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ deposit_join as (
deposit_lines.index,
deposits.transaction_date,
deposit_lines.amount,
deposit_lines.amount * (coalesce(deposits.home_total_amount/deposits.total_amount, 1)) as converted_amount,
deposit_lines.amount * (coalesce(deposits.home_total_amount/nullif(deposits.total_amount, 0), 1)) as converted_amount,
deposits.account_id as deposit_to_acct_id,
coalesce(deposit_lines.deposit_account_id, uf_accounts.account_id) as deposit_from_acct_id,
deposit_customer_id as customer_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ final as (
cast(null as {{ dbt.type_string() }}) as billable_status,
deposit_lines.description,
deposit_lines.amount,
deposit_lines.amount * (coalesce(deposits.home_total_amount/deposits.total_amount, 1)) as converted_amount,
deposit_lines.amount * (coalesce(deposits.home_total_amount/nullif(deposits.total_amount, 0), 1)) as converted_amount,
deposits.total_amount,
deposits.total_amount * (coalesce(deposits.home_total_amount/deposits.total_amount, 1)) as total_converted_amount
deposits.total_amount * (coalesce(deposits.home_total_amount/nullif(deposits.total_amount, 0), 1)) as total_converted_amount
from deposits

inner join deposit_lines
Expand Down