Skip to content

Commit

Permalink
schema with specific field-types now follows typesense api (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored May 28, 2022
1 parent b08ae8d commit d5143d7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
27 changes: 24 additions & 3 deletions src/Typesense/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,33 @@ public record Field
[JsonPropertyName("facet")]
public bool Facet { get; init; }
[JsonPropertyName("optional")]
public bool Optional { get; init; }
public bool? Optional { get; init; }
[JsonPropertyName("index")]
public bool Index { get; init; }
public bool? Index { get; init; }

public Field(string name, FieldType type)
{
Name = name;
Type = type;
}

public Field(string name, FieldType type, bool facet)
{
Name = name;
Type = type;
Facet = facet;
}

public Field(string name, FieldType type, bool facet, bool? optional)
{
Name = name;
Type = type;
Facet = facet;
Optional = optional;
}

[JsonConstructor]
public Field(string name, FieldType type, bool facet, bool optional = false, bool index = true)
public Field(string name, FieldType type, bool facet, bool? optional, bool? index)
{
Name = name;
Type = type;
Expand Down
53 changes: 41 additions & 12 deletions test/Typesense.Tests/TypesenseClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public async Task Create_schema()
0,
new List<Field>
{
new Field("company_name", FieldType.String, false),
new Field("num_employees", FieldType.Int32, false),
new Field("country", FieldType.String, true),
new Field("company_name", FieldType.String, false, false, true),
new Field("num_employees", FieldType.Int32, false, false, true),
new Field("country", FieldType.String, true, false, true),
},
"num_employees");

Expand All @@ -60,6 +60,35 @@ public async Task Create_schema()
response.Should().BeEquivalentTo(expected);
}

// We test wildcard field individually,
// because it is a bit different than other types of fields.
[Fact, TestPriority(0)]
public async Task Create_schema_with_wildcard_field()
{
var expected = new CollectionResponse(
"wildcard-collection",
0,
new List<Field>
{
new Field(".*", FieldType.String, false, true, true),
},
"");

var schema = new Schema(
"wildcard-collection",
new List<Field>
{
new Field(".*", FieldType.String, false),
});

var response = await _client.CreateCollection(schema);

response.Should().BeEquivalentTo(expected);

// Cleanup
await _client.DeleteCollection("wildcard-collection");
}

[Fact, TestPriority(1)]
public async Task Retrieve_collection()
{
Expand All @@ -68,9 +97,9 @@ public async Task Retrieve_collection()
0,
new List<Field>
{
new Field("company_name", FieldType.String, false),
new Field("num_employees", FieldType.Int32, false),
new Field("country", FieldType.String, true),
new Field("company_name", FieldType.String, false, false, true),
new Field("num_employees", FieldType.Int32, false, false, true),
new Field("country", FieldType.String, true, false, true),
},
"num_employees");

Expand All @@ -89,9 +118,9 @@ public async Task Retrieve_collections()
0,
new List<Field>
{
new Field("company_name", FieldType.String, false),
new Field("num_employees", FieldType.Int32, false),
new Field("country", FieldType.String, true),
new Field("company_name", FieldType.String, false, false, true),
new Field("num_employees", FieldType.Int32, false, false, true),
new Field("country", FieldType.String, true, false, true),
},
"num_employees")
};
Expand All @@ -108,9 +137,9 @@ public async Task Delete_collection()
0,
new List<Field>
{
new Field("company_name", FieldType.String, false),
new Field("num_employees", FieldType.Int32, false),
new Field("country", FieldType.String, true),
new Field("company_name", FieldType.String, false, false, true),
new Field("num_employees", FieldType.Int32, false, false, true),
new Field("country", FieldType.String, true, false, true),
},
"num_employees");

Expand Down

0 comments on commit d5143d7

Please sign in to comment.