package onextent.bluecql

import onextent.bluecql.cql.Statements
import org.apache.cassandra.cql3.statements.{CreateKeyspaceStatement, CreateTableStatement, CreateTypeStatement}
import org.scalatest.FlatSpec

class TestCollections extends FlatSpec with Config {

  val testcql = getClass.getResource("/test.cql").getPath
  property(FILE_PROP, testcql)
  property(PACKAGE_PROP, "onextent.bluecql.test")
  private def fixture =
    new {
      val statements = new Statements()
      val tables = Statements.tables()
      val types = Statements.types()
      val keyspaces = Statements.keyspaces()
    }

  def size(stuff: Iterator[Any]): Int = {
    stuff.foldLeft(0)((sum,_) => sum + 1)
  }

  it should "get each parsed stmt" in {
    val f = fixture
    for (stmt <- f.statements)
      println(stmt)
  }

  it should "get separate each parsed stmt" in {
    val f = fixture
    for (stmt <- f.statements)
      stmt match {
        case ks: CreateKeyspaceStatement => println(s"keyspace $ks")
        case tp: CreateTypeStatement => println(s"type $tp")
        case tb: CreateTableStatement => println(s"table $tb")
        case raw: CreateTableStatement.RawStatement => println(s"raw table $raw")
        case _ => println(s"other ${stmt}")
      }
  }

  it should "get separate each parsed type stmt" in {
    val f = fixture
    for (stmt <- f.types)
      stmt match {
        case raw: CreateTypeStatement => {
          println(s"type row $raw")
          //println(s" ejs type: ${raw.createType()}")
        }
        case _ => println(s"tble other ${stmt}")
      }
  }
  it should "get separate each parsed table stmt" in {
    val f = fixture
    for (stmt <- f.tables)
      stmt match {
        case raw: CreateTableStatement.RawStatement => {
          println(s"table row $raw")
          println(s" ejs cf: ${raw.columnFamily()}")
        }
        case _ => println(s"tble other ${stmt}")
      }
  }

  it should "get keyspace name" in {
    val f = fixture
    assert(f.keyspaces.next().keyspace() == "mykeyspace")
  }

  it should "get size" in {
    val f = fixture
    assert(size(f.statements) == 4)
    assert(size(f.keyspaces) == 1)
    assert(size(f.types) == 1)
    assert(size(f.tables) == 2)
  }
}

