Skip to content

Commit

Permalink
Fixing things
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Álvarez committed Sep 4, 2017
1 parent 4b016b8 commit 0b62270
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 58 deletions.
16 changes: 10 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
language: python
sudo: false
env:
-ES_VERSION=2.4.1 ES_DOWNLOAD_URL=https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/${ES_VERSION}/elasticsearch-${ES_VERSION}.tar.gz
- DB=postgres
- DB=sqlite3
services:
- postgresql
- redis-server
python:
- "2.7"
Expand All @@ -13,16 +15,18 @@ addons:
- libgraphicsmagick++1-dev
- libboost-python-dev
- python-pgmagick
- postgresql-client
install:
- wget ${ES_DOWNLOAD_URL}
- tar -xzf elasticsearch-${ES_VERSION}.tar.gz
- ./elasticsearch-${ES_VERSION}/bin/elasticsearch &
- pip install -r requirements.txt
- wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.1/elasticsearch-2.4.1.tar.gz
- tar -xzf elasticsearch-2.4.1.tar.gz
- ./elasticsearch-2.4.1/bin/elasticsearch &
- pip install -r requirements.txt
- pip install coveralls
- pip install psycopg2
# command to run tests
script:
- wget -q --waitretry=1 --retry-connrefused -T 10 -O - http://127.0.0.1:9200
- coverage run manage.py test
- coverage run manage.py test --noinput
- coverage report -m
after_script:
- coveralls --verbose
7 changes: 3 additions & 4 deletions backend_candidate/tests/questions_loader_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ def test_questions_loading(self):
self.assertEquals(category.name, u"preguntas Buenas")

self.assertEquals(len(category.topics.all()), 2)
question1 = category.topics.all()[0]
question1 = category.topics.get(label=u"Esto es una pregunta y quiero una respuesta")
self.assertTrue(question1.label, u"Esto es una pregunta y quiero una respuesta")
question2 = category.topics.all()[1]
self.assertTrue(question2.label, u"Pregunta 2")
question2 = category.topics.get(label=u"Pregunta 2")

a1 = question1.positions.get(label=u"Respuesta 1")
a2 = question1.positions.get(label=u"Respuesta 2")
Expand All @@ -39,4 +38,4 @@ def test_questions_loading(self):
self.assertTrue(a3)
self.assertTrue(a4)
self.assertTrue(a5)
self.assertTrue(a6)
self.assertTrue(a6)
14 changes: 7 additions & 7 deletions backend_candidate/tests/ranking_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ def test_ordering_by_completeness_of_12_naranja(self):
self.assertEquals(self.candidate2, ordered_candidates[2])
self.assertEquals(ordered_candidates[0].possible_answers,
Topic.objects.filter(category__in=self.candidate4.election.categories.all()).distinct().count())

self.assertEquals(ordered_candidates[0].num_answers, 3)
self.assertEquals(ordered_candidates[0].naranja_completeness, float(100))
self.assertEquals(ordered_candidates[1].naranja_completeness, (float(2) / float(3)) * 100)
self.assertEquals(ordered_candidates[2].naranja_completeness, (float(1) / float(3)) * 100)
self.assertAlmostEqual(ordered_candidates[1].naranja_completeness, (float(2) / float(3)) * 100)
self.assertAlmostEqual(ordered_candidates[2].naranja_completeness, (float(1) / float(3)) * 100)

def test_ordering_according_to_commitment(self):
# Candidate 4 has commited with all proposals
Expand Down Expand Up @@ -129,13 +129,13 @@ def test_ordering_according_to_commitment(self):
commited=False)
ordered_candidates = Candidate.ranking.all()
self.assertEquals(ordered_candidates.count(), Candidate.objects.all().count())

print ordered_candidates[1].elections.first().area.proposals.count()
self.assertEquals(ordered_candidates[1].num_proposals, 3)
self.assertEquals(ordered_candidates[1].num_commitments, 2)

self.assertEquals(ordered_candidates[0].commitmenness, float(100))
self.assertEquals(ordered_candidates[1].commitmenness, (float(2) / float(3)) * 100)
self.assertEquals(ordered_candidates[2].commitmenness, (float(1) / float(3)) * 100)
self.assertAlmostEqual(ordered_candidates[1].commitmenness, (float(2) / float(3)) * 100)
self.assertAlmostEqual(ordered_candidates[2].commitmenness, (float(1) / float(3)) * 100)

