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

Droth 3541 separate queries for valid and expired links #2344

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
@@ -0,0 +1 @@
ALTER TABLE kgv_roadlink ADD CONSTRAINT expired_date_constraint CHECK (expired_date <= current_timestamp);
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ class RoadLinkDAO {
def fetchWalkwaysByBoundsAndMunicipalities(bounds: BoundingRectangle, municipalities: Set[Int]): Seq[RoadLinkFetched] = {
getByMunicipalitiesAndBounds(bounds, municipalities, Some(withMtkClassFilter(Set(12314))))
}

def fetchExpiredRoadLinks(): Seq[RoadLinkFetched] = {
getExpiredRoadLinks()
}

/**
* Calls db operation to fetch roadlinks with given filter.
Expand All @@ -312,20 +316,32 @@ class RoadLinkDAO {
getLinksWithFilter(filter(values)).asInstanceOf[Seq[T]]
} else Seq.empty[T]
}
protected def getLinksWithFilter(filter: String): Seq[RoadLinkFetched] = {
LogUtils.time(logger,"TEST LOG Getting roadlinks" ){
sql"""select linkid, mtkid, mtkhereflip, municipalitycode, shape, adminclass, directiontype, mtkclass, roadname_fi,

protected def getLinksWithFilter(filter: String): Seq[RoadLinkFetched] = {
LogUtils.time(logger,"TEST LOG Getting roadlinks" ){
sql"""select linkid, mtkid, mtkhereflip, municipalitycode, shape, adminclass, directiontype, mtkclass, roadname_fi,
roadname_se, roadnamesme, roadnamesmn, roadnamesms, roadnumber, roadpartnumber, constructiontype, verticallevel, horizontalaccuracy,
verticalaccuracy, created_date, last_edited_date, from_left, to_left, from_right, to_right,
surfacetype, geometrylength
from kgv_roadlink
where #$filter and constructiontype in (${ConstructionType.InUse.value},
where #$filter
and expired_date is null
and constructiontype in (${ConstructionType.InUse.value},
${ConstructionType.UnderConstruction.value},
${ConstructionType.Planned.value},
${ConstructionType.TemporarilyOutOfUse.value})
""".as[RoadLinkFetched].list
}
}
}

protected def getExpiredRoadLinks(): Seq[RoadLinkFetched] = {
sql"""select linkid, mtkid, mtkhereflip, municipalitycode, shape, adminclass, directiontype, mtkclass, roadname_fi,
roadname_se, roadnamesme, roadnamesmn, roadnamesms, roadnumber, roadpartnumber, constructiontype, verticallevel, horizontalaccuracy,
verticalaccuracy, created_date, last_edited_date, from_left, to_left, from_right, to_right,
surfacetype, geometrylength
from kgv_roadlink
where expired_date is not null
""".as[RoadLinkFetched].list
}

private def getByMunicipalitiesAndBounds(bounds: BoundingRectangle, municipalities: Set[Int],
Expand Down Expand Up @@ -365,6 +381,7 @@ class RoadLinkDAO {
sql"""select linkid
from kgv_roadlink
where #$polygonFilter
and expired_date is null
""".as[String].list
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import fi.liikennevirasto.digiroad2.util.{LinkIdGenerator, TestTransactions}
import org.joda.time.DateTime
import org.joda.time.DateTime.now
import org.scalatest.FunSuite
import org.scalatest.Matchers.{be, convertToAnyShouldWrapper}
import org.scalatest.Matchers.{be, contain, convertToAnyShouldWrapper}
import slick.jdbc.StaticQuery.interpolation
import slick.driver.JdbcDriver.backend.Database.dynamicSession

class RoadLinkDAOSpec extends FunSuite {


val dao = new RoadLinkDAO
def runWithRollback(test: => Unit): Unit = TestTransactions.runWithRollback()(test)

test("Fetch link where road names has hyphen symbol in middle of word") {
runWithRollback {
val dao = new RoadLinkDAO
val (linkId1, linkId2) = (LinkIdGenerator.generateRandom(), LinkIdGenerator.generateRandom())
sqlu"""INSERT INTO kgv_roadlink (linkid, municipalitycode, roadname_fi, roadname_se,constructiontype,shape) VALUES($linkId1, 853, 'Tarkk''ampujankatu', 'Skarpskyttegatan',${ConstructionType.InUse.value},'SRID=3067;LINESTRING ZM(385935.666 6671107.833 19.858 0, 386028.217 6671112.363 20.596 92.661)'::geometry)""".execute
sqlu"""INSERT INTO kgv_roadlink (linkid, municipalitycode, roadname_fi, roadname_se,constructiontype,shape) VALUES($linkId2, 441, 'Sammalinen', NULL,${ConstructionType.InUse.value},'SRID=3067;LINESTRING ZM(385935.666 6671107.833 19.85 0, 386028.217 6671112.363 20.596 92.661)'::geometry)""".execute
Expand All @@ -30,11 +31,45 @@ class RoadLinkDAOSpec extends FunSuite {
super.extractModifiedDate(createdDate,lastEdited)
}
}
val dao = new ExposeDao
val exposeDao = new ExposeDao
val dateLong = new DateTime(now)

dao.extractModifiedDate(Some(1L),Some(dateLong.getMillis)).get.toString should be(dateLong.toString)
dao.extractModifiedDate(Some(1L),None).get.toString should be(new DateTime(1).toString())
exposeDao.extractModifiedDate(Some(1L),Some(dateLong.getMillis)).get.toString should be(dateLong.toString)
exposeDao.extractModifiedDate(Some(1L),None).get.toString should be(new DateTime(1).toString())
}

test("only valid links are fetched") {
val (linkId1, linkId2, linkId3) = (LinkIdGenerator.generateRandom(), LinkIdGenerator.generateRandom(), LinkIdGenerator.generateRandom())
runWithRollback {
sqlu"""insert into kgv_roadlink (linkId, municipalitycode, constructiontype, shape) values ($linkId1, 235, ${ConstructionType.InUse.value},
'SRID=3067;LINESTRING ZM(385935.666 6671107.833 19.858 0, 386028.217 6671112.363 20.596 92.661)'::geometry)""".execute
sqlu"""insert into kgv_roadlink (linkId, municipalitycode, constructiontype, shape) values ($linkId2, 235, ${ConstructionType.InUse.value},
'SRID=3067;LINESTRING ZM(385935.666 6671107.833 19.85 0, 386028.217 6671112.363 20.596 92.661)'::geometry)""".execute
sqlu"""insert into kgv_roadlink (linkId, municipalitycode, constructiontype, shape, expired_date) values ($linkId3, 235, ${ConstructionType.InUse.value},
'SRID=3067;LINESTRING ZM(385935.666 6671107.833 19.85 0, 386028.217 6671112.363 20.596 92.661)'::geometry, '2022-05-10 10:52:28.783')""".execute
val validLinks = dao.fetchByLinkIds(Set(linkId1, linkId2, linkId3))
validLinks.size should be(2)
val validLinkIds = validLinks.map(_.linkId)
validLinkIds should contain(linkId1)
validLinkIds should contain(linkId2)
validLinkIds shouldNot contain(linkId3)
}
}


test("only expired links are fetched") {
val (linkId1, linkId2, linkId3) = (LinkIdGenerator.generateRandom(), LinkIdGenerator.generateRandom(), LinkIdGenerator.generateRandom())
runWithRollback {
sqlu"""insert into kgv_roadlink (linkId, municipalitycode, constructiontype, shape) values ($linkId1, 235, ${ConstructionType.InUse.value},
'SRID=3067;LINESTRING ZM(385935.666 6671107.833 19.858 0, 386028.217 6671112.363 20.596 92.661)'::geometry)""".execute
sqlu"""insert into kgv_roadlink (linkId, municipalitycode, constructiontype, shape) values ($linkId2, 235, ${ConstructionType.InUse.value},
'SRID=3067;LINESTRING ZM(385935.666 6671107.833 19.85 0, 386028.217 6671112.363 20.596 92.661)'::geometry)""".execute
sqlu"""insert into kgv_roadlink (linkId, municipalitycode, constructiontype, shape, expired_date) values ($linkId3, 235, ${ConstructionType.InUse.value},
'SRID=3067;LINESTRING ZM(385935.666 6671107.833 19.85 0, 386028.217 6671112.363 20.596 92.661)'::geometry, '2022-05-10 10:52:28.783')""".execute
val expiredLinks = dao.fetchExpiredRoadLinks()
val expiredLinkIds = expiredLinks.map(_.linkId)
expiredLinkIds shouldNot contain (linkId1)
expiredLinkIds shouldNot contain (linkId2)
expiredLinkIds should contain (linkId3)
}
}
}