Skip to content

Commit

Permalink
Merge pull request #30 from Villanueva-del-Trabuco-EGC/exportacion-ce…
Browse files Browse the repository at this point in the history
…nso/dieruigil

Test unitario y manejo de errores
  • Loading branch information
Framigdom authored Dec 3, 2022
2 parents 1f13269 + 0cb820d commit 7f2ee33
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
3 changes: 1 addition & 2 deletions decide/census/census_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_user_atributes():

return atributes_list

# csvtext -> Header1,Header2,Headder3/value1,value2,value3/value1,value2,value3/
# csvtext -> Header1,Header2,Header3/value1,value2,value3/value1,value2,value3/
def get_csvtext_and_data(form_values, census):
atributes_list = get_user_atributes()
voters_data = []
Expand All @@ -30,7 +30,6 @@ def get_csvtext_and_data(form_values, census):
census_text += ','
else:
census_text += '/'


# CSV values
for c in census:
Expand Down
20 changes: 20 additions & 0 deletions decide/census/templates/errors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends "base.html" %}
{% load i18n static %}
{% load index %}

{% block extrahead %}
<link type="text/css" rel="stylesheet"
href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet"
href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" />
{% endblock %}

{% block content %}
<div class="row justify-content-center pt-3">
<div class="col-md-5">
<div class="alert alert-danger text-center" role="alert">
<h2>{{message}}.</h2> <a href="/booth/all" class="alert-link">Go back to the voting list</a>.
</div>
</div>
</div>
{% endblock %}
33 changes: 33 additions & 0 deletions decide/census/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .models import Census
from base import mods
from base.tests import BaseTestCase
from census import census_utils as censusUtils


class CensusTestCase(BaseTestCase):
Expand Down Expand Up @@ -73,3 +74,35 @@ def test_destroy_voter(self):
response = self.client.delete('/census/{}/'.format(1), data, format='json')
self.assertEqual(response.status_code, 204)
self.assertEqual(0, Census.objects.count())

class ExportCensusTestCase(BaseTestCase):

def setUp(self):
super().setUp()
self.census = Census(voting_id=1, voter_id=12)
self.census.save()

def tearDown(self):
super().tearDown()
self.census = None

def test_census_utils(self):
form_values = ['2','4','5']
selected_atributes = ['id']
voters_data = []

census = Census.objects.filter(voting_id=1).values()
user_atributes = censusUtils.get_user_atributes()
data = censusUtils.get_csvtext_and_data(form_values, census)

for value in form_values:
atribute = str(user_atributes[int(value)][1])
selected_atributes.append(atribute)
self.assertEquals(selected_atributes, data[1]) # headers

voter = User.objects.filter(id=12).values()[0]
voter_data = []
for atribute in selected_atributes:
voter_data.append(str(voter[atribute]))
voters_data.append(voter_data)
self.assertEquals(voters_data, data[2]) # atribures values
11 changes: 9 additions & 2 deletions decide/census/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
HTTP_204_NO_CONTENT as ST_204,
HTTP_400_BAD_REQUEST as ST_400,
HTTP_401_UNAUTHORIZED as ST_401,
HTTP_404_NOT_FOUND as ST_404,
HTTP_409_CONFLICT as ST_409
)
from rest_framework.views import APIView
Expand Down Expand Up @@ -112,10 +113,16 @@ def add_census(voters_pk, voting_pk):

add_census(voters_pk, votation)
return Response({'Votación poblada satisfactoriamente, '+ str(len(voters_pk))+ ' votantes añadidos' }, status=ST_201)



@staff_member_required(login_url='/admin/login')
def export_census(request, voting_id):
if list(Voting.objects.filter(id=voting_id).values())== []:
template = loader.get_template('errors.html')
context = {
'message':'404 Voting not found'
}
return HttpResponse(template.render(context, request), status=404)

template = loader.get_template('export_census.html')
formulario = AtributosUser()
voting = Voting.objects.filter(id=voting_id).values()[0]
Expand Down

0 comments on commit 7f2ee33

Please sign in to comment.