Исходный код СУБД 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
using System;
using System.Collections.Generic;
using Xunit;
using Xunit.Sdk;
 
public abstract class BaseTriggerTestExceptionFsharpTests : PlDotNetTest
{
    protected abstract string FunctionBody { get; }
 
    protected abstract LanguageType Language { get; }
 
    public BaseTriggerTestExceptionFsharpTests()
    {
        FunctionInfo = new SqlFunctionInfo
        {
            Name = "trigger_test_exception_fsharp",
            Arguments = new List<FunctionArgument>(),
            ReturnType = "TRIGGER",
            Body = FunctionBody,
            Language = Language,
            IsStrict = false
        };
    }
 
    public static object[][] TestCases()
    {
        return new[]
        {
            new object[]
            {
                "f#-trigger",
                "exceptionThrown",
                @"
INSERT INTO trigger_test_table(id, message) VALUES (1, 'Initial');
UPDATE trigger_test_table SET message = 'Changed' WHERE id = 1;
"
            }
        };
    }
 
    [Theory]
    [MemberData(nameof(TestCases))]
    public void TestTriggerExceptionFsharp(string featureName, string testName, string cteStatement)
    {
        var createFunctionSql = GetFunctionDefinition(FunctionInfo!);
        ExecuteSql(createFunctionSql);
 
        var triggerSql = @"
DROP TRIGGER IF EXISTS test_trigger_AUS_4 ON trigger_test_table;
CREATE TRIGGER test_trigger_AUS_4
    AFTER UPDATE ON trigger_test_table
    FOR EACH STATEMENT
    EXECUTE FUNCTION trigger_test_exception_fsharp ('AFTER/UPDATE/STATEMENT', '4');
";
        ExecuteSql(triggerSql);
 
        try
        {
            // This is going to fail internally and it will throw TrueException
            RunTestWithSuffix(
                featureName: featureName,
                testName: testName,
                cteStatement: cteStatement,
                customAssertion: null!,
                querySuffix: null!,
                forceCte: false
            );
 
            // If the test gets here, the exception didn't happen and we force the failing
            Assert.Fail("Expected a trigger execution, but none was thrown.");
        }
        catch (Xunit.Sdk.TrueException)
        {
            // Capture the fail of harness, then register "success" manually
            var insertSql = $@"
    INSERT INTO automated_test_results (FEATURE, TEST_NAME, RESULT)
    VALUES ('{featureName}', '{testName}', TRUE) RETURNING id;
    ";
 
            var testId = ExecuteSqlReturnId(insertSql);
            Assert.True(testId.HasValue, "Fail to insert the result of 'exceptionThrown'.");
        }
    }
}
 
[Trait("Language", "FSharp")]
[Trait("Category", "Trigger")]
public class TriggerTestExceptionTestsFSharp : BaseTriggerTestExceptionFsharpTests
{
    protected override string FunctionBody => @"
        raise (SystemException((""This is a test of exception handling"")))
    ";
    protected override LanguageType Language => LanguageType.PlfSharp;
}