<commit_before>package org.intermine.task;

/*
 * Copyright (C) 2002-2009 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.intermine.metadata.Model;

import junit.framework.TestCase;

public class CreateIndexesTaskTest extends TestCase
{
    Model m;

    public void setUp() throws Exception {
        m = Model.getInstanceByName("testmodel");
    }

    //test defined keys and N-1 key
    public void testCreateStandardIndexes1() throws Exception {
        List expected = new ArrayList();
        expected.add("create index Department__key1 on Department(name, companyId, id)");
        expected.add("create index Department__key1__nulls on Department((name IS NULL))");
        expected.add("create index Department__key2 on Department(name, managerId, id)");
        expected.add("create index Department__key2__nulls on Department((name IS NULL))");
        expected.add("create index Department__company on Department(companyId, id)");
        expected.add("create index Department__manager on Department(managerId, id)");

        CreateIndexesTask task = new CreateIndexesTask();
        task.setAlias("os.unittest");
        task.setUp();
        Map statements = new LinkedHashMap();
        task.getStandardIndexStatements(m.getClassDescriptorByName("org.intermine.model.testmodel.Department"),
                                        statements);
        assertEquals(expected, getIndexStatementStrings(statements));
    }

    //test indirection table columns
    public void testCreateStandardIndexes2() throws Exception {
        List expected = new ArrayList();
        expected.add("create index HasSecretarysSecretarys__HasSecretarys on HasSecretarysSecretarys(HasSecretarys, Secretarys)");
        expected.add("create index HasSecretarysSecretarys__Secretarys on HasSecretarysSecretarys(Secretarys, HasSecretarys)");

        CreateIndexesTask task = new CreateIndexesTask();
        task.setAlias("os.unittest");
        task.setUp();

        Map statements = new LinkedHashMap();
        task.getStandardIndexStatements(m.getClassDescriptorByName("org.intermine.model.testmodel.HasSecretarys"),
                                   statements);
        assertEquals(expected, getIndexStatementStrings(statements));

        expected = new ArrayList();
        expected.add("create index Secretary__key on Secretary(name, id)");
        expected.add("create index Secretary__key__nulls on Secretary((name IS NULL))");
        task = new CreateIndexesTask();
        task.setAlias("os.unittest");
        task.setUp();
        statements = new LinkedHashMap();
        task.getStandardIndexStatements(m.getClassDescriptorByName("org.intermine.model.testmodel.Secretary"),
                                   statements);
        assertEquals(expected, getIndexStatementStrings(statements));
    }

    // test that primary key indexes are created on subclasses
    public void testCreateIndexesSubclasses() throws Exception {
        Set expected = new HashSet();
        expected.add("create index Contractor__ImportantPerson__key on Contractor(seniority, id)");
        expected.add("create index Contractor__ImportantPerson__key__nulls on Contractor((seniority IS NULL))");
        expected.add("create index CEO__ImportantPerson__key on CEO(seniority, id)");
        expected.add("create index CEO__ImportantPerson__key__nulls on CEO((seniority IS NULL))");
        expected.add("create index ImportantPerson__key on ImportantPerson(seniority, id)");
        expected.add("create index ImportantPerson__key__nulls on ImportantPerson((seniority IS NULL))");
        expected.add("create index Manager__ImportantPerson__key on Manager(seniority, id)");
        expected.add("create index Manager__ImportantPerson__key__nulls on Manager((seniority IS NULL))");

        CreateIndexesTask task = new CreateIndexesTask();
        task.setAlias("os.unittest");
        task.setUp();
        Map statements = new LinkedHashMap();
        task.getStandardIndexStatements(m.getClassDescriptorByName("org.intermine.model.testmodel.ImportantPerson"),
                                   statements);
        assertEquals(expected, new HashSet(getIndexStatementStrings(statements)));

        //assertEquals(new HashSet(expected), new HashSet(task.sqlStatements));
    }


    public void testCreateAttributeIndexes() throws Exception {
        List expected = new ArrayList();
        expected.add("create index CEO__salary on CEO(salary)");
        expected.add("create index CEO__title_like on CEO(lower(title) text_pattern_ops)");
        expected.add("create index CEO__title_equals on CEO(lower(title))");
        expected.add("create index CEO__title__nulls on CEO((title IS NULL))");
        expected.add("create index CEO__fullTime on CEO(fullTime)");
        expected.add("create index CEO__age on CEO(age)");
        expected.add("create index CEO__end_like on CEO(lower(intermine_end) text_pattern_ops)");
        expected.add("create index CEO__end_equals on CEO(lower(intermine_end))");
        expected.add("create index CEO__end__nulls on CEO((intermine_end IS NULL))");
        expected.add("create index CEO__name_like on CEO(lower(name) text_pattern_ops)");
        expected.add("create index CEO__name_equals on CEO(lower(name))");
        expected.add("create index CEO__name__nulls on CEO((name IS NULL))");
        expected.add("create index CEO__seniority on CEO(seniority)");
        expected.add("create index CEO__seniority__nulls on CEO((seniority IS NULL))");
<commit_msg>Fix tests. Refs #1944<commit_after>package org.intermine.task;

/*
 * Copyright (C) 2002-2009 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.intermine.metadata.Model;

import junit.framework.TestCase;

public class CreateIndexesTaskTest extends TestCase
{
    Model m;

    public void setUp() throws Exception {
        m = Model.getInstanceByName("testmodel");
    }

    //test defined keys and N-1 key
    public void testCreateStandardIndexes1() throws Exception {
        List expected = new ArrayList();
        expected.add("create index Department__key1 on Department(name, companyId, id)");
        expected.add("create index Department__key2 on Department(name, managerId, id)");
        expected.add("create index Department__company on Department(companyId, id)");
        expected.add("create index Department__manager on Department(managerId, id)");

        CreateIndexesTask task = new CreateIndexesTask();
        task.setAlias("os.unittest");
        task.setUp();
        Map statements = new LinkedHashMap();
        task.getStandardIndexStatements(m.getClassDescriptorByName("org.intermine.model.testmodel.Department"),
                                        statements);
        assertEquals(expected, getIndexStatementStrings(statements));
    }

    //test indirection table columns
    public void testCreateStandardIndexes2() throws Exception {
        List expected = new ArrayList();
        expected.add("create index HasSecretarysSecretarys__HasSecretarys on HasSecretarysSecretarys(HasSecretarys, Secretarys)");
        expected.add("create index HasSecretarysSecretarys__Secretarys on HasSecretarysSecretarys(Secretarys, HasSecretarys)");

        CreateIndexesTask task = new CreateIndexesTask();
        task.setAlias("os.unittest");
        task.setUp();

        Map statements = new LinkedHashMap();
        task.getStandardIndexStatements(m.getClassDescriptorByName("org.intermine.model.testmodel.HasSecretarys"),
                                   statements);
        assertEquals(expected, getIndexStatementStrings(statements));

        expected = new ArrayList();
        expected.add("create index Secretary__key on Secretary(name, id)");
        task = new CreateIndexesTask();
        task.setAlias("os.unittest");
        task.setUp();
        statements = new LinkedHashMap();
        task.getStandardIndexStatements(m.getClassDescriptorByName("org.intermine.model.testmodel.Secretary"),
                                   statements);
        assertEquals(expected, getIndexStatementStrings(statements));
    }

    // test that primary key indexes are created on subclasses
    public void testCreateIndexesSubclasses() throws Exception {
        Set expected = new HashSet();
        expected.add("create index Contractor__ImportantPerson__key on Contractor(seniority, id)");
        expected.add("create index CEO__ImportantPerson__key on CEO(seniority, id)");
        expected.add("create index ImportantPerson__key on ImportantPerson(seniority, id)");
        expected.add("create index Manager__ImportantPerson__key on Manager(seniority, id)");

        CreateIndexesTask task = new CreateIndexesTask();
        task.setAlias("os.unittest");
        task.setUp();
        Map statements = new LinkedHashMap();
        task.getStandardIndexStatements(m.getClassDescriptorByName("org.intermine.model.testmodel.ImportantPerson"),
                                   statements);
        assertEquals(expected, new HashSet(getIndexStatementStrings(statements)));

        //assertEquals(new HashSet(expected), new HashSet(task.sqlStatements));
    }


    public void testCreateAttributeIndexes() throws Exception {
        List expected = new ArrayList();
        expected.add("create index CEO__salary on CEO(salary)");
        expected.add("create index CEO__title_like on CEO(lower(title) text_pattern_ops)");
        expected.add("create index CEO__title_equals on CEO(lower(title))");
        expected.add("create index CEO__fullTime on CEO(fullTime)");
        expected.add("create index CEO__age on CEO(age)");
        expected.add("create index CEO__end_like on CEO(lower(intermine_end) text_pattern_ops)");
        expected.add("create index CEO__end_equals on CEO(lower(intermine_end))");
        expected.add("create index CEO__name_like on CEO(lower(name) text_pattern_ops)");
        expected.add("create index CEO__name_equals on CEO(lower(name))");
        expected.add("create index CEO__seniority on CEO(seniority)");

{'fraction_non_alphanumeric': 0.09830007390983, 'fraction_numerical': 0.0029563932002956393, 'mean_word_length': 3.6005099872503186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3525125', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2796, 'n_words': 700}