Commit afafbb0c authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby
Browse files

tests: Add unittest for Model and minor change.

Showing with 154 additions and 26 deletions
+154 -26
......@@ -246,6 +246,22 @@ build-windows:
# TESTS #
#########
unittest:
stage: test
tags:
- linux
needs:
- job: set-version
artifacts: true
script:
- python3 -m venv venv
- . venv/bin/activate
- pip3 install -U pip
- pip3 install -r ../full-requirements.txt
- pip3 install -U -r ../full-requirements.txt
- cd src
- python3 -m unittest discover -t .
test-pep8:
stage: test
tags:
......@@ -253,6 +269,7 @@ test-pep8:
needs:
- job: set-version
artifacts: true
- job: unittest
script:
- mkdir -p pep8
- cd pep8
......@@ -266,32 +283,6 @@ test-pep8:
- pycodestyle ../src
allow_failure: true
# test-windows:
# stage: test
# tags:
# - wine
# needs:
# - job: set-version
# artifacts: true
# - job: build-windows
# artifacts: true
# script:
# - cd windows\pamhyr
# - pamhyr\pamhyr.exe hello
# test-linux:
# stage: test
# tags:
# - linux
# needs:
# - job: set-version
# artifacts: true
# - job: build-linux
# artifacts: true
# script:
# - cd linux/pamhyr
# - ./Pamhyr2 hello
############
# PACKAGES #
############
......
......@@ -290,3 +290,11 @@ class Study(SQLModel):
self._save_submodel([self._river])
self.commit()
def close(self):
"""Close db connection
Returns:
Nothing.
"""
self._close()
# __init__.py -- Pamhyr
# Copyright (C) 2023 INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
# test_Model.py -- Pamhyr
# Copyright (C) 2023 INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
import os
import unittest
import tempfile
from Model.Saved import SavedStatus
from Model.Study import Study
from Model.River import River
class StudyTestCase(unittest.TestCase):
def test_create_study(self):
study = Study.new("foo", "bar")
self.assertEqual(study.name, "foo")
self.assertEqual(study.description, "bar")
def test_open_study(self):
study = Study.open("../tests_cases/Enlargement/Enlargement.pamhyr")
self.assertNotEqual(study, None)
self.assertEqual(study.name, "Enlargement")
def test_save_open_study(self):
study = Study.new("foo", "bar")
dir = tempfile.mkdtemp()
f = os.path.join(dir, "foo.pamhyr")
# Save study
study.filename = f
study.save()
study.close()
# Reopen study
study = Study.open(f)
# Check
self.assertNotEqual(study, None)
self.assertEqual(study.name, "foo")
self.assertEqual(study.description, "bar")
def test_create_study_river(self):
study = Study.new("foo", "bar")
self.assertNotEqual(study.river, None)
class RiverTestCase(unittest.TestCase):
def test_create_river(self):
status = SavedStatus()
river = River(status=status)
self.assertNotEqual(river, None)
def test_create_river_nodes(self):
status = SavedStatus()
river = River(status=status)
self.assertNotEqual(river, None)
# Add nodes
n0 = river.add_node()
n1 = river.add_node(x=1.0, y=0.0)
n2 = river.add_node(x=0.0, y=1.0)
# Checks
self.assertEqual(river.nodes_counts(), 3)
nodes = river.nodes()
self.assertEqual(nodes[0], n0)
self.assertEqual(nodes[1], n1)
self.assertEqual(nodes[2], n2)
def test_create_river_edges(self):
status = SavedStatus()
river = River(status=status)
self.assertNotEqual(river, None)
# Add nodes
n0 = river.add_node()
n1 = river.add_node(x=1.0, y=0.0)
n2 = river.add_node(x=0.0, y=1.0)
self.assertEqual(river.nodes_counts(), 3)
# Add edges
e0 = river.add_edge(n0, n1)
e1 = river.add_edge(n1, n2)
# Checks
self.assertEqual(river.edges_counts(), 2)
edges = river.edges()
self.assertEqual(edges[0], e0)
self.assertEqual(edges[1], e1)
......@@ -272,6 +272,10 @@ class SQL(object):
logger.debug("SQL - commit")
self._db.commit()
def _close(self):
self.commit()
self._db.close()
def _fetch_string(self, s):
return s.replace("&#39;", "'")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment