{"action":"created","comment":{"url":"https://api.github.com/repos/edx-solutions/edx-search/pulls/comments/22880739","id":22880739,"diff_hunk":"@@ -0,0 +1,205 @@\n+\"\"\" Implementation of search interface to be used for tests where ElasticSearch is unavailable \"\"\"\n+import copy\n+import datetime\n+from numbers import Number\n+from search.manager import SearchEngine\n+\n+\n+def _convert_to_date(json_date_string_value):\n+    ''' converts json date string to date object '''\n+    if json_date_string_value is None:\n+        return None\n+\n+    if json_date_string_value == \"now\":\n+        return datetime.datetime.utcnow()\n+\n+    try:\n+        if \"T\" in json_date_string_value:\n+            if \".\" in json_date_string_value:\n+                format_string = \"%Y-%m-%dT%H:%M:%S.%fZ\"\n+            else:\n+                format_string = \"%Y-%m-%dT%H:%M:%SZ\"\n+        else:\n+            format_string = \"%Y-%m-%d\"\n+\n+        return datetime.datetime.strptime(\n+            json_date_string_value,\n+            format_string\n+        )\n+    except ValueError:\n+        return None\n+\n+\n+def _null_conversion(value):\n+    \"\"\" no-op function, just returns what you give it \"\"\"\n+    return value\n+\n+\n+def contains_numbers(array_of_values):\n+    \"\"\" True if anything value in the array is a number \"\"\"\n+    for test_value in array_of_values:\n+        if isinstance(test_value, Number):\n+            return True\n+    return False\n+\n+\n+def find_field(doc, field_name):\n+    \"\"\" find the dictionary field corresponding to the . limited name \"\"\"\n+    field_chain = field_name.split('.', 1)\n+    if len(field_chain) > 1:\n+        return find_field(doc[field_chain[0]], field_chain[1]) if field_chain[0] in doc else None\n+    else:\n+        return doc[field_chain[0]] if field_chain[0] in doc else None\n+\n+\n+def _filter_field_dictionary(documents_to_search, field_dictionary):\n+    \"\"\" remove the documents for which the fields do not match \"\"\"\n+    filtered_documents = documents_to_search\n+    for field_name in field_dictionary:\n+        field_value = field_dictionary[field_name]\n+        if isinstance(field_value, list) and len(field_value) == 2:\n+            fn_conv = _null_conversion if contains_numbers(field_value) else _convert_to_date\n+            if field_value[0]:\n+                filtered_documents = [d for d in filtered_documents if fn_conv(\n+                    find_field(d, field_name)) >= fn_conv(field_value[0])]\n+            if field_value[1]:\n+                filtered_documents = [d for d in filtered_documents if fn_conv(\n+                    find_field(d, field_name)) <= fn_conv(field_value[1])]\n+        else:\n+            filtered_documents = [d for d in filtered_documents if find_field(d, field_name) == field_value]\n+\n+    return filtered_documents\n+\n+\n+def _filter_filter_dictionary(documents_to_search, filter_dictionary):\n+    \"\"\" remove the documents for which the fields do not match iff the field is present \"\"\"\n+    filtered_documents = documents_to_search\n+    for field_name in filter_dictionary:\n+        field_value = filter_dictionary[field_name]\n+        if isinstance(field_value, list) and len(field_value) == 2:\n+            fn_conv = _null_conversion if contains_numbers(field_value) else _convert_to_date\n+            if field_value[0]:\n+                filtered_documents = [d for d in filtered_documents if (\n+                    find_field(d, field_name) is None or\n+                    fn_conv(find_field(d, field_name)) >= fn_conv(field_value[0])\n+                )]\n+            if field_value[1]:\n+                filtered_documents = [d for d in filtered_documents if (\n+                    find_field(d, field_name) is None or\n+                    fn_conv(find_field(d, field_name)) <= fn_conv(field_value[1])\n+                )]\n+        else:\n+            filtered_documents = [d for d in filtered_documents if (\n+                find_field(d, field_name) == field_value or\n+                find_field(d, field_name) is None\n+            )]\n+\n+    return filtered_documents\n+\n+\n+def _process_query_string(documents_to_search, search_strings):\n+    \"\"\" keep the documents that contain at least one of the search strings provided \"\"\"\n+    def has_string(dictionary_object, search_string):\n+        \"\"\" search for string in dictionary items, look down into nested dictionaries \"\"\"\n+        for name in dictionary_object:\n+            if isinstance(dictionary_object[name], dict):\n+                return has_string(dictionary_object[name], search_string)\n+            elif search_string in dictionary_object[name]:\n+                return True\n+        return False\n+\n+    documents_to_keep = []\n+    for search_string in search_strings:\n+        documents_to_keep.extend([d for d in documents_to_search if has_string(d[\"content\"], search_string)])\n+\n+    return documents_to_keep\n+\n+\n+class MockSearchEngine(SearchEngine):\n+\n+    \"\"\"\n+    Mock implementation of SearchEngine for test purposes\n+    \"\"\"\n+    _mock_elastic = {}\n+\n+    @staticmethod\n+    def _paginate_results(size, from_, raw_results):\n+        \"\"\" Give the correct page of results \"\"\"\n+        results = raw_results\n+        if size:\n+            start = 0\n+            if from_ is not None:\n+                start = from_\n+            results = raw_results[start:start + size]\n+\n+        return results\n+\n+    @classmethod\n+    def destroy(cls):\n+        \"\"\" Clean out the dictionary for test resets \"\"\"\n+        cls._mock_elastic = {}\n+\n+    def __init__(self, index=None):\n+        super(MockSearchEngine, self).__init__(index)\n+\n+    def index(self, doc_type, body, **kwargs):\n+        if self.index_name not in MockSearchEngine._mock_elastic:\n+            MockSearchEngine._mock_elastic[self.index_name] = {}\n+\n+        _mock_index = MockSearchEngine._mock_elastic[self.index_name]\n+        if doc_type not in _mock_index:\n+            _mock_index[doc_type] = []\n+\n+        _mock_index[doc_type].append(body)\n+\n+    def remove(self, doc_type, doc_id, **kwargs):\n+        _mock_index = MockSearchEngine._mock_elastic[self.index_name]\n+        _mock_index[doc_type] = [d for d in _mock_index[doc_type] if \"id\" not in d or d[\"id\"] != doc_id]","path":"search/tests/mock_search_engine.py","position":129,"original_position":157,"commit_id":"2be4557ab0ff6344c5d9393493460fa678bc699c","original_commit_id":"d3dc36b96ac0607136413356257d5099567f2d67","user":{"login":"ormsbee","id":41625,"avatar_url":"https://avatars.githubusercontent.com/u/41625?v=3","gravatar_id":"","url":"https://api.github.com/users/ormsbee","html_url":"https://github.com/ormsbee","followers_url":"https://api.github.com/users/ormsbee/followers","following_url":"https://api.github.com/users/ormsbee/following{/other_user}","gists_url":"https://api.github.com/users/ormsbee/gists{/gist_id}","starred_url":"https://api.github.com/users/ormsbee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ormsbee/subscriptions","organizations_url":"https://api.github.com/users/ormsbee/orgs","repos_url":"https://api.github.com/users/ormsbee/repos","events_url":"https://api.github.com/users/ormsbee/events{/privacy}","received_events_url":"https://api.github.com/users/ormsbee/received_events","type":"User","site_admin":false},"body":"What does this mean?","created_at":"2015-01-13T18:03:04Z","updated_at":"2015-01-13T18:03:04Z","html_url":"https://github.com/edx-solutions/edx-search/pull/1#discussion_r22880739","pull_request_url":"https://api.github.com/repos/edx-solutions/edx-search/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/edx-solutions/edx-search/pulls/comments/22880739"},"html":{"href":"https://github.com/edx-solutions/edx-search/pull/1#discussion_r22880739"},"pull_request":{"href":"https://api.github.com/repos/edx-solutions/edx-search/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/edx-solutions/edx-search/pulls/1","id":26988112,"html_url":"https://github.com/edx-solutions/edx-search/pull/1","diff_url":"https://github.com/edx-solutions/edx-search/pull/1.diff","patch_url":"https://github.com/edx-solutions/edx-search/pull/1.patch","issue_url":"https://api.github.com/repos/edx-solutions/edx-search/issues/1","number":1,"state":"open","locked":false,"title":"Individual search app for courseware search","user":{"login":"martynjames","id":675165,"avatar_url":"https://avatars.githubusercontent.com/u/675165?v=3","gravatar_id":"","url":"https://api.github.com/users/martynjames","html_url":"https://github.com/martynjames","followers_url":"https://api.github.com/users/martynjames/followers","following_url":"https://api.github.com/users/martynjames/following{/other_user}","gists_url":"https://api.github.com/users/martynjames/gists{/gist_id}","starred_url":"https://api.github.com/users/martynjames/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martynjames/subscriptions","organizations_url":"https://api.github.com/users/martynjames/orgs","repos_url":"https://api.github.com/users/martynjames/repos","events_url":"https://api.github.com/users/martynjames/events{/privacy}","received_events_url":"https://api.github.com/users/martynjames/received_events","type":"User","site_admin":false},"body":"@chrisndodge - Please take a look when you can. This is the initial implementation of the search app.\r\n\r\nThings of note:\r\n* Calling application provides an (optional) override for class `SearchResultProcessor` in order to post-process results _it is intended that one would inherit from `SearchResultProcessor` and add in additional properties as well as provide a last-chance to remove a result from the list","created_at":"2015-01-07T21:12:33Z","updated_at":"2015-01-13T18:03:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f324e579e67e9ecaf6a052d2e9a1c5866da9ca29","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx-solutions/edx-search/pulls/1/commits","review_comments_url":"https://api.github.com/repos/edx-solutions/edx-search/pulls/1/comments","review_comment_url":"https://api.github.com/repos/edx-solutions/edx-search/pulls/comments/{number}","comments_url":"https://api.github.com/repos/edx-solutions/edx-search/issues/1/comments","statuses_url":"https://api.github.com/repos/edx-solutions/edx-search/statuses/2be4557ab0ff6344c5d9393493460fa678bc699c","head":{"label":"edx-solutions:mjames/initial_commit","ref":"mjames/initial_commit","sha":"2be4557ab0ff6344c5d9393493460fa678bc699c","user":{"login":"edx-solutions","id":6585054,"avatar_url":"https://avatars.githubusercontent.com/u/6585054?v=3","gravatar_id":"","url":"https://api.github.com/users/edx-solutions","html_url":"https://github.com/edx-solutions","followers_url":"https://api.github.com/users/edx-solutions/followers","following_url":"https://api.github.com/users/edx-solutions/following{/other_user}","gists_url":"https://api.github.com/users/edx-solutions/gists{/gist_id}","starred_url":"https://api.github.com/users/edx-solutions/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx-solutions/subscriptions","organizations_url":"https://api.github.com/users/edx-solutions/orgs","repos_url":"https://api.github.com/users/edx-solutions/repos","events_url":"https://api.github.com/users/edx-solutions/events{/privacy}","received_events_url":"https://api.github.com/users/edx-solutions/received_events","type":"Organization","site_admin":false},"repo":{"id":27890225,"name":"edx-search","full_name":"edx-solutions/edx-search","owner":{"login":"edx-solutions","id":6585054,"avatar_url":"https://avatars.githubusercontent.com/u/6585054?v=3","gravatar_id":"","url":"https://api.github.com/users/edx-solutions","html_url":"https://github.com/edx-solutions","followers_url":"https://api.github.com/users/edx-solutions/followers","following_url":"https://api.github.com/users/edx-solutions/following{/other_user}","gists_url":"https://api.github.com/users/edx-solutions/gists{/gist_id}","starred_url":"https://api.github.com/users/edx-solutions/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx-solutions/subscriptions","organizations_url":"https://api.github.com/users/edx-solutions/orgs","repos_url":"https://api.github.com/users/edx-solutions/repos","events_url":"https://api.github.com/users/edx-solutions/events{/privacy}","received_events_url":"https://api.github.com/users/edx-solutions/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx-solutions/edx-search","description":"Search/index programming interface for use within edx platform components","fork":false,"url":"https://api.github.com/repos/edx-solutions/edx-search","forks_url":"https://api.github.com/repos/edx-solutions/edx-search/forks","keys_url":"https://api.github.com/repos/edx-solutions/edx-search/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx-solutions/edx-search/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx-solutions/edx-search/teams","hooks_url":"https://api.github.com/repos/edx-solutions/edx-search/hooks","issue_events_url":"https://api.github.com/repos/edx-solutions/edx-search/issues/events{/number}","events_url":"https://api.github.com/repos/edx-solutions/edx-search/events","assignees_url":"https://api.github.com/repos/edx-solutions/edx-search/assignees{/user}","branches_url":"https://api.github.com/repos/edx-solutions/edx-search/branches{/branch}","tags_url":"https://api.github.com/repos/edx-solutions/edx-search/tags","blobs_url":"https://api.github.com/repos/edx-solutions/edx-search/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx-solutions/edx-search/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx-solutions/edx-search/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx-solutions/edx-search/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx-solutions/edx-search/statuses/{sha}","languages_url":"https://api.github.com/repos/edx-solutions/edx-search/languages","stargazers_url":"https://api.github.com/repos/edx-solutions/edx-search/stargazers","contributors_url":"https://api.github.com/repos/edx-solutions/edx-search/contributors","subscribers_url":"https://api.github.com/repos/edx-solutions/edx-search/subscribers","subscription_url":"https://api.github.com/repos/edx-solutions/edx-search/subscription","commits_url":"https://api.github.com/repos/edx-solutions/edx-search/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx-solutions/edx-search/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx-solutions/edx-search/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx-solutions/edx-search/issues/comments/{number}","contents_url":"https://api.github.com/repos/edx-solutions/edx-search/contents/{+path}","compare_url":"https://api.github.com/repos/edx-solutions/edx-search/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx-solutions/edx-search/merges","archive_url":"https://api.github.com/repos/edx-solutions/edx-search/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx-solutions/edx-search/downloads","issues_url":"https://api.github.com/repos/edx-solutions/edx-search/issues{/number}","pulls_url":"https://api.github.com/repos/edx-solutions/edx-search/pulls{/number}","milestones_url":"https://api.github.com/repos/edx-solutions/edx-search/milestones{/number}","notifications_url":"https://api.github.com/repos/edx-solutions/edx-search/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx-solutions/edx-search/labels{/name}","releases_url":"https://api.github.com/repos/edx-solutions/edx-search/releases{/id}","created_at":"2014-12-11T20:26:57Z","updated_at":"2014-12-17T18:38:08Z","pushed_at":"2015-01-13T17:22:26Z","git_url":"git://github.com/edx-solutions/edx-search.git","ssh_url":"git@github.com:edx-solutions/edx-search.git","clone_url":"https://github.com/edx-solutions/edx-search.git","svn_url":"https://github.com/edx-solutions/edx-search","homepage":null,"size":360,"stargazers_count":1,"watchers_count":1,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"master"}},"base":{"label":"edx-solutions:master","ref":"master","sha":"710decd58e1c121b1dcd0b1ddb7fe28720f0e5a9","user":{"login":"edx-solutions","id":6585054,"avatar_url":"https://avatars.githubusercontent.com/u/6585054?v=3","gravatar_id":"","url":"https://api.github.com/users/edx-solutions","html_url":"https://github.com/edx-solutions","followers_url":"https://api.github.com/users/edx-solutions/followers","following_url":"https://api.github.com/users/edx-solutions/following{/other_user}","gists_url":"https://api.github.com/users/edx-solutions/gists{/gist_id}","starred_url":"https://api.github.com/users/edx-solutions/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx-solutions/subscriptions","organizations_url":"https://api.github.com/users/edx-solutions/orgs","repos_url":"https://api.github.com/users/edx-solutions/repos","events_url":"https://api.github.com/users/edx-solutions/events{/privacy}","received_events_url":"https://api.github.com/users/edx-solutions/received_events","type":"Organization","site_admin":false},"repo":{"id":27890225,"name":"edx-search","full_name":"edx-solutions/edx-search","owner":{"login":"edx-solutions","id":6585054,"avatar_url":"https://avatars.githubusercontent.com/u/6585054?v=3","gravatar_id":"","url":"https://api.github.com/users/edx-solutions","html_url":"https://github.com/edx-solutions","followers_url":"https://api.github.com/users/edx-solutions/followers","following_url":"https://api.github.com/users/edx-solutions/following{/other_user}","gists_url":"https://api.github.com/users/edx-solutions/gists{/gist_id}","starred_url":"https://api.github.com/users/edx-solutions/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx-solutions/subscriptions","organizations_url":"https://api.github.com/users/edx-solutions/orgs","repos_url":"https://api.github.com/users/edx-solutions/repos","events_url":"https://api.github.com/users/edx-solutions/events{/privacy}","received_events_url":"https://api.github.com/users/edx-solutions/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx-solutions/edx-search","description":"Search/index programming interface for use within edx platform components","fork":false,"url":"https://api.github.com/repos/edx-solutions/edx-search","forks_url":"https://api.github.com/repos/edx-solutions/edx-search/forks","keys_url":"https://api.github.com/repos/edx-solutions/edx-search/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx-solutions/edx-search/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx-solutions/edx-search/teams","hooks_url":"https://api.github.com/repos/edx-solutions/edx-search/hooks","issue_events_url":"https://api.github.com/repos/edx-solutions/edx-search/issues/events{/number}","events_url":"https://api.github.com/repos/edx-solutions/edx-search/events","assignees_url":"https://api.github.com/repos/edx-solutions/edx-search/assignees{/user}","branches_url":"https://api.github.com/repos/edx-solutions/edx-search/branches{/branch}","tags_url":"https://api.github.com/repos/edx-solutions/edx-search/tags","blobs_url":"https://api.github.com/repos/edx-solutions/edx-search/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx-solutions/edx-search/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx-solutions/edx-search/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx-solutions/edx-search/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx-solutions/edx-search/statuses/{sha}","languages_url":"https://api.github.com/repos/edx-solutions/edx-search/languages","stargazers_url":"https://api.github.com/repos/edx-solutions/edx-search/stargazers","contributors_url":"https://api.github.com/repos/edx-solutions/edx-search/contributors","subscribers_url":"https://api.github.com/repos/edx-solutions/edx-search/subscribers","subscription_url":"https://api.github.com/repos/edx-solutions/edx-search/subscription","commits_url":"https://api.github.com/repos/edx-solutions/edx-search/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx-solutions/edx-search/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx-solutions/edx-search/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx-solutions/edx-search/issues/comments/{number}","contents_url":"https://api.github.com/repos/edx-solutions/edx-search/contents/{+path}","compare_url":"https://api.github.com/repos/edx-solutions/edx-search/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx-solutions/edx-search/merges","archive_url":"https://api.github.com/repos/edx-solutions/edx-search/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx-solutions/edx-search/downloads","issues_url":"https://api.github.com/repos/edx-solutions/edx-search/issues{/number}","pulls_url":"https://api.github.com/repos/edx-solutions/edx-search/pulls{/number}","milestones_url":"https://api.github.com/repos/edx-solutions/edx-search/milestones{/number}","notifications_url":"https://api.github.com/repos/edx-solutions/edx-search/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx-solutions/edx-search/labels{/name}","releases_url":"https://api.github.com/repos/edx-solutions/edx-search/releases{/id}","created_at":"2014-12-11T20:26:57Z","updated_at":"2014-12-17T18:38:08Z","pushed_at":"2015-01-13T17:22:26Z","git_url":"git://github.com/edx-solutions/edx-search.git","ssh_url":"git@github.com:edx-solutions/edx-search.git","clone_url":"https://github.com/edx-solutions/edx-search.git","svn_url":"https://github.com/edx-solutions/edx-search","homepage":null,"size":360,"stargazers_count":1,"watchers_count":1,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx-solutions/edx-search/pulls/1"},"html":{"href":"https://github.com/edx-solutions/edx-search/pull/1"},"issue":{"href":"https://api.github.com/repos/edx-solutions/edx-search/issues/1"},"comments":{"href":"https://api.github.com/repos/edx-solutions/edx-search/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/edx-solutions/edx-search/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/edx-solutions/edx-search/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/edx-solutions/edx-search/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/edx-solutions/edx-search/statuses/2be4557ab0ff6344c5d9393493460fa678bc699c"}}}}