Skip to content

Commit

Permalink
Merge f183e84 into e244557
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher DeCairos committed Oct 16, 2018
2 parents e244557 + f183e84 commit f993740
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 44 deletions.
52 changes: 27 additions & 25 deletions network-api/networkapi/buyersguide/models.py
@@ -1,4 +1,5 @@
import re
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
from django.forms import model_to_dict
Expand Down Expand Up @@ -291,31 +292,32 @@ class Product(models.Model):

@property
def votes(self):
votes = []
for range_product_vote in self.range_product_votes.all():
breakdown = {
'attribute': 'creepiness',
'average': range_product_vote.average,
'vote_breakdown': {}
}

for vote_breakdown in range_product_vote.rangevotebreakdown_set.all():
breakdown['vote_breakdown'][str(vote_breakdown.bucket)] = vote_breakdown.count

votes.append(breakdown)

for boolean_product_vote in self.boolean_product_votes.all():
breakdown = {
'attribute': 'confidence',
'vote_breakdown': {}
}

for vote_breakdown in boolean_product_vote.booleanvotebreakdown_set.all():
breakdown['vote_breakdown'][str(vote_breakdown.bucket)] = vote_breakdown.count

votes.append(breakdown)

return votes
votes = {}
confidence_vote_breakdown = {}
creepiness = {'vote_breakdown': {}}

try:
# Get vote QuerySets
creepiness_votes = self.range_product_votes.get(attribute='creepiness')
confidence_votes = self.boolean_product_votes.get(attribute='confidence')

# Aggregate the Creepiness votes
creepiness['average'] = creepiness_votes.average
for vote_breakdown in creepiness_votes.rangevotebreakdown_set.all():
creepiness['vote_breakdown'][str(vote_breakdown.bucket)] = vote_breakdown.count

# Aggregate the confidence votes
for boolean_vote_breakdown in confidence_votes.booleanvotebreakdown_set.all():
confidence_vote_breakdown[str(boolean_vote_breakdown.bucket)] = boolean_vote_breakdown.count

# Build + return the votes dict
votes['creepiness'] = creepiness
votes['confidence'] = confidence_vote_breakdown
return votes

except ObjectDoesNotExist:
# There's no aggregate data available yet, return None
return None

def to_dict(self):
model_dict = model_to_dict(self)
Expand Down
67 changes: 48 additions & 19 deletions network-api/networkapi/buyersguide/tests.py
Expand Up @@ -14,9 +14,42 @@

class ManagementCommandTest(APITestCase):

def test_aggregate_product_votes_range(self):
def test_votes_before_management_command_has_run(self):
"""
Test that aggregate_product_votes properly aggregates range votes
Test that the votes attribute is None when there is no aggregated vote data for it
"""

product = ProductFactory.create()
self.assertIsNone(product.votes)

def test_aggregate_product_votes_default(self):
"""
Test that aggregate_product_votes provides default vote data for a product with no votes
"""
product = ProductFactory.create()

call_command('aggregate_product_votes')

self.assertDictEqual(product.votes, {
'creepiness': {
'average': 50,
'vote_breakdown': {
'0': 0,
'1': 0,
'2': 0,
'3': 0,
'4': 0
}
},
'confidence': {
'0': 0,
'1': 0
}
})

def test_aggregate_product_votes(self):
"""
Test that aggregate_product_votes properly aggregates votes
"""

product = ProductFactory.create()
Expand All @@ -26,8 +59,6 @@ def test_aggregate_product_votes_range(self):
'productID': test_product_id
}

self.assertListEqual(product.votes, [])

# Make 10 creepiness votes
for i in (1, 10, 20, 30, 40, 50, 60, 70, 80, 90):
request_data['value'] = i
Expand All @@ -42,24 +73,22 @@ def test_aggregate_product_votes_range(self):
self.assertEqual(response.status_code, 201)

call_command('aggregate_product_votes')

self.assertListEqual(product.votes, [{
'attribute': 'creepiness',
'average': 45,
'vote_breakdown': {
'0': 3,
'1': 2,
'2': 2,
'3': 2,
'4': 1
}
}, {
'attribute': 'confidence',
'vote_breakdown': {
self.assertDictEqual(product.votes, {
'creepiness': {
'average': 45,
'vote_breakdown': {
'0': 3,
'1': 2,
'2': 2,
'3': 2,
'4': 1
}
},
'confidence': {
'0': 5,
'1': 5
}
}])
})


class BuyersGuideVoteTest(APITestCase):
Expand Down

0 comments on commit f993740

Please sign in to comment.