Skip to content

Commit e1ddec4

Browse files
committed
Initial commit
0 parents  commit e1ddec4

12 files changed

+399
-0
lines changed

.gitignore

+246
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
2+
# Created by https://www.gitignore.io/api/intellij,linux,osx,pycharm,python
3+
4+
### Intellij ###
5+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
6+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7+
8+
# User-specific stuff:
9+
.idea/
10+
.idea/workspace.xml
11+
.idea/tasks.xml
12+
.idea/dictionaries
13+
.idea/vcs.xml
14+
.idea/jsLibraryMappings.xml
15+
16+
# Sensitive or high-churn files:
17+
.idea/dataSources.ids
18+
.idea/dataSources.xml
19+
.idea/dataSources.local.xml
20+
.idea/sqlDataSources.xml
21+
.idea/dynamic.xml
22+
.idea/uiDesigner.xml
23+
24+
# Gradle:
25+
.idea/gradle.xml
26+
.idea/libraries
27+
28+
# Mongo Explorer plugin:
29+
.idea/mongoSettings.xml
30+
31+
## File-based project format:
32+
*.iws
33+
34+
## Plugin-specific files:
35+
36+
# IntelliJ
37+
/out/
38+
39+
# mpeltonen/sbt-idea plugin
40+
.idea_modules/
41+
42+
# JIRA plugin
43+
atlassian-ide-plugin.xml
44+
45+
# Crashlytics plugin (for Android Studio and IntelliJ)
46+
com_crashlytics_export_strings.xml
47+
crashlytics.properties
48+
crashlytics-build.properties
49+
fabric.properties
50+
51+
### Intellij Patch ###
52+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
53+
54+
# *.iml
55+
# modules.xml
56+
# .idea/misc.xml
57+
# *.ipr
58+
59+
60+
### Linux ###
61+
*~
62+
63+
# temporary files which can be created if a process still has a handle open of a deleted file
64+
.fuse_hidden*
65+
66+
# KDE directory preferences
67+
.directory
68+
69+
# Linux trash folder which might appear on any partition or disk
70+
.Trash-*
71+
72+
73+
### OSX ###
74+
*.DS_Store
75+
.AppleDouble
76+
.LSOverride
77+
78+
# Icon must end with two \r
79+
Icon
80+
81+
82+
# Thumbnails
83+
._*
84+
85+
# Files that might appear in the root of a volume
86+
.DocumentRevisions-V100
87+
.fseventsd
88+
.Spotlight-V100
89+
.TemporaryItems
90+
.Trashes
91+
.VolumeIcon.icns
92+
.com.apple.timemachine.donotpresent
93+
94+
# Directories potentially created on remote AFP share
95+
.AppleDB
96+
.AppleDesktop
97+
Network Trash Folder
98+
Temporary Items
99+
.apdisk
100+
101+
102+
### PyCharm ###
103+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
104+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
105+
106+
# User-specific stuff:
107+
.idea/workspace.xml
108+
.idea/tasks.xml
109+
.idea/dictionaries
110+
.idea/vcs.xml
111+
.idea/jsLibraryMappings.xml
112+
113+
# Sensitive or high-churn files:
114+
.idea/dataSources.ids
115+
.idea/dataSources.xml
116+
.idea/dataSources.local.xml
117+
.idea/sqlDataSources.xml
118+
.idea/dynamic.xml
119+
.idea/uiDesigner.xml
120+
121+
# Gradle:
122+
.idea/gradle.xml
123+
.idea/libraries
124+
125+
# Mongo Explorer plugin:
126+
.idea/mongoSettings.xml
127+
128+
## File-based project format:
129+
*.iws
130+
131+
## Plugin-specific files:
132+
133+
# IntelliJ
134+
/out/
135+
136+
# mpeltonen/sbt-idea plugin
137+
.idea_modules/
138+
139+
# JIRA plugin
140+
atlassian-ide-plugin.xml
141+
142+
# Crashlytics plugin (for Android Studio and IntelliJ)
143+
com_crashlytics_export_strings.xml
144+
crashlytics.properties
145+
crashlytics-build.properties
146+
fabric.properties
147+
148+
### PyCharm Patch ###
149+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
150+
151+
# *.iml
152+
# modules.xml
153+
# .idea/misc.xml
154+
# *.ipr
155+
156+
157+
### Python ###
158+
# Byte-compiled / optimized / DLL files
159+
__pycache__/
160+
*.py[cod]
161+
*$py.class
162+
163+
# C extensions
164+
*.so
165+
166+
# Distribution / packaging
167+
.Python
168+
env/
169+
build/
170+
develop-eggs/
171+
dist/
172+
downloads/
173+
eggs/
174+
.eggs/
175+
lib/
176+
lib64/
177+
parts/
178+
sdist/
179+
var/
180+
*.egg-info/
181+
.installed.cfg
182+
*.egg
183+
184+
# PyInstaller
185+
# Usually these files are written by a python script from a template
186+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
187+
*.manifest
188+
*.spec
189+
190+
# Installer logs
191+
pip-log.txt
192+
pip-delete-this-directory.txt
193+
194+
# Unit test / coverage reports
195+
htmlcov/
196+
.tox/
197+
.coverage
198+
.coverage.*
199+
.cache
200+
nosetests.xml
201+
coverage.xml
202+
*,cover
203+
.hypothesis/
204+
205+
# Translations
206+
*.mo
207+
*.pot
208+
209+
# Django stuff:
210+
*.log
211+
local_settings.py
212+
213+
# Flask stuff:
214+
instance/
215+
.webassets-cache
216+
217+
# Scrapy stuff:
218+
.scrapy
219+
220+
# Sphinx documentation
221+
docs/_build/
222+
223+
# PyBuilder
224+
target/
225+
226+
# IPython Notebook
227+
.ipynb_checkpoints
228+
229+
# pyenv
230+
.python-version
231+
232+
# celery beat schedule file
233+
celerybeat-schedule
234+
235+
# dotenv
236+
.env
237+
238+
# virtualenv
239+
venv/
240+
ENV/
241+
242+
# Spyder project settings
243+
.spyderproject
244+
245+
# Rope project settings
246+
.ropeproject

Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
install:
2+
@pip install -r requirements.txt
3+
4+
test_install: install
5+
@pip install -r requirements_test.txt
6+
7+
test: test_install
8+
@py.test --cov-report term-missing --cov=elasticmock

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# ElasticMock
2+
3+
Python Elasticsearch Mock for test purposes
4+
5+
## Usage
6+
7+
To use ElasticMock, you can decorate your TestClass with *patch* decorator:
8+
9+
```python
10+
from unittest import TestCase
11+
12+
from mock import patch
13+
from elasticmock import get_elasticmock
14+
15+
16+
@patch('elasticsearch.Elasticsearch', get_elasticmock)
17+
class TestClass(TestCase):
18+
19+
def test_should_return_something_from_elasticsearch(self):
20+
self.assertIsNotNone(some_function_that_uses_elasticsearch())
21+
```
22+
23+
## Changelog
24+
25+
#### 0.0.1:
26+
- **elasticmock**: Mocked **index** method

