• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

b1f6c1c4 / ProfessionalAccounting / 327

28 Apr 2025 05:37PM UTC coverage: 51.346% (-3.0%) from 54.325%
327

push

appveyor

b1f6c1c4
ci buildx

6620 of 12893 relevant lines covered (51.35%)

126.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.44
/AccountingServer.Test/IntegrationTest/DistributedTest/QueryTest.cs
1
/* Copyright (C) 2020-2025 b1f6c1c4
2
 *
3
 * This file is part of ProfessionalAccounting.
4
 *
5
 * ProfessionalAccounting is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU Affero General Public License as
7
 * published by the Free Software Foundation, version 3.
8
 *
9
 * ProfessionalAccounting is distributed in the hope that it will be useful, but
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
12
 * for more details.
13
 *
14
 * You should have received a copy of the GNU Affero General Public License
15
 * along with ProfessionalAccounting.  If not, see
16
 * <https://www.gnu.org/licenses/>.
17
 */
18

19
using System;
20
using System.Collections;
21
using System.Collections.Generic;
22
using System.Diagnostics.CodeAnalysis;
23
using System.Linq;
24
using System.Threading.Tasks;
25
using AccountingServer.BLL;
26
using AccountingServer.DAL;
27
using AccountingServer.Entities;
28
using AccountingServer.Entities.Util;
29
using Xunit;
30
using static AccountingServer.BLL.Parsing.FacadeF;
31
using static AccountingServer.BLL.Parsing.Synthesizer;
32

33
namespace AccountingServer.Test.IntegrationTest.DistributedTest;
34

35
public abstract class QueryTestBase
36
{
37
    private readonly Client m_Client = new() { User = "b1", Today = DateTime.UtcNow.Date };
16✔
38

39
    protected abstract ValueTask PrepareAsset(Asset asset);
40
    protected abstract ValueTask PrepareAmort(Amortization amort);
41
    protected abstract ValueTask<bool> RunAssetQuery(IQueryCompounded<IDistributedQueryAtom> query);
42
    protected abstract ValueTask<bool> RunAmortQuery(IQueryCompounded<IDistributedQueryAtom> query);
43
    protected abstract ValueTask ResetAssets();
44
    protected abstract ValueTask ResetAmorts();
45

46
    public virtual async Task RunTestA(bool expectedA, bool expectedB, string query)
47
    {
16✔
48
        var asset = AssetDataProvider.Create("2017-02-03", DepreciationMethod.StraightLine);
16✔
49
        asset.Name = "a nm";
16✔
50
        asset.User = "b1";
16✔
51
        asset.Remark = "rmk1";
16✔
52
        var amort = AmortDataProvider.Create("2017-04-05", AmortizeInterval.EveryDay);
16✔
53
        amort.Name = "o nm";
16✔
54
        amort.User = "b2";
16✔
55
        amort.Remark = null;
16✔
56

57
        await ResetAssets();
16✔
58
        await ResetAmorts();
16✔
59
        await PrepareAsset(asset);
16✔
60
        await PrepareAmort(amort);
16✔
61
        Assert.Equal(expectedA, await RunAssetQuery(ParsingF.DistributedQuery(query, m_Client)));
16✔
62
        Assert.Equal(expectedB, await RunAmortQuery(ParsingF.DistributedQuery(query, m_Client)));
16✔
63
        await ResetAssets();
16✔
64
        await ResetAmorts();
16✔
65
    }
16✔
66

67
    internal protected class DataProvider : IEnumerable<object[]>
68
    {
69
        private static readonly List<object[]> Data = new()
1✔
70
            {
71
                new object[] { true, false, "9F4FDBA8-94BD-45C2-AF53-36BCFFB591FF" },
72
                new object[] { true, false, "" },
73
                new object[] { true, true, "U" },
74
                new object[] { false, true, "Ub2" },
75
                new object[] { true, true, "U /[ao] n/" },
76
                new object[] { false, false, "U %whatever%" },
77
                new object[] { false, true, "U %%" },
78
                new object[] { true, true, "{U %%}+{[[201702]]}" },
79
            };
80

81
        public IEnumerator<object[]> GetEnumerator() => Data.GetEnumerator();
3✔
82
        IEnumerator IEnumerable.GetEnumerator() => Data.GetEnumerator();
×
83
    }
84
}
85

86
public class SynthTest
87
{
88
    [Theory]
89
    [ClassData(typeof(QueryTestBase.DataProvider))]
90
    public void Matches(bool _a, bool _b, string query)
91
    {
8✔
92
        var client = new Client { User = "uuu", Today = DateTime.UtcNow.Date };
8✔
93
        var q1 = ParsingF.DistributedQuery(query, client);
8✔
94
        var s1 = Synth(q1);
8✔
95
        var q2 = ParsingF.DistributedQuery(s1, client);
8✔
96
        var s2 = Synth(q2);
8✔
97
        Assert.Equal(s1, s2);
8✔
98
    }
8✔
99
}
100

