Commits

Artur Barseghyan committed c356a6a Draft

tests added; minor documentation fixes

  • Participants
  • Parent commits 1c6f6fe
  • Tags 0.7.2

Comments (0)

Files changed (17)

 *.py[cod]
 .hgignore~
 .gitignore~
+tox.ini~
 .hg/
+.tox/
 
 example/db/example.db
 example/tmp/
 \.pyc$
 \.hgignore~
 \.gitignore~
+tox.ini~
 \.git/
+\.tox/
 
 ^example/db/example.db
 ^example/tmp/

File CHANGELOG.rst

 Release history
 =====================================
+0.7.2
+-------------------------------------
+2013-12-24
+
+- Tests added.
+- Minor fixes in documentation.
+
 0.7.1
 -------------------------------------
-Release date: 2013-09-20
+2013-09-20
 
 - Updated django-localeurl dependancy to 2.0.1.
 
 0.7
 -------------------------------------
-Release date: 2013-09-19
+2013-09-19
 
 - Simplified use of local language names.
-Package
 ===================================
 django-slim
-
-Description
 ===================================
 Simple implementation of multi-lingual models for Django. Django-admin integration works out of the box.
 Supports `django-localeurl` integration.
 In order to be able to quickly evaluate the django-slim, a demo app (with a quick installer) has been created
 (Debian only). Follow the instructions below for having the demo running within a minute.
 
-Grab the latest `django-slim-example-app-install.sh`
+Grab the latest `django_slim_example_app_installer.sh`
 
-    $ wget https://raw.github.com/barseghyanartur/django-slim/stable/django-slim-example-app-install.sh
+    $ wget https://raw.github.com/barseghyanartur/django-slim/stable/example/django_slim_example_app_installer.sh
 
 Create a new- or switch to existing- virtual environement, assign execute rights to the installer and run
 the `django-slim-example-app-install.sh`.
 
-    $ chmod +x django-slim-example-app-install.sh
+    $ chmod +x django_slim_example_app_installer.sh
 
-    $ ./django-slim-example-app-install.sh
+    $ ./django_slim_example_app_installer.sh
 
 Go to the front/back -end and test the app.
 
-- Front-end URL: http://127.0.0.1:8000/en/foo/
-- Admin URL: http://127.0.0.1:8000/admin/foo/fooitem/
+- Front-end URL: http://127.0.0.1:8001/en/foo/
+- Admin URL: http://127.0.0.1:8001/admin/foo/fooitem/
 - Admin username: admin
 - Password: test
 
 
 >>> LANGUAGES = (
 >>>     ('en', gettext("English")), # Main language!
->>>     ('am', gettext("Armenian")),
+>>>     ('hy', gettext("Armenian")),
 >>>     ('nl', gettext("Dutch")),
 >>>     ('ru', gettext("Russian")),
 >>> )
 Let's assume, we have such record and it has been translated to Armenian (`am`) and Dutch (`nl`). Original
 translation is named `Lorem ipsum`. Other translations have the language code appended to the title.
 
->>> armenian_foo = foo.get_translation_for('am')
-<FooItem: Lorem ipsum AM>
+>>> armenian_foo = foo.get_translation_for('hy')
+<FooItem: Lorem ipsum HY>
 >>> dutch_foo = foo.get_translation_for('nl')
 <FooItem: Lorem ipsum NL>
 
 
 All available translations for ``foo``:
 
->>> foo.available_translations.all()
-[<FooItem: Lorem ipsum AM>, <FooItem: Lorem ipsum NL>]
+>>> foo.available_translations()
+[<FooItem: Lorem ipsum HY>, <FooItem: Lorem ipsum NL>]
 
 All available translations for Armenian ``foo``.
 
->>> armenian_foo.available_translations.all()
+>>> armenian_foo.available_translations()
 [<FooItem: Lorem ipsum>, <FooItem: Lorem ipsum NL>]
 
-See https://bitbucket.org/barseghyanartur/django-slim/src (example) directory for a working example.
+See https://github.com/barseghyanartur/django-slim/tree/stable/example directory for a working example.
 
 django-localeurl integration
 -----------------------------------

File django-slim-example-app-install.sh

-wget -O django_slim_example_app.tar.gz https://github.com/barseghyanartur/django-slim/archive/stable.tar.gz
-mkdir django_slim_example_app/
-tar -xvf django_slim_example_app.tar.gz -C django_slim_example_app
-cd django_slim_example_app/django-slim-stable/example/example/
-pip install Django
-pip install -r ../requirements.txt
-mkdir ../media/
-mkdir ../media/static/
-mkdir ../static/
-mkdir ../db/
-cp local_settings.example local_settings.py
-./manage.py syncdb --noinput
-./manage.py collectstatic --noinput
-./manage.py runserver

