<commit_before>package org.influxdb.querybuilder;

import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.influxdb.dto.Query;
import org.junit.jupiter.api.Test;

public class BuiltQueryTest {

  private static final String DATABASE = "testdb";

  @Test
  public void testAlias() {
    Query query = new Query("SELECT MAX(k) AS hello FROM foobar;", DATABASE);
    Query select = select().max("k").as("hello").from(DATABASE, "foobar");

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testRegex() {
    Query query = new Query("SELECT MAX(k) FROM foobar WHERE k =~ /[0-9]/;", DATABASE);
    Query select = select().max("k").from(DATABASE, "foobar").where(regex("k", "/[0-9]/"));

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testNegativeRegex() {
    Query query = new Query("SELECT MAX(k) FROM foobar WHERE k !~ /[0-9]/;", DATABASE);
    Query select = select().max("k").from(DATABASE, "foobar").where(nregex("k", "/[0-9]/"));

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testContains() {
    Query query = new Query("SELECT MAX(k) FROM foobar WHERE k =~ /text/;", DATABASE);
    Query select = select().max("k").from(DATABASE, "foobar").where(contains("k", "text"));

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testDistinct() {
    Query query = new Query("SELECT DISTINCT k FROM foobar;", DATABASE);
    Query select = select().column("k").distinct().from(DATABASE, "foobar");

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testDistinctWithExpression() {
    Query query =
        new Query("SELECT DISTINCT COUNT(test1) FROM foobar LIMIT 1 OFFSET 20;", DATABASE);
    Query select = select().count("test1").distinct().from(DATABASE, "foobar").limit(1, 20);

    assertEquals(query.getCommand(), select.getCommand());<commit_msg>Added test on invalid negative regex<commit_after>package org.influxdb.querybuilder;

import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.sun.javaws.exceptions.InvalidArgumentException;
import org.influxdb.dto.Query;
import org.junit.jupiter.api.Test;

public class BuiltQueryTest {

  private static final String DATABASE = "testdb";

  @Test
  public void testAlias() {
    Query query = new Query("SELECT MAX(k) AS hello FROM foobar;", DATABASE);
    Query select = select().max("k").as("hello").from(DATABASE, "foobar");

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testRegex() {
    Query query = new Query("SELECT MAX(k) FROM foobar WHERE k =~ /[0-9]/;", DATABASE);
    Query select = select().max("k").from(DATABASE, "foobar").where(regex("k", "/[0-9]/"));

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testNegativeRegex() {
    Query query = new Query("SELECT MAX(k) FROM foobar WHERE k !~ /[0-9]/;", DATABASE);
    Query select = select().max("k").from(DATABASE, "foobar").where(nregex("k", "/[0-9]/"));

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testInvalidNegativeRegex() {
    assertThrows(IllegalArgumentException.class, () -> select().max("k").from(DATABASE, "foobar").where(nregex("k", null)),"Missing text for expression");
  }

  @Test
  public void testContains() {
    Query query = new Query("SELECT MAX(k) FROM foobar WHERE k =~ /text/;", DATABASE);
    Query select = select().max("k").from(DATABASE, "foobar").where(contains("k", "text"));

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testDistinct() {
    Query query = new Query("SELECT DISTINCT k FROM foobar;", DATABASE);
    Query select = select().column("k").distinct().from(DATABASE, "foobar");

    assertEquals(query.getCommand(), select.getCommand());
    assertEquals(query.getDatabase(), select.getDatabase());
  }

  @Test
  public void testDistinctWithExpression() {
    Query query =
        new Query("SELECT DISTINCT COUNT(test1) FROM foobar LIMIT 1 OFFSET 20;", DATABASE);
    Query select = select().count("test1").distinct().from(DATABASE, "foobar").limit(1, 20);

    assertEquals(query.getCommand(), select.getCommand());
{'fraction_non_alphanumeric': 0.1646400308820691, 'fraction_numerical': 0.006176413819725921, 'mean_word_length': 4.751387347391787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2766809', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1558, 'n_words': 349}