-
Notifications
You must be signed in to change notification settings - Fork 5
CASL-448 old -> new tests #402
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
265 changes: 265 additions & 0 deletions
265
here-naksha-lib-psql/src/commonTest/kotlin/naksha/psql/PgQueryBuilderTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,265 @@ | ||
| package naksha.psql | ||
|
|
||
| import naksha.geo.PointCoord | ||
| import naksha.geo.SpGeometry | ||
| import naksha.model.Naksha.NakshaCompanion.VIRT_COLLECTIONS | ||
| import naksha.model.request.ReadCollections | ||
| import naksha.model.request.ReadFeatures | ||
| import naksha.model.request.RequestQuery | ||
| import naksha.model.request.query.* | ||
| import naksha.model.request.query.StringOp.QStringOpCompanion.EQUALS | ||
| import naksha.model.request.query.TupleColumn.TupleColumn_C.ID | ||
| import naksha.model.request.query.TupleColumn.TupleColumn_C.UID | ||
| import naksha.psql.base.PgTestBase | ||
| import naksha.psql.executors.query.PgQueryBuilder | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertTrue | ||
|
|
||
| @Suppress("UNCHECKED_CAST") | ||
| class PgQueryBuilderTest : PgTestBase() { | ||
|
|
||
| private val session = storage.newReadSession() as PgSession | ||
|
|
||
| @Test | ||
| fun testReadNoConditions() { | ||
| // given | ||
| val req = ReadFeatures().apply { collectionIds += "foo" } | ||
|
|
||
| // when | ||
|
|
||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
| // then | ||
| assertEquals(0, query.argValues.size) | ||
| assertEquals( | ||
| """ | ||
| SELECT gzip(bytea_agg(tuple_number)) AS rs FROM (SELECT tuple_number FROM ( | ||
| (SELECT tuple_number, id FROM foo) | ||
| ) ORDER BY id, tuple_number) LIMIT 1000000; | ||
| """.trimIndent(), query.sql.trimIndent() | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testReadMultipleCollections() { | ||
| // given | ||
| val req = ReadFeatures().apply { | ||
| collectionIds += "foo1" | ||
| collectionIds += "foo2" | ||
| } | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
| // then | ||
| assertEquals(0, query.argValues.size) | ||
| assertEquals( | ||
| """ | ||
| SELECT gzip(bytea_agg(tuple_number)) AS rs FROM (SELECT tuple_number FROM ( | ||
| (SELECT tuple_number, id FROM foo1) UNION ALL | ||
| (SELECT tuple_number, id FROM foo2) | ||
| ) ORDER BY id, tuple_number) LIMIT 1000000; | ||
| """.trimIndent(), query.sql.trimIndent() | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testReadById() { | ||
| // given | ||
| val req = ReadFeatures().apply { | ||
| collectionIds += "foo" | ||
| featureIds += "f1" | ||
| } | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
| // then | ||
| assertEquals(1, query.argValues.size) | ||
| assertEquals("f1", (query.argValues[0] as Array<String>)[0]) | ||
| assertEquals( | ||
| """(SELECT tuple_number, id FROM foo WHERE id = ANY($1))""", | ||
| removeLimitWrapper(query.sql) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testReadWithOr() { | ||
| // given | ||
| val req = ReadFeatures().apply { | ||
| collectionIds += "foo" | ||
| featureIds += "f1" | ||
| featureIds += "f2" | ||
| } | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
| // then | ||
| assertEquals(1, query.argValues.size) | ||
| assertTrue(arrayOf("f1", "f2") contentEquals (query.argValues[0] as Array<String>)) | ||
| assertEquals( | ||
| """(SELECT tuple_number, id FROM foo WHERE id = ANY($1))""", | ||
| removeLimitWrapper(query.sql) | ||
| ) | ||
| } | ||
|
|
||
| // TODO FIXME uncomment me once property read is ready. | ||
| // @Test | ||
| fun testReadWithAnd() { | ||
| // given | ||
| val req = ReadFeatures().apply { | ||
| collectionIds += "foo" | ||
| query = RequestQuery().apply { | ||
| properties = POr( | ||
| PQuery(Property(ID), EQUALS, "f1"), | ||
| PAnd( | ||
| PQuery(Property(ID), EQUALS, "f2"), | ||
| PQuery(Property(UID), DoubleOp.LT, 2.0) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
| // then | ||
| assertEquals(0, query.argValues.size) | ||
| assertEquals( | ||
| """((SELECT tuple_number, id FROM foo WHERE (id=$1 OR (id=$2 AND uid<$3)))""", | ||
| removeLimitWrapper(query.sql) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testReadHistory() { | ||
| // given | ||
| val req = ReadFeatures().apply { | ||
| collectionIds += "foo" | ||
| queryHistory = true | ||
| } | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
|
|
||
| // then | ||
| assertEquals( | ||
| """ | ||
| (SELECT tuple_number, id FROM foo) UNION ALL | ||
| (SELECT tuple_number, id FROM "foo${'$'}hst") | ||
| """.trimIndent(), removeLimitWrapper(query.sql) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testReadWithHistoryAndDel() { | ||
| // given | ||
| val req = ReadFeatures().apply { | ||
| collectionIds += "foo" | ||
| featureIds += "f1" | ||
| queryHistory = true | ||
| queryDeleted = true | ||
| } | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
|
|
||
| // then | ||
| assertEquals( | ||
| """ | ||
| (SELECT tuple_number, id FROM foo WHERE id = ANY(${'$'}1)) UNION ALL | ||
| (SELECT tuple_number, id FROM "foo${'$'}del" WHERE id = ANY($1)) UNION ALL | ||
| (SELECT tuple_number, id FROM "foo${'$'}hst" WHERE id = ANY($1)) | ||
| """.trimIndent(), removeLimitWrapper(query.sql) | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| fun testReadBySpatial() { | ||
| // given | ||
| val req = ReadFeatures().apply { | ||
| collectionIds += "foo" | ||
| query = RequestQuery().apply { | ||
| spatial = SpIntersects(SpGeometry(PointCoord(1.0, 1.0, 1.0))) | ||
| } | ||
| } | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
| // then | ||
| assertEquals( | ||
| """(SELECT tuple_number, id FROM foo WHERE (ST_Intersects(naksha_geometry(geo, flags), naksha_geometry($1, 0))))""", | ||
| removeLimitWrapper(query.sql) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testReadBySpatialWithBuffer() { | ||
| // given | ||
| val geometryTransformation = SpBuffer(22.2, geography = true) | ||
| val req = ReadFeatures().apply { | ||
| collectionIds += "foo" | ||
| query = RequestQuery().apply { | ||
| spatial = SpIntersects(SpGeometry(PointCoord(1.0, 1.0, 1.0)), geometryTransformation) | ||
| } | ||
| } | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
| // then | ||
| assertEquals( | ||
| """(SELECT tuple_number, id FROM foo WHERE (ST_Intersects(naksha_geometry(geo, flags), ST_Buffer(naksha_geometry($1, 0)::geography, $2))))""", | ||
| removeLimitWrapper(query.sql) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testReadAllCollections() { | ||
| // given | ||
| val req = ReadCollections() | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
| // then | ||
| assertEquals(0, query.argValues.size) | ||
| assertEquals( | ||
| """(SELECT tuple_number, id FROM "$VIRT_COLLECTIONS")""", | ||
| removeLimitWrapper(query.sql) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testTagsQuery() { | ||
| // given | ||
| val req = ReadFeatures().apply { | ||
| collectionIds += "foo" | ||
| query = RequestQuery().apply { | ||
| tags = TagExists("stg") | ||
| } | ||
| } | ||
|
|
||
| // when | ||
| val query = PgQueryBuilder(session, req).build() | ||
|
|
||
| // then | ||
| assertEquals(1, query.argValues.size) | ||
| assertEquals( | ||
| """(SELECT tuple_number, id FROM foo WHERE (naksha_tags(tags, flags) ?? $1))""", | ||
| removeLimitWrapper(query.sql) | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| private fun removeLimitWrapper(sql: String) = | ||
| sql.replace("SELECT gzip(bytea_agg(tuple_number)) AS rs FROM (SELECT tuple_number FROM (\n", "") | ||
| .replace("\n) ORDER BY id, tuple_number) LIMIT 1000000;", "") | ||
| .trimIndent() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.