<commit_before><commit_msg>#1293 Provided tests (need to name them properly).<commit_after>package ua.com.fielden.platform.entity.query.metadata;

import static org.junit.Assert.assertEquals;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.expr;
import static ua.com.fielden.platform.entity.query.metadata.EntityKeyExpressionGenerator.EMPTY_STRING;
import static ua.com.fielden.platform.entity.query.metadata.EntityKeyExpressionGenerator.getVirtualKeyPropForEntityWithCompositeKey;
import static ua.com.fielden.platform.entity.query.metadata.EntityKeyExpressionGenerator.TypeInfo.ENTITY;
import static ua.com.fielden.platform.entity.query.metadata.EntityKeyExpressionGenerator.TypeInfo.NON_STRING;
import static ua.com.fielden.platform.entity.query.metadata.EntityKeyExpressionGenerator.TypeInfo.STRING;
import static ua.com.fielden.platform.utils.CollectionUtil.listOf;

import org.junit.Test;

import ua.com.fielden.platform.entity.query.metadata.EntityKeyExpressionGenerator.TypeInfo;
import ua.com.fielden.platform.entity.query.model.ExpressionModel;

public class EntityKeyExpressionGeneratorTest {
    private final String separator = " ";

    private static EntityKeyExpressionGenerator.KeyMemberInfo kmi(final String name, final TypeInfo type, final boolean optional) {
        return new EntityKeyExpressionGenerator.KeyMemberInfo(name, type, optional);
    }
    
    @Test
    public void tes_1sk_1() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(kmi("prop1", STRING, false)));
        ExpressionModel exp = expr().prop("prop1").model();
        assertEquals(exp, act);
    }

    @Test
    public void tes_1sk_2() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(kmi("prop1", ENTITY, false)));
        ExpressionModel exp = expr().prop("prop1.key").model();
        assertEquals(exp, act);
    }
    
    @Test
    public void tes_1sk_3() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(kmi("prop1", NON_STRING, false)));
        ExpressionModel exp = expr().concat().prop("prop1").with().val(EMPTY_STRING).end().model();
        assertEquals(exp, act);
    }
    
    @Test
    public void tes_2op_1() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(
                kmi("prop1", STRING, false), 
                kmi("prop2", STRING, true)));
        ExpressionModel exp = expr().
                concat().
                expr(expr().prop("prop1").model()).
                with().
                expr(expr().caseWhen().prop("prop2").isNotNull().then().concat().val(separator).with().expr(expr().prop("prop2").model()).end().otherwise().val(EMPTY_STRING).end().model()).end().model();
        assertEquals(exp, act);
    }

    @Test
    public void tes_2op_2() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(
                kmi("prop1", STRING, false), 
                kmi("prop2", NON_STRING, true)));
        ExpressionModel exp = expr().
                concat().
                expr(expr().prop("prop1").model()).
                with().
                expr(expr().caseWhen().prop("prop2").isNotNull().then().concat().val(separator).with().expr(expr().prop("prop2").model()).end().otherwise().val(EMPTY_STRING).end().model()).end().model();
        assertEquals(exp, act);
    }
    
    @Test
    public void tes_2op_3() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(
                kmi("prop1", STRING, false), 
                kmi("prop2", ENTITY, true)));
        ExpressionModel exp = expr().
                concat().
                expr(expr().prop("prop1").model()).
                with().
                expr(expr().caseWhen().prop("prop2").isNotNull().then().concat().val(separator).with().expr(expr().prop("prop2.key").model()).end().otherwise().val(EMPTY_STRING).end().model()).end().model();
        assertEquals(exp, act);
    }
    
    @Test
    public void tes_3_1op() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(
                kmi("prop1", STRING, false), 
                kmi("prop2", STRING, true), 
                kmi("prop3", STRING, false)));
        ExpressionModel exp = expr().
                concat().
                expr(expr().prop("prop1").model()).
                with().
                expr(expr().caseWhen().prop("prop2").isNotNull().then().concat().val(separator).with().expr(expr().prop("prop2").model()).end().otherwise().val(EMPTY_STRING).end().model()).
                with().
                val(separator).
                with().
                expr(expr().prop("prop3").model()).
                end().model();
        assertEquals(exp, act);
    }

    @Test
    public void tes_3_2op() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(
                kmi("prop1", STRING, false), 
                kmi("prop2", STRING, true), 
                kmi("prop3", STRING, true)));
        ExpressionModel exp = expr().
                concat().
                expr(expr().prop("prop1").model()).
                with().
                expr(expr().caseWhen().prop("prop2").isNotNull().then().concat().val(separator).with().expr(expr().prop("prop2").model()).end().otherwise().val(EMPTY_STRING).end().model()).
                with().
                expr(expr().caseWhen().prop("prop3").isNotNull().then().concat().val(separator).with().expr(expr().prop("prop3").model()).end().otherwise().val(EMPTY_STRING).end().model()).
                end().model();
        assertEquals(exp, act);
    }
    
    @Test
    public void tes1() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(
                kmi("prop1", STRING, false), 
                kmi("prop2", NON_STRING, false), 
                kmi("prop3", ENTITY, false)));
        ExpressionModel exp = expr().
                concat().
                expr(expr().prop("prop1").model()).
                with().
                val(separator).
                with().
                expr(expr().prop("prop2").model()).
                with().
                val(separator).
                with().
                expr(expr().prop("prop3.key").model()).
                end().
                model();
        assertEquals(exp, act);
    }
    
    @Test
    public void tes_3ns() {
        ExpressionModel act = getVirtualKeyPropForEntityWithCompositeKey(separator, listOf(
                kmi("prop1", NON_STRING, false), 
                kmi("prop2", NON_STRING, false), 
                kmi("prop3", NON_STRING, false)));
        ExpressionModel exp = expr().
                concat().
                expr(expr().prop("prop1").model()).
                with().
                val(separator).
                with().
                expr(expr().prop("prop2").model()).
                with().
                val(separator).
                with().
                expr(expr().prop("prop3").model()).
                end().
                model();
        assertEquals(exp, act);
    }
}
{'fraction_non_alphanumeric': 0.15399449035812673, 'fraction_numerical': 0.009641873278236915, 'mean_word_length': 2.3322625057365762, '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': '1470459', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1926, 'n_words': 305}