<commit_before>import deebee.sql.ast.Node
import org.scalatest.{FlatSpec, Matchers}
import deebee.sql.SQLParser
import scala.io.Source

import scala.util.{Try, Success}

/**
 * Tests for the SQL parser
 * Created by hawk on 11/22/14.
 */
class ParserSpec extends FlatSpec with Matchers {

  private def writersSchema = Source fromURL (getClass getResource "/Writers.sql") getLines()

  /**
   * Helper method for testing. Asserts that the a SQL query can be reconstructed from its'
   * AST representation (which should more or less ensure that it was parsed correctly).
   * @param sql the SQL query to test
   */
  private def assertReconstructed (sql: String): Unit = {
    val result: Try[Node] = SQLParser.parse(sql)
    result shouldBe a [Success[Node]]
    result.get.emitSQL should include (sql)
  }
  "The SQL Parser" should "parse a simple CREATE TABLE statement" in {
    assertReconstructed(
      "CREATE TABLE Test (\n" +
        "\ttest INTEGER PRIMARY KEY NOT NULL" +
      "\n);"
    )
  }

  it should "parse a CREATE TABLE statement with multiple attributes and types" in {
    assertReconstructed("CREATE TABLE Test (\n" +
      "\ttestint INTEGER PRIMARY KEY NOT NULL,\n" +
      "\ttestvarchar VARCHAR(255),\n" +
      "\ttestnumeric NUMERIC(5, 8) NOT NULL,\n" +
      "\ttestchar CHAR(15)\n" +
      ");"
    )
  }

  it should "parse the first CREATE TABLE from Writers.sql, ignoring comments" in {
    val result: Try[Node] = SQLParser.parse(writersSchema take 15 mkString "\n")
    result shouldBe a [Success[Node]]
    result.get.emitSQL should include (
    "CREATE TABLE Writers (\n" +
      "\tid INTEGER NOT NULL PRIMARY KEY,\n" +
      "\tfirst_name VARCHAR(15) NOT NULL,\n" +
      "\tmiddle_name VARCHAR(15),\n" +
      "\tlast_name VARCHAR(15) NOT NULL,\n" +
      "\tbirth_date VARCHAR(10) NOT NULL,\n" +
      "\tdeath_date VARCHAR(10),\n" +
      "\tcountry_of_origin VARCHAR(20) NOT NULL\n" +
      ");"
    )
  }
  it should "parse a CREATE TABLE statement from Writers.sql with FOREIGN KEY constraints" in {
    val result: Try[Node] = SQLParser.parse(writersSchema slice (27,33)  mkString "\n")
    result shouldBe a [Success[Node]]
    result.get.emitSQL should include (
    "CREATE TABLE Contemporaries (\n" +
      "\twriter_id INTEGER NOT NULL,\n" +
      "\tcontemporary_id INTEGER NOT NULL,\n" +
      "\tFOREIGN KEY (writer_id) REFERENCES Writers (id)\n" +<commit_msg>Add a test for the DECIMAL data type (mostly for coverage purposes)<commit_after>import deebee.sql.ast.Node
import org.scalatest.{FlatSpec, Matchers}
import deebee.sql.SQLParser
import scala.io.Source

import scala.util.{Try, Success}

/**
 * Tests for the SQL parser
 * Created by hawk on 11/22/14.
 */
class ParserSpec extends FlatSpec with Matchers {

  private def writersSchema = Source fromURL (getClass getResource "/Writers.sql") getLines()

  /**
   * Helper method for testing. Asserts that the a SQL query can be reconstructed from its'
   * AST representation (which should more or less ensure that it was parsed correctly).
   * @param sql the SQL query to test
   */
  private def assertReconstructed (sql: String): Unit = {
    val result: Try[Node] = SQLParser.parse(sql)
    result shouldBe a [Success[Node]]
    result.get.emitSQL should include (sql)
  }
  "The SQL Parser" should "parse a simple CREATE TABLE statement" in {
    assertReconstructed(
      "CREATE TABLE Test (\n" +
        "\ttest INTEGER PRIMARY KEY NOT NULL" +
      "\n);"
    )
  }

  it should "parse a CREATE TABLE statement with multiple attributes and types" in {
    assertReconstructed("CREATE TABLE Test (\n" +
      "\ttestint INTEGER PRIMARY KEY NOT NULL,\n" +
      "\ttestvarchar VARCHAR(255),\n" +
      "\ttestnumeric NUMERIC(5, 8) NOT NULL,\n" +
      "\ttestdec DECIMAL(1, 2),\n" +
      "\ttestchar CHAR(15)\n" +
      ");"
    )
  }

  it should "parse the first CREATE TABLE from Writers.sql, ignoring comments" in {
    val result: Try[Node] = SQLParser.parse(writersSchema take 15 mkString "\n")
    result shouldBe a [Success[Node]]
    result.get.emitSQL should include (
    "CREATE TABLE Writers (\n" +
      "\tid INTEGER NOT NULL PRIMARY KEY,\n" +
      "\tfirst_name VARCHAR(15) NOT NULL,\n" +
      "\tmiddle_name VARCHAR(15),\n" +
      "\tlast_name VARCHAR(15) NOT NULL,\n" +
      "\tbirth_date VARCHAR(10) NOT NULL,\n" +
      "\tdeath_date VARCHAR(10),\n" +
      "\tcountry_of_origin VARCHAR(20) NOT NULL\n" +
      ");"
    )
  }
  it should "parse a CREATE TABLE statement from Writers.sql with FOREIGN KEY constraints" in {
    val result: Try[Node] = SQLParser.parse(writersSchema slice (27,33)  mkString "\n")
    result shouldBe a [Success[Node]]
    result.get.emitSQL should include (
    "CREATE TABLE Contemporaries (\n" +
      "\twriter_id INTEGER NOT NULL,\n" +
      "\tcontemporary_id INTEGER NOT NULL,\n" +
      "\tFOREIGN KEY (writer_id) REFERENCES Writers (id)\n" +
{'fraction_non_alphanumeric': 0.11548609701644003, 'fraction_numerical': 0.012989648873553887, 'mean_word_length': 3.086235489220564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2991427', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1533, 'n_words': 512}