Skip to content

Update expectNoDifference #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2025
Merged
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
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 67 additions & 16 deletions Sources/ElasticsearchQueryBuilder/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@ extension esb {
}
}

/// Adds `query` block to the query syntax.
public struct Query<Component: DictComponent>: DictComponent {
var component: Component
public init(@QueryDictBuilder component: () -> Component) {
self.component = component()
}
public func makeDict() -> QueryDict {
let dict = self.component.makeDict()
if dict.isEmpty {
return [:]
} else {
return [ "query" : .dict(self.component.makeDict()) ]
}
}
}

/// Adds a `key` with any type of value to the query syntax.
public struct Key: DictComponent {
var key: String
Expand Down Expand Up @@ -108,6 +92,73 @@ extension esb {
}
}

/// Adds a block to the syntax.
public struct Dict<Component: DictComponent>: DictComponent {
var key: String
var component: Component
public init(_ key: String, @QueryDictBuilder component: () -> Component) {
self.key = key
self.component = component()
}
public func makeDict() -> QueryDict {
let dict = self.component.makeDict()
if dict.isEmpty {
return [:]
} else {
return [ key : .dict(self.component.makeDict()) ]
}
}
}

/// Adds a `query` block to the syntax.
public struct Query<Component: DictComponent>: DictComponent {
var component: Component
public init(@QueryDictBuilder component: () -> Component) {
self.component = component()
}
public func makeDict() -> QueryDict {
let dict = self.component.makeDict()
if dict.isEmpty {
return [:]
} else {
return [ "query" : .dict(self.component.makeDict()) ]
}
}
}

/// Adds an `aggs` block to the syntax.
public struct Aggs<Component: DictComponent>: DictComponent {
var component: Component
public init(@QueryDictBuilder component: () -> Component) {
self.component = component()
}
public func makeDict() -> QueryDict {
let dict = self.component.makeDict()
if dict.isEmpty {
return [:]
} else {
return [ "aggs" : .dict(self.component.makeDict()) ]
}
}
}

/// Defines and named aggregate within `Aggs`
public struct Agg: DictComponent {
var name: String
var term: QueryDict
public init(_ name: String, field: String) {
self.name = name
self.term = [ "field" : .string(field) ]
}
public init(_ name: String, term: QueryDict) {
self.name = name
self.term = term
}
public func makeDict() -> QueryDict {
return [ self.name : [ "terms" : .dict(self.term) ] ]
}
}