File docs/conf.py

 # built documents.
 #
 # The short X.Y version.
-version = '0.7.1'
+version = '0.7.2'
 # The full version, including alpha/beta/rc tags.
-release = '0.7.1'
+release = '0.7.2'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.

File docs/index.rst

-Package
 ===================================
 django-slim
-
-Description
 ===================================
 Simple implementation of multi-lingual models for Django. Django-admin integration works out of the box.
 Supports `django-localeurl` integration.
 
 1. Installation
 
-Latest stable version on PyPI
------------------------------------
+Latest stable version on PyPI:
+
     $ pip install django-slim
 
-Latest stable version on bitbucket
------------------------------------
+Latest stable version on bitbucket:
 
     $ pip install -e hg+https://bitbucket.org/barseghyanartur/django-slim@stable#egg=django-slim
 
-Latest stable version on github
------------------------------------
+Latest stable version on github:
 
     $ pip install -e git+https://github.com/barseghyanartur/django-slim/@stable#egg=django-slim
 
 An extensive example project is available at https://github.com/barseghyanartur/django-slim/tree/stable/example
 directory.
 
-Screenshots are present in documentation on PythonHosted (http://pythonhosted.org/django-slim/).
+Screenshots are present in documentation on PythonHosted (http://pythonhosted.org/django-slim/#screenshots).
 
 Demo
 -----------------------------------
 In order to be able to quickly evaluate the django-slim, a demo app (with a quick installer) has been created
 (Debian only). Follow the instructions below for having the demo running within a minute.
 
-Grab the latest `django-slim-example-app-install.sh`
+Grab the latest `django_slim_example_app_installer.sh`
 
-    $ wget https://raw.github.com/barseghyanartur/django-slim/stable/django-slim-example-app-install.sh
+    $ wget https://raw.github.com/barseghyanartur/django-slim/stable/example/django_slim_example_app_installer.sh
 
 Create a new- or switch to existing- virtual environement, assign execute rights to the installer and run
 the `django-slim-example-app-install.sh`.
 
-    $ chmod +x django-slim-example-app-install.sh
+    $ chmod +x django_slim_example_app_installer.sh
 
-    $ ./django-slim-example-app-install.sh
+    $ ./django_slim_example_app_installer.sh
 
 Go to the front/back -end and test the app.
 
-- Front-end URL: http://127.0.0.1:8000/en/foo/
-- Admin URL: http://127.0.0.1:8000/admin/foo/fooitem/
+- Front-end URL: http://127.0.0.1:8001/en/foo/
+- Admin URL: http://127.0.0.1:8001/admin/foo/fooitem/
 - Admin username: admin
 - Password: test
 
 
 >>> LANGUAGES = (
 >>>     ('en', gettext("English")), # Main language!
->>>     ('am', gettext("Armenian")),
+>>>     ('hy', gettext("Armenian")),
 >>>     ('nl', gettext("Dutch")),
 >>>     ('ru', gettext("Russian")),
 >>> )
 Let's assume, we have such record and it has been translated to Armenian (`am`) and Dutch (`nl`). Original
 translation is named `Lorem ipsum`. Other translations have the language code appended to the title.
 
->>> armenian_foo = foo.get_translation_for('am')
-<FooItem: Lorem ipsum AM>
+>>> armenian_foo = foo.get_translation_for('hy')
+<FooItem: Lorem ipsum HY>
 >>> dutch_foo = foo.get_translation_for('nl')
 <FooItem: Lorem ipsum NL>
 
 
 All available translations for ``foo``:
 
->>> foo.available_translations.all()
-[<FooItem: Lorem ipsum AM>, <FooItem: Lorem ipsum NL>]
+>>> foo.available_translations()
+[<FooItem: Lorem ipsum HY>, <FooItem: Lorem ipsum NL>]
 
 All available translations for Armenian ``foo``.
 
->>> armenian_foo.available_translations.all()
+>>> armenian_foo.available_translations()
 [<FooItem: Lorem ipsum>, <FooItem: Lorem ipsum NL>]
 
-See https://bitbucket.org/barseghyanartur/django-slim/src (example) directory for a working example.
+See https://github.com/barseghyanartur/django-slim/tree/stable/example directory for a working example.
 
 django-localeurl integration
 -----------------------------------

File example/README.rst

+Description
+============================
+Example project for `django-slim`. It provides good examples of how to build a multi-lingual site using `django-slim`,
+and `django-localeurl`.
+
+Installation
+============================
+1. Install requirements
+----------------------------
+    $ pip install -r requirements.txt
+
+2. Create database, which will be automatically filled with initial (test) data.
+----------------------------
+    $ ./manage.py syncdb --noinput
+
+3. Collect static files
+----------------------------
+    $ ./manage.py collectstatic --noinput
+
+Usage
+============================
+- Example app comes with initial (test) data installed. Admin credentials are admin:test
+- Add some more items and translations to ``FooItem`` model here http://127.0.0.1:8000/admin/foo/fooitem/
+- Open http://127.0.0.1:8000/en/foo/ in your browser and navigate through the URLs

File example/django_slim_example_app_installer.sh

+wget -O django_slim_example_app_installer.tar.gz https://github.com/barseghyanartur/django-slim/archive/stable.tar.gz
+virtualenv slim
+source slim/bin/activate
+mkdir django_slim_example_app_installer/
+tar -xvf django_slim_example_app_installer.tar.gz -C django_slim_example_app_installer
+cd django_slim_example_app_installer/django-slim-stable/example/example/
+pip install Django==1.5.5
+pip install -r ../requirements.txt
+pip install -e git+https://github.com/barseghyanartur/django-slim@stable#egg=django-slim
+mkdir -p ../media/ ../media/static/ ../static/ ../db/ ../logs/ ../tmp/
+cp local_settings.example local_settings.py
+./manage.py syncdb --noinput --traceback -v 3
+./manage.py migrate --noinput
+./manage.py collectstatic --noinput --traceback -v 3
+./manage.py foo_create_test_data --traceback -v 3
+./manage.py runserver 0.0.0.0:8001 --traceback -v 3

File example/example/settings.py

 
 LANGUAGES = (
     ('en', ugettext("English")), # Main language!
-    ('ar', ugettext("Armenian")),
+    ('hy', ugettext("Armenian")),
     ('nl', ugettext("Dutch")),
     ('ru', ugettext("Russian")),
 )

File example/readme.rst

-Description
-============================
-Example project for `django-slim`. It provides good examples of how to build a multi-lingual site using `django-slim`,
-and `django-localeurl`.
-
-Installation
-============================
-1. Install requirements
-----------------------------
-    $ pip install -r requirements.txt
-
-2. Create database, which will be automatically filled with initial (test) data.
-----------------------------
-    $ ./manage.py syncdb --noinput
-
-3. Collect static files
-----------------------------
-    $ ./manage.py collectstatic --noinput
-
-Usage
-============================
-- Example app comes with initial (test) data installed. Admin credentials are admin:test
-- Add some more items and translations to ``FooItem`` model here http://127.0.0.1:8000/admin/foo/fooitem/
-- Open http://127.0.0.1:8000/en/foo/ in your browser and navigate through the URLs

File example/requirements.txt

-Django==1.5.2
+Django==1.5.5
 Pillow==2.1.0
 argparse==1.2.1
 django-localeurl==1.5

File example/requirements3.txt

+# django-localeurl integration is fully supported for Python 2.6.* and 2.7.* and installs
+# automatically when installing django-slim. If you are using Python 3, install a forked
+# version of django-localeurl (since official version does not yet have support for Python 3).
+-e hg+https://bitbucket.org/barseghyanartur/django-localeurl@stable#egg=django-localeurl
 except:
   readme = ''
 
-version = '0.7.1'
+version = '0.7.2'
 
 install_requires = [
     'six==1.4.1',

File src/slim/tests.py

+from __future__ import print_function
+
+__title__ = 'slim.tests'
+__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
+__copyright__ = 'Copyright (c) 2013 Artur Barseghyan'
+__license__ = 'GPL 2.0/LGPL 2.1'
+
+import unittest
+import os
+
+from six import text_type
+
+PROJECT_DIR = lambda base : os.path.join(os.path.dirname(__file__), base).replace('\\','/')
+
+PRINT_INFO = True
+
+def print_info(func):
+    """
+    Prints some useful info.
+    """
+    if not PRINT_INFO:
+        return func
+
+    def inner(self, *args, **kwargs):
+        result = func(self, *args, **kwargs)
+
+        print('\n\n{0}'.format(func.__name__))
+        print('============================')
+        if func.__doc__:
+            print('""" {0} """'.format(func.__doc__.strip()))
+        print('----------------------------')
+        if result is not None:
+            print(result)
+        print('\n++++++++++++++++++++++++++++')
+
+        return result
+    return inner
+
+# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+import sys
+
+PY3 = sys.version_info[0] == 3
+
+_ = lambda s: s
+
+# Skipping from non-Django tests.
+if os.environ.get("DJANGO_SETTINGS_MODULE", None):
+
+    from foo.models import FooItem
+
+# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+    class SlimTest(unittest.TestCase): #unittest.TestCase
+        """
+        Tests of ``slim.models`` module.
+        """
+
+        def setUp(self):
+            super(SlimTest, self).setUp()
+
+            self.FOO_ITEM_EN_TITLE = "Foo title EN"
+            self.FOO_ITEM_EN_BODY = "Foo body EN"
+            self.FOO_ITEM_EN_SLUG = "foo-title-en"
+            self.FOO_ITEM_EN_LANGUAGE = "en"
+
+            self.FOO_ITEM_HY_TITLE = "Foo title HY"
+            self.FOO_ITEM_HY_BODY = "Foo body HY"
+            self.FOO_ITEM_HY_SLUG = "foo-title-hy"
+            self.FOO_ITEM_HY_LANGUAGE = "hy"
+
+            self.FOO_ITEM_NL_TITLE = "Foo title NL"
+            self.FOO_ITEM_NL_BODY = "Foo body NL"
+            self.FOO_ITEM_NL_SLUG = "foo-title-nl"
+            self.FOO_ITEM_NL_LANGUAGE = "nl"
+
+            self.FOO_ITEM_RU_TITLE = "Foo title RU"
+            self.FOO_ITEM_RU_BODY = "Foo body RU"
+            self.FOO_ITEM_RU_SLUG = "foo-title-ru"
+            self.FOO_ITEM_RU_LANGUAGE = "ru"
+
+        def __get_or_create_foo_items(self):
+            # *************************************
+            # ******** Creating main item *********
+            # *************************************
+            try:
+                foo_item_en = FooItem._default_manager.get(slug=self.FOO_ITEM_EN_SLUG)
+
+            except Exception as e:
+                foo_item_en = FooItem(
+                    title = self.FOO_ITEM_EN_TITLE,
+                    body = self.FOO_ITEM_EN_BODY,
+                    slug = self.FOO_ITEM_EN_SLUG,
+                    language = self.FOO_ITEM_EN_LANGUAGE
+                    )
+                foo_item_en.save()
+
+            # *************************************
+            # ********* Armenian translation ******
+            # *************************************
+            try:
+                foo_item_hy = FooItem._default_manager.get(slug=self.FOO_ITEM_HY_SLUG)
+
+            except Exception as e:
+                foo_item_hy = FooItem(
+                    title = self.FOO_ITEM_HY_TITLE,
+                    body = self.FOO_ITEM_HY_BODY,
+                    slug = self.FOO_ITEM_HY_SLUG,
+                    language = self.FOO_ITEM_HY_LANGUAGE,
+                    translation_of = foo_item_en
+                    )
+                foo_item_hy.save()
+
+                # *************************************
+                # ********* Dutch translation *********
+                # *************************************
+            try:
+                foo_item_nl = FooItem._default_manager.get(slug=self.FOO_ITEM_NL_SLUG)
+
+            except Exception as e:
+                foo_item_nl = FooItem(
+                    title = self.FOO_ITEM_NL_TITLE,
+                    body = self.FOO_ITEM_NL_BODY,
+                    slug = self.FOO_ITEM_NL_SLUG,
+                    language = self.FOO_ITEM_NL_LANGUAGE,
+                    translation_of = foo_item_en
+                    )
+                foo_item_nl.save()
+
+                # *************************************
+                # ********* Russian translation *******
+                # *************************************
+            try:
+                foo_item_ru = FooItem._default_manager.get(slug=self.FOO_ITEM_RU_SLUG)
+
+            except Exception as e:
+                foo_item_ru = FooItem(
+                    title = self.FOO_ITEM_RU_TITLE,
+                    body = self.FOO_ITEM_RU_BODY,
+                    slug = self.FOO_ITEM_RU_SLUG,
+                    language = self.FOO_ITEM_RU_LANGUAGE,
+                    translation_of = foo_item_en
+                    )
+                foo_item_ru.save()
+
+            return (foo_item_en, foo_item_hy, foo_item_nl, foo_item_ru)
+
+        @print_info
+        def test_01_get_translations(self):
+            """
+            Test ``get_translation_for`` method.
+            """
+            flow = []
+
+            foo_item_en, foo_item_hy, foo_item_nl, foo_item_ru = self.__get_or_create_foo_items()
+
+            flow.append(foo_item_en)
+
+            # Test Armenian translation
+            self.assertTrue(foo_item_en.get_translation_for('hy') == foo_item_hy)
+
+            flow.append(foo_item_hy)
+
+            # Test Dutch translation
+            self.assertTrue(foo_item_en.get_translation_for('nl') == foo_item_nl)
+
+            flow.append(foo_item_nl)
+
+            # Test Russian translation
+            self.assertTrue(foo_item_en.get_translation_for('ru') == foo_item_ru)
+
+            flow.append(foo_item_ru)
+
+            return flow
+
+        @print_info
+        def test_02_available_translations(self):
+            """
+            Test ``available_translations`` method.
+            """
+            flow = []
+
+            foo_item_en, foo_item_hy, foo_item_nl, foo_item_ru = self.__get_or_create_foo_items()
+
+            available_translations = [foo_item.pk for foo_item in foo_item_en.available_translations()]
+            available_translations.sort()
+
+            translations = [foo_item_hy.pk, foo_item_nl.pk, foo_item_ru.pk]
+            translations.sort()
+
+            self.assertTrue(available_translations == translations)
+
+            flow.append(available_translations)
+            flow.append(translations)
+
+            return flow
+
+        @print_info
+        def test_03_auto_prepend_language_model_decorator(self):
+            """
+            Test the ``auto_prepend_language`` model decorator.
+            """
+            flow = []
+
+            foo_item_en, foo_item_hy, foo_item_nl, foo_item_ru = self.__get_or_create_foo_items()
+
+            self.assertTrue(
+                text_type(foo_item_en.get_absolute_url()) == \
+                text_type('/{0}/foo/{1}/').format(self.FOO_ITEM_EN_LANGUAGE, self.FOO_ITEM_EN_SLUG)
+                )
+
+            flow.append(foo_item_en.get_absolute_url())
+
+            self.assertTrue(
+                text_type(foo_item_hy.get_absolute_url()) == \
+                text_type('/{0}/foo/{1}/').format(self.FOO_ITEM_HY_LANGUAGE, self.FOO_ITEM_HY_SLUG)
+                )
+
+            flow.append(foo_item_hy.get_absolute_url())
+
+            self.assertTrue(
+                text_type(foo_item_nl.get_absolute_url()) == \
+                text_type('/{0}/foo/{1}/').format(self.FOO_ITEM_NL_LANGUAGE, self.FOO_ITEM_NL_SLUG)
+                )
+
+            flow.append(foo_item_nl.get_absolute_url())
+
+            self.assertTrue(
+                text_type(foo_item_ru.get_absolute_url()) == \
+                text_type('/{0}/foo/{1}/').format(self.FOO_ITEM_RU_LANGUAGE, self.FOO_ITEM_RU_SLUG)
+                )
+
+            flow.append(foo_item_ru.get_absolute_url())
+
+            return flow
+
+        @print_info
+        def test_04_original_translation(self):
+            """
+            Test ``original_translation`` method.
+            """
+            flow = []
+
+            foo_item_en, foo_item_hy, foo_item_nl, foo_item_ru = self.__get_or_create_foo_items()
+
+            self.assertTrue(foo_item_hy.original_translation == foo_item_en)
+            self.assertTrue(foo_item_nl.original_translation == foo_item_en)
+            self.assertTrue(foo_item_ru.original_translation == foo_item_en)
+
+            #return flow
+
+
+if __name__ == "__main__":
+    # Tests
+    unittest.main()
+reset
+./uninstall.sh
+./install.sh
+python example/example/manage.py test slim --traceback -v 3
+[tox]
+envlist = py26_six141,py27_six141,py33_six141
+
+[testenv:py26_six141]
+deps=unittest2
+commands=
+    {envpython} setup.py install
+    pip install -r example/requirements.txt
+    {envpython} example/example/manage.py syncdb --noinput --traceback -v 3
+    {envpython} example/example/manage.py collectstatic --noinput --traceback -v 3
+    {envpython} example/example/manage.py test slim --traceback -v 3
+
+[testenv:py27_six141]
+deps=unittest2
+commands=
+    {envpython} setup.py install
+    pip install -r example/requirements.txt
+    {envpython} example/example/manage.py syncdb --noinput --traceback -v 3
+    {envpython} example/example/manage.py collectstatic --noinput --traceback -v 3
+    {envpython} example/example/manage.py test slim --traceback -v 3
+
+[testenv:py33_six141]
+basepython=/opt/python3.3/bin/python3.3
+commands=
+    {envpython} setup.py install
+    pip install -r example/requirements.txt
+    pip install -r example/requirements3.txt
+    {envpython} example/example/manage.py syncdb --noinput --traceback -v 3
+    {envpython} example/example/manage.py collectstatic --noinput --traceback -v 3
+    {envpython} example/example/manage.py test slim --traceback -v 3