<commit_before># Copyright (C) 2016 Google Inc.
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>

"""Tests for /query api endpoint."""

from datetime import datetime
from os.path import abspath, dirname, join
from flask import json
from nose.plugins.skip import SkipTest

from ggrc.app import app
from integration.ggrc import TestCase


THIS_ABS_PATH = abspath(dirname(__file__))
CSV_DIR = join(THIS_ABS_PATH, '../converters/test_csvs/')

# to be moved into converters.query_helper
DATE_FORMAT_REQUEST = "%m/%d/%Y"
DATE_FORMAT_RESPONSE = "%Y-%m-%d"


class TestAdvancedQueryAPI(TestCase):
  """Basic tests for /query api."""

  @classmethod
  def setUpClass(cls):
    """Set up test cases for all tests."""
    TestCase.clear_data()
    # This imported file could be simplified a bit to speed up testing.
    cls.import_file("data_for_export_testing.csv")

  @classmethod
  def import_file(cls, filename):
    """Import a csv file.

    The file should contain all objects and mappings needed for writing proper
    query api tests.
    """
    client = app.test_client()
    client.get("/login")
    data = {"file": (open(join(CSV_DIR, filename)), filename)}
    headers = {"X-test-only": "false", "X-requested-by": "gGRC"}
    client.post("/_service/import_csv", data=data, headers=headers)

  def setUp(self):
    self.client.get("/login")

  def _post(self, data):
    """Make a POST to /query endpoint."""
    if not isinstance(data, list):
      data = [data]
    headers = {'Content-Type': 'application/json', }
    return self.client.post("/query", data=json.dumps(data), headers=headers)

  def test_basic_query_eq(self):
    """Filter by = operator."""
    title = "Cat ipsum 1"
    data = {
        "object_name": "Program",
        "filters": {
            "expression": {
                "left": "title",
                "op": {"name": "="},
                "right": title,
            },
        },
    }
    response = json.loads(self._post(data).data)[0]
    programs = response.get("Program")
    self.assertIsNot(programs, None)
    self.assertEqual(programs["count"], 1)
    self.assertEqual(len(programs["values"]), programs["count"])
    self.assertEqual(programs["values"][0]["title"], title)

  def test_basic_query_in(self):
    """Filter by ~ operator."""<commit_msg>Replace ' with " in Query API tests<commit_after># Copyright (C) 2016 Google Inc.
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>

"""Tests for /query api endpoint."""

from datetime import datetime
from os.path import abspath, dirname, join
from flask import json
from nose.plugins.skip import SkipTest

from ggrc.app import app
from integration.ggrc import TestCase


THIS_ABS_PATH = abspath(dirname(__file__))
CSV_DIR = join(THIS_ABS_PATH, "../converters/test_csvs/")

# to be moved into converters.query_helper
DATE_FORMAT_REQUEST = "%m/%d/%Y"
DATE_FORMAT_RESPONSE = "%Y-%m-%d"


class TestAdvancedQueryAPI(TestCase):
  """Basic tests for /query api."""

  @classmethod
  def setUpClass(cls):
    """Set up test cases for all tests."""
    TestCase.clear_data()
    # This imported file could be simplified a bit to speed up testing.
    cls.import_file("data_for_export_testing.csv")

  @classmethod
  def import_file(cls, filename):
    """Import a csv file.

    The file should contain all objects and mappings needed for writing proper
    query api tests.
    """
    client = app.test_client()
    client.get("/login")
    data = {"file": (open(join(CSV_DIR, filename)), filename)}
    headers = {"X-test-only": "false", "X-requested-by": "gGRC"}
    client.post("/_service/import_csv", data=data, headers=headers)

  def setUp(self):
    self.client.get("/login")

  def _post(self, data):
    """Make a POST to /query endpoint."""
    if not isinstance(data, list):
      data = [data]
    headers = {"Content-Type": "application/json", }
    return self.client.post("/query", data=json.dumps(data), headers=headers)

  def test_basic_query_eq(self):
    """Filter by = operator."""
    title = "Cat ipsum 1"
    data = {
        "object_name": "Program",
        "filters": {
            "expression": {
                "left": "title",
                "op": {"name": "="},
                "right": title,
            },
        },
    }
    response = json.loads(self._post(data).data)[0]
    programs = response.get("Program")
    self.assertIsNot(programs, None)
    self.assertEqual(programs["count"], 1)
    self.assertEqual(len(programs["values"]), programs["count"])
    self.assertEqual(programs["values"][0]["title"], title)

  def test_basic_query_in(self):
    """Filter by ~ operator."""
{'fraction_non_alphanumeric': 0.1483069009858551, 'fraction_numerical': 0.0042863266180882984, 'mean_word_length': 3.353544776119403, 'pattern_counts': {'":': 21, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6531158', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1441, 'n_words': 382}