Skip to content

Commit

Permalink
Merge 949d62d into d853cd7
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Feb 3, 2020
2 parents d853cd7 + 949d62d commit 00c7c28
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.sql.parser.integrate.asserts.segment.schema;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.integrate.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.SQLSegmentAssert;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.segment.impl.schema.ExpectedSchema;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.SchemaSegment;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* Schema assert.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SchemaAssert {

/**
* Assert actual schema segment is correct with expected schema.
*
* @param assertContext assert context
* @param actual actual schema segment
* @param expected expected schema
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final SchemaSegment actual, final ExpectedSchema expected) {
assertThat(assertContext.getText("Owner name assertion error: "), actual.getName(), is(expected.getName()));
assertThat(assertContext.getText("Owner start delimiter assertion error: "), actual.getQuoteCharacter().getStartDelimiter(), is(expected.getStartDelimiter()));
assertThat(assertContext.getText("Owner end delimiter assertion error: "), actual.getQuoteCharacter().getEndDelimiter(), is(expected.getEndDelimiter()));
SQLSegmentAssert.assertIs(assertContext, actual, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public final class TableAssert {

/**
* Assert actual table segment is correct with expected table.
* Assert actual table segments is correct with expected tables.
*
* @param assertContext assert context
* @param actual actual tables
Expand All @@ -52,12 +52,19 @@ public static void assertIs(final SQLCaseAssertContext assertContext, final Coll
assertThat(assertContext.getText("Tables size assertion error: "), actual.size(), is(expected.size()));
int count = 0;
for (TableSegment each : actual) {
assertTable(assertContext, each, expected.get(count));
assertIs(assertContext, each, expected.get(count));
count++;
}
}

private static void assertTable(final SQLCaseAssertContext assertContext, final TableSegment actual, final ExpectedTable expected) {
/**
* Assert actual table segment is correct with expected table.
*
* @param assertContext assert context
* @param actual actual table
* @param expected expected table
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final TableSegment actual, final ExpectedTable expected) {
assertThat(assertContext.getText("Table name assertion error: "), actual.getTableName(), is(expected.getName()));
assertThat(assertContext.getText("Table alias assertion error: "), actual.getAlias().orNull(), is(expected.getAlias()));
if (null != expected.getOwner()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.ShowColumnsStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.ShowCreateTableStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.ShowDatabasesStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.ShowIndexStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.ShowTableStatusStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.ShowTablesStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.UseStatementAssert;
Expand All @@ -32,6 +33,7 @@
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowColumnsStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowCreateTableStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowDatabasesStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowIndexStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowTableStatusStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowTablesStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.UseStatementTestCase;
Expand All @@ -40,6 +42,7 @@
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowColumnsStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowCreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowDatabasesStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowIndexStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowTableStatusStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowTablesStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.UseStatement;
Expand Down Expand Up @@ -74,6 +77,8 @@ public static void assertIs(final SQLCaseAssertContext assertContext, final DALS
ShowCreateTableStatementAssert.assertIs(assertContext, (ShowCreateTableStatement) actual, (ShowCreateTableStatementTestCase) expected);
} else if (actual instanceof ShowTableStatusStatement) {
ShowTableStatusStatementAssert.assertIs(assertContext, (ShowTableStatusStatement) actual, (ShowTableStatusStatementTestCase) expected);
} else if (actual instanceof ShowIndexStatement) {
ShowIndexStatementAssert.assertIs(assertContext, (ShowIndexStatement) actual, (ShowIndexStatementTestCase) expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.integrate.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.schema.SchemaAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.segment.table.TableAssert;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowIndexStatementTestCase;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.SchemaSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowIndexStatement;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Show index statement assert.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShowIndexStatementAssert {

/**
* Assert show index statement is correct with expected parser result.
*
* @param assertContext assert context
* @param actual actual show index statement
* @param expected expected show index statement test case
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final ShowIndexStatement actual, final ShowIndexStatementTestCase expected) {
assertTable(assertContext, actual, expected);
assertSchema(assertContext, actual, expected);
}

private static void assertTable(final SQLCaseAssertContext assertContext, final ShowIndexStatement actual, final ShowIndexStatementTestCase expected) {
if (null != expected.getTable()) {
assertTrue(assertContext.getText("Actual table segment should exist."), actual.findSQLSegment(TableSegment.class).isPresent());
TableAssert.assertIs(assertContext, actual.findSQLSegment(TableSegment.class).get(), expected.getTable());
} else {
assertFalse(assertContext.getText("Actual table segment should not exist."), actual.findSQLSegment(TableSegment.class).isPresent());
}
}

private static void assertSchema(final SQLCaseAssertContext assertContext, final ShowIndexStatement actual, final ShowIndexStatementTestCase expected) {
if (null != expected.getSchema()) {
assertTrue(assertContext.getText("Actual schema segment should exist."), actual.findSQLSegment(SchemaSegment.class).isPresent());
SchemaAssert.assertIs(assertContext, actual.findSQLSegment(SchemaSegment.class).get(), expected.getSchema());
} else {
assertFalse(assertContext.getText("Actual schema segment should not exist."), actual.findSQLSegment(SchemaSegment.class).isPresent());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.DescribeStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowCreateTableStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowDatabasesStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowIndexStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowTableStatusStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.ShowTablesStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal.UseStatementTestCase;
Expand Down Expand Up @@ -124,6 +125,9 @@ public final class SQLParserTestCases {
@XmlElement(name = "show-table-status")
private final List<ShowTableStatusStatementTestCase> showTableStatusTestCases = new LinkedList<>();

@XmlElement(name = "show-index")
private final List<ShowIndexStatementTestCase> showIndexTestCases = new LinkedList<>();

@XmlElement(name = "common")
private final List<CommonStatementTestCase> commonTestCases = new LinkedList<>();

Expand Down Expand Up @@ -156,6 +160,7 @@ public Map<String, SQLParserTestCase> getAllSQLParserTestCases() {
result.putAll(getSQLParserTestCases(showColumnsTestCases));
result.putAll(getSQLParserTestCases(showCreateTableTestCases));
result.putAll(getSQLParserTestCases(showTableStatusTestCases));
result.putAll(getSQLParserTestCases(showIndexTestCases));
result.putAll(getSQLParserTestCases(commonTestCases));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.dal;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.segment.impl.schema.ExpectedSchema;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.segment.impl.table.ExpectedTable;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.statement.SQLParserTestCase;

import javax.xml.bind.annotation.XmlElement;

/**
* Show index statement test case.
*
* @author zhangliang
*/
@Getter
@Setter
public final class ShowIndexStatementTestCase extends SQLParserTestCase {

@XmlElement
private ExpectedTable table;

@XmlElement
private ExpectedSchema schema;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,40 @@
-->

<sql-parser-test-cases>
<common sql-case-id="show_index_with_index_with_table">
<tables>
<table name="t_order" start-index="16" stop-index="22" />
</tables>
</common>
<show-index sql-case-id="show_index_with_index_with_table">
<table name="t_order" start-index="16" stop-index="22" />
</show-index>

<common sql-case-id="show_index_with_indexes_with_table_and_database">
<tables>
<table name="t_order" start-index="18" stop-index="24" />
</tables>
<schemas>
<schema name="sharding_db" />
</schemas>
</common>
<show-index sql-case-id="show_index_with_indexes_with_table_and_database">
<table name="t_order" start-index="18" stop-index="24" />
<!-- FIXME assert schema -->
<!--<schema name="sharding_db" />-->
</show-index>

<common sql-case-id="show_index_with_keys_with_database_and_table">
<tables>
<table name="t_order" start-index="15" stop-index="33">
<owner name="sharding_db" start-index="15" stop-index="25" />
</table>
</tables>
</common>
<show-index sql-case-id="show_index_with_keys_with_database_and_table">
<table name="t_order" start-index="15" stop-index="33">
<owner name="sharding_db" start-index="15" stop-index="25" />
</table>
</show-index>

<common sql-case-id="show_index_with_table_back_quotes">
<tables>
<table name="t_order" start-delimiter="`" end-delimiter="`" start-index="18" stop-index="26" />
</tables>
<schemas>
<schema name="sharding_db" />
</schemas>
</common>
<show-index sql-case-id="show_index_with_table_back_quotes">
<table name="t_order" start-delimiter="`" end-delimiter="`" start-index="18" stop-index="26" />
<!-- FIXME assert schema -->
<!--<schema name="sharding_db" />-->
</show-index>

<common sql-case-id="show_index_with_database_back_quotes">
<tables>
<table name="t_order" start-index="18" stop-index="24" />
</tables>
<schemas>
<schema name="sharding_db" />
</schemas>
</common>
<show-index sql-case-id="show_index_with_database_back_quotes">
<table name="t_order" start-index="18" stop-index="24" />
<!-- FIXME assert schema -->
<!--<schema name="sharding_db" />-->
</show-index>

<show-index sql-case-id="show_index_with_back_quotes">
<table name="t_order" start-delimiter="`" end-delimiter="`" start-index="15" stop-index="37">
<owner name="sharding_db" start-delimiter="`" end-delimiter="`" start-index="15" stop-index="27" />
</table>
</show-index>

<common sql-case-id="show_index_with_back_quotes">
<tables>
<table name="t_order" start-delimiter="`" end-delimiter="`" start-index="15" stop-index="37">
<owner name="sharding_db" start-delimiter="`" end-delimiter="`" start-index="15" stop-index="27" />
</table>
</tables>
</common>

<show-table-status sql-case-id="show_table_status" />
<common sql-case-id="show_all" />
<common sql-case-id="show_server_version" />
Expand Down

0 comments on commit 00c7c28

Please sign in to comment.