Skip to content

Commit

Permalink
Add unit test and run them in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimas committed Nov 15, 2023
1 parent 44d745c commit be5589b
Show file tree
Hide file tree
Showing 10 changed files with 464 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
Binary file added tests/__pycache__/testglossarysql.cpython-311.pyc
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/data/index_data.json
Original file line number Diff line number Diff line change
@@ -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
}
]
Binary file not shown.
Binary file not shown.
68 changes: 68 additions & 0 deletions tests/models-tests/testpagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Jordi Mas i Hernandez <jmas@softcatala.org>
#
# 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()
200 changes: 200 additions & 0 deletions tests/models-tests/testsearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Jordi Mas i Hernandez <jmas@softcatala.org>
#
# 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()
Loading

0 comments on commit be5589b

Please sign in to comment.