You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2019/11/19 09:51:01 UTC

[GitHub] [incubator-shardingsphere-doc] terrymanu commented on a change in pull request #355: Add: add document for rewrite test

terrymanu commented on a change in pull request #355: Add: add document for rewrite test
URL: https://github.com/apache/incubator-shardingsphere-doc/pull/355#discussion_r347824443
 
 

 ##########
 File path: document/current/content/features/test-engine/rewrite-test-engine.en.md
 ##########
 @@ -5,4 +5,65 @@ title = "SQL Rewrite Test Engine"
 weight = 4
 +++
 
-TODO
+## Target
+
+Facing logic databases and tables cannot be executed directly in actual databases. SQL rewrite is used to rewrite logic SQL into rightly executable ones in actual databases, including two parts, correctness rewrite and optimization rewrite. rewrite tests are for these targets.
+
+### Test
+
+The rewrite tests are in the test folder under `sharding-core/sharding-core-rewrite` . Followings are the main part for rewrite tests:
+
+  - test engine
+  - environment configuration
+  - assert data
+
+Test engine is the entrance of rewrite tests, just like other test engines, through Junit [Parameterized](https://github.com/junit-team/junit4/wiki/Parameterized-tests), read every and each data in the xml file under the target test type in `test\resources`, and then assert by the engine one by one
+
+Environment configuration is the yaml file under test type under `test\resources\yaml`. The configuration file contains dataSources,shardingRule,encryptRule and other info. The default database is H2, for example:
+
+```yaml
+dataSources:
+  db: !!com.zaxxer.hikari.HikariDataSource
+    ## The default database is H2, it's available to switch to other database by changing the Driver and config
+    driverClassName: org.h2.Driver
+    jdbcUrl: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
+    username: sa
+    password:
+
+## sharding Rules
+shardingRule:
+  tables:
+    t_account:
+      actualDataNodes: db.t_account_${0..1}
+      tableStrategy: 
+        inline:
+          shardingColumn: account_id
+          algorithmExpression: t_account_${account_id % 2}
+      keyGenerator:
+        type: TEST
+        column: account_id
+    t_account_detail:
+      actualDataNodes: db.t_account_detail_${0..1}
+      tableStrategy: 
+        inline:
+          shardingColumn: order_id
+          algorithmExpression: t_account_detail_${account_id % 2}
+  bindingTables:
+    - t_account, t_account_detail
+```
+
+Assert data are in the xml under test type in `test\resources`. In the xml file, `yaml-rule` means the environment configuration file path, `input` contains the target SQL and parameters, `output` contains the expected SQL and parameters. For example: 
+
+```xml
+<rewrite-assertions yaml-rule="yaml/sharding/sharding-rule.yaml">
+    <rewrite-assertion id="select_without_sharding_value_for_parameters">
+        <input sql="SELECT * FROM sharding_db.t_account WHERE amount = ?" parameters="1000" />
+        <output sql="SELECT * FROM t_account_0 WHERE amount = ?" parameters="1000" />
+        <output sql="SELECT * FROM t_account_1 WHERE amount = ?" parameters="1000" />
+    </rewrite-assertion>
+</rewrite-assertions>
+```
+
+After set up the assert data and environment configuration, rewrite test engine will assert the corresponding SQL without any Java code modification.
+
+
 
 Review comment:
   Please useless blank line

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services