Skip to content

Commit

Permalink
Fix/export documents params (#87)
Browse files Browse the repository at this point in the history
* Add tests for export documents functionality

* Fix export documents include_fields

* Fix for trailing newline on export with filter_by
  • Loading branch information
brianweet authored Jun 17, 2022
1 parent 8e681e3 commit 0e75a4b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Typesense/TypesenseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public async Task<List<T>> ExportDocuments<T>(string collection, ExportParameter

var extraParameters = new List<string>();
if (exportParameters.IncludeFields is not null)
extraParameters.Add($"include_fields={exportParameters.ExcludeFields}");
extraParameters.Add($"include_fields={exportParameters.IncludeFields}");
if (exportParameters.FilterBy is not null)
extraParameters.Add($"filter_by={exportParameters.FilterBy}");
if (exportParameters.ExcludeFields is not null)
Expand All @@ -212,6 +212,7 @@ public async Task<List<T>> ExportDocuments<T>(string collection, ExportParameter
var response = await Get($"/collections/{collection}/documents/export?{searchParameters}").ConfigureAwait(false);

return response.Split('\n')
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select((x) => JsonSerializer.Deserialize<T>(x, _jsonNameCaseInsentiveTrue)
?? throw new ArgumentException("Null is not valid for documents"))
.ToList();
Expand Down
87 changes: 87 additions & 0 deletions test/Typesense.Tests/TypesenseClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,93 @@ public async Task Export_documents()
response.OrderBy(x => x.Id).Should().BeEquivalentTo(expected);
}

[Fact, TestPriority(8)]
public async Task Export_documents_filter_by_query()
{
var expected = new List<Company>
{
new Company
{
Id = "124",
CompanyName = "Stark Industries",
NumEmployees = 5215,
Country = "USA",
}
};

var response = await _client.ExportDocuments<Company>("companies", new ExportParameters { FilterBy = "id: [124]" });

response.OrderBy(x => x.Id).Should().BeEquivalentTo(expected);
}

[Fact, TestPriority(8)]
public async Task Export_documents_include_fields()
{
var expected = new List<Company>
{
new Company
{
Id = "124",
CompanyName = "Stark Industries"
},
new Company
{
Id = "125",
CompanyName = "Future Technology"
},
new Company
{
Id = "126",
CompanyName = "Random Corp."
},
new Company
{
Id = "999",
CompanyName = "Awesome A/S"
}
};

var response = await _client.ExportDocuments<Company>("companies", new ExportParameters { IncludeFields = "id,company_name" });

response.OrderBy(x => x.Id).Should().BeEquivalentTo(expected);
}

[Fact, TestPriority(8)]
public async Task Export_documents_exclude_fields()
{
var expected = new List<Company>
{
new Company
{
Id = "124",
CompanyName = "Stark Industries",
Country = "USA",
},
new Company
{
Id = "125",
CompanyName = "Future Technology",
Country = "UK",
},
new Company
{
Id = "126",
CompanyName = "Random Corp.",
Country = "AU",
},
new Company
{
Id = "999",
CompanyName = "Awesome A/S",
Country = "SWE",
}
};

var response = await _client.ExportDocuments<Company>("companies", new ExportParameters { ExcludeFields = "num_employees" });

response.OrderBy(x => x.Id).Should().BeEquivalentTo(expected);
}

[Fact, TestPriority(9)]
public async Task Retrieve_document()
{
Expand Down

0 comments on commit 0e75a4b

Please sign in to comment.