From b464c7138e6681fdc7b03dcba774dae70fc72df7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rodrigo=20Ram=C3=ADrez=20Norambuena?= <a@rodrigoramirez.com>
Date: Mon, 9 Sep 2019 16:14:54 -0300
Subject: [PATCH 5/5] Refactor: Remove extra else in __str__ for Quote:

Add a testing init for enviroment to build tests againt model. This
change include a migration to run the varnish_local.sql into the test
enviroment.

diff --git a/pgweb/core/migrations/0003_function_test_enviroment.py b/pgweb/core/migrations/0003_function_test_enviroment.py
new file mode 100644
index 0000000..dcf6445
--- /dev/null
+++ b/pgweb/core/migrations/0003_function_test_enviroment.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models, connection
+from django.conf import settings
+import os
+
+
+def load_local_varnish_function(apps, schema_editor):
+    sql_file = os.path.join(settings.BASE_DIR, 'sql', 'varnish_local.sql')
+    with open(sql_file, 'rb') as query:
+        connection.cursor().execute(query.read())
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('core', '0002_block_oauth'),
+    ]
+
+    if settings.TESTING:
+        operations = [migrations.RunPython(load_local_varnish_function),]
diff --git a/pgweb/quotes/models.py b/pgweb/quotes/models.py
index ec7b1ed..1573d24 100644
--- a/pgweb/quotes/models.py
+++ b/pgweb/quotes/models.py
@@ -15,5 +15,4 @@ class Quote(models.Model):
     def __str__(self):
         if len(self.quote) > 75:
             return "%s..." % self.quote[:75]
-        else:
-            return self.quote
+        return self.quote
diff --git a/pgweb/quotes/tests/__init__.py b/pgweb/quotes/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/pgweb/quotes/tests/test_quotes.py b/pgweb/quotes/tests/test_quotes.py
new file mode 100644
index 0000000..320dd26
--- /dev/null
+++ b/pgweb/quotes/tests/test_quotes.py
@@ -0,0 +1,16 @@
+from django.test import TestCase
+from django.db.models import signals
+from pgweb.quotes.models import Quote
+from pgweb.util.signals import my_post_save_handler
+
+class QuoteTestCase(TestCase):
+
+    def test_str_max_long(self):
+        text = "Quorum commit for synchronous replication in PostgreSQL 10" \
+            " gives more options to extend our ability to promote database " \
+            "infrastructure with nearly zero downtime from the application perspective." \
+            "This allows us to continuously deploy and update our database " \
+            "infrastructure without incurring long maintenance windows."
+
+        q = Quote.objects.create(quote=text, link="https://www.postgresql.org/")
+        self.assertEqual(str(q), "Quorum commit for synchronous replication in PostgreSQL 10 gives more optio...")
diff --git a/pgweb/settings.py b/pgweb/settings.py
index 540a099..4e17311 100644
--- a/pgweb/settings.py
+++ b/pgweb/settings.py
@@ -1,4 +1,9 @@
 # Django settings for pgweb project.
+import sys
+import os
+
+# Root full path for project
+BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 DEBUG = False
 
@@ -161,5 +166,9 @@ FRONTEND_SMTP_RELAY = "magus.postgresql.org"                # Where to relay use
 OAUTH = {}                                                  # OAuth providers and keys
 PGDG_ORG_ID = -1                                            # id of the PGDG organisation entry
 
+
+# Testing
+TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
+
 # Load local settings overrides
 from .settings_local import *
-- 
2.11.0

