Commit 3a1c03ba90b2ec3549f8fc0c581e26e45aa8eb85
Exists in
master
and in
4 other branches
Merge pull request #121 from colab/test_search
Testing base_indexes class
Showing
2 changed files
with
107 additions
and
7 deletions
Show diff stats
colab/search/base_indexes.py
| @@ -76,13 +76,7 @@ class BaseIndex(indexes.SearchIndex): | @@ -76,13 +76,7 @@ class BaseIndex(indexes.SearchIndex): | ||
| 76 | ) | 76 | ) |
| 77 | 77 | ||
| 78 | def prepare_modified_by(self, obj): | 78 | def prepare_modified_by(self, obj): |
| 79 | - if hasattr(obj, 'modified_by'): | ||
| 80 | - modified_by = obj.get_modified_by() | ||
| 81 | - if modified_by: | ||
| 82 | - return modified_by.get_full_name() | ||
| 83 | - if self.author_obj: | ||
| 84 | - return self.author_obj.get_full_name() | ||
| 85 | - return obj.author | 79 | + return self.prepare_fullname(obj) |
| 86 | 80 | ||
| 87 | def prepare_modified_by_url(self, obj): | 81 | def prepare_modified_by_url(self, obj): |
| 88 | if hasattr(obj, 'modified_by'): | 82 | if hasattr(obj, 'modified_by'): |
| @@ -0,0 +1,106 @@ | @@ -0,0 +1,106 @@ | ||
| 1 | +# -*- coding:utf-8 -*- | ||
| 2 | + | ||
| 3 | +import math | ||
| 4 | +from mock import Mock | ||
| 5 | + | ||
| 6 | +from django.test import TestCase, Client | ||
| 7 | +from colab.search.base_indexes import BaseIndex | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +class SearchViewTest(TestCase): | ||
| 11 | + | ||
| 12 | + def setUp(self): | ||
| 13 | + self.client = Client() | ||
| 14 | + | ||
| 15 | + def tearDown(self): | ||
| 16 | + pass | ||
| 17 | + | ||
| 18 | + def test_get_updated_field(self): | ||
| 19 | + base_index = BaseIndex() | ||
| 20 | + | ||
| 21 | + self.assertEquals('modified', base_index.get_updated_field()) | ||
| 22 | + | ||
| 23 | + def test_get_boost(self): | ||
| 24 | + obj = Mock(hits=10) | ||
| 25 | + base_index = BaseIndex() | ||
| 26 | + | ||
| 27 | + self.assertEquals(1, base_index.get_boost(obj)) | ||
| 28 | + | ||
| 29 | + obj = Mock(hits=11) | ||
| 30 | + self.assertEquals(math.log(11), base_index.get_boost(obj)) | ||
| 31 | + | ||
| 32 | + def test_prepare_author(self): | ||
| 33 | + obj = Mock(author="author") | ||
| 34 | + base_index = BaseIndex() | ||
| 35 | + setattr(base_index, 'author_obj', None) | ||
| 36 | + | ||
| 37 | + self.assertEquals("author", base_index.prepare_author(obj)) | ||
| 38 | + | ||
| 39 | + base_index.author_obj = Mock(username="carlin") | ||
| 40 | + self.assertEquals("carlin", base_index.prepare_author(obj)) | ||
| 41 | + | ||
| 42 | + def test_prepare_author_url(self): | ||
| 43 | + base_index = BaseIndex() | ||
| 44 | + setattr(base_index, 'author_obj', None) | ||
| 45 | + | ||
| 46 | + self.assertEquals(None, base_index.prepare_author_url(None)) | ||
| 47 | + | ||
| 48 | + base_index.author_obj = Mock(get_absolute_url=lambda: "url_test") | ||
| 49 | + self.assertEquals("url_test", base_index.prepare_author_url(None)) | ||
| 50 | + | ||
| 51 | + class AuthorMockObject: | ||
| 52 | + author = "author" | ||
| 53 | + | ||
| 54 | + def test_prepare_modified_by(self): | ||
| 55 | + base_index = BaseIndex() | ||
| 56 | + setattr(base_index, 'author_obj', None) | ||
| 57 | + obj = self.AuthorMockObject() | ||
| 58 | + | ||
| 59 | + self.assertEquals("author", base_index.prepare_modified_by(obj)) | ||
| 60 | + | ||
| 61 | + base_index.author_obj = Mock(get_full_name=lambda: "full_name") | ||
| 62 | + self.assertEquals("full_name", base_index.prepare_modified_by(obj)) | ||
| 63 | + | ||
| 64 | + mock_modified_by = Mock(get_full_name=lambda: "full_name") | ||
| 65 | + obj = Mock(modified_by="somebody", | ||
| 66 | + get_modified_by=lambda: mock_modified_by) | ||
| 67 | + | ||
| 68 | + self.assertEquals("full_name", base_index.prepare_modified_by(obj)) | ||
| 69 | + | ||
| 70 | + def test_prepare_fullname_and_username(self): | ||
| 71 | + base_index = BaseIndex() | ||
| 72 | + setattr(base_index, 'author_obj', Mock(username="user", | ||
| 73 | + get_full_name=lambda: "name")) | ||
| 74 | + | ||
| 75 | + expected = "{}\n{}".format("name", "user") | ||
| 76 | + self.assertEquals(expected, | ||
| 77 | + base_index.prepare_fullname_and_username(None)) | ||
| 78 | + | ||
| 79 | + base_index.author_obj = None | ||
| 80 | + obj = self.AuthorMockObject() | ||
| 81 | + self.assertEquals("author", | ||
| 82 | + base_index.prepare_fullname_and_username(obj)) | ||
| 83 | + | ||
| 84 | + mock_modified_by = Mock(get_full_name=lambda: "full_name", | ||
| 85 | + username="user") | ||
| 86 | + obj = Mock(modified_by="somebody", | ||
| 87 | + get_modified_by=lambda: mock_modified_by) | ||
| 88 | + | ||
| 89 | + expected = "{}\n{}".format("full_name", "user") | ||
| 90 | + self.assertEquals(expected, | ||
| 91 | + base_index.prepare_fullname_and_username(obj)) | ||
| 92 | + | ||
| 93 | + def test_prepare_modified_by_url(self): | ||
| 94 | + base_index = BaseIndex() | ||
| 95 | + setattr(base_index, 'author_obj', None) | ||
| 96 | + | ||
| 97 | + self.assertEquals(None, base_index.prepare_modified_by_url(None)) | ||
| 98 | + | ||
| 99 | + base_index.author_obj = Mock(get_absolute_url=lambda: "urlurl") | ||
| 100 | + self.assertEquals("urlurl", base_index.prepare_modified_by_url(None)) | ||
| 101 | + | ||
| 102 | + mock_modified_by = Mock(get_absolute_url=lambda: "urlurl") | ||
| 103 | + obj = Mock(modified_by="somebody", | ||
| 104 | + get_modified_by=lambda: mock_modified_by) | ||
| 105 | + | ||
| 106 | + self.assertEquals("urlurl", base_index.prepare_modified_by_url(obj)) |