Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-04-30 9f2ef60949ced4fab4d077a7e1c6d913f3620c9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import pandas as pd
import re
import sys
import os
import psycopg2
from typing import List
 
directory_to_save = "tests/npgsql/results/"
 
 
def match_regex(data: str, regex: str) -> str:
    result_regex = re.compile(regex)
    result_match = re.search(result_regex, data)
 
    if result_match:
        result = result_match.group(1)
        return result.strip()
    else:
        raise Exception("There is no result for the regex expression.")
 
 
def create_data_frame_from_postgresql(table_name: str):
    conn = psycopg2.connect(
        host="localhost",
        database="postgres",
        user="postgres",
        password="postgres",
    )
    df = pd.read_sql(f"SELECT * FROM {table_name}", conn)
    return df
 
 
def create_data_frame_from_csv(path: str):
    df = pd.read_csv(path)
    return df
 
 
def find_cause_of_failure(result: str, test: str) -> str:
    # Test is working
    if "Working fine on pldotnet 👍" in result:
        return "Working fine on pldotnet 👍", "Working fine 👍"
 
    # PostgresException
    if "ErrorData in C#. Here's the message: " in result:
        message = match_regex(result, "ErrorData in C#. Here's the message: +(.*)")
        return message, "Failed in C"
 
 
    # Test fails with an ERROR message
    if "ERROR:  " in result:
        message = match_regex(result, "ERROR: +(.*)")
        return message, "Failed in C"
 
    # Test fails in the PG_TRY statement of a C function in pldotnet_spi.c
    # PG_TRY();
    # {
    # }
    # PG_CATCH();
    # {
    #     elog(WARNING, "Exception: pldotnet_X");
    #     PG_RE_THROW();
    # }
    if "WARNING:  Exception: " in result:
        fail_on = match_regex(result, "WARNING:  Exception: +(.*)")
        return (
            f'the "{fail_on}" function failed with no error message',
            "Failed in C",
        )
 
    # Test fails and breaks the database without an explanation
    if "server closed the connection unexpectedly" in result:
        return (
            "server closed the connection unexpectedly.",
            "Failed in C",
        )
 
    # Test fails in C#, we could return the caught exception
    if "Fail on C#..." in result:
        lines = result.split("\n")
        start = 0
        end = len(lines)
        for cont, line in zip(range(end), lines):
            if "Fail on C#..." in line:
                start = cont + 1
 
            if (
                test in line
            ):  # todo - also check the file name with the function name
                end = cont
                break
        return (
            " \\n ".join(
                [
                    line[: len(line) - 1].strip()
                    for line in lines[start:end]
                    if "--->" not in line
                    # if ("at PlDotNET." in line or "at Npgsql." in line)
                ]
            ),
            "Failed in C#",
        )
 
    raise Exception(f"Failed to extract the cause of the failure to {test}.")
 
 
def export_data_frame(df, file_name: str):
    # Write the markdown table to a file
    with open(directory_to_save + file_name, "w") as f:
        f.write(df.to_markdown(index=False))
 
 
def compare_working_tests(current_working_tests: List[str]):
    # Reading file
    with open(
        directory_to_save + "working_tests.txt", "r", encoding="utf-8"
    ) as f:
        last_working_tests = f.read().split("\n")
 
    not_working_anymore = []
 
    for test in last_working_tests:
        if test not in current_working_tests:
            not_working_anymore.append(test)
 
    working_now = []
 
    for test in current_working_tests:
        if test not in last_working_tests:
            working_now.append(test)
 
    if working_now:
        aux = "- " + "\n- ".join(working_now)
        print(
            f"Great! You make {len(working_now)} test(s) pass. Here is the list:\n{aux}"
        )
 
    if not_working_anymore:
        aux = "- " + "\n- ".join(not_working_anymore)
        print(
            f"Please, take a look at your changes. You broke {len(not_working_anymore)} test(s)."
            f"\nThe following tests are not working anymore:\n{aux}"
        )
 
    if not working_now and not not_working_anymore:
        print("No changes to the tests that were already passing.")
    else:
 
        last_working_tests.extend(working_now)
        with open(
            directory_to_save + "working_tests.txt", "w", encoding="utf-8"
        ) as f:
            f.write("\n".join(last_working_tests))
 
 
def create_data_frame_from_out_files():
    # TODO - set it dynamically
    directory = "tests/npgsql/sql"
 
    # Create pandas dataframe
    df = pd.DataFrame(columns=["Feature", "Test Name", "Result", "Failed in"])
 
    # List of tests that are working
    working_tests = []
 
    # Loop over all the .out files in the directory
    for filename in sorted(os.listdir(directory)):
        if filename.endswith(".out"):
            # Read the contents of the file and set the variable value
            with open(os.path.join(directory, filename), "r") as f:
                try:
                    file = f.read()
                    split_name = filename.replace(".out", "").split("_")
                    feature = split_name[0]
                    test_name = "_".join(split_name[1:])
                    result, failed_on = find_cause_of_failure(
                        file, f"{feature}.{test_name}"
                    )
 
                    result = result.replace("System.AggregateException: One or more errors occurred. ", '')
 
                    result = (
                        f"{failed_on}: {result}"
                        if result != "Working fine on pldotnet 👍"
                        else result
                    )
 
                    if result == "Working fine on pldotnet 👍":
                        working_tests.append(filename.replace(".out", ""))
 
                    row = pd.Series(
                        [feature, test_name, result, failed_on],
                        index=["Feature", "Test Name", "Result", "Failed in"],
                    )
 
                    df = pd.concat([df, row.to_frame().T], ignore_index=True)
 
                except Exception as excepetion_message:
                    print(excepetion_message)
 
    compare_working_tests(working_tests)
 
    # Grouping by the failure
    grouped_df_result = df.groupby(["Result"]).size().reset_index(name="Count")
    grouped_df_result = grouped_df_result[["Count", "Result"]]
    grouped_df_result = grouped_df_result.sort_values("Count", ascending=False)
    export_data_frame(grouped_df_result, "results.md")
 
    # Add column with failed tests
    grouped_df_result["Failed Tests"] = grouped_df_result.apply(
        lambda x: ", ".join(df.loc[df["Result"] == x["Result"], "Test Name"])
        if x["Result"] != "Working fine on pldotnet 👍"
        else "OK",
        axis=1,
    )
    export_data_frame(grouped_df_result, "results_with_test_names.md")
 
    # Grouping by the location it failed
    grouped_df_failed = (
        df.groupby(["Failed in"]).size().reset_index(name="Count")
    )
    grouped_df_failed = grouped_df_failed[["Count", "Failed in"]]
    grouped_df_failed = grouped_df_failed.sort_values("Count", ascending=False)
    print(
        f"\nHere is a summary of the results:\n{grouped_df_failed.to_markdown(index=False)}")
    export_data_frame(grouped_df_failed, "fail_types.md")
 
 
def main(argv) -> None:
    """Main function"""
    create_data_frame_from_out_files()
 
 
if __name__ == "__main__":
    main(sys.argv)