+ HR database test (gentle start)
authorEric Prud'hommeaux <eric@w3.org>
Sat, 27 Feb 2010 11:09:43 -0500
changeset 180 f87818a1cb27
parent 179 3ddae4e60614
child 181 4e52dade27c1
+ HR database test (gentle start)
src/test/scala/SparqlToSparqlToSqlTest.scala
--- a/src/test/scala/SparqlToSparqlToSqlTest.scala	Sat Feb 27 11:08:51 2010 -0500
+++ b/src/test/scala/SparqlToSparqlToSqlTest.scala	Sat Feb 27 11:09:43 2010 -0500
@@ -17,7 +17,58 @@
 class SparqlToSparqlToSqlTest extends FunSuite {
 
   val DDLParser = Sql()
-  val ddl = """
+
+  val dbDdl = """
+CREATE TABLE Employee (empid INT, PRIMARY KEY (empid), lastName STRING, birthday DATE, manager INT, FOREIGN KEY (manager) REFERENCES Employee(empid));
+CREATE TABLE Tasks (taskid INT, PRIMARY KEY (taskid), name STRING, lead INT, FOREIGN KEY (lead) REFERENCES Employee(empid));
+CREATE TABLE TaskAssignments (id INT PRIMARY KEY, PRIMARY KEY (id), task INT, FOREIGN KEY (task) REFERENCES Tasks(taskid), employee INT, FOREIGN KEY (employee) REFERENCES Employee(empid));
+"""
+  val HR:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
+
+  test("foaf:last_name FILTER") {
+    val sparqlParser = Sparql()
+    val foafQuery = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp  foaf:last_name    ?name
+FILTER (?name = "Smith"^^xsd:string)
+}
+""").get
+    val hr2foaf = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?who foaf:first_name ?fname .
+            ?who foaf:last_name  ?lname }
+    WHERE { ?who empP:firstName  ?fname .
+            ?who empP:lastName   ?lname }
+""").get
+    val asStem = SparqlToSparql(foafQuery, List(hr2foaf))
+    val stemQuery = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp  empP:lastName    ?name
+FILTER (?name = "Smith"^^xsd:string)
+}
+""").get
+    assert(stemQuery == asStem)
+    val sqlParser = Sql()
+    val sqlQuery = sqlParser.parseAll(sqlParser.select, """
+SELECT R_emp.empid AS emp
+  FROM Employee AS R_emp
+ WHERE R_emp.empid IS NOT NULL AND R_emp.lastName="Smith"
+""").get
+    val asSql = SparqlToSql(HR, asStem, StemURI("http://hr.example/DB/"), false, false)
+    assert(asSql === sqlQuery)
+    val output = """
+"""
+  }
+
+  /* ========== DiabeticPatient database tests ==========
+   *
+   */
+  val hosp1Ddl = """
 CREATE TABLE Person (ID INT PRIMARY KEY, MiddleName STRING, DateOfBirth DATE, SexDE INT, PRIMARY KEY (ID), FOREIGN KEY (SexDE) REFERENCES Sex_DE(ID));
 CREATE TABLE Sex_DE (ID INT PRIMARY KEY, EntryName STRING);
 CREATE TABLE Item_Medication (ID INT PRIMARY KEY, PRIMARY KEY (ID), PatientID INT, FOREIGN KEY (PatientID) REFERENCES Person(ID), PerformedDTTM DATE, EntryName STRING);
@@ -25,7 +76,7 @@
 CREATE TABLE Medication_DE (ID INT PRIMARY KEY, NDC INT);
 CREATE TABLE NDCcodes (ID INT PRIMARY KEY, NDC INT, ingredient INT);
 """
