Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions QuickbaseNet.UnitTests/Tests/QuickbaseQueryBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Newtonsoft.Json;
using QuickbaseNet.Helpers;

namespace QuickbaseNet.UnitTests.Tests;

public class QuickbaseQueryBuilderTests
{
[Fact]
public void Build_SetsSkipAndTop_WhenBothAreCalled()
{
// Act
var request = new QuickbaseQueryBuilder().From("bck7").Skip(100).Top(50).Build();

// Assert
Assert.NotNull(request.Options);
Assert.Equal(100, request.Options.Skip);
Assert.Equal(50, request.Options.Top);
}

[Fact]
public void Build_LeavesOptionsNull_WhenNeitherIsCalled()
{
// Act
var request = new QuickbaseQueryBuilder().From("bck7").Select(3, 6).Build();

// Assert
Assert.Null(request.Options);
}

[Fact]
public void Serialize_SendsSkipWithoutTop_WhenOnlySkipIsCalled()
{
// Arrange
var request = new QuickbaseQueryBuilder().From("bck7").Skip(100).Build();

// Act
var json = JsonConvert.SerializeObject(request);

// Assert
Assert.Contains("\"skip\":100", json);
Assert.DoesNotContain("\"top\"", json);
}
}
29 changes: 28 additions & 1 deletion QuickbaseNet/Helpers/QuickbaseQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class QuickbaseQueryBuilder
private string _where;
private List<SortByItem> _sortBy;
private List<GroupByItem> _groupBy;
private int? _skip;
private int? _top;

/// <summary>
/// Specifies the table to query.
Expand Down Expand Up @@ -79,6 +81,28 @@ public QuickbaseQueryBuilder GroupBy(int fieldId, string grouping)
return this;
}

/// <summary>
/// Skips the given number of records at the start of the result set, for paging.
/// </summary>
/// <param name="count">The number of records to skip.</param>
/// <returns>The current instance of QuickbaseQueryBuilder.</returns>
public QuickbaseQueryBuilder Skip(int count)
{
_skip = count;
return this;
}

/// <summary>
/// Returns at most the given number of records, for paging.
/// </summary>
/// <param name="count">The maximum number of records to return.</param>
/// <returns>The current instance of QuickbaseQueryBuilder.</returns>
public QuickbaseQueryBuilder Top(int count)
{
_top = count;
return this;
}

/// <summary>
/// Builds the query.
/// </summary>
Expand All @@ -91,7 +115,10 @@ public QuickbaseQueryRequest Build()
Select = _select,
Where = _where,
SortBy = _sortBy,
GroupBy = _groupBy
GroupBy = _groupBy,
Options = _skip == null && _top == null
? null
: new Options { Skip = _skip ?? 0, Top = _top ?? 0 }
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions QuickbaseNet/Models/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class Options
/// <summary>
/// Gets or sets the number of records to skip in the result set.
/// </summary>
[JsonProperty("skip")]
[JsonProperty("skip", DefaultValueHandling = DefaultValueHandling.Ignore)]
public int Skip { get; set; }

/// <summary>
/// Gets or sets the maximum number of records to return in the result set.
/// </summary>
[JsonProperty("top")]
[JsonProperty("top", DefaultValueHandling = DefaultValueHandling.Ignore)]
public int Top { get; set; }

/// <summary>
Expand Down
Loading