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

Fix sub-contracts endpoint returning duplicate #578

Open
wants to merge 1 commit into
base: mobula-integration
Choose a base branch
from
Open
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 @@ -141,10 +141,14 @@ object ContractQueries {
def getSubContractsQuery(parent: Address, pagination: Pagination): DBActionSR[Address] = {
sql"""
SELECT contract
FROM contracts
WHERE parent = $parent
FROM (
SELECT DISTINCT ON (contract) contract, creation_timestamp, creation_event_order
FROM contracts
WHERE parent = $parent
ORDER BY contract, creation_timestamp DESC, creation_event_order
) AS distinct_contracts
ORDER BY creation_timestamp DESC, creation_event_order
"""
"""
.paginate(pagination)
.asASE[Address](addressGetResult)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,34 @@ class ContractQueriesSpec

}
}

"getSubContractsQuery when duplicate contracts exists" in {
val parent = addressGen.sample.get
val pagination = Pagination.unsafe(1, 5)

forAll(
createEventsGen(Some(parent)),
createEventsGen()
) { case ((groupIndex, events), (otherGroup, otherEvents)) =>
val eventsWithDuplicates = events ++ events.map(event =>
event.copy(timestamp = event.timestamp.plusSecondsUnsafe(1))
)

run(ContractSchema.table.delete).futureValue
run(ContractQueries.insertContractCreation(eventsWithDuplicates, groupIndex)).futureValue
run(ContractQueries.insertContractCreation(otherEvents, otherGroup)).futureValue

run(
ContractQueries.getSubContractsQuery(parent, pagination)
).futureValue is eventsWithDuplicates
.sortBy(_.timestamp)
.reverse
.flatMap(ContractEntity.creationFromEventEntity(_, groupIndex))
.map(_.contract)
.distinct
.take(pagination.limit)

}
}
}
}
Loading