diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..fe1b247 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,35 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python + +name: CI + +on: [push, pull_request] + +jobs: + build: + + runs-on: ubuntu-22.04 + + steps: + - uses: actions/checkout@v3 + - name: Install dependencies + run: | + export DEBIAN_FRONTEND=noninteractive + sudo apt-get clean && sudo apt-get update + python --version + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install -r requirements_indexer.txt + pip install nose2 black flake8 + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: '18' + - name: Test + run: | + nose2 + - name: black + run: | + black --check . + - name: flake8 + run: | + flake8 . --ignore E501,W503,E203,E722,E402 diff --git a/tests/__pycache__/testglossarysql.cpython-311.pyc b/tests/__pycache__/testglossarysql.cpython-311.pyc new file mode 100644 index 0000000..049c5f5 Binary files /dev/null and b/tests/__pycache__/testglossarysql.cpython-311.pyc differ diff --git a/tests/__pycache__/testindexcreator.cpython-311.pyc b/tests/__pycache__/testindexcreator.cpython-311.pyc new file mode 100644 index 0000000..49158af Binary files /dev/null and b/tests/__pycache__/testindexcreator.cpython-311.pyc differ diff --git a/tests/data/index_data.json b/tests/data/index_data.json new file mode 100644 index 0000000..c06d60b --- /dev/null +++ b/tests/data/index_data.json @@ -0,0 +1,20 @@ +[ + { + "s": " - Reserved. \n You cannot use this name. Choose Another \n", + "t": " - est\u00e0 reservat. \n No podeu utilitzar aquest nom. Escolliu-ne un altre \n", + "c": "Source: /abiword-ca.po from project 'Abiword'\r\nDLG_Styles_ErrNotTitle2", + "x": "context", + "pi": "abiword", + "pn": "Abiword", + "sc": true + }, + { + "s": "% of normal size", + "t": "% de la mida normal", + "c": "Source: /abiword-ca.po from project 'Abiword'\r\nDLG_PageSetup_Percent", + "x": null, + "pi": "abiword", + "pn": "Abiword", + "sc": true + } +] diff --git a/tests/models-tests/__pycache__/testpagination.cpython-311.pyc b/tests/models-tests/__pycache__/testpagination.cpython-311.pyc new file mode 100644 index 0000000..c332fed Binary files /dev/null and b/tests/models-tests/__pycache__/testpagination.cpython-311.pyc differ diff --git a/tests/models-tests/__pycache__/testsearch.cpython-311.pyc b/tests/models-tests/__pycache__/testsearch.cpython-311.pyc new file mode 100644 index 0000000..8d2c85f Binary files /dev/null and b/tests/models-tests/__pycache__/testsearch.cpython-311.pyc differ diff --git a/tests/models-tests/testpagination.py b/tests/models-tests/testpagination.py new file mode 100644 index 0000000..7bdd2e0 --- /dev/null +++ b/tests/models-tests/testpagination.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2015 Jordi Mas i Hernandez +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import unittest + +from models.pagination import Pagination + + +class TestPagination(unittest.TestCase): + def test_page(self): + pagination = Pagination( + 50, 100, "http://www.softcatala.org/web_search.py?page=2" + ) + self.assertEqual(pagination.page, 2) + + def test_url(self): + pagination = Pagination( + 50, 100, "http://www.softcatala.org/web_search.py?page=2&project=gnome" + ) + self.assertEqual( + pagination.url, "http://www.softcatala.org/web_search.py?project=gnome" + ) + + def test_pages_one(self): + pagination = Pagination(50, 10, "") + self.assertEqual(pagination.pages, 1) + + def test_pages_three(self): + pagination = Pagination(50, 101, "") + self.assertEqual(pagination.pages, 3) + + def test_has_prev_true(self): + pagination = Pagination( + 50, 100, "http://www.softcatala.org/web_search.py?page=2" + ) + self.assertTrue(pagination.has_prev) + + def test_has_prev_false(self): + pagination = Pagination(50, 1, "") + self.assertFalse(pagination.has_prev) + + def test_has_next_true(self): + pagination = Pagination(50, 100, "") + self.assertTrue(pagination.has_next) + + def test_has_next_false(self): + pagination = Pagination(50, 1, "") + self.assertFalse(pagination.has_next) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/models-tests/testsearch.py b/tests/models-tests/testsearch.py new file mode 100644 index 0000000..cce5b2e --- /dev/null +++ b/tests/models-tests/testsearch.py @@ -0,0 +1,200 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2015 Jordi Mas i Hernandez +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import unittest +import json +import sys + +sys.path.append("web/") + +from indexcreator import IndexCreator +from models.search import Search + + +class TestSearch(unittest.TestCase): + PROJECT_GNOME = "gnome" + PROJECT_ABI = "abiword" + PROJECT_MICROSOFT = "Microsoft Terminology" + SC = True + NO_SC = False + + data_set = [ + "Nox Documents Found", + "No s'han trobat", + PROJECT_GNOME, + SC, + "No Documents Found Today", + "No s'han trobat documents avui", + PROJECT_GNOME, + SC, + "No Documents Found late Yesterday", + "No s'han trobat documents ahir", + PROJECT_GNOME, + SC, + "Many documents found Yesterday", + "S'han trobat molts errors ahir", + PROJECT_ABI, + SC, + "Many documents found Yesterday Microsoft", + "S'han trobat molts errors ahir Microsoft", + PROJECT_MICROSOFT, + NO_SC, + ] + + FIELDS = 4 + + def _create_index(self): + """Creates an index in RAM with the entries from data_set array""" + self.index = IndexCreator("") + index = self.index.create(True) + + for idx in range(0, len(self.data_set), self.FIELDS): + self.index.write_entry( + source=self.data_set[idx + 0], + target=self.data_set[idx + 1], + comment="", + context="", + project_name=self.data_set[idx + 2], + project_id=self.data_set[idx + 2] + "_id", + softcatala=self.data_set[idx + 3], + ) + + self.index.writer.commit() + return index + + def test_query_simple_query_source(self): + ix = self._create_index() + search = Search("Today", None, "gnome_id") + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 1) + self.assertEqual(results[0]["source"], "No Documents Found Today") + + def test_query_simple_query_source_with_two_projects(self): + ix = self._create_index() + search = Search("Yesterday", None, "gnome_id,abiword_id") + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 2) + self.assertEqual(results[0]["source"], "Many documents found Yesterday") + self.assertEqual(results[1]["source"], "No Documents Found late Yesterday") + + def test_query_simple_query_source_with_three_projects_with_spaces(self): + ix = self._create_index() + search = Search( + "Yesterday", None, "gnome_id,abiword_id,Microsoft Terminology_id" + ) + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 3) + self.assertEqual(results[0]["source"], "Many documents found Yesterday") + self.assertEqual(results[1]["source"], "No Documents Found late Yesterday") + self.assertEqual( + results[2]["source"], "Many documents found Yesterday Microsoft" + ) + + def test_query_simple_query_source_with_single_project_with_spaces(self): + ix = self._create_index() + search = Search("Yesterday", None, "Microsoft Terminology_id") + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 1) + self.assertEqual( + results[0]["source"], "Many documents found Yesterday Microsoft" + ) + + def test_query_simple_query_source_with_no_project(self): + ix = self._create_index() + search = Search("Yesterday", None, "") + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 3) + self.assertEqual(results[0]["source"], "Many documents found Yesterday") + self.assertEqual(results[1]["source"], "No Documents Found late Yesterday") + self.assertEqual( + results[2]["source"], "Many documents found Yesterday Microsoft" + ) + + def test_query_simple_with_or_query_source(self): + ix = self._create_index() + search = Search("Today OR Yesterday", None, "gnome_id") + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 2) + self.assertEqual(results[0]["source"], "No Documents Found Today") + self.assertEqual(results[1]["source"], "No Documents Found late Yesterday") + + def test_query_simple_with_not_query_source(self): + ix = self._create_index() + search = Search("Documents NOT Yesterday", None, "gnome_id") + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 2) + for i in range(0, len(results)): + self.assertFalse("Yesterday" in results[i]["source"]) + + def test_query_simple_with_and_query_source(self): + ix = self._create_index() + search = Search("Documents AND late", None, "gnome_id") + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 1) + self.assertEqual(results[0]["source"], "No Documents Found late Yesterday") + + def test_query_source_target(self): + ix = self._create_index() + search = Search("Documents", "Ahir", "gnome_id") + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 1) + self.assertEqual(results[0]["source"], "No Documents Found late Yesterday") + + def test_query_source_OR_target(self): + ix = self._create_index() + search = Search("Today OR No", "trobat", "gnome_id") + search.search(ix) + results = search.get_results() + + self.assertEqual(len(results), 2) + self.assertEqual(results[0]["source"], "No Documents Found Today") + self.assertEqual(results[1]["source"], "No Documents Found late Yesterday") + + def test_get_json(self): + ix = self._create_index() + search = Search("Today OR No", "trobat", "gnome_id") + search.search(ix) + results = search.get_json() + + json_array = json.loads(results) + self.assertEqual(len(json_array), 2) + self.assertEqual(json_array[0]["source"], "No Documents Found Today") + self.assertEqual(json_array[1]["source"], "No Documents Found late Yesterday") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/testglossarysql.py b/tests/testglossarysql.py new file mode 100644 index 0000000..b83c44c --- /dev/null +++ b/tests/testglossarysql.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2022 Jordi Mas i Hernandez +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +from glossarysql import Entry, database +import unittest + + +class TestGlossarySql(unittest.TestCase): + SOURCE_TERM = "Hello" + TRANSLATION = "Hola" + FREQUENCY = 10 + PERCENTATGE = 5 + TERMCAT = True + + @classmethod + def setUpClass(cls): + name = ":memory:" + database.create(name) + database.create_schema() + + db_entry = cls._get_entry(cls) + db_entry.save() + + def _get_entry(self): + db_entry = Entry() + db_entry.term = self.SOURCE_TERM + db_entry.translation = self.TRANSLATION + db_entry.frequency = self.FREQUENCY + db_entry.percentage = self.PERCENTATGE + db_entry.termcat = self.TERMCAT + return db_entry + + def test_save_glossary(self): + glossary = Entry.select().where(Entry.term == self.SOURCE_TERM) + cnt = glossary.count() + + item = list(glossary)[0] + self.assertEqual(1, cnt) + self.assertEqual(self.SOURCE_TERM, item.term) + self.assertEqual(self.TRANSLATION, item.translation) + self.assertEqual(self.FREQUENCY, item.frequency) + self.assertEqual(self.PERCENTATGE, item.percentage) + self.assertEqual(self.TERMCAT, item.termcat) + + def test_fields(self): + POS_ONLY_ITEM = 0 + glossary = Entry.select().where(Entry.term == self.SOURCE_TERM) + result = list(glossary)[POS_ONLY_ITEM] + + values = result.dict.items() + self.assertTrue(("id", "1") in values) + self.assertTrue(("term", self.SOURCE_TERM) in values) + self.assertTrue(("translation", self.TRANSLATION) in values) + self.assertTrue(("frequency", str(self.FREQUENCY)) in values) + self.assertTrue(("percentage", "5.0") in values) + self.assertTrue(("termcat", "1") in values) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/testindexcreator.py b/tests/testindexcreator.py new file mode 100644 index 0000000..295438b --- /dev/null +++ b/tests/testindexcreator.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2014 Jordi Mas i Hernandez +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import unittest +from whoosh.writing import IndexWriter +import os +from indexcreator import IndexCreator + + +class IndexWriterMock(IndexWriter): + def __init__(self): + self.store = [] + + def add_document(self, **fields): + d = dict() + d.update(fields) + self.store.append(d) + + +class TestIndexCreator(unittest.TestCase): + def test_process_project(self): + json_dir = os.path.dirname(os.path.realpath(__file__)) + json_file = os.path.join(json_dir, "data/index_data.json") + + index = IndexCreator(json_file) + index.writer = IndexWriterMock() + index.process_entries() + stored = index.writer.store + + self.assertEqual( + stored[0]["source"], + " - Reserved. \n You cannot use this name. Choose Another \n", + ) + self.assertEqual( + stored[0]["target"], + " - est\u00e0 reservat. \n No podeu utilitzar aquest nom. Escolliu-ne un altre \n", + ) + self.assertEqual(stored[0]["context"], "context") + self.assertEqual( + stored[0]["comment"], + "Source: /abiword-ca.po from project 'Abiword'\r\nDLG_Styles_ErrNotTitle2", + ) + self.assertEqual(stored[0]["softcatala"], True) + self.assertEqual(stored[0]["project"], "Abiword") + self.assertEqual(2, len(stored), 5) + + +if __name__ == "__main__": + unittest.main()