Skip to content

Compare the results of two PostgreSQL queries to determine if they are "approximately" the same

Notifications You must be signed in to change notification settings

kljensen/pgtap-results-approx-eq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 

Repository files navigation

pgTAP approximately equal

I need a pgTAP test that is similar to results_eq but allows for some kind of tolerance when comparing numeric values. E.g. I'd like to say if one row has 30.23223 and another 30.23225 for some column a that those should be consider equal.

This is my attempt to write such a function. You can see the API in the approx-equal.sql file. It's like

SELECT pg_temp.results_approx_equal(
    $$select * from (values (1,50), (1, 10)) vals(a, b)$$,
    $$select * from (values (2,55), (0, 5)) vals(a, b)$$,
    json_build_object('a', 1, 'b', 5)
);

In that code block I'm saying "consider two rows to be equivalent if their a columns differ by no more than 1 and their b columns differ by no more than 5.

You can run the tests in the approx-equal.sql file like

psql service=your-pg-service-or-whatever -f ./approx-equal.sql

That assumes you've installed pgTAP.

License

pgTap code is Copyright (c) 2008-2020 David E. Wheeler. Code I wrote and didn't copy is available under the Unlicense.

About

Compare the results of two PostgreSQL queries to determine if they are "approximately" the same

Topics

Resources

Stars

Watchers

Forks