Skip to content

Commit

Permalink
Tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
saschwarz committed Aug 10, 2014
1 parent 918d3b4 commit 25c20f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions gcse/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ def facet_item_labels(self):

def facet_item_labels_counts(self):
"""Return all the Labels for the FacetItems associated with this instance and the counts of Annotations associated with them."""
labels = self.all_labels() # self.facet_item_labels()
labels = self.facet_item_labels()
annotations = Annotation.objects.filter(Q(labels__in=labels) | Q(labels__in=self.background_labels.all())).values('labels__id').order_by().annotate(Count('labels__id'))
count_by_id = dict([(x['labels__id'], x['labels__id__count']) for x in annotations])
label_counts = [ (label, count_by_id.setdefault(label.id, 0)) for label in labels]
label_counts = [(label, count_by_id.setdefault(label.id, 0)) for label in labels]
return label_counts

def get_absolute_url(self):
Expand Down
35 changes: 17 additions & 18 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<!-- max of FOUR Facets each with at most FOUR FacetItems -->
<Facet>
<FacetItem title="Blogs">
<Label name="blogs" mode="BOOST"/>
<Label name="blog" mode="BOOST"/>
</FacetItem>
<FacetItem title="Clubs">
<Label name="club" mode="FILTER"/>
Expand Down Expand Up @@ -198,21 +198,21 @@ def test_output_xml_has_new_gid_when_no_changes_to_instance(self):
cse = CustomSearchEngine(gid="c12345-r999",
input_xml=CSE_XML)
cse.save()
self.assertEqual('c12345-r999',
self.assertEqual('c12345-r999',
_extractPath(cse.output_xml,
"/GoogleCustomizations/CustomSearchEngine")[0].attrib['id'])

self.assertEqual('', cse.title) # no title set so leave XML alone
self.assertEqual("AgilityNerd Site Search",
self.assertEqual("AgilityNerd Site Search",
_extractPathElementText(cse.output_xml, "/GoogleCustomizations/CustomSearchEngine/Title"))

def test_output_xml_has_new_title_when_title_is_changed(self):
cse = CustomSearchEngine(gid="c12345-r678",
title="""Here's a new title in need of escaping: &<>""",
input_xml=CSE_XML)
cse.save()
self.assertEqual(cse.title,
_extractPathElementText(cse.output_xml,
self.assertEqual(cse.title,
_extractPathElementText(cse.output_xml,
"/GoogleCustomizations/CustomSearchEngine/Title"))

def test_output_xml_has_new_title_element_when_there_is_no_title_element(self):
Expand All @@ -221,17 +221,17 @@ def test_output_xml_has_new_title_element_when_there_is_no_title_element(self):
title="""Here's a new title in need of escaping: &<>""",
input_xml=input_xml)
cse.save()
self.assertEqual(cse.title,
_extractPathElementText(cse.output_xml,
self.assertEqual(cse.title,
_extractPathElementText(cse.output_xml,
"/GoogleCustomizations/CustomSearchEngine/Title"))

def test_output_xml_has_new_description_when_description_is_changed(self):
cse = CustomSearchEngine(gid="c12345-r678",
description="""Here's a new description in need of escaping: &<>""",
input_xml=CSE_XML)
cse.save()
self.assertEqual(cse.description,
_extractPathElementText(cse.output_xml,
self.assertEqual(cse.description,
_extractPathElementText(cse.output_xml,
"/GoogleCustomizations/CustomSearchEngine/Description"))

def test_output_xml_has_new_description_element_when_there_is_no_description_element(self):
Expand All @@ -241,8 +241,8 @@ def test_output_xml_has_new_description_element_when_there_is_no_description_ele
input_xml=input_xml)
cse.save()

self.assertEqual(cse.description,
_extractPathElementText(cse.output_xml,
self.assertEqual(cse.description,
_extractPathElementText(cse.output_xml,
"/GoogleCustomizations/CustomSearchEngine/Description"))

def test_output_xml_has_new_title_and_description_when_neither_exist(self):
Expand Down Expand Up @@ -456,18 +456,17 @@ def test_labels_are_parsed_from_facets_in_xml(self):
self.cse, annotation_urls = self.handler.parseString(FACETED_XML)
cse = self.cse
self.assertEqual(12, cse.facetitem_set.count())
labels = cse.facetitems_labels()
self.assertEqual(12, len(cse.facet_item_labels_counts()))
labels = cse.facet_item_labels()
label_names = set([x.name for x in labels])
self.assertEqual(set(["blogs", "club", "equipment", "forum", "general", "organization", "service", "store", "training", "facility", "video", "rental"]),
self.assertEqual(set(["blog", "club", "equipment", "forum", "general", "organization", "service", "store", "training", "facility", "video", "rental"]),
label_names)

label_modes = [x.mode for x in labels]
self.assertTrue(Label.MODE.filter in label_modes)
self.assertFalse(Label.MODE.eliminate in label_modes)
self.assertTrue(Label.MODE.boost in label_modes)

self.assertEqual(1, len([x for x in labels if x.weight==0.8]))

self.assertEqual(set(["_cse_csekeystring", "_cse_exclude_csekeystring"]),
set([x.name for x in cse.background_labels.all()]))

Expand Down Expand Up @@ -736,16 +735,16 @@ def setUp(self):
self.annotation = active

def test_labels_as_links_shows_all_labels(self):
self.assertEqual('<a class="label-link" href="/site/label/Background%20Label">Background Label</a><a class="label-link" href="/site/label/Label%20%26%20Name">Label & Name</a>',
self.assertEqual('<a class="label-link" href="/labels/2/">Background Label</a><a class="label-link" href="/labels/1/">Label & Name</a>',
self.annotation.labels_as_links())

def test_labels_as_links_hides_background_labels(self):
self.assertEqual('<a class="label-link" href="/site/label/Label%20%26%20Name">Label & Name</a>',
self.assertEqual('<a class="label-link" href="/labels/1/">Label & Name</a>',
self.annotation.labels_as_links(include_background_labels=False))


class AnnotationsLabels(TestCase):

def test_guess_google_url_for_a_single_page(self):
self.assertEqual('example.com/foo.html',
Annotation.guess_google_url("http://example.com/foo.html"))
Expand Down

0 comments on commit 25c20f8

Please sign in to comment.