/// Adds `minimum_should_match` to the query syntax.
public struct MinimumShouldMatch: DictComponent {
var count: Int
Expand Down
38 changes: 19 additions & 19 deletions Tests/ElasticsearchQueryBuilderTests/BuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ElasticsearchQueryBuilderTests: XCTestCase {
esb.Pagination(from: 10)
}
let query = build(tags: nil)
XCTAssertNoDifference(query.makeQuery(), [
expectNoDifference(query.makeQuery(), [
"query": [
"match": [
"title": "Hello World"
Expand All @@ -32,11 +32,11 @@ final class ElasticsearchQueryBuilderTests: XCTestCase {
}
}
let queryTrue = build(bool: true)
XCTAssertNoDifference(queryTrue.makeQuery(), [
expectNoDifference(queryTrue.makeQuery(), [
"from": 10
])
let queryFalse = build(bool: false)
XCTAssertNoDifference(queryFalse.makeQuery(), [:])
expectNoDifference(queryFalse.makeQuery(), [:])
}
}

Expand All @@ -46,7 +46,7 @@ final class DictQueryBuilderTests: XCTestCase {
esb.Pagination(from: 10)
}
let query = build(tags: nil)
XCTAssertNoDifference(query.makeQuery(), [
expectNoDifference(query.makeQuery(), [
"from": 10
])
}
Expand All @@ -56,7 +56,7 @@ final class DictQueryBuilderTests: XCTestCase {
esb.Pagination(size: 20)
}
let query = build(tags: nil)
XCTAssertNoDifference(query.makeQuery(), [
expectNoDifference(query.makeQuery(), [
"from": 10,
"size": 20
])
Expand All @@ -70,13 +70,13 @@ final class DictQueryBuilderTests: XCTestCase {
}
}
let queryTrue = build(bool: true)
XCTAssertNoDifference(queryTrue.makeQuery(), [
expectNoDifference(queryTrue.makeQuery(), [
"query": [
"from": 10
]
])
let queryFalse = build(bool: false)
XCTAssertNoDifference(queryFalse.makeQuery(), [:])
expectNoDifference(queryFalse.makeQuery(), [:])
}
func testBuildEither() throws {
@ElasticsearchQueryBuilder func build(bool: Bool) -> some esb.QueryDSL {
Expand All @@ -89,13 +89,13 @@ final class DictQueryBuilderTests: XCTestCase {
}
}
let queryTrue = build(bool: true)
XCTAssertNoDifference(queryTrue.makeQuery(), [
expectNoDifference(queryTrue.makeQuery(), [
"query": [
"from": 10
]
])
let queryFalse = build(bool: false)
XCTAssertNoDifference(queryFalse.makeQuery(), [
expectNoDifference(queryFalse.makeQuery(), [
"query": [
"from": 20,
]
Expand All @@ -115,7 +115,7 @@ final class ArrayQueryBuilderTests: XCTestCase {
}
}
let query = build()
XCTAssertNoDifference(query.makeQuery(), [
expectNoDifference(query.makeQuery(), [
"should": [
[ "match": [ "title": "Hello World" ] ]
]
Expand All @@ -137,7 +137,7 @@ final class ArrayQueryBuilderTests: XCTestCase {
}
}
let query = build()
XCTAssertNoDifference(query.makeQuery(), [
expectNoDifference(query.makeQuery(), [
"should": [
[ "match": [ "title": "Hello World" ] ],
[ "match": [ "content": "Elasticsearch" ] ],
Expand All @@ -156,7 +156,7 @@ final class ArrayQueryBuilderTests: XCTestCase {
}
}
let query = build()
XCTAssertNoDifference(query.makeQuery(), [
expectNoDifference(query.makeQuery(), [
"should": [
[ "match": [ "title": "Hello World" ] ],
[ "from": 10 ]
Expand All @@ -181,13 +181,13 @@ final class ArrayQueryBuilderTests: XCTestCase {
}
}
let queryFalse = build(title: nil)
XCTAssertNoDifference(queryFalse.makeQuery(), [
expectNoDifference(queryFalse.makeQuery(), [
"should": [
[ "match": [ "content": "Elasticsearch" ] ],
]
])
let queryTrue = build(title: "Hello World")
XCTAssertNoDifference(queryTrue.makeQuery(), [
expectNoDifference(queryTrue.makeQuery(), [
"should": [
[ "match": [ "title": "Hello World" ] ],
[ "match": [ "content": "Elasticsearch" ] ],
Expand All @@ -214,9 +214,9 @@ final class ArrayQueryBuilderTests: XCTestCase {
}
}
let queryFalse = build(title: nil)
XCTAssertNoDifference(queryFalse.makeQuery(), [:])
expectNoDifference(queryFalse.makeQuery(), [:])
let queryTrue = build(title: "Hello World")
XCTAssertNoDifference(queryTrue.makeQuery(), [
expectNoDifference(queryTrue.makeQuery(), [
"should": [
[ "match": [ "title": "Hello World" ] ],
[ "match": [ "content": "Hello World" ] ],
Expand All @@ -234,13 +234,13 @@ final class ArrayQueryBuilderTests: XCTestCase {
}
}
let queryTrue = build(true)
XCTAssertNoDifference(queryTrue.makeQuery(), [
expectNoDifference(queryTrue.makeQuery(), [
"should": [
[ "from": 10 ]
]
])
let queryFalse = build(false)
XCTAssertNoDifference(queryFalse.makeQuery(), [
expectNoDifference(queryFalse.makeQuery(), [
"should": [
[ "from": 20 ]
]
Expand All @@ -259,7 +259,7 @@ final class ArrayQueryBuilderTests: XCTestCase {
}
}
let query = build()
XCTAssertNoDifference(query.makeQuery(), [
expectNoDifference(query.makeQuery(), [
"should": [
[ "match": [ "title": "Hello" ] ],
[ "match": [ "title": "World" ] ],
Expand Down
54 changes: 54 additions & 0 deletions Tests/ElasticsearchQueryBuilderTests/ComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ final class KeyTests: XCTestCase {
}
}

final class DictTests: XCTestCase {
func testBuildDict() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Dict("query") {
esb.Key("match_bool_prefix") {
[
"message": "quick brown f"
]
}
}
}
expectNoDifference(build().makeQuery(), [
"query": [
"match_bool_prefix": [
"message": "quick brown f"
]
]
])
}
func testBuildDictEmpty() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Dict("query") {
esb.Key("match", .dict([:]))
}
}
expectNoDifference(build().makeQuery(), [:])
}
}

final class ComposableBuilderTests: XCTestCase {
func testBuildDict() throws {
@QueryDictBuilder func makeKey() -> some DictComponent {
Expand Down Expand Up @@ -153,6 +182,31 @@ final class QueryTests: XCTestCase {
}
}

final class AggsTests: XCTestCase {
func testBuild() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Aggs {
esb.Agg("name", field: "name")
}
}
expectNoDifference(build().makeQuery(), [
"aggs": [
"name": [
"terms": [ "field": "name" ]
]
]
])
}
func testBuildEmpty() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Aggs {
esb.Key("name", .dict([:]))
}
}
expectNoDifference(build().makeQuery(), [:])
}
}

final class BoolTests: XCTestCase {
func testBuild() throws {
@ElasticsearchQueryBuilder func build(_ enabled: Bool) -> some esb.QueryDSL {
Expand Down
20 changes: 12 additions & 8 deletions Tests/ElasticsearchQueryBuilderTests/EncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@ final class EncodingTests: XCTestCase {
func assertFormattedDate(
_ value: QueryValue,
_ want: String,
file: StaticString = #file,
line: UInt = #line
fileID: StaticString = #fileID,
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) throws {
let encoder = JSONEncoder()
let got = try encoder.encode(value)
let gotString = try XCTUnwrap(String(data: got, encoding: .utf8), file: file, line: line)
XCTAssertNoDifference(gotString, want, file: file, line: line)
let gotString = try XCTUnwrap(String(data: got, encoding: .utf8), file: filePath, line: line)
expectNoDifference(gotString, want, fileID: fileID, filePath: filePath, line: line, column: column)
}

func assertFormattedDate(
_ value: Date,
dateEncodingStrategy: JSONEncoder.DateEncodingStrategy,
_ want: String,
file: StaticString = #file,
line: UInt = #line
fileID: StaticString = #fileID,
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) throws {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = dateEncodingStrategy
let got = try encoder.encode(value)
let gotString = try XCTUnwrap(String(data: got, encoding: .utf8), file: file, line: line)
XCTAssertNoDifference(gotString, want, file: file, line: line)
let gotString = try XCTUnwrap(String(data: got, encoding: .utf8), file: filePath, line: line)
expectNoDifference(gotString, want, fileID: fileID, filePath: filePath, line: line, column: column)
}

let date = Date(timeIntervalSince1970: 1001.12345)
Expand Down
Loading
Loading