From b5e94615aab6a95600120618e4ca33c292f5b361 Mon Sep 17 00:00:00 2001 From: Matthew Davidson Date: Thu, 11 Apr 2024 18:18:05 +0700 Subject: [PATCH] test: Add CONDITIONED BY * EXCEPT tests --- test/inferenceql/query/plan_test.cljc | 20 ++++++++++++------- .../inferenceql/query/strict/parser_test.cljc | 6 +++++- test/inferenceql/query/strict_test.cljc | 13 ++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/test/inferenceql/query/plan_test.cljc b/test/inferenceql/query/plan_test.cljc index 772a348..5b285be 100644 --- a/test/inferenceql/query/plan_test.cljc +++ b/test/inferenceql/query/plan_test.cljc @@ -299,6 +299,9 @@ (plan/plan) (plan/plan?)) "table GENERATIVE JOIN model" + "table GENERATIVE JOIN model CONDITIONED BY *" + "table GENERATIVE JOIN model CONDITIONED BY * EXCEPT (VAR x)" + "table GENERATIVE JOIN model CONDITIONED BY * EXCEPT VAR x, VAR y" "table GENERATIVE JOIN model CONDITIONED BY VAR x = 0" "table GENERATIVE JOIN model CONSTRAINED BY VAR x > 0")) @@ -311,10 +314,13 @@ "table GENERATIVE JOIN model" "table GENERATIVE JOIN model GIVEN x")) -#?(:clj (deftest generative-join - (are [query tbl expected] (let [env {"table" tbl "model" model}] - (= expected (eval query env))) - "table GENERATIVE JOIN model CONDITIONED BY VAR x = x" [{"x" "yes"}] [{"x" "yes" "y" "yes"}] - "table GENERATIVE JOIN model CONDITIONED BY VAR x = x" [{"x" "no"}] [{"x" "no" "y" "no"}] - "table GENERATIVE JOIN model CONDITIONED BY *" [{"x" "yes"}] [{"x" "yes" "y" "yes"}] - "table GENERATIVE JOIN model CONDITIONED BY *" [{"x" "no"}] [{"x" "no" "y" "no"}]))) +#?(:clj + (deftest generative-join + (are [query tbl expected] (let [env {"table" tbl "model" model}] + (= expected (eval query env))) + "table GENERATIVE JOIN model CONDITIONED BY VAR x = x" [{"x" "yes"}] [{"x" "yes" "y" "yes"}] + "table GENERATIVE JOIN model CONDITIONED BY VAR x = x" [{"x" "no"}] [{"x" "no" "y" "no"}] + "table GENERATIVE JOIN model CONDITIONED BY *" [{"x" "yes"}] [{"x" "yes" "y" "yes"}] + "table GENERATIVE JOIN model CONDITIONED BY *" [{"x" "no"}] [{"x" "no" "y" "no"}] + "table GENERATIVE JOIN model CONDITIONED BY * EXCEPT VAR x" [{"y" "yes"}] [{"x" "yes" "y" "yes"}] + "table GENERATIVE JOIN model CONDITIONED BY * EXCEPT (VAR x)" [{"y" "yes"}] [{"x" "yes" "y" "yes"}]))) diff --git a/test/inferenceql/query/strict/parser_test.cljc b/test/inferenceql/query/strict/parser_test.cljc index 7054e4c..1d8b757 100644 --- a/test/inferenceql/query/strict/parser_test.cljc +++ b/test/inferenceql/query/strict/parser_test.cljc @@ -40,6 +40,8 @@ (are [s] (not (insta/failure? (parser/parse s))) "data GENERATIVE JOIN model" "data GENERATIVE JOIN model CONDITIONED BY VAR x = 0" + "data GENERATIVE JOIN model CONDITIONED BY *" + "data GENERATIVE JOIN model CONDITIONED BY * EXCEPT (VAR x)" "data GENERATIVE JOIN model CONSTRAINED BY VAR x > 0")) (deftest generate-valid @@ -51,4 +53,6 @@ (deftest conditioned-by-valid (are [s] (not (insta/failure? (parser/parse s))) "SELECT * FROM (GENERATE * UNDER model CONDITIONED BY VAR x = x)" - "SELECT * FROM (GENERATE * UNDER model CONDITIONED BY *)")) + "SELECT * FROM (GENERATE * UNDER model CONDITIONED BY *)" + "SELECT * FROM (GENERATE * UNDER model CONDITIONED BY * EXCEPT (VAR x))" + "SELECT * FROM (GENERATE * UNDER model CONDITIONED BY * EXCEPT VAR x)")) diff --git a/test/inferenceql/query/strict_test.cljc b/test/inferenceql/query/strict_test.cljc index 16b0c1f..be983de 100644 --- a/test/inferenceql/query/strict_test.cljc +++ b/test/inferenceql/query/strict_test.cljc @@ -254,6 +254,9 @@ "y" {"yes" 0.0 "no" 1.0}}}]]}) q1 (comp first vals first #(q %1 %2 %3))] (is (= 0.5 (q1 "SELECT (PROBABILITY DENSITY OF VAR y = 'yes' UNDER model CONDITIONED BY VAR x = x) FROM data;" + (with-meta [{}] {:iql/columns ["x" "y"]}) + {"model" model}))) + (is (= 0.5 (q1 "SELECT (PROBABILITY DENSITY OF VAR y = 'yes' UNDER (model CONDITIONED BY * EXCEPT VAR y)) FROM data;" (with-meta [{}] {:iql/columns ["x" "y"]}) {"model" model}))))) @@ -498,5 +501,15 @@ (is (= 0.75 (q "SELECT PROBABILITY DENSITY OF VAR x = 'yes' UNDER model CONDITIONED BY VAR y = y FROM data" (with-meta [{}] {:iql/columns ["x" "y"]}))))) + + (testing "* except" + (is (= 0.75 (q "SELECT PROBABILITY DENSITY OF VAR x = 'yes' UNDER model CONDITIONED BY * EXCEPT VAR x FROM data" + (with-meta [{}] + {:iql/columns ["x" "y"]})))) + + (is (= 0.75 (q "SELECT PROBABILITY DENSITY OF VAR x = 'yes' UNDER model CONDITIONED BY * EXCEPT (VAR x) FROM data" + (with-meta [{}] + {:iql/columns ["x" "y"]}))))) + (testing "in with" (is (= 0.0 (q "WITH model CONDITIONED BY VAR y = 'no' AS model: SELECT PROBABILITY DENSITY OF VAR x = x UNDER model FROM data" [{"x" "yes"}]))))))))