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

loresoft / MongoDB.Abstracts / 13118055932

03 Feb 2025 04:24PM UTC coverage: 77.474% (-1.4%) from 78.879%
13118055932

push

github

pwelter34
add tests, update readme

32 of 64 branches covered (50.0%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

21 existing lines in 4 files now uncovered.

195 of 229 relevant lines covered (85.15%)

7.63 hits per line

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

84.06
/src/MongoDB.Abstracts/MongoQuery.cs
1
// Ignore Spelling: Mongo
2

3
using System.Diagnostics;
4
using System.Linq.Expressions;
5

6
using MongoDB.Driver;
7

8
namespace MongoDB.Abstracts;
9

10
/// <summary>
11
/// A MongoDB data query base class.
12
/// </summary>
13
/// <typeparam name="TEntity">The type of the entity.</typeparam>
14
/// <typeparam name="TKey">The type of the key.</typeparam>
15
public abstract class MongoQuery<TEntity, TKey> : IMongoQuery<TEntity, TKey>
16
    where TEntity : class
17
{
18
    private readonly Lazy<IMongoCollection<TEntity>> _collection;
19
    private readonly IMongoDatabase _mongoDatabase;
20

21
    /// <summary>
22
    /// Initializes a new instance of the <see cref="MongoQuery{TEntity, TKey}"/> class.
23
    /// </summary>
24
    /// <exception cref="ArgumentNullException"><paramref name="mongoDatabase"/> is <see langword="null" />.</exception>
25
    protected MongoQuery(IMongoDatabase mongoDatabase)
18✔
26
    {
27
        _mongoDatabase = mongoDatabase ?? throw new ArgumentNullException(nameof(mongoDatabase));
18!
28
        _collection = new Lazy<IMongoCollection<TEntity>>(CreateCollection);
18✔
29
    }
18✔
30

31

32
    /// <inheritdoc/>
33
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
34
    public IMongoCollection<TEntity> Collection => _collection.Value;
87✔
35

36

37
    /// <inheritdoc/>
38
    public TEntity? Find(TKey key)
39
    {
40
        if (key == null)
8!
UNCOV
41
            throw new ArgumentNullException(nameof(key));
×
42

43
        var filter = KeyExpression(key);
8✔
44

45
        return Collection
8✔
46
            .Find(filter)
8✔
47
            .FirstOrDefault();
8✔
48
    }
49

50
    /// <inheritdoc/>
51
    public async Task<TEntity?> FindAsync(TKey key, CancellationToken cancellationToken = default)
52
    {
53
        if (key == null)
10!
UNCOV
54
            throw new ArgumentNullException(nameof(key));
×
55

56
        var filter = KeyExpression(key);
10✔
57

58
        var result = await Collection
10✔
59
            .Find(filter)
10✔
60
            .FirstOrDefaultAsync(cancellationToken)
10✔
61
            .ConfigureAwait(false);
10✔
62

63
        return result;
10✔
64
    }
10✔
65

66
    /// <inheritdoc/>
67
    public TEntity? FindOne(Expression<Func<TEntity, bool>> criteria)
68
    {
69
        if (criteria == null)
4!
70
            throw new ArgumentNullException(nameof(criteria));
×
71

72
        return Collection
4✔
73
            .Find(criteria)
4✔
74
            .FirstOrDefault();
4✔
75
    }
76

77
    /// <inheritdoc/>
78
    public async Task<TEntity?> FindOneAsync(Expression<Func<TEntity, bool>> criteria, CancellationToken cancellationToken = default)
79
    {
80
        if (criteria == null)
5!
81
            throw new ArgumentNullException(nameof(criteria));
×
82

83
        var result = await Collection
5✔
84
            .Find(criteria)
5✔
85
            .FirstOrDefaultAsync(cancellationToken)
5✔
86
            .ConfigureAwait(false);
5✔
87

88
        return result;
5✔
89
    }
5✔
90

91
    /// <inheritdoc/>
92
    public IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> criteria)
93
    {
94
        if (criteria == null)
4!
UNCOV
95
            throw new ArgumentNullException(nameof(criteria));
×
96

97
        return Collection
4✔
98
            .AsQueryable()
4✔
99
            .Where(criteria);
4✔
100
    }
101

102
    /// <inheritdoc/>
103
    public async Task<IReadOnlyList<TEntity>> FindAllAsync(Expression<Func<TEntity, bool>> criteria, CancellationToken cancellationToken = default)
104
    {
105
        var results = await Collection
7✔
106
            .Find(criteria)
7✔
107
            .ToListAsync(cancellationToken)
7✔
108
            .ConfigureAwait(false);
7✔
109

110
        return results;
7✔
111
    }
7✔
112

113

114
    /// <inheritdoc/>
115
    public IQueryable<TEntity> All()
116
    {
117
        return Collection.AsQueryable();
3✔
118
    }
119

120

121
    /// <inheritdoc/>
122
    public long Count()
123
    {
124
        return Collection.CountDocuments(FilterDefinition<TEntity>.Empty);
1✔
125
    }
126

127
    /// <inheritdoc/>
128
    public Task<long> CountAsync(CancellationToken cancellationToken = default)
129
    {
130
        return Collection.CountDocumentsAsync(FilterDefinition<TEntity>.Empty, cancellationToken: cancellationToken);
1✔
131
    }
132

133
    /// <inheritdoc/>
134
    public long Count(Expression<Func<TEntity, bool>> criteria)
135
    {
136
        return Collection.CountDocuments(criteria);
1✔
137
    }
138

139
    /// <inheritdoc/>
140
    public Task<long> CountAsync(Expression<Func<TEntity, bool>> criteria, CancellationToken cancellationToken = default)
141
    {
142
        return Collection.CountDocumentsAsync(criteria, cancellationToken: cancellationToken);
1✔
143
    }
144

145

146
    /// <summary>
147
    /// Gets the key for the specified <paramref name="entity" />.
148
    /// </summary>
149
    /// <param name="entity">The entity to get the key from.</param>
150
    /// <returns>
151
    /// The key for the specified entity.
152
    /// </returns>
153
    protected abstract TKey EntityKey(TEntity entity);
154

155
    /// <summary>
156
    /// Gets the key expression with the specified <paramref name="key" />.
157
    /// </summary>
158
    /// <param name="key">The key to get expression with.</param>
159
    /// <returns>
160
    /// The key expression for the specified key.
161
    /// </returns>
162
    /// <example>
163
    /// <code>
164
    /// Example expression for an entity key.
165
    /// <![CDATA[entity => entity.Id == key]]>
166
    /// </code>
167
    /// </example>
168
    protected abstract Expression<Func<TEntity, bool>> KeyExpression(TKey key);
169

170

171
    /// <summary>
172
    /// Gets the name of the collection.
173
    /// </summary>
174
    /// <returns></returns>
175
    protected virtual string CollectionName()
176
    {
177
        return typeof(TEntity).Name;
6✔
178
    }
179

180
    /// <summary>
181
    /// Creates the collection.
182
    /// </summary>
183
    /// <returns></returns>
184
    protected virtual IMongoCollection<TEntity> CreateCollection()
185
    {
186
        var database = _mongoDatabase;
6✔
187

188
        string collectionName = CollectionName();
6✔
189
        var mongoCollection = CreateCollection(database, collectionName);
6✔
190

191
        EnsureIndexes(mongoCollection);
6✔
192

193
        return mongoCollection;
6✔
194
    }
195

196
    /// <summary>
197
    /// Creates the collection.
198
    /// </summary>
199
    /// <param name="database">The database.</param>
200
    /// <param name="collectionName">Name of the collection.</param>
201
    /// <returns></returns>
202
    protected virtual IMongoCollection<TEntity> CreateCollection(IMongoDatabase database, string collectionName)
203
    {
204
        return database.GetCollection<TEntity>(collectionName);
6✔
205
    }
206

207
    /// <summary>
208
    /// Create indexes on the collection.
209
    /// </summary>
210
    /// <param name="mongoCollection">The mongo collection.</param>
211
    protected virtual void EnsureIndexes(IMongoCollection<TEntity> mongoCollection)
212
    {
213

214
    }
6✔
215
}
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

© 2026 Coveralls, Inc