add test for searchCache and searchWordListCache
authorJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Thu, 19 Aug 2010 08:53:24 +0000 (10:53 +0200)
committerJakub Jaszczynski <j.j.jaszczynski@gmail.com>
Thu, 19 Aug 2010 08:53:24 +0000 (10:53 +0200)
data/dicts/eng-thai.xdxf
data/dicts/eng-us.xdxf
trunk/src/base/gui/TranslationWidget.cpp
trunk/src/base/xsl.xsl
trunk/src/plugins/xdxf/src/xdxfplugin.cpp
trunk/src/plugins/xdxf/tests/test.cpp
trunk/src/plugins/xdxf/tests/test.h

index d519da3..353129f 100644 (file)
@@ -19,6 +19,8 @@ sib</ar>
 nueng-muen</ar>
 <ar><k>100</k>
 nueng-roy</ar>
+<ar><k>Aluminum</k>
+Aluminium to kolejne tlumaczenie z innego slownika</ar>
 <ar><k>100,000</k>
 nueng-saen</ar>
 <ar><k>101</k>
index dc51445..c45be96 100644 (file)
@@ -301,7 +301,7 @@ Courgette</ar>
 Tartlane</ar>
 <ar><k>airplane</k>
 Aeroplane</ar>
-<ar><k>Aluminum</k>
+<ar><k>aluminum</k>
 Aluminium to ala ma kota a kot ma ale Aluminium to ala ma kota a kot ma alAluminium to ala ma kota a kot ma alAluminium to ala ma kota a kot ma al</ar>
 <ar><k>amrtment</k>
 Medium Wave</ar>
index f89c5b2..f8d8e27 100644 (file)
@@ -55,7 +55,7 @@ void TranslationWidget::show(QStringList translations) {
         trans += t + "\n";
     }
 
-//    qDebug()<<trans;
+ //   qDebug()<<trans;
     trans=tr("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>") + tr("\n <ar>") + trans + tr("\n </ar>");
 
     trans=XslConversion(trans);
index 5cd6a73..d937301 100644 (file)
@@ -5,11 +5,11 @@
        <html>
        <body>
        <table border="1" WIDTH="100%">
-       <tr bgcolor="#FF0000">
+       <tr bgcolor="#99CCFF">
                <th><xsl:value-of select="ar/dict[1]/key"/></th>
        </tr>
        <xsl:for-each select="ar/dict"> 
-               <tr bgcolor="#9acd32">
+               <tr bgcolor="#99FF99">
                        <th><xsl:value-of select="info"/></th>
                </tr>
                <xsl:for-each select="t">
index 7f1e985..663fafb 100644 (file)
@@ -87,29 +87,37 @@ QList<Translation*> XdxfPlugin::searchWordListCache(QString word, int limit) {
         }
 
         stopped = false;
-        if(word.indexOf("*")==-1 && word.indexOf("?")== 0)
-            word+="%";
         word = word.toLower();
         word = word.replace("*", "%");
         word = word.replace("?", "_");
         word = removeAccents(word);
-        qDebug() << word;
+        //qDebug() << word;
 
         QSqlQuery cur(db);
-        cur.prepare("select word from dict where word like ? limit ?");
+        if(limit !=0)
+            cur.prepare("select word from dict where word like ? limit ?");
+        else
+            cur.prepare("select word from dict where word like ?");
         cur.addBindValue(word);
-        cur.addBindValue(limit);
+        if(limit !=0)
+            cur.addBindValue(limit);
         cur.exec();
