Skip to content

Commit

Permalink
add test data and test for haversine
Browse files Browse the repository at this point in the history
too many curly brackets

make params be strings

pass strings

remove round to see if this was the problem

let's try casting

round the output field instead

add commas

cast the macro result directly

rename expected to output

is it a rounding error

swap the units

what is going on...

throw a log to see what we get

remove curlies

try computing in a CTE first'

add comma

I'm sleeping...

fixup! I'm sleeping...

rename in CTE

dont quote?

fix macro and separate unit in two tests

fix yaml indent

fix indentation in schema yaml

alias the field

clean up

fixup! clean up

make the macro log stuff

remove logging in test macro

remove the select star
  • Loading branch information
bastienboutonnet committed Mar 6, 2021
1 parent 250eb00 commit cf3c822
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 5 deletions.
2 changes: 2 additions & 0 deletions integration_tests/data/geo/data_haversine_km.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lat_1,lon_1,lat_2,lon_2,output
48.864716,2.349014,52.379189,4.899431,430
2 changes: 2 additions & 0 deletions integration_tests/data/geo/data_haversine_mi.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lat_1,lon_1,lat_2,lon_2,output
48.864716,2.349014,52.379189,4.899431,267
2 changes: 1 addition & 1 deletion integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ seeds:

sql:
data_events_20180103:
+schema: events
+schema: events
1 change: 0 additions & 1 deletion integration_tests/macros/tests.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

{% macro test_assert_equal(model, actual, expected) %}

select count(*) from {{ model }} where {{ actual }} != {{ expected }}

{% endmacro %}
Expand Down
13 changes: 13 additions & 0 deletions integration_tests/models/geo/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2

models:
- name: test_haversine_distance_km
tests:
- assert_equal:
actual: actual
expected: expected
- name: test_haversine_distance_mi
tests:
- assert_equal:
actual: actual
expected: expected
23 changes: 23 additions & 0 deletions integration_tests/models/geo/test_haversine_distance_km.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
with data as (
select * from {{ ref('data_haversine_km') }}
),
final as (
select
output as expected,
cast(
{{
dbt_utils.haversine_distance(
lat1='lat_1',
lon1='lon_1',
lat2='lat_2',
lon2='lon_2',
unit='km'
)
}} as numeric
) as actual
from data
)
select
expected,
round(actual,0) as actual
from final
23 changes: 23 additions & 0 deletions integration_tests/models/geo/test_haversine_distance_mi.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
with data as (
select * from {{ ref('data_haversine_mi') }}
),
final as (
select
output as expected,
cast(
{{
dbt_utils.haversine_distance(
lat1='lat_1',
lon1='lon_1',
lat2='lat_2',
lon2='lon_2',
unit='mi'
)
}} as numeric
) as actual
from data
)
select
expected,
round(actual,0) as actual
from final
7 changes: 4 additions & 3 deletions macros/geo/haversine_distance.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ The arguments should be float type.
{{ return(adapter.dispatch('haversine_distance', packages = dbt_utils._get_utils_namespaces())(lat1,lon1,lat2,lon2,unit)) }}
{% endmacro %}

{% macro default__haversine_distance(lat1,lon1,lat2,lon2,unit='km') -%}
{% macro default__haversine_distance(lat1,lon1,lat2,lon2,unit) -%}
{{log(unit, info=true)}}
{# vanilla macro is in miles #}
{% set conversion = '' %}
{% set conversion_rate = '' %}
{% if unit == 'km' %}
{# we multiply miles result to get it in kms #}
{% set conversion = '* 1.60934' %}
{% set conversion_rate = '* 1.60934' %}
{% endif %}

2 * 3961 * asin(sqrt((sin(radians(({{lat2}} - {{lat1}}) / 2))) ^ 2 +
Expand Down

0 comments on commit cf3c822

Please sign in to comment.