AKG
AKG

Reputation: 618

Scala Pattern match the result of a method during unit testing

I am unit testing a scala method which returns a string of the following form

SELECT col1 FROM tableName WHERE ts <= 1533499389

The integer value is epoch and would change on every execution. I was wondering if there is a way in Scala to just match the pattern to assert the value returned

Pattern can be something like :

SELECT col1 FROM tableName WHERE ts <= {}

Upvotes: 0

Views: 347

Answers (1)

prayagupadhyay
prayagupadhyay

Reputation: 31192

You can use match pattern,

import java.time.OffsetDateTime

import org.scalatest.{FunSuite, Matchers}

class SqlSpec extends FunSuite with Matchers {

  def fn = s"""SELECT col1 FROM tableName WHERE ts <= ${OffsetDateTime.now().toEpochSecond}"""

  test("whatever") {
    fn.matches("SELECT col1 FROM tableName WHERE ts <= \\d{10}") should be(true)
  }

}

Upvotes: 2

Related Questions