Skip to content

Add sqlnode test #3258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* 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
*
* https://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 org.apache.ibatis.scripting.xmltags;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.domain.blog.Author;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* <pre>{@code
* SELECT *
* FROM BLOG
* WHERE state = 'active'
* <choose>
* <when test="title != null">
* AND title like #{title}
* </when>
* <when test="author != null && author.username != null">
* AND author_name like #{author.username}
* </when>
* <otherwise>
* AND featured = 1
* </otherwise>
* </choose>
* }</pre>
*
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#choose-when-otherwise">choose</a>
*/
class ChooseSqlNodeTest extends SqlNodeTest{

private static final String FIRST_TEXT = " AND title like #{title}";
private static final String SECOND_TEXT = " AND author_name like #{author.username}";
private static final String OTHERWISE_TEXT = " AND featured = 1";

private SqlNode sqlNode;

@BeforeEach
void setup() {
SqlNode first = new IfSqlNode(new StaticTextSqlNode(FIRST_TEXT), "title != null");
SqlNode second = new IfSqlNode(new StaticTextSqlNode(SECOND_TEXT), "author != null && author.username != null");
List<SqlNode> ifNodes = Arrays.asList(first, second);

SqlNode defaultNode = new StaticTextSqlNode(OTHERWISE_TEXT);

this.sqlNode = new ChooseSqlNode(ifNodes, defaultNode);
}

@Test
@Override
public void shouldApply() throws Exception {
when(context.getBindings()).thenReturn(new HashMap<>() {{
put("title", "abc");
put("author", new Author(1, "mybatis", "***", null, null, null));
}});

boolean result = sqlNode.apply(context);

assertTrue(result);
verify(context).appendSql(FIRST_TEXT);
}

@Test
public void shouldAppendSecond() throws Exception {
when(context.getBindings()).thenReturn(new HashMap<>() {{
put("author", new Author(1, "mybatis", "***", null, null, null));
}});

boolean result = sqlNode.apply(context);

assertTrue(result);
verify(context).appendSql(SECOND_TEXT);
}

@Test
public void shouldAppendOtherwise() throws Exception {
when(context.getBindings()).thenReturn(new HashMap<>());

boolean result = sqlNode.apply(context);

assertTrue(result);
verify(context).appendSql(OTHERWISE_TEXT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* 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
*
* https://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 org.apache.ibatis.scripting.xmltags;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* <pre>{@code
* SELECT *
* FROM POST
* <where>
* <foreach item="item" index="index" collection="list" open="ID in (" separator="," close=")" nullable="true">
* #{item}
* </foreach>
* </where>
* }</pre>
*
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#foreach">foreach</a>
*/
class ForEachSqlNodeTest extends SqlNodeTest{

private SqlNode sqlNode;

@BeforeEach
void setup() {
SqlNode contents = new StaticTextSqlNode("#{name}");
this.sqlNode = new ForEachSqlNode(configuration, contents, "list", "index", "item", "ID in (", ")", ",");
}

@Test
@Override
public void shouldApply() throws Exception {
ArgumentCaptor<String> bindKeyCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Object> bindValueCaptor = ArgumentCaptor.forClass(Object.class);
doNothing().when(context).bind(bindKeyCaptor.capture(), bindValueCaptor.capture());

when(context.getBindings()).thenReturn(new HashMap<>() {{
put("list", Arrays.asList("a", "b", "c"));
}});

boolean result = sqlNode.apply(context);

assertTrue(result);
verify(context).appendSql("ID in (");
verify(context).appendSql(")");

List<String> allKeyValues = bindKeyCaptor.getAllValues();
List<Object> allValValues = bindValueCaptor.getAllValues();
assertEquals(Arrays.asList("index", "__frch_index_0", "item", "__frch_item_0",
"index", "__frch_index_0", "item", "__frch_item_0",
"index", "__frch_index_0", "item", "__frch_item_0"), allKeyValues);
assertEquals(Arrays.asList(0, 0, "a", "a",
1, 1, "b", "b",
2, 2, "c", "c"), allValValues);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* 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
*
* https://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 org.apache.ibatis.scripting.xmltags;

import java.util.HashMap;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* <pre>{@code
* <if test="title != null>
* AND title like #{title}
* </if>
* }</pre>
*
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#if">if</a>
*/
class IfSqlNodeTest extends SqlNodeTest {

private static final String CONDITION = "title != null";
private static final String TEXT = "AND title like #{title}";

private SqlNode sqlNode;

@BeforeEach
void setup() {
SqlNode contents = new StaticTextSqlNode(TEXT);
this.sqlNode = new IfSqlNode(contents, CONDITION);
}

@Test
@Override
public void shouldApply() throws Exception {
when(context.getBindings()).thenReturn(new HashMap<>() {{
put("title", "ENGLISH");
}});

boolean result = sqlNode.apply(context);

assertTrue(result);
verify(context).appendSql(TEXT);
}

@Test
public void shouldAppendNone() {
when(context.getBindings()).thenReturn(new HashMap<>() {{
put("title", null);
}});

boolean result = sqlNode.apply(context);

assertFalse(result);
verify(context, never()).appendSql(TEXT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* 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
*
* https://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 org.apache.ibatis.scripting.xmltags;

import java.util.Arrays;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.*;

/**
* @author <a href="[email protected]">mawen12</a>
*/
class MixedSqlNodeTest extends SqlNodeTest{

private static final String FIRST_TEXT = "abc";
private static final String SECOND_TEXT = "bcd";
private SqlNode sqlNode;

@BeforeEach
void setup() {
SqlNode first = new StaticTextSqlNode(FIRST_TEXT);
SqlNode second = new StaticTextSqlNode(SECOND_TEXT);
this.sqlNode = new MixedSqlNode(Arrays.asList(first, second));
}

@Test
@Override
public void shouldApply() throws Exception {
sqlNode.apply(context);

verify(context).appendSql("abc");
verify(context).appendSql("bcd");
}
}
Loading