Skip to content

Commit 74a4175

Browse files
committed
Added JSONLogic official tests support
Signed-off-by: Marcin Olko <molko@google.com>
1 parent 5724941 commit 74a4175

5 files changed

Lines changed: 3917 additions & 131 deletions

File tree

providers/flagd/tests/evaluator/json_logic/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ cc_test(
1919
"@nlohmann_json//:json",
2020
],
2121
)
22+
23+
cc_test(
24+
name = "json_logic_official_test",
25+
srcs = ["json_logic_official_test.cpp"],
26+
data = [
27+
"tests.json",
28+
],
29+
deps = [
30+
"//providers/flagd/src/evaluator/json_logic",
31+
"@googletest//:gtest_main",
32+
"@nlohmann_json//:json",
33+
],
34+
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python3
2+
import subprocess
3+
import re
4+
import sys
5+
6+
def run_tests():
7+
# Run the bazel test command and capture output
8+
cmd = [
9+
"gbazelisk", "test",
10+
"//providers/flagd/tests/evaluator/json_logic:json_logic_official_test",
11+
"--test_output=all",
12+
"--cache_test_results=no",
13+
"--keep_going"
14+
]
15+
16+
# Run command, capturing stdout and stderr
17+
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
18+
19+
return result.stdout
20+
21+
def parse_output(output):
22+
# Look for [ PASSED ] X tests
23+
# Look for [ FAILED ] Y tests
24+
25+
passed_pattern = r"(?:\[\s+PASSED\s+\])\s+(\d+)\s+tests"
26+
failed_pattern = r"(?:\[\s+FAILED\s+\])\s+(\d+)\s+tests"
27+
28+
passed_match = re.search(passed_pattern, output)
29+
failed_match = re.search(failed_pattern, output)
30+
31+
passed = int(passed_match.group(1)) if passed_match else 0
32+
failed = int(failed_match.group(1)) if failed_match else 0
33+
34+
return passed, failed
35+
36+
def main():
37+
print("Running tests...")
38+
output = run_tests()
39+
40+
passed, failed = parse_output(output)
41+
total = passed + failed
42+
43+
print("\n" + "="*40)
44+
print("Test Progress Report")
45+
print("="*40)
46+
print(f"Total Tests: {total}")
47+
print(f"Passing: {passed}")
48+
print(f"Failing: {failed}")
49+
50+
if total > 0:
51+
percent = (passed / total) * 100
52+
print(f"Success Rate: {percent:.1f}%")
53+
else:
54+
print("No tests found or execution failed completely.")
55+
56+
print("="*40)
57+
58+
if __name__ == "__main__":
59+
main()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <fstream>
4+
#include <iostream>
5+
#include <nlohmann/json.hpp>
6+
#include <vector>
7+
8+
#include "providers/flagd/src/evaluator/json_logic/json_logic.h"
9+
10+
using json_logic::JsonLogic;
11+
using nlohmann::json;
12+
13+
class JsonLogicTest : public ::testing::TestWithParam<json> {
14+
protected:
15+
JsonLogic json_logic_;
16+
};
17+
18+
TEST_P(JsonLogicTest, RunCase) {
19+
const json& test_case = GetParam();
20+
21+
ASSERT_TRUE(test_case.is_array());
22+
ASSERT_GE(test_case.size(), 3);
23+
24+
json rule = test_case[0];
25+
json data = test_case[1];
26+
json expected = test_case[2];
27+
28+
json result = json_logic_.Apply(rule, data);
29+
30+
EXPECT_EQ(result, expected)
31+
<< "Rule: " << rule << "\nData: " << data << "\nExpected: " << expected
32+
<< "\nGot: " << result;
33+
}
34+
35+
std::vector<json> LoadTests() {
36+
// This file is downloaded from: https://jsonlogic.com/tests.json
37+
std::string file_path =
38+
"providers/flagd/tests/evaluator/json_logic/tests.json";
39+
std::ifstream f(file_path);
40+
if (!f.is_open()) {
41+
// Fallback or error. In Bazel, runfiles might be needed, but relative path
42+
// often works if CWD is correct. Let's try to print error.
43+
std::cerr << "Could not open " << file_path << std::endl;
44+
return {};
45+
}
46+
json j;
47+
f >> j;
48+
std::vector<json> valid_cases;
49+
for (const auto& element : j) {
50+
if (element.is_array() && element.size() >= 3) {
51+
valid_cases.push_back(element);
52+
}
53+
}
54+
return valid_cases;
55+
}
56+
57+
INSTANTIATE_TEST_SUITE_P(JsonLogicSuite, JsonLogicTest,
58+
::testing::ValuesIn(LoadTests()));

providers/flagd/tests/evaluator/json_logic/ops_test.cpp

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)