Skip to content
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

feat(pollux): fix the lookup count in the credential schema DAL. ATL-1334 #315

Merged
merged 1 commit into from
Jan 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ object VerifiableCredentialSchema {
}
}

def lookupCount(
authorOpt: Option[String] = None,
nameOpt: Option[String] = None,
versionOpt: Option[String] = None,
attributeOpt: Option[String] = None,
tagOpt: Option[String] = None
) = run {
val query = dynamicQuery[VerifiableCredentialSchema]
.filterOpt(authorOpt)((vcs, author) => quote(vcs.author.like(author)))
.filterOpt(nameOpt)((vcs, name) => quote(vcs.name.like(name)))
.filterOpt(versionOpt)((vcs, version) => quote(vcs.version.like(version)))
.filter(vcs =>
attributeOpt
.fold(quote(true))(attr => quote(vcs.attributes.contains(lift(attr))))
)
.filter(vcs =>
tagOpt
.fold(quote(true))(tag => quote(vcs.tags.contains(lift(tag))))
)
.size

query
}

def lookup(
authorOpt: Option[String] = None,
nameOpt: Option[String] = None,
Expand All @@ -77,7 +101,7 @@ object VerifiableCredentialSchema {
tagOpt: Option[String] = None,
offsetOpt: Option[Int] = Some(0),
limitOpt: Option[Int] = Some(100)
) = {
) = run {
val quotedQuery = limitOpt
.fold(quote(query[VerifiableCredentialSchema]))(limit =>
quote(query[VerifiableCredentialSchema].take(lift(limit)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,16 @@ object VerifiableCredentialSchemaSqlIntegrationSpec extends ZIOSpecDefault {

schemaCreated = assert(firstActual)(equalTo(firstExpected.get))

} yield allSchemasHaveUniqueId && allSchemasHaveUniqueConstraint && schemaCreated
totalCount <- sql.totalCount.transact(tx)
lookupCount <- sql.lookupCount().transact(tx)

totalCountIsN = assert(totalCount)(equalTo(generatedSchemas.length))
lookupCountIsN = assert(lookupCount)(equalTo(generatedSchemas.length))

} yield allSchemasHaveUniqueId &&
allSchemasHaveUniqueConstraint &&
schemaCreated &&
totalCountIsN && lookupCountIsN
}
) @@ nondeterministic @@ sequential @@ timed

Expand Down Expand Up @@ -209,15 +218,23 @@ object VerifiableCredentialSchemaSqlIntegrationSpec extends ZIOSpecDefault {
// prepare and run samples
testResultTasks = lookupSamples
.map(lookupDataSample =>
sql
.run(lookupDataSample.lookupQuery)
lookupDataSample.lookupQuery
.transact(tx)
.map(expected => lookupDataSample.assertion(expected))
)

testResultCountTasks = lookupSamples
.map(lookupDataSample =>
lookupDataSample.lookupCountQuery
.transact(tx)
.map(actualCount => assert(actualCount)(equalTo(lookupDataSample.expectedCount) ?? s"$this"))
)

// compose the results
testResults <- ZIO.collectAll(testResultTasks)
testResult = testResults.reduce(_ && _)
testResultCounts <- ZIO.collectAll(testResultCountTasks)

testResult = (testResults ++ testResultCounts).reduce(_ && _)
} yield testResult
}
}
Expand All @@ -231,7 +248,7 @@ case class LookupDataSample(
offsetOpt: Option[Int],
limitOpt: Option[Int],
expected: List[VerifiableCredentialSchema],
expectedCount: Int
expectedCount: Long
) {

import model.VerifiableCredentialSchema.sql
Expand All @@ -247,6 +264,9 @@ case class LookupDataSample(
limitOpt
)

lazy val lookupCountQuery =
sql.lookupCount(authorOpt, nameOpt, versionOpt, attributeOpt, tagOpt)

def assertion(actual: List[VerifiableCredentialSchema]) = {
assert(actual)(equalTo(expected) ?? s"$this")
}
Expand Down