elasticmock/__init__.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from elasticsearch import Transport
4+
5+
from elasticmock.fake_elasticsearch import FakeElasticsearch
6+
7+
8+
def get_elasticmock(hosts=None, transport_class=Transport, **kwargs):
9+
return FakeElasticsearch(hosts=hosts, transport_class=transport_class, **kwargs)

elasticmock/fake_elasticsearch.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from elasticsearch import Elasticsearch, Transport
4+
from elasticmock.utilities import get_random_id
5+
6+
7+
class FakeElasticsearch(Elasticsearch):
8+
__data_dict = None
9+
10+
def __init__(self, hosts=None, transport_class=Transport, **kwargs):
11+
self.__data_dict = {}
12+
13+
def index(self, index, doc_type, body, id=None, params=None):
14+
if index not in self.__data_dict:
15+
self.__data_dict[index] = {}
16+
17+
document_id = get_random_id()
18+
self.__data_dict[index] = {
19+
'_type': doc_type,
20+
'_id': document_id,
21+
'_source': body
22+
}
23+
24+
return {
25+
'_type': doc_type,
26+
'_id': document_id,
27+
'created': True,
28+
'_version': 1,
29+
'_index': index
30+
}

elasticmock/utilities/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import random
4+
import string
5+
6+
DEFAULT_ELASTICSEARCH_ID_SIZE = 20
7+
CHARSET_FOR_ELASTICSEARCH_ID = string.ascii_letters + string.digits
8+
9+
10+
def get_random_id(size=DEFAULT_ELASTICSEARCH_ID_SIZE):
11+
return ''.join(random.choice(CHARSET_FOR_ELASTICSEARCH_ID) for _ in range(size))

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
elasticsearch==1.9.0

requirements_test.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mock==1.0.1
2+
pytest==2.8.2
3+
pytest-cov==2.2.0
4+
pytest-xdist==1.13.1

setup.cfg

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[pytest]
2+
python_files=test*.py
3+
4+
[coverage:run]
5+
omit =
6+
*/tests/*

setup.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from setuptools import setup, find_packages
4+
5+
setup(
6+
name='ElasticMock',
7+
version='0.0.1',
8+
url='https://github.com/elasticmock',
9+
license='GNU GPLv3',
10+
author='Marcos Cardoso',
11+
author_email='[email protected]',
12+
description='Python Elasticsearch Mock for test purposes',
13+
packages=find_packages(),
14+
zip_safe=False,
15+
include_package_data=True,
16+
platforms='any',
17+
install_requires=[
18+
'elasticsearch<=1.9.0'
19+
],
20+
classifiers=[
21+
'Environment :: Web Environment',
22+
'Intended Audience :: Developers',
23+
'Operating System :: OS Independent',
24+
'Programming Language :: Python',
25+
'Topic :: Software Development :: Libraries :: Python Modules'
26+
]
27+
)

tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

0 commit comments

Comments
 (0)