Skip to content

Commit cf3c822

Browse files
add test data and test for haversine
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
1 parent 250eb00 commit cf3c822

8 files changed

+68
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lat_1,lon_1,lat_2,lon_2,output
2+
48.864716,2.349014,52.379189,4.899431,430
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lat_1,lon_1,lat_2,lon_2,output
2+
48.864716,2.349014,52.379189,4.899431,267

integration_tests/dbt_project.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ seeds:
5252

5353
sql:
5454
data_events_20180103:
55-
+schema: events
55+
+schema: events

integration_tests/macros/tests.sql

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
{% macro test_assert_equal(model, actual, expected) %}
3-
43
select count(*) from {{ model }} where {{ actual }} != {{ expected }}
54

65
{% endmacro %}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 2
2+
3+
models:
4+
- name: test_haversine_distance_km
5+
tests:
6+
- assert_equal:
7+
actual: actual
8+
expected: expected
9+
- name: test_haversine_distance_mi
10+
tests:
11+
- assert_equal:
12+
actual: actual
13+
expected: expected
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
with data as (
2+
select * from {{ ref('data_haversine_km') }}
3+
),
4+
final as (
5+
select
6+
output as expected,
7+
cast(
8+
{{
9+
dbt_utils.haversine_distance(
10+
lat1='lat_1',
11+
lon1='lon_1',
12+
lat2='lat_2',
13+
lon2='lon_2',
14+
unit='km'
15+
)
16+
}} as numeric
17+
) as actual
18+
from data
19+
)
20+
select
21+
expected,
22+
round(actual,0) as actual
23+
from final
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
with data as (
2+
select * from {{ ref('data_haversine_mi') }}
3+
),
4+
final as (
5+
select
6+
output as expected,
7+
cast(
8+
{{
9+
dbt_utils.haversine_distance(
10+
lat1='lat_1',
11+
lon1='lon_1',
12+
lat2='lat_2',
13+
lon2='lon_2',
14+
unit='mi'
15+
)
16+
}} as numeric
17+
) as actual
18+
from data
19+
)
20+
select
21+
expected,
22+
round(actual,0) as actual
23+
from final

macros/geo/haversine_distance.sql

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ The arguments should be float type.
1010
{{ return(adapter.dispatch('haversine_distance', packages = dbt_utils._get_utils_namespaces())(lat1,lon1,lat2,lon2,unit)) }}
1111
{% endmacro %}
1212

13-
{% macro default__haversine_distance(lat1,lon1,lat2,lon2,unit='km') -%}
13+
{% macro default__haversine_distance(lat1,lon1,lat2,lon2,unit) -%}
14+
{{log(unit, info=true)}}
1415
{# vanilla macro is in miles #}
15-
{% set conversion = '' %}
16+
{% set conversion_rate = '' %}
1617
{% if unit == 'km' %}
1718
{# we multiply miles result to get it in kms #}
18-
{% set conversion = '* 1.60934' %}
19+
{% set conversion_rate = '* 1.60934' %}
1920
{% endif %}
2021

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

0 commit comments

Comments
 (0)