1+ #! /usr/bin/env bash
2+
3+ # Tests formatting of Cypher query results as Markdown table.
4+
5+ # Requires formatQueryResultAsMarkdownTable.sh
6+
7+ # Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
8+ set -o errexit -o pipefail
9+
10+ # # Get this "scripts" directory if not already set
11+ # Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
12+ # CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
13+ # This way non-standard tools like readlink aren't needed.
14+ MARKDOWN_SCRIPTS_DIR=${MARKDOWN_SCRIPTS_DIR:- $( CDPATH=. cd -- " $( dirname -- " ${BASH_SOURCE[0]} " ) " && pwd -P )} # Repository directory containing the shell scripts
15+ # echo "testFormatQueryResultAsMarkdownTable: MARKDOWN_SCRIPTS_DIR=${MARKDOWN_SCRIPTS_DIR}" >&2
16+
17+ tearDown () {
18+ # echo "testFormatQueryResultAsMarkdownTable: Tear down tests...."
19+ rm -rf " ${temporaryTestDirectory} "
20+ }
21+
22+ successful () {
23+ local COLOR_SUCCESSFUL=" \033[0;32m" # green
24+ local COLOR_DEFAULT=' \033[0m'
25+
26+ echo -e " testFormatQueryResultAsMarkdownTable: ${COLOR_SUCCESSFUL} Tests finished successfully.${COLOR_DEFAULT} "
27+
28+ tearDown
29+ }
30+
31+ fail () {
32+ local COLOR_ERROR=' \033[0;31m' # red
33+ local COLOR_DEFAULT=' \033[0m'
34+
35+ local errorMessage=" ${1} "
36+
37+ echo -e " testFormatQueryResultAsMarkdownTable: ${COLOR_ERROR}${errorMessage}${COLOR_DEFAULT} "
38+ tearDown
39+ return 1
40+ }
41+
42+ echo " testFormatQueryResultAsMarkdownTable: Starting tests...."
43+
44+ # Create testing resources
45+ temporaryTestDirectory=$( mktemp -d 2> /dev/null || mktemp -d -t ' temporaryTestDirectory' )
46+
47+ # ------------------------------------------------------------
48+ # Test case --
49+ # ------------------------------------------------------------
50+ echo " testFormatQueryResultAsMarkdownTable: 1.) Convert a simple query result to a Markdown table."
51+
52+ # Read expected result from test_data_cypher_query_result_simple_expected
53+ expected_result=$( < " ${MARKDOWN_SCRIPTS_DIR} /test_data_cypher_query_result_simple_expected.md" )
54+
55+ # - Execute script under test
56+ embeddedContent=$( cd " ${temporaryTestDirectory} " ; cat " ${MARKDOWN_SCRIPTS_DIR} /test_data_cypher_query_result_simple.json" | " ${MARKDOWN_SCRIPTS_DIR} /formatQueryResultAsMarkdownTable.sh" )
57+
58+ # - Verify results
59+ if [ " ${embeddedContent} " != " ${expected_result} " ]; then
60+ fail " 1.) Test failed: Expected Markdown table to be \n${expected_result} , but got:\n${embeddedContent} "
61+ fi
62+
63+
64+ echo " testFormatQueryResultAsMarkdownTable: 2.) Convert an array query result to a Markdown table."
65+
66+ # Read expected result from test_data_cypher_query_result_simple_expected
67+ expected_result=$( < " ${MARKDOWN_SCRIPTS_DIR} /test_data_cypher_query_result_with_array_expected.md" )
68+
69+ # - Execute script under test
70+ embeddedContent=$( cd " ${temporaryTestDirectory} " ; cat " ${MARKDOWN_SCRIPTS_DIR} /test_data_cypher_query_result_with_array.json" | " ${MARKDOWN_SCRIPTS_DIR} /formatQueryResultAsMarkdownTable.sh" )
71+
72+ # - Verify results
73+ if [ " ${embeddedContent} " != " ${expected_result} " ]; then
74+ fail " 2.) Test failed: Expected Markdown table to be \n${expected_result} , but got:\n${embeddedContent} "
75+ fi
76+
77+ successful
78+ return 0
0 commit comments