-        while(cur.next())
-            translations.insert(new TranslationXdxf(
+        while(cur.next()){
+            bool ok=true;
+            Translation *tran;
+            foreach(tran,translations) {
+                if(tran->key().toLower()==cur.value(0).toString().toLower())
+                        ok=false;
+            }
+            if(ok)  /*add key word to list*/
+                translations.insert(new TranslationXdxf(
                         cur.value(0).toString().toLower(),
                         _infoNote, this));
+        }
         db.close();
        return translations.toList();
 }
 
-
-
 QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
     QSet<Translation*> translations;
     QFile dictionaryFile(path);
@@ -141,7 +149,7 @@ QList<Translation*> XdxfPlugin::searchWordListFile(QString word, int limit) {
                 bool ok=true;
                 Translation *tran;
                 foreach(tran,translations) {
-                    if(tran->key()==a)
+                    if(tran->key().toLower()==a.toLower())
                         ok=false;  /*if key word is in the dictionary more that one */
                 }
                 if(ok)  /*add key word to list*/
@@ -166,10 +174,8 @@ QString XdxfPlugin::search(QString key) {
     return searchFile(key);
 }
 
-
-
 QString XdxfPlugin::searchCache(QString key) {
-    QString result;
+    QString result("");
     QString cacheFilePath = _settings->value("cache_path");
     db.setDatabaseName(cacheFilePath);
     key = key.toLower();
@@ -180,19 +186,19 @@ QString XdxfPlugin::searchCache(QString key) {
     }
 
     QSqlQuery cur(db);
-    cur.prepare("select translation from dict where word like ? limit 1");
+//    cur.prepare("select translation from dict where word like ? limit 1");
+    cur.prepare("select translation from dict where word like ?");
     cur.addBindValue(key);
     cur.exec();
-    if(cur.next())
-        result = cur.value(0).toString();
+//  if(cur.next())
+    while(cur.next())
+//      result = cur.value(0).toString();
+        result += cur.value(0).toString();
     db.close();
     return result;
 
 }
 
-
-
-
 QString XdxfPlugin::searchFile(QString key) {
     key = key.toLower();
     QFile dictionaryFile(path);
@@ -213,7 +219,7 @@ QString XdxfPlugin::searchFile(QString key) {
         if(reader.tokenType() == QXmlStreamReader::StartElement) {
             if(reader.name()=="k") {
                 a = reader.readElementText();
-                if(a==key)
+                if(a.toLower()==key.toLower())
                     match = true;
             }
         }
@@ -258,7 +264,6 @@ void XdxfPlugin::setPath(QString path){
     //getDictionaryInfo();
 }
 
-
 CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
     XdxfPlugin *plugin = new XdxfPlugin();
     if(settings){
@@ -271,8 +276,8 @@ CommonDictInterface* XdxfPlugin::getNew(const Settings *settings) const {
 
         plugin->db_name = plugin->_settings->value("type")
                + plugin->_settings->value("path");
-        if(!plugin->db.connectionName().isEmpty())
-        plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
+  //      if(!plugin->db.connectionName().isEmpty() || settings->value("generateCache")=="true")
+            plugin->db = QSqlDatabase::addDatabase("QSQLITE", plugin->db_name);
 
         if(settings->value("cached").isEmpty() &&
            settings->value("generateCache") == "true") {
@@ -325,7 +330,6 @@ void XdxfPlugin::setSettings(Settings *settings) {
     emit settingsChanged();
 }
 
-
 void XdxfPlugin::getDictionaryInfo() {
     QFile dictionaryFile(path);
     if(!dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
@@ -401,8 +405,6 @@ int XdxfPlugin::countWords() {
     return wordsCount;
 }
 
-
-
 bool XdxfPlugin::makeCache(QString dir) {
     cachingDialog->setVisible(true);
     QCoreApplication::processEvents();
@@ -472,7 +474,7 @@ bool XdxfPlugin::makeCache(QString dir) {
                 temp+= reader.text().toString();
                 reader.readNext();
             }
-            temp += tr("<t>") + temp.replace("\n","") + tr("</t>");
+            temp=tr("<t>") + temp.replace("\n","") + tr("</t>");
             match=false;
             cur.prepare("insert into dict values(?,?)");
             cur.addBindValue(a);
@@ -506,5 +508,4 @@ bool XdxfPlugin::makeCache(QString dir) {
     return true;
 }
 
-
 Q_EXPORT_PLUGIN2(xdxf, XdxfPlugin)
index 93b5c1c..69c8f5b 100644 (file)
 #include <QSignalSpy>
 
 void XdxfTest::getNew() {
+
     XdxfPlugin xdxfPlugin(this);
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
-
     CommonDictInterface *xdxfPlugin2 = xdxfPlugin.getNew(settings);
+
     QList<Translation*> te6=xdxfPlugin2->searchWordList("*",8);
-        QCOMPARE(te6.size(),8);
+    QCOMPARE(te6.size(),8);
 }
 
-void XdxfTest::search() {
-    /*test for English-Polish dictionary */
+void XdxfTest::searchFile() {
+
     XdxfPlugin xdxfPluginB(this);
+    Settings *settings=new Settings;
+    settings->setValue("path","../tests/dict.xdxf");
+    CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
 
+    QCOMPARE(xdxfPlugin->search("."), QString("<t>kropka</t>"));
+    QCOMPARE(xdxfPlugin->search("1"), QString("<t>one</t>"));
+    QCOMPARE(xdxfPlugin->search("test"), QString("<t><c c=\"FF00FF\">kro</c>test01<pos>krowa</pos></t>"));
+}
 
+void XdxfTest::makeCache()
+{
+    XdxfPlugin xdxfPluginB(this);
+    Settings *settings=new Settings;
+    settings->setValue("path","../tests/dict.xdxf");
+    settings->setValue("generateCache", "true");
+    CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
+    QCOMPARE(xdxfPlugin->settings()->value("cached"),tr("true"));
+}
 
+void XdxfTest::searchCache() {
+    XdxfPlugin xdxfPluginB(this);
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
+    settings->setValue("generateCache", "true");
     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
 
-    xdxfPlugin->search("wino");
+    QCOMPARE(xdxfPlugin->settings()->value("cached"),tr("true"));
     QCOMPARE(xdxfPlugin->search("."), QString("<t>kropka</t>"));
     QCOMPARE(xdxfPlugin->search("1"), QString("<t>one</t>"));
-
     QCOMPARE(xdxfPlugin->search("test"), QString("<t><c c=\"FF00FF\">kro</c>test01<pos>krowa</pos></t>"));
 }
 
-void XdxfTest::searchWordList() {
-     /*test for English-Polish dictionary */
+void XdxfTest::searchWordListCache(){
     XdxfPlugin xdxfPluginB(this);
+    Settings *settings=new Settings;
+    settings->setValue("path","../tests/dict.xdxf");
+    settings->setValue("generateCache", "true");
+    CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
+
+    QList<Translation*> te = xdxfPlugin->searchWordList(".", 10);
+    if(te.size()>0)
+        QCOMPARE(te.size(), 9);
+    QList<Translation*> te2 = xdxfPlugin->searchWordList("1",10);
+    QCOMPARE(te2.size(), 5);
+
+    QList<Translation*> te3 = xdxfPlugin->searchWordList("1", 2);
+        QCOMPARE(te3.size(), 2);
 
+    QList<Translation*> te4 = xdxfPlugin->searchWordList("ho*SE", 10);
+        QCOMPARE(te4.at(0)->key(), QString("house"));
+
+    QList<Translation*> te5 = xdxfPlugin->searchWordList("*");
+        QCOMPARE(te5.size(), 9);
+
+    QList<Translation*> te6 = xdxfPlugin->searchWordList("*", 8);
+        QCOMPARE(te6.size(), 8);
+
+    QList<Translation*> te7 = xdxfPlugin->searchWordList("*ou*");
+        QCOMPARE(te7.at(0)->key(), QString("house"));
+
+    QList<Translation*> te8 = xdxfPlugin->searchWordList("1?");
+        QCOMPARE(te8.at(0)->key(), QString("10"));
+
+    QList<Translation*> te9 = xdxfPlugin->searchWordList("1???");
+        QCOMPARE(te9.at(0)->key(), QString("1000"));
+
+    QList<Translation*> te10 = xdxfPlugin->searchWordList("1????*");
+        QCOMPARE(te10.at(0)->key(), QString("1 000 000"));
+
+    QList<Translation*> te11 = xdxfPlugin->searchWordList("h**?*?**e");
+        QCOMPARE(te11.at(0)->key(), QString("house"));
+}
+
+void XdxfTest::searchWordListFile() {
+
+    XdxfPlugin xdxfPluginB(this);
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
+
     QList<Translation*> te = xdxfPlugin->searchWordList(".", 10);
     if(te.size()>0)
         QCOMPARE(te.size(), 9);
@@ -92,9 +152,9 @@ void XdxfTest::searchWordList() {
 }
 
 void XdxfTest::stop() {
-    /*test for English-Polish dictionary */
+
     XdxfPlugin xdxfPluginB(this);
-//    xdxfPlugin.setPatch("dict.xdxf");
+//  xdxfPlugin.setPatch("dict.xdxf");
 
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
@@ -103,20 +163,17 @@ void XdxfTest::stop() {
     QString string("*");
     QFuture<QList<Translation*> > future = QtConcurrent::run(xdxfPlugin, &CommonDictInterface::searchWordList, string, 10);
     QList<Translation*> te5 = future.result();
-
     QCOMPARE(te5.size(), 9);
 }
 
 
 void XdxfTest::langFrom() {
-     /*test for English-Polish dictionary */
-    XdxfPlugin xdxfPluginB(this);
-//    xdxfPlugin.setPatch("dict.xdxf");
 
+    XdxfPlugin xdxfPluginB(this);
     Settings *settings=new Settings;
     settings->setValue("path","../tests/dict.xdxf");
     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
-    xdxfPlugin->searchWordList("rope", 10);
+
     QCOMPARE(xdxfPlugin->langFrom(), QString("ENG"));
 }
 
index 084c5d8..100b171 100644 (file)
 
 
  private slots:
-
-     void search();
-     void searchWordList();
      void langFrom();
      void getNew();
      void stop();
+     void searchFile();
+     void searchWordListFile();
+     void makeCache();
+     void searchCache();
+     void searchWordListCache();
  };