<commit_before>/*
 * The aspiredb project
 * 
 * Copyright (c) 2013 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ubc.pavlab.aspiredb.server.dao;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

import java.util.Collection;

import org.apache.commons.lang.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ubc.pavlab.aspiredb.server.BaseSpringContextTest;
import ubc.pavlab.aspiredb.server.model.Label;
import ubc.pavlab.aspiredb.server.model.Project;
import ubc.pavlab.aspiredb.server.model.Subject;
import ubc.pavlab.aspiredb.server.model.Variant;
import ubc.pavlab.aspiredb.server.util.PersistentTestObjectHelper;

@RunWith(SpringJUnit4ClassRunner.class)
public class LabelDaoTest extends BaseSpringContextTest {
    @Autowired
    private LabelDao labelDao;

    @Autowired
    private ProjectDao projectDao;
    
    @Autowired
    private SubjectDao subjectDao;

    @Autowired
    private VariantDao variantDao;

    @Autowired
    PersistentTestObjectHelper testObjectHelper;

    @Autowired
    HibernateTransactionManager txManager;

    String testSubjectId;
    Subject testSubject;
    Variant testVariant;
    Project testProject;
    
    @Before
    public void init() {
        testSubjectId = RandomStringUtils.randomAlphanumeric( 5 );
        testSubject = testObjectHelper.createPersistentTestSubjectObjectWithCNV(testSubjectId);
        testVariant = testSubject.getVariants().iterator().next();
        
        testProject = new Project();
        testProject.setName(RandomStringUtils.randomAlphabetic( 4 ));
        testProject.getSubjects().add(testSubject);

        testProject = testObjectHelper.createPersistentProject(testProject);

    }

    @Test
    public void addSubjectLabel() {
        String name = RandomStringUtils.randomAlphabetic( 4 );
        Label label = new Label( name, "FF0000" );
        labelDao.create( label );
        testSubject.addLabel( label );
        subjectDao.update( testSubject );

        new InlineTransaction() {
            @Override
            public void instructions() {
                Subject s = subjectDao.findByPatientId( testSubjectId );
                assertTrue(s.getLabels().size() > 0);
            }
        }.execute();
    }

    @Test
    public void addVariantLabel() {
        String name = RandomStringUtils.randomAlphabetic( 4 );
        Label label = new Label( name, "FF0000" );
        labelDao.create( label );
        testVariant.addLabel( label );
        variantDao.update( testVariant );

        new InlineTransaction() {
            @Override
            public void instructions() {
                Subject s = subjectDao.findByPatientId( testSubjectId );
                Variant v = s.getVariants().iterator().next();<commit_msg>fix test<commit_after>/*
 * The aspiredb project
 * 
 * Copyright (c) 2013 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ubc.pavlab.aspiredb.server.dao;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

import java.util.Collection;

import org.apache.commons.lang.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ubc.pavlab.aspiredb.server.BaseSpringContextTest;
import ubc.pavlab.aspiredb.server.model.Label;
import ubc.pavlab.aspiredb.server.model.Project;
import ubc.pavlab.aspiredb.server.model.Subject;
import ubc.pavlab.aspiredb.server.model.Variant;
import ubc.pavlab.aspiredb.server.util.PersistentTestObjectHelper;

@RunWith(SpringJUnit4ClassRunner.class)
public class LabelDaoTest extends BaseSpringContextTest {
    @Autowired
    private LabelDao labelDao;

    @Autowired
    private ProjectDao projectDao;
    
    @Autowired
    private SubjectDao subjectDao;

    @Autowired
    private VariantDao variantDao;

    @Autowired
    PersistentTestObjectHelper testObjectHelper;

    @Autowired
    HibernateTransactionManager txManager;

    String testSubjectId;
    Subject testSubject;
    Variant testVariant;
    Project testProject;
    
    @Before
    public void init() {
        testSubjectId = RandomStringUtils.randomAlphanumeric( 5 );
        testSubject = testObjectHelper.createPersistentTestSubjectObjectWithCNV(testSubjectId);
        testVariant = testSubject.getVariants().iterator().next();
        
        testProject = new Project();
        testProject.setName(RandomStringUtils.randomAlphabetic( 4 ));
        

        testProject = testObjectHelper.createPersistentProject(testProject);
        
        testObjectHelper.addSubjectToProject( testSubject, testProject );
        

    }

    @Test
    public void addSubjectLabel() {
        

        new InlineTransaction() {
            @Override
            public void instructions() {
                String name = RandomStringUtils.randomAlphabetic( 4 );
                Label label = new Label( name, "FF0000" );
                labelDao.create( label );
                testSubject.addLabel( label );
                subjectDao.update( testSubject );
                
                
                Subject s = subjectDao.findByPatientId( testSubjectId );
                assertTrue(s.getLabels().size() > 0);
            }
        }.execute();
    }

    @Test
    public void addVariantLabel() {
        String name = RandomStringUtils.randomAlphabetic( 4 );
        Label label = new Label( name, "FF0000" );
        labelDao.create( label );
        testVariant.addLabel( label );
        variantDao.update( testVariant );

        new InlineTransaction() {
            @Override
            public void instructions() {
                Subject s = subjectDao.findByPatientId( testSubjectId );
                Variant v = s.getVariants().iterator().next();
{'fraction_non_alphanumeric': 0.0841284773551942, 'fraction_numerical': 0.006719526945303051, 'mean_word_length': 3.089010989010989, 'pattern_counts': {'":': 0, '<': 3, '<?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': '419798', 'n_tokens_mistral': 2034, 'n_tokens_neox': 2004, 'n_words': 500}