From: Thomas Fillon Date: Tue, 18 Nov 2014 14:29:40 +0000 (+0100) Subject: Tests: add pytest support X-Git-Tag: 1.5.0rc1~27^2~13 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=d606aa9804b3968f82b2510e09d779467f2034f8;p=telemeta.git Tests: add pytest support --- diff --git a/.travis.yml b/.travis.yml index ec548646..92f680d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,5 +23,4 @@ install: # command to run tests script: - -# - "python tests/testcomponent.py" + - py.test -v --cov telemeta diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..8e1f5368 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +#django_find_project = false +DJANGO_SETTINGS_MODULE = telemeta.tests.settings_sqlite \ No newline at end of file diff --git a/telemeta/tests/__init__.py b/telemeta/tests/__init__.py index 6e25d67a..b90a4bce 100644 --- a/telemeta/tests/__init__.py +++ b/telemeta/tests/__init__.py @@ -33,5 +33,5 @@ # Authors: Olivier Guilyardi # David LIPSZYC -from model_tests import * -from util_tests import * +#from model_tests import * +#from util_tests import * diff --git a/telemeta/tests/conftest.py b/telemeta/tests/conftest.py new file mode 100644 index 00000000..ad563285 --- /dev/null +++ b/telemeta/tests/conftest.py @@ -0,0 +1,4 @@ +import os +import sys + +sys.path.append(os.path.dirname(__file__)) diff --git a/telemeta/tests/model_tests.py b/telemeta/tests/model_tests.py index ba6de4a4..db90d1db 100644 --- a/telemeta/tests/model_tests.py +++ b/telemeta/tests/model_tests.py @@ -35,14 +35,19 @@ from django.contrib.auth.models import User import unittest -from telemeta.models import * +from telemeta.models import LocationType, Location, EthnicGroup, Publisher +from telemeta.models import MediaCollection, MediaItem from datetime import datetime, timedelta +import pytest -class CollectionItemTestCase(unittest.TestCase): +pytestmark = pytest.mark.django_db +@pytest.mark.django_db +class CollectionItemTestCase(unittest.TestCase): + pytestmark = pytest.mark.django_db def setUp(self): "Create a test database based on objects created in Django" - + # self.david = User.objects.create(username="david", level="user") # self.olivier = User.objects.create(username="olivier", level="admin") self.david = User.objects.create_user(username="david") @@ -66,76 +71,76 @@ class CollectionItemTestCase(unittest.TestCase): self.c = EthnicGroup.objects.create(value="c") self.d = EthnicGroup.objects.create(value="d") - self.persepolis = MediaCollection(id=1, code="CNRSMH_E_1970_001_002", reference="A1", title="persepolis", + self.persepolis = MediaCollection(id=1, code="CNRSMH_E_1970_001_002", reference="A1", title="persepolis", creator="Abraham LINCOLN", collector="Friedrich HEINZ", year_published=2009, is_published=True, recorded_from_year=1970, recorded_to_year=1980) - + self.persepolis.save_with_revision(self.david) - self.volonte = MediaCollection(id=2, reference="A2", code="CNRSMH_I_1960_001", title="Volonté de puissance", - creator="Friedrich NIETZSCHE", collector="Jean AMORA", year_published=1999, + self.volonte = MediaCollection(id=2, reference="A2", code="CNRSMH_I_1960_001", title="Volonté de puissance", + creator="Friedrich NIETZSCHE", collector="Jean AMORA", year_published=1999, recorded_from_year=1960, recorded_to_year=2000) self.volonte.save_with_revision(self.olivier) - self.nicolas = MediaCollection(id=3, reference="A3", code="CNRSMH_I_1967_123", title="petit nicolas", - creator="Georgette McKenic", collector="Paul MAILLE", year_published=1999, + self.nicolas = MediaCollection(id=3, reference="A3", code="CNRSMH_I_1967_123", title="petit nicolas", + creator="Georgette McKenic", collector="Paul MAILLE", year_published=1999, recorded_from_year=1967, recorded_to_year=1968) - + self.nicolas.save_with_revision(self.olivier) - - self.item_1 = MediaItem(id=1, collection=self.persepolis, code="CNRSMH_E_1970_001_002_44", - recorded_from_date="1971-01-12", recorded_to_date="1971-02-24", location=self.paris, - ethnic_group=self.a, title="item 1", author="Mickael SHEPHERD", collector="Charles PREMIER", - comment="comment 1") + + self.item_1 = MediaItem(id=1, collection=self.persepolis, code="CNRSMH_E_1970_001_002_44", + recorded_from_date="1971-01-12", recorded_to_date="1971-02-24", location=self.paris, + ethnic_group=self.a, title="item 1", author="Mickael SHEPHERD", collector="Charles PREMIER", + comment="comment 1") self.item_1.save_with_revision(self.david) - self.item_2 = MediaItem(id=2, collection=self.volonte, code="CNRSMH_I_1960_001_129", - recorded_from_date="1981-01-12", recorded_to_date="1991-02-24", location=self.france, - ethnic_group=self.a, title="item 2", author="Rick ROLL", comment="comment 2") + self.item_2 = MediaItem(id=2, collection=self.volonte, code="CNRSMH_I_1960_001_129", + recorded_from_date="1981-01-12", recorded_to_date="1991-02-24", location=self.france, + ethnic_group=self.a, title="item 2", author="Rick ROLL", comment="comment 2") self.item_2.save_with_revision(self.david) - self.item_3 = MediaItem(id=3, collection=self.nicolas, code="CNRSMH_I_1967_123_456_01", - recorded_from_date="1968-01-12", recorded_to_date="1968-02-24", location=self.belgique, + self.item_3 = MediaItem(id=3, collection=self.nicolas, code="CNRSMH_I_1967_123_456_01", + recorded_from_date="1968-01-12", recorded_to_date="1968-02-24", location=self.belgique, ethnic_group=self.b, title="item 3", author="John SMITH", collector="Paul CARLOS", comment="comment 3", ) self.item_3.save_with_revision(self.olivier) - self.item_4 = MediaItem(id=4, collection=self.persepolis, code="CNRSMH_E_1970_001_002_22_33", - recorded_from_date="1972-01-12", recorded_to_date="1972-02-24", location=self.europe, - ethnic_group=self.a, title="item 4", alt_title="I4", author="Keanu REAVES", + self.item_4 = MediaItem(id=4, collection=self.persepolis, code="CNRSMH_E_1970_001_002_22_33", + recorded_from_date="1972-01-12", recorded_to_date="1972-02-24", location=self.europe, + ethnic_group=self.a, title="item 4", alt_title="I4", author="Keanu REAVES", collector="Christina BARCELONA", comment="comment 4") self.item_4.save_with_revision(self.olivier) - self.item_5 = MediaItem(id=5, collection=self.volonte,code="CNRSMH_I_1960_001_789_85_22", - approx_duration="00:05:00", recorded_from_date="1978-01-12", recorded_to_date="1978-02-24", - location=self.belgique, ethnic_group=self.a, title="item 5", alt_title="I5", - author="Simon PAUL", collector="Javier BARDEM", + self.item_5 = MediaItem(id=5, collection=self.volonte,code="CNRSMH_I_1960_001_789_85_22", + approx_duration="00:05:00", recorded_from_date="1978-01-12", recorded_to_date="1978-02-24", + location=self.belgique, ethnic_group=self.a, title="item 5", alt_title="I5", + author="Simon PAUL", collector="Javier BARDEM", comment="comment 5") self.item_5.save_with_revision(self.olivier) - self.item_6 = MediaItem(id=6, collection=self.persepolis, code="CNRSMH_E_1970_001_002_90", - recorded_from_date="1968-01-12", recorded_to_date="1968-02-11", location=self.france, - ethnic_group=self.b, title="item 6", author="Paul ANDERSON", + self.item_6 = MediaItem(id=6, collection=self.persepolis, code="CNRSMH_E_1970_001_002_90", + recorded_from_date="1968-01-12", recorded_to_date="1968-02-11", location=self.france, + ethnic_group=self.b, title="item 6", author="Paul ANDERSON", collector="Jim CARLSON", comment="comment 10000") - + self.item_6.save_with_revision(self.david) self.collections = MediaCollection.objects.all() self.items = MediaItem.objects.all() def tearDown(self): - User.objects.all().delete() + User.objects.all().delete() LocationType.objects.all().delete() - Location.objects.all().delete() + Location.objects.all().delete() EthnicGroup.objects.all().delete() MediaCollection.objects.all().delete() - MediaItem.objects.all().delete() + MediaItem.objects.all().delete() def testQuickSearchOnCollections(self): "Test quick_search property of MediaCollection class" @@ -166,7 +171,7 @@ class CollectionItemTestCase(unittest.TestCase): self.assertEquals(result[0], self.volonte) result = self.collections.quick_search("puissance volonte") self.assertEquals(result[0], self.volonte) - + def testLocationSearch(self): "Test by_country and by_continent properties of MediaCollection class" self.assertEquals(self.collections.by_location(self.france)[0], self.persepolis) @@ -174,19 +179,19 @@ class CollectionItemTestCase(unittest.TestCase): self.assertEquals(self.collections.by_location(self.belgique).order_by("title")[0], self.nicolas) self.assertEquals(self.collections.by_location(self.belgique).order_by("title")[1], self.volonte) - def testRecordingYear(self): + def testRecordingYear(self): "Test by_recording_year property of MediaCollection class" self.assertEquals(self.collections.by_recording_year(1970, 1980)[0], self.persepolis) result = self.collections.by_recording_year(1975).order_by("title") self.assertEquals(result[0], self.persepolis) self.assertEquals(result[1], self.volonte) - + def testPublishYearOnCollection(self): "Test by_publish_year property of MediaCollection class" result=self.collections.by_publish_year(1999).order_by("title") self.assertEquals(result[0], self.nicolas) self.assertEquals(result[1], self.volonte) - + def testEthnicGroup(self): "Test by_ethnic_group property of MediaCollection class" result=self.collections.by_ethnic_group(self.a).order_by("title") @@ -230,7 +235,7 @@ class CollectionItemTestCase(unittest.TestCase): self.assertEquals(result[0], self.item_2) self.assertEquals(result[1], self.item_3) self.assertEquals(result[2], self.item_5) - + def testWordSearchCore(self): "Test word_search property of CoreQuerySet class" self.assertEquals(self.collections.word_search("title", "volonté")[0], self.volonte) @@ -243,7 +248,7 @@ class CollectionItemTestCase(unittest.TestCase): self.assertEquals(result[3], self.item_4) self.assertEquals(result[4], self.item_5) self.assertEquals(result[5], self.item_6) - + def testByChangeTimeOnCollection(self): "Test by_change_time property of MediaCollection class" now = datetime.now() @@ -269,7 +274,7 @@ class CollectionItemTestCase(unittest.TestCase): self.assertEquals(e.field.name, 'code') else: self.fail("No exception raised") - + def testDomForeignKey(self): "Test DOM foreign key embedding" doc = self.item_4.to_dom() @@ -286,13 +291,17 @@ class CollectionItemTestCase(unittest.TestCase): "Test the MediaCollection.countries() method" self.assertEquals(self.volonte.countries(), [self.belgique, self.france]) - +pytestmark = pytest.mark.django_db + +@pytest.mark.django_db class RelatedDeleteTestCase(unittest.TestCase): + pytestmark = pytest.mark.django_db + def setUp(self): self.publisher1 = Publisher.objects.create(id=1, value='publisher1') self.publisher2 = Publisher.objects.create(id=2, value='publisher2') self.pubcollection1 = PublisherCollection.objects.create(publisher=self.publisher1, value='pub1_collection1') - + self.rights1 = LegalRight.objects.create(id=1, value='right1') MediaCollection.objects.create(id=1, code='CNRSMH_I_1256_456', title='Collection1', @@ -336,5 +345,5 @@ class RelatedDeleteTestCase(unittest.TestCase): self.assertEquals(PublisherCollection.objects.count(), 0) self.assertEquals(MediaCollection.objects.count(), 2) - + diff --git a/telemeta/tests/settings_sqlite.py b/telemeta/tests/settings_sqlite.py new file mode 100644 index 00000000..bc96253a --- /dev/null +++ b/telemeta/tests/settings_sqlite.py @@ -0,0 +1,6 @@ +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:' + }, +} diff --git a/telemeta/tests/test_utils.py b/telemeta/tests/test_utils.py new file mode 100644 index 00000000..4a75b5ab --- /dev/null +++ b/telemeta/tests/test_utils.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2007-2010 Samalyse SARL + +# This software is a computer program whose purpose is to backup, analyse, +# transcode and stream any audio content with its metadata over a web frontend. + +# This software is governed by the CeCILL license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL +# license as circulated by CEA, CNRS and INRIA at the following URL +# "http://www.cecill.info". + +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. + +# In this respect, the user's attention is drawn to the risks associated +# with loading, using, modifying and/or developing or reproducing the +# software by the user in light of its specific status of free software, +# that may mean that it is complicated to manipulate, and that also +# therefore means that it is reserved for developers and experienced +# professionals having in-depth computer knowledge. Users are therefore +# encouraged to load and test the software's suitability as regards their +# requirements in conditions enabling the security of their systems and/or +# data to be ensured and, more generally, to use and operate it in the +# same conditions as regards security. + +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL license and that you accept its terms. +# +# Authors: Olivier Guilyardi + +import unittest +from telemeta.util.unaccent import unaccent_icmp + +class UnaccentTestCase(unittest.TestCase): + def testSorting(self): + strings = [u'Métro', u'évasion', u'àccent', u'È', u'île', u'arrivée', u'elle'] + strings.sort(unaccent_icmp) + expected = [u'àccent', u'arrivée', u'È', u'elle', u'évasion', u'île', u'Métro'] + self.assertEquals(strings, expected) + diff --git a/telemeta/tests/util_tests.py b/telemeta/tests/util_tests.py deleted file mode 100644 index 4a75b5ab..00000000 --- a/telemeta/tests/util_tests.py +++ /dev/null @@ -1,44 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright (C) 2007-2010 Samalyse SARL - -# This software is a computer program whose purpose is to backup, analyse, -# transcode and stream any audio content with its metadata over a web frontend. - -# This software is governed by the CeCILL license under French law and -# abiding by the rules of distribution of free software. You can use, -# modify and/ or redistribute the software under the terms of the CeCILL -# license as circulated by CEA, CNRS and INRIA at the following URL -# "http://www.cecill.info". - -# As a counterpart to the access to the source code and rights to copy, -# modify and redistribute granted by the license, users are provided only -# with a limited warranty and the software's author, the holder of the -# economic rights, and the successive licensors have only limited -# liability. - -# In this respect, the user's attention is drawn to the risks associated -# with loading, using, modifying and/or developing or reproducing the -# software by the user in light of its specific status of free software, -# that may mean that it is complicated to manipulate, and that also -# therefore means that it is reserved for developers and experienced -# professionals having in-depth computer knowledge. Users are therefore -# encouraged to load and test the software's suitability as regards their -# requirements in conditions enabling the security of their systems and/or -# data to be ensured and, more generally, to use and operate it in the -# same conditions as regards security. - -# The fact that you are presently reading this means that you have had -# knowledge of the CeCILL license and that you accept its terms. -# -# Authors: Olivier Guilyardi - -import unittest -from telemeta.util.unaccent import unaccent_icmp - -class UnaccentTestCase(unittest.TestCase): - def testSorting(self): - strings = [u'Métro', u'évasion', u'àccent', u'È', u'île', u'arrivée', u'elle'] - strings.sort(unaccent_icmp) - expected = [u'àccent', u'arrivée', u'È', u'elle', u'évasion', u'île', u'Métro'] - self.assertEquals(strings, expected) -