101
[Collection("DbTestCollection")]
102
[SuppressMessage("ReSharper", "InvokeAsExtensionMethod")]
103
public class DbQueryTest : QueryTestBase, IDisposable
104
{
105
    private readonly IDbAdapter m_Adapter;
106

107
    public DbQueryTest()
8✔
108
    {
8✔
109
        m_Adapter = Facade.Create(db: "accounting-test");
8✔
110

111
        m_Adapter.DeleteAssets(DistributedQueryUnconstrained.Instance).AsTask().Wait();
8✔
112
        m_Adapter.DeleteAmortizations(DistributedQueryUnconstrained.Instance).AsTask().Wait();
8✔
113
    }
8✔
114

115
    public void Dispose()
116
    {
8✔
117
        m_Adapter.DeleteAssets(DistributedQueryUnconstrained.Instance).AsTask().Wait();
8✔
118
        m_Adapter.DeleteAmortizations(DistributedQueryUnconstrained.Instance).AsTask().Wait();
8✔
119
    }
8✔
120

121
    protected override async ValueTask PrepareAsset(Asset asset) => await m_Adapter.Upsert(asset);
8✔
122
    protected override async ValueTask PrepareAmort(Amortization amort) => await m_Adapter.Upsert(amort);
8✔
123

124
    protected override async ValueTask<bool> RunAssetQuery(IQueryCompounded<IDistributedQueryAtom> query)
125
        => await m_Adapter.SelectAssets(query).SingleOrDefaultAsync() != null;
8✔
126

127
    protected override async ValueTask<bool> RunAmortQuery(IQueryCompounded<IDistributedQueryAtom> query)
128
        => await m_Adapter.SelectAmortizations(query).SingleOrDefaultAsync() != null;
8✔
129

130
    protected override async ValueTask ResetAssets()
131
        => await m_Adapter.DeleteAssets(DistributedQueryUnconstrained.Instance);
16✔
132

133
    protected override async ValueTask ResetAmorts()
134
        => await m_Adapter.DeleteAmortizations(DistributedQueryUnconstrained.Instance);
16✔
135

136
    [Theory]
137
    [ClassData(typeof(DataProvider))]
138
    public override Task RunTestA(bool expectedA, bool expectedB, string query)
139
        => base.RunTestA(expectedA, expectedB, query);
8✔
140
}
141

142
[Collection("DbTestCollection")]
143
[SuppressMessage("ReSharper", "InvokeAsExtensionMethod")]
144
public class BLLQueryTest : QueryTestBase, IDisposable
145
{
146
    private readonly Accountant m_Accountant;
147

148
    public BLLQueryTest()
8✔
149
    {
8✔
150
        m_Accountant = new(new(db: "accounting-test"), "b1", DateTime.UtcNow.Date);
8✔
151

152
        m_Accountant.DeleteAssetsAsync(DistributedQueryUnconstrained.Instance).AsTask().Wait();
8✔
153
        m_Accountant.DeleteAmortizationsAsync(DistributedQueryUnconstrained.Instance).AsTask().Wait();
8✔
154
    }
8✔
155

156
    public void Dispose()
157
    {
8✔
158
        m_Accountant.DeleteAssetsAsync(DistributedQueryUnconstrained.Instance).AsTask().Wait();
8✔
159
        m_Accountant.DeleteAmortizationsAsync(DistributedQueryUnconstrained.Instance).AsTask().Wait();
8✔
160
    }
8✔
161

162
    protected override async ValueTask PrepareAsset(Asset asset) => await m_Accountant.UpsertAsync(asset);
8✔
163
    protected override async ValueTask PrepareAmort(Amortization amort) => await m_Accountant.UpsertAsync(amort);
8✔
164

165
    protected override async ValueTask<bool> RunAssetQuery(IQueryCompounded<IDistributedQueryAtom> query)
166
        => await m_Accountant.SelectAssetsAsync(query).SingleOrDefaultAsync() != null;
8✔
167

168
    protected override async ValueTask<bool> RunAmortQuery(IQueryCompounded<IDistributedQueryAtom> query)
169
        => await m_Accountant.SelectAmortizationsAsync(query).SingleOrDefaultAsync() != null;
8✔
170

171
    protected override async ValueTask ResetAssets()
172
        => await m_Accountant.DeleteAssetsAsync(DistributedQueryUnconstrained.Instance);
16✔
173

174
    protected override async ValueTask ResetAmorts()
175
        => await m_Accountant.DeleteAmortizationsAsync(DistributedQueryUnconstrained.Instance);
16✔
176

177
    [Theory]
178
    [ClassData(typeof(DataProvider))]
179
    public override Task RunTestA(bool expectedA, bool expectedB, string query)
180
        => base.RunTestA(expectedA, expectedB, query);
8✔
181
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc