<commit_before>package com.github.tminglei.slickpg

import org.scalatest.funsuite.AnyFunSuite
import slick.jdbc.GetResult

import scala.concurrent.Await
import scala.concurrent.duration._

class PgSearchSupportSuite extends AnyFunSuite with PostgresContainer {
  import MyPostgresProfile.api._

  lazy val db = Database.forURL(url = container.jdbcUrl, driver = "org.postgresql.Driver")

  case class TestBean(id: Long, text: String, search: TsVector, comment: String)

  class TestTable(tag: Tag) extends Table[TestBean](tag, "tsTestTable") {
    def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
    def text = column[String]("text")
    def search = column[TsVector]("search")
    def comment = column[String]("comment")

    def * = (id, text, search, comment) <> (TestBean.tupled, TestBean.unapply)
  }
  val Tests = TableQuery[TestTable]

  //-----------------------------------------------------------------------------

  val testRec1 = TestBean(33L, "fat cat ate rat", TsVector("'ate' 'cat' 'fat' 'rat'"), "")
  val testRec2 = TestBean(37L, "cat", TsVector("'ca'"), "fat")
  val testRec3 = TestBean(38L, "<NAME>", TsVector("'mike' 'rose'"), "")
  val testRec4 = TestBean(39L, "mike and rose", TsVector("'mike' 'and' 'rose'"), "")

  test("Text search Lifted support") {
    Await.result(db.run(
      DBIO.seq(
        Tests.schema create,
        ///
        Tests forceInsertAll List(testRec1, testRec2, testRec3, testRec4)
      ).andThen(
        DBIO.seq(
          // get_current_ts_config
          Tests.filter(_.id === 33L).map(r => currTsConfig()).result.head.map(
            r => assert("english" === r)
          ),
          // @@
          Tests.filter(r => toTsVector(r.text, Some("english")) @@ tsQuery("cat & rat")).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          // '||' (concatenate)
          Tests.filter(r => (toTsVector(r.text) @+ toTsVector(r.comment)) @@ tsQuery("cat & fat".bind)).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1, testRec2) === r)
          ),
          // &&
          Tests.filter(r => toTsVector(r.text) @@ (tsQuery("cat".bind) @& tsQuery("rat".bind))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          // '||' (or)
          Tests.filter(r => toTsVector(r.text) @@ (tsQuery("cat".bind) @| tsQuery("rat".bind))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1, testRec2) === r)
          ),
          // !!
          Tests.filter(r => toTsVector(r.text) @@ (tsQuery("cat".bind) @& tsQuery("rat".bind).!!)).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec2) === r)
          ),
          // @>
          Tests.filter(r => plainToTsQuery(r.text, Some("english")) @> toTsQuery(r.comment, Some("english"))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          Tests.filter(r => phraseToTsQuery(r.text, Some("english")) @> toTsQuery(r.comment, Some("english"))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          Tests.filter(r => webSearchToTsQuery(r.text, Some("english")) @> toTsQuery(r.comment, Some("english"))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          // <->
          Tests.filter(r => toTsVector(r.text, Some("english")) @@ (tsQuery("mike".bind) <-> tsQuery("rose".bind))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          // length<commit_msg>Fix test<commit_after>package com.github.tminglei.slickpg

import org.scalatest.funsuite.AnyFunSuite
import slick.jdbc.GetResult

import scala.concurrent.Await
import scala.concurrent.duration._

class PgSearchSupportSuite extends AnyFunSuite with PostgresContainer {
  import MyPostgresProfile.api._

  lazy val db = Database.forURL(url = container.jdbcUrl, driver = "org.postgresql.Driver")

  case class TestBean(id: Long, text: String, search: TsVector, comment: String)

  class TestTable(tag: Tag) extends Table[TestBean](tag, "tsTestTable") {
    def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
    def text = column[String]("text")
    def search = column[TsVector]("search")
    def comment = column[String]("comment")

    def * = (id, text, search, comment) <> (TestBean.tupled, TestBean.unapply)
  }
  val Tests = TableQuery[TestTable]

  //-----------------------------------------------------------------------------

  val testRec1 = TestBean(33L, "fat cat ate rat", TsVector("'ate' 'cat' 'fat' 'rat'"), "")
  val testRec2 = TestBean(37L, "cat", TsVector("'ca'"), "fat")
  val testRec3 = TestBean(38L, "mike rose", TsVector("'mike' 'rose'"), "")
  val testRec4 = TestBean(39L, "mike and rose", TsVector("'mike' 'and' 'rose'"), "")

  test("Text search Lifted support") {
    Await.result(db.run(
      DBIO.seq(
        Tests.schema create,
        ///
        Tests forceInsertAll List(testRec1, testRec2, testRec3, testRec4)
      ).andThen(
        DBIO.seq(
          // get_current_ts_config
          Tests.filter(_.id === 33L).map(r => currTsConfig()).result.head.map(
            r => assert("english" === r)
          ),
          // @@
          Tests.filter(r => toTsVector(r.text, Some("english")) @@ tsQuery("cat & rat")).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          // '||' (concatenate)
          Tests.filter(r => (toTsVector(r.text) @+ toTsVector(r.comment)) @@ tsQuery("cat & fat".bind)).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1, testRec2) === r)
          ),
          // &&
          Tests.filter(r => toTsVector(r.text) @@ (tsQuery("cat".bind) @& tsQuery("rat".bind))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          // '||' (or)
          Tests.filter(r => toTsVector(r.text) @@ (tsQuery("cat".bind) @| tsQuery("rat".bind))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1, testRec2) === r)
          ),
          // !!
          Tests.filter(r => toTsVector(r.text) @@ (tsQuery("cat".bind) @& tsQuery("rat".bind).!!)).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec2) === r)
          ),
          // @>
          Tests.filter(r => plainToTsQuery(r.text, Some("english")) @> toTsQuery(r.comment, Some("english"))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1, testRec3, testRec4) === r)
          ),
          Tests.filter(r => phraseToTsQuery(r.text, Some("english")) @> toTsQuery(r.comment, Some("english"))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          Tests.filter(r => webSearchToTsQuery(r.text, Some("english")) @> toTsQuery(r.comment, Some("english"))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          // <->
          Tests.filter(r => toTsVector(r.text, Some("english")) @@ (tsQuery("mike".bind) <-> tsQuery("rose".bind))).sortBy(_.id).to[List].result.map(
            r => assert(List(testRec1) === r)
          ),
          // length
{'fraction_non_alphanumeric': 0.21119733924611975, 'fraction_numerical': 0.008314855875831485, 'mean_word_length': 2.542955326460481, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3437312', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2397, 'n_words': 391}