-  val hosp1:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, ddl).get
+  val hosp1:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, hosp1Ddl).get
 
   test("~/swobjects/tests/healthCare/lists-notBound/hl7.rq short") {
     val sparqlParser = Sparql()
@@ -154,8 +205,9 @@
   ?inclu_subs hl7:consumable ?inclu_consu .
   ?inclu_consu hl7:displayName ?inclu_name .
   ?inclu_consu hl7:activeIngredient ?inclu_intred .
-  ?inclu_intred hl7:classCode 6809 .
+  ?inclu_intred hl7:classCode ?incluCode .
   ?inclu_subs hl7:effectiveTime ?inclu_span .
+  FILTER (?incluCode = 6809)
   MINUS {
     ?patient hl7:substanceAdministration ?exclu_subs .
     ?exclu_subs a hl7:SubstanceAdministration .
@@ -231,7 +283,8 @@
     ?_0_0_indicMed Medication:MedDictDE ?_0_0_indicDE .
     ?_0_0_indicDE Medication_DE:NDC ?_0_0_indicNDC .
     ?_0_0_indicCode NDCcodes:NDC ?_0_0_indicNDC .
-    ?_0_0_indicCode NDCcodes:ingredient 6809
+    ?_0_0_indicCode NDCcodes:ingredient ?incluCode
+    FILTER (?incluCode = 6809)
 
     MINUS {
         ?_1_0_indicItem Item_Medication:PatientID ?patient .
@@ -286,163 +339,4 @@
 """
   }
 
-//   test("~/swobjects/tests/healthCare/lists-notBound/hl7.rq") {
-//     val sparqlParser = Sparql()
-//     val hl7Query = sparqlParser.parseAll(sparqlParser.select, """
-// PREFIX hl7: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#> 
-// SELECT ?patient ?dob ?sex ?takes ?indicDate 
-// WHERE
-// {
-//   {
-//     ?patient a hl7:Person .
-//     ?patient hl7:entityName ?middleName .
-//     ?patient hl7:livingSubjectBirthTime ?dob .
-//     ?patient hl7:administrativeGenderCodePrintName ?sex .
-//     ?patient hl7:substanceAdministration ?subs_admin .
-//     ?subs_admin a hl7:SubstanceAdministration .
-//     ?subs_admin hl7:consumable ?consumable .
-//     ?consumable hl7:displayName ?takes .
-//     ?consumable hl7:activeIngredient ?ingredient .
-//     ?ingredient hl7:classCode ?code .
-//     ?subs_admin hl7:effectiveTime ?indic_span .
-//     ?indic_span hl7:start ?indicDate .
-//     FILTER ( ?code = 6809 )
-//   }
-//   MINUS {
-//     ?b0x8ea18a8_gen4 a hl7:SubstanceAdministration .
-//     ?b0x8ea1a58_gen1 hl7:displayName ?takes_gen1 .
-//     ?b0x8ea1f30_gen2 hl7:classCode ?contCode .
-//     ?b0x8ea1a58_gen1 hl7:activeIngredient ?b0x8ea1f30_gen2 .
-//     ?b0x8ea18a8_gen4 hl7:consumable ?b0x8ea1a58_gen1 .
-//     ?b0x8ea17f0_gen3 hl7:start ?indicDate .
-//     ?b0x8ea18a8_gen4 hl7:effectiveTime ?b0x8ea17f0_gen3 .
-//     ?patient hl7:substanceAdministration ?b0x8ea18a8_gen4 .
-//     FILTER ( ?contCode = 11289 )
-//   }
-// }""").get
-
-//     val db2hl7 = sparqlParser.parseAll(sparqlParser.construct, """
-// PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
-// PREFIX Person: <http://hospital.example/DB/Person#>
-// PREFIX Sex_DE: <http://hospital.example/DB/Sex_DE#>
-// PREFIX Item_Medication: <http://hospital.example/DB/Item_Medication#>
-// PREFIX Medication: <http://hospital.example/DB/Medication#>
-// PREFIX Medication_DE: <http://hospital.example/DB/Medication_DE#>
-// PREFIX NDCcodes: <http://hospital.example/DB/NDCcodes#>
-
-// PREFIX hl7: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#>
-// PREFIX spl: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#>
-
-// CONSTRUCT {
-// ?person     a					  hl7:Person .
-// ?person     hl7:entityName			  ?middleName .
-// ?person     hl7:livingSubjectBirthTime		  ?dob .
-// ?person     hl7:administrativeGenderCodePrintName ?sex .
-// ?person     hl7:substanceAdministration		  ?subst .
-// ?subst	a	       hl7:SubstanceAdministration .
-// ?subst 	hl7:consumable ?cons .
-// ?cons	    hl7:displayName	 ?takes .
-// ?cons	    spl:activeIngredient ?ingred .
-// ?ingred		spl:classCode ?ingredCode .
-// ?subst	hl7:effectiveTime ?interval .
-// ?interval	    hl7:start ?indicDate 
-// } WHERE {
-//   ?person     Person:MiddleName		     ?middleName .
-//   ?person     Person:DateOfBirth	     ?dob .
-//   ?person     Person:SexDE		     ?sexEntry   .
-
-//               ?sexEntry   Sex_DE:EntryName   ?sex .
-
-//   ?indicItem  Item_Medication:PatientID	     ?person .
-//   ?indicItem  Item_Medication:PerformedDTTM  ?indicDate .
-//   ?indicItem  Item_Medication:EntryName	     ?takes .
-//   ?indicMed   Medication:ItemID		     ?indicItem .
-//   ?indicMed   Medication:DaysToTake	     ?indicDuration .
-//   ?indicMed   Medication:MedDictDE	     ?indicDE .
-//   ?indicDE    Medication_DE:NDC		     ?indicNDC .
-//   ?indicCode  NDCcodes:NDC         ?indicNDC .
-//   ?indicCode  NDCcodes:ingredient  ?ingredCode }
-// """).get
-//     val stemQuery = sparqlParser.parseAll(sparqlParser.select, """
-// PREFIX Person: <http://hospital.example/DB/Person#>
-// PREFIX Sex_DE: <http://hospital.example/DB/Sex_DE#>
-// PREFIX Item_Medication: <http://hospital.example/DB/Item_Medication#>
-// PREFIX Medication: <http://hospital.example/DB/Medication#>
-// PREFIX Medication_DE: <http://hospital.example/DB/Medication_DE#>
-// PREFIX NDCcodes: <http://hospital.example/DB/NDCcodes#>
-// PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-
-// SELECT ?patient
-//  WHERE {
-//     ?patient Person:MiddleName ?middleName .
-//     ?patient Person:DateOfBirth ?dob .
-//     ?patient Person:SexDE ?_0_sexEntry .
-//     ?_0_sexEntry Sex_DE:EntryName ?sex .
-
-//     ?_0_indicItem Item_Medication:PatientID ?patient .
-//     ?_0_indicItem Item_Medication:PerformedDTTM ?indicDate .
-//     ?_0_indicItem Item_Medication:EntryName ?takes .
-//     ?_0_indicMed Medication:ItemID ?_0_indicItem .
-//     ?_0_indicMed Medication:MedDictDE ?_0_indicDE .
-//     ?_0_indicDE Medication_DE:NDC ?indicNDC .
-//     ?_0_indicCode NDCcodes:NDC ?indicNDC .
-//     ?_0_indicCode NDCcodes:ingredient 6809
-
-//     MINUS {
-//         ?disqualItem Item_Medication:PatientID ?patient .
-//         ?disqualItem Item_Medication:PerformedDTTM ?disqualDate .
-//         ?disqualItem Item_Medication:EntryName ?takes .
-//         ?disqualMed Medication:ItemID ?disqualItem .
-//         ?disqualMed Medication:MedDictDE ?disqualDE .
-//         ?disqualDE Medication_DE:NDC ?disqualNDC .
-//         ?disqualCode NDCcodes:NDC ?disqualNDC .
-//         ?disqualCode NDCcodes:ingredient 11289
-//     }
-//       }
-// """).get
-//     val sqlParser = Sql()
-//     val sqlQuery = sqlParser.parseAll(sqlParser.select, """
-// SELECT R_patient.ID AS patient
-//   FROM Person AS R_patient
-//        INNER JOIN Sex_DE AS R_sexEntry ON R_sexEntry.ID=R_patient.SexDE
-//        INNER JOIN Item_Medication AS R_indicItem ON R_indicItem.PatientID=R_patient.ID
-//        INNER JOIN Medication AS R__0_indicMed ON R__0_indicMed.ItemID=R_indicItem.ID
-//        INNER JOIN Medication_DE AS R__0_indicDE ON R__0_indicDE.ID=R__0_indicMed.MedDictDE
-//        INNER JOIN NDCcodes AS R__0_indicCode ON R__0_indicCode.NDC=R__0_indicDE.NDC
-//        LEFT OUTER JOIN (
-//    SELECT R_disqualCode.ID AS disqualCode, R_disqualMed.MedDictDE AS disqualDE,
-//           R_disqualItem.PerformedDTTM AS disqualDate, R_disqualItem.ID AS disqualItem,
-//           R_disqualMed.ID AS disqualMed, R_disqualDE.NDC AS disqualNDC,
-//           R_disqualItem.PatientID AS patient, R_disqualItem.EntryName AS takes, 6 AS _DISJOINT_
-//      FROM Item_Medication AS R_disqualItem
-//           INNER JOIN Medication AS R_disqualMed ON R_disqualMed.ItemID=R_disqualItem.ID
-//           INNER JOIN Medication_DE AS R_disqualDE ON R_disqualDE.ID=R_disqualMed.MedDictDE
-//           INNER JOIN NDCcodes AS R_disqualCode ON R_disqualCode.NDC=R_disqualDE.NDC
-//     WHERE R_disqualCode.ingredient=11289
-//       AND R_disqualItem.EntryName IS NOT NULL
-//       AND R_disqualItem.PatientID IS NOT NULL
-//       AND R_disqualItem.PerformedDTTM IS NOT NULL
-//               ) AS G_opt6 ON G_opt6.patient=R_patient.ID
-//                          AND G_opt6.takes=R_indicItem.EntryName
-//  WHERE G_opt6._DISJOINT_ IS NULL
-//    AND R__0_indicCode.ingredient=6809
-//    AND R_indicItem.EntryName IS NOT NULL
-//    AND R_indicItem.PerformedDTTM IS NOT NULL
-//    AND R_patient.DateOfBirth IS NOT NULL
-//    AND R_patient.MiddleName IS NOT NULL
-//    AND R_sexEntry.EntryName IS NOT NULL
-// """).get
-//     val asStem = SparqlToSparql(hl7Query, List(db2hl7))
-//     if (!(asStem == stemQuery)) {
-//       println(asStem.toString())
-//       println("---")
-//       println(stemQuery.toString())
-//     }
-//     assert(asStem == stemQuery)
-//     val asSql = SparqlToSql(hosp1, asStem, StemURI("http://hospital.example/DB/"), false, false)
-//     assert(asSql === sqlQuery)
-//     val output = """
-// """
-//   }
-
 }