self.assertEquals(self.candidate4, ordered_candidates[0])
self.assertEquals(self.candidate3, ordered_candidates[1])
Expand Down Expand Up @@ -172,4 +172,4 @@ def test_get_position_in_ranking(self):

self.assertEquals(Candidate.ranking.position(self.candidate4), 1)
self.assertEquals(Candidate.ranking.position(self.candidate3), 2)
self.assertEquals(Candidate.ranking.position(self.candidate1), 3)
self.assertEquals(Candidate.ranking.position(self.candidate1), 3)
1 change: 0 additions & 1 deletion backend_staff/tests/mails_for_staff_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ def test_send_mails_to_staff_when_new_proposal(self):
the_mail = mail.outbox[0]
self.assertIn(self.staff1.email, the_mail.to)
self.assertIn(self.staff2.email, the_mail.to)
self.assertIn(str(self.proposal.id), the_mail.body)
self.assertIn(self.temporary_data.get_title(), the_mail.body)
self.assertIn(self.temporary_data.area.name, the_mail.subject)
5 changes: 2 additions & 3 deletions backend_staff/tests/views_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,9 @@ def test_stats_v2_organizations_supporting(self):
self.assertIn(org, stats.organizations.all())
self.assertIn(org2, stats.organizations.all())
self.assertNotIn(normal_user, stats.organizations.all())


def test_stats_mixin(self):
User.objects.all().delete()
stats = Stats()
self.assertTrue(Candidate.objects.count())
self.assertEquals(stats.total_candidates(), Candidate.objects.count())
Expand Down Expand Up @@ -382,7 +381,7 @@ def test_stats_mixin(self):

self.assertEquals(stats.participation().with_us, 2)
self.assertEquals(stats.participation().got_email, 2)
self.assertGreater(stats.participation().no_contact, 2)
self.assertGreater(stats.participation().no_contact, 1)

self.assertEquals(stats.participation_alcalde().with_us, 1)
self.assertEquals(stats.participation_alcalde().got_email, 1)
Expand Down
20 changes: 2 additions & 18 deletions elections/fixtures/mini_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@
fields: {user: 2, image: '', description: '', first_time_in_backend_citizen: true}
- fields: {description: '', extra_info_content: null, extra_info_title: null,
highlighted: true, name: "2a Circunscripcion Antofagasta",
candidates_can_commit_everywhere: false,
searchable: true, slug: 2a-circunscripcion-antofagasta, area: algarrobo-5602}
model: elections.election
pk: 1
- fields: {description: '', extra_info_content: null, extra_info_title: null,
candidates_can_commit_everywhere: false,
highlighted: true, name: "4a Circunscripcion Coquimbo",
searchable: true, slug: 4a-circunscripcion-coquimbo, area: algarrobo-5602}
model: elections.election
Expand Down Expand Up @@ -200,24 +202,6 @@
- fields: {person_ptr: 6, name: Daniel Guevara, elections: [1], extra_info: {}}
model: elections.candidate
pk: 6
- fields: {person_ptr: 7, name: Jimena Orrego}
model: elections.candidate
pk: 7
- fields: {person_ptr: 8, name: Gonzalo Uriarte}
model: elections.candidate
pk: 8
- fields: {person_ptr: 9, name: Mario Bertolino}
model: elections.candidate
pk: 9
- fields: {person_ptr: 10, name: Jorge Pizarro}
model: elections.candidate
pk: 10
- fields: {person_ptr: 11, name: "Adriana Munoz"}
model: elections.candidate
pk: 11
- fields: {person_ptr: 12, name: Luis Vega}
model: elections.candidate
pk: 12
- fields: {topic: 4, position: 8, person: 1}
model: candidator.takenposition
pk: 1
Expand Down
45 changes: 36 additions & 9 deletions elections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
from django.contrib.flatpages.models import FlatPage
import copy
from votainteligente.open_graph import OGPMixin
from django.db.models import Count, F, FloatField, ExpressionWrapper
from django.db.models import Count, F, FloatField, IntegerField, ExpressionWrapper, Case, Value, When
from django.shortcuts import render
from constance import config
from django.db.models import Case, Value, When, PositiveSmallIntegerField


class AreaManager(models.Manager):
def get_queryset(self):
Expand Down Expand Up @@ -65,7 +67,12 @@ def __init__(self, *args, **kwargs):
class HaveAnsweredFirst(models.Manager):
def get_queryset(self):
qs = super(HaveAnsweredFirst, self).get_queryset().annotate(num_answers=Count('taken_positions'))
qs = qs.order_by('-num_answers', '-image')
qs = qs.annotate(
has_image_0_1=Case(When(image__isnull=True, then=Value(0)),
default=Value(1),
output_field=PositiveSmallIntegerField())
)
qs = qs.order_by('-num_answers', '-has_image_0_1')
return qs

class WinnersFirst(models.Manager):
Expand All @@ -79,14 +86,34 @@ def get_queryset(self):
qs = super(RankingManager, self).get_queryset()
qs = qs.annotate(possible_answers=Count(F('elections__categories__topics'), distinct=True))
qs = qs.annotate(num_answers=Count('taken_positions', distinct=True))
qs = qs.annotate(naranja_completeness=ExpressionWrapper((F('num_answers') * 1.0 / F('possible_answers') * 1.0) * 100,
output_field=FloatField()))

qs = qs.annotate(num_proposals=Count(F('elections__area__proposals'), distinct=True))
answers_then = (F('num_answers') * 1.0 / F('possible_answers') * 1.0) * 100
qs = qs.annotate(naranja_completeness=Case(
When(
possible_answers__gt=0, then=answers_then
),
default=0.0,
output_field=FloatField()
))
qs = qs.annotate(num_proposals=
Case(
When(elections__isnull=True, then=1.0),
When(elections__candidates_can_commit_everywhere=False, then=Count(F('elections__area__proposals'),distinct=True)),
When(elections__candidates_can_commit_everywhere=True, then=1.0),
output_field=FloatField(),
distinct=True,
)
)
for q in qs:
print q, q.elections.all()
# print q.elections.first().candidates_can_commit_everywhere, q.num_proposals
qs = qs.annotate(num_commitments=Count(F('commitments'), distinct=True))
qs = qs.annotate(commitmenness=ExpressionWrapper((F('num_commitments') * 1.0 / F('num_proposals') * 1.0) * 100,
output_field=FloatField()))

first_then = (F('num_commitments') * 1.0 / F('num_proposals') * 1.0) * 100
second_then = F('num_commitments') * 1.0
qs = qs.annotate(commitmenness=Case(When(num_proposals__gt=0.0, then=first_then),
When(num_proposals=0.0, then=second_then),
output_field=FloatField()
)
)
# This can be a bit tricky
# and it is the sum of the percentage of completeness of 1/2 naranja and the commitmenness
qs = qs.annotate(participation_index=ExpressionWrapper(F('naranja_completeness') + F('commitmenness'),
Expand Down
30 changes: 29 additions & 1 deletion elections/tests/version2/elections_per_area_tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
from elections.tests import VotaInteligenteTestCase as TestCase
from elections.models import Election, Area, Candidate
from elections.models import Election, Area, Candidate, QuestionCategory
from django.core.urlresolvers import reverse
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.contrib.sites.models import Site
Expand Down Expand Up @@ -103,6 +103,34 @@ def test_get_position_in_ranking(self):
self.assertEquals(position_c1, election.position_in_ranking(c1))
self.assertEquals(position_c2, election.position_in_ranking(c2))

def test_get_position_no_questions(self):
QuestionCategory.objects.all().delete()
argentina = Area.objects.create(name=u'Argentina')

election = Election.objects.create(
name='the name',
slug='the-slug',
description='this is a description',
extra_info_title=u'ver más',
area=argentina,
extra_info_content=u'Más Información')
c1 = Candidate.objects.create(name='Candidate1')
c2 = Candidate.objects.create(name='Candidate2')
c3 = Candidate.objects.create(name='Candidate3')
election.candidates.add(c1)
election.candidates.add(c2)
self.assertIsNotNone(election.position_in_ranking(c1))
self.assertIsNotNone(election.position_in_ranking(c2))
self.assertIsNone(election.position_in_ranking(c3))

position_c1 = c1.ranking_in_election()
position_c2 = c2.ranking_in_election()
position_c3 = c3.ranking_in_election()

self.assertIsNone(position_c3)
self.assertEquals(position_c1, election.position_in_ranking(c1))
self.assertEquals(position_c2, election.position_in_ranking(c2))

class AreaOGPTestCase(TestCase):
def setUp(self):
self.argentina = Area.objects.create(name=u'Argentina')
Expand Down
11 changes: 7 additions & 4 deletions popular_proposal/tests/proposal_search_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def setUp(self):
)
for key, opts in haystack.connections.connections_info.items():
haystack.connections.reload(key)
call_command('update_index', interactive=False, verbosity=0)
call_command('rebuild_index', interactive=False, verbosity=0)

def tearDown(self):
call_command('clear_index', interactive=False, verbosity=0)

def test_index_template(self):
r = SearchQuerySet().all()
Expand All @@ -44,7 +47,7 @@ def test_index_template(self):
def test_index_getProposals(self):
r = SearchQuerySet().models(PopularProposal).auto_query("bicicletas")
self.assertEquals(r[0].object, self.p2)

def test_what_texts_it_searchs(self):
data_with_texts = {'title': u'Que solucionen los problemas',
'terms_and_conditions': True,
Expand All @@ -67,7 +70,7 @@ def test_what_texts_it_searchs(self):
self.assertIn(data_with_texts['solution'], p1.data_as_text)
self.assertIn(data_with_texts['problem'], p1.data_as_text)
self.assertIn(data_with_texts['causes'], p1.data_as_text)

self.assertIn(data_with_texts['solution'], indexed_proposal)
self.assertIn(data_with_texts['problem'], indexed_proposal)
self.assertIn(data_with_texts['causes'], indexed_proposal)
self.assertIn(data_with_texts['causes'], indexed_proposal)
8 changes: 4 additions & 4 deletions popular_proposal/tests/subscription_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,17 @@ def test_like_a_proposal_signal(self):
self.assertIn(str(2), the_mail.body)
# this should be the email to the candidate
the_mail = mail.outbox[1]
self.assertIn(self.contact.mail, the_mail.to)
self.assertIn(the_mail.to[0], [self.contact.mail, self.contact2.mail])
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertIn(self.candidate.name, the_mail.body)
self.assertTrue(self.candidate.name in the_mail.body or self.candidate2.name in the_mail.body)
self.assertIn(str(2), the_mail.body)
# this should be the email to the other candidate
the_mail = mail.outbox[2]
self.assertIn(self.contact2.mail, the_mail.to)
self.assertIn(the_mail.to[0], [self.contact.mail, self.contact2.mail])
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertIn(self.candidate2.name, the_mail.body)
self.assertTrue(self.candidate.name in the_mail.body or self.candidate2.name in the_mail.body)
self.assertIn(str(2), the_mail.body)

def test_proposal_notification_for_candidates(self):
Expand Down
8 changes: 7 additions & 1 deletion preguntales/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.utils import timezone
from popolo.models import Person
from votainteligente.send_mails import send_mail
from django.db.models import Case, Value, When, PositiveSmallIntegerField


class MessageManagerBase(models.Manager):
Expand All @@ -23,7 +24,12 @@ def needing_moderation_messages(self):
class MessageManager(MessageManagerBase):
def get_queryset(self):
queryset = super(MessageManager, self).get_queryset().annotate(num_answers=Count('answers'))
return queryset.order_by('-num_answers', '-status__accepted', '-created')
queryset = queryset.annotate(
accepted_0_1=Case(When(status__accepted=True, then=Value(1)),
default=Value(0),
output_field=PositiveSmallIntegerField())
)
return queryset.order_by('-num_answers', '-accepted_0_1', '-created')


def uuid_with_no_dashes():
Expand Down
27 changes: 27 additions & 0 deletions votainteligente/testing_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
## TESTING ENVIRONMENT
LOCAL_ELASTICSEARCH = True
CELERY_ALWAYS_EAGER = True
TRAVIS = os.environ.get('TRAVIS')


if TRAVIS:
DB = os.environ.get('DB')

if DB == 'postgres':
default_db = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'travis_ci_test',
# The following settings are not used with sqlite3:
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': '',
}
else:
default_db = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'writeit.db', # Or path to database file if using sqlite3.
}

DATABASES = {'default': default_db}
3 changes: 3 additions & 0 deletions votainteligente/votainteligente_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,6 @@
from local_settings import *
except ImportError, e:
pass

if TESTING:
from testing_settings import *

0 comments on commit 0b62270

Please sign in to comment.