Skip to content

Commit 148669a

Browse files
authored
Merge pull request #5 from cloudblue/enh/LITE-24418
LITE-24418 Running tests on real MongoDB
2 parents 536160e + 4fe9ff3 commit 148669a

File tree

8 files changed

+102
-19
lines changed

8 files changed

+102
-19
lines changed

.github/workflows/build.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ jobs:
1515
strategy:
1616
matrix:
1717
python-version: ['3.8', '3.9', '3.10']
18+
services:
19+
mongo:
20+
image: mongo:5
21+
ports:
22+
- 27017:27017
23+
env:
24+
MONGO_INITDB_ROOT_USERNAME: root
25+
MONGO_INITDB_ROOT_PASSWORD: 1q2w3e
1826
steps:
1927
- name: Checkout project
2028
uses: actions/checkout@v2
@@ -35,6 +43,10 @@ jobs:
3543
- name: Testing
3644
run: |
3745
poetry run pytest
46+
env:
47+
MONGO_HOST: localhost
48+
MONGO_USER: root
49+
MONGO_PASSWORD: 1q2w3e
3850
sonar:
3951
name: Quality Analysis by sonar
4052
needs: [build]

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ARG PYTHON_VERSION=3.10
2+
3+
FROM python:${PYTHON_VERSION}
4+
5+
WORKDIR /app
6+
7+
ADD . /app
8+
9+
RUN pip install poetry && poetry install

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Due to implementation and Mongo engine features there may be some limitations in
9595

9696
Check code style: `poetry run flake8`
9797
Run tests: `poetry run pytest`
98+
Run integration tests: `docker compose run app_test`
9899

99100
Tests reports are generated in `tests/reports`.
100101
* `out.xml` - JUnit test results

docker-compose.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version: "3"
2+
3+
services:
4+
mongo:
5+
image: mongo:5
6+
restart: on-failure
7+
environment:
8+
MONGO_INITDB_ROOT_USERNAME: root
9+
MONGO_INITDB_ROOT_PASSWORD: 1q2w3e
10+
11+
app_test:
12+
container_name: django_mongoengine_rql_test
13+
image: django_mongoengine_rql_test
14+
build:
15+
context: .
16+
dockerfile: Dockerfile
17+
command: bash -c 'poetry run flake8 && poetry run pytest'
18+
volumes:
19+
- ./:/app
20+
depends_on:
21+
- mongo
22+
environment:
23+
MONGO_HOST: mongo
24+
MONGO_USER: root
25+
MONGO_PASSWORD: 1q2w3e

tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright © 2022 CloudBlue LLC. All rights reserved.
3+
#
4+
5+
import pytest
6+
7+
from tests.documents import Doc
8+
9+
10+
@pytest.fixture
11+
def is_real_mongo(settings):
12+
is_real = bool(settings.MONGODB_DATABASES['default']['username'])
13+
14+
if is_real:
15+
Doc.objects.all().delete()
16+
17+
yield is_real

tests/documents.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright © 2022 CloudBlue LLC. All rights reserved.
3+
#
4+
5+
from django_mongoengine import Document, EmbeddedDocument, fields
6+
7+
8+
class EmbDoc(EmbeddedDocument):
9+
str_f = fields.StringField()
10+
int_f = fields.IntField(blank=True)
11+
12+
13+
class Doc(Document):
14+
str_f = fields.StringField(max_length=255, blank=True)
15+
bl = fields.BooleanField(default=True)
16+
dt = fields.DateTimeField(blank=True)
17+
d = fields.DateField(blank=True)
18+
dec = fields.DecimalField(blank=True)
19+
flt = fields.FloatField(blank=True)
20+
int_f = fields.IntField(blank=True, db_field='other_int_f')
21+
22+
related_doc = fields.EmbeddedDocumentField('EmbDoc', blank=True)

tests/settings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import os
2+
13
INSTALLED_APPS = [
24
'django_mongoengine',
35
]
46

57
MONGODB_DATABASES = {'default': {
68
'name': 'mongoenginetest',
7-
'host': 'mongomock://localhost',
9+
'host': os.environ.get('MONGO_HOST', 'mongomock://localhost'),
10+
'username': os.environ.get('MONGO_USER'),
11+
'password': os.environ.get('MONGO_PASSWORD'),
812
}}

tests/test_filter_cls.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,10 @@
22
# Copyright © 2022 CloudBlue LLC. All rights reserved.
33
#
44

5-
from django_mongoengine import Document, EmbeddedDocument, fields
65
from py_rql.constants import FilterLookups
76

87
from dj_mongoengine_rql.filter_cls import MongoengineRQLFilterClass
9-
10-
11-
class EmbDoc(EmbeddedDocument):
12-
str_f = fields.StringField()
13-
int_f = fields.IntField(blank=True)
14-
15-
16-
class Doc(Document):
17-
str_f = fields.StringField(max_length=255, blank=True)
18-
bl = fields.BooleanField(default=True)
19-
dt = fields.DateTimeField(blank=True)
20-
d = fields.DateField(blank=True)
21-
dec = fields.DecimalField(blank=True)
22-
flt = fields.FloatField(blank=True)
23-
int_f = fields.IntField(blank=True, db_field='other_int_f')
24-
25-
related_doc = fields.EmbeddedDocumentField('EmbDoc', blank=True)
8+
from tests.documents import Doc
269

2710

2811
class DocFilterClass(MongoengineRQLFilterClass):
@@ -76,3 +59,13 @@ def test_not():
7659
_, qs = DocFilterClass(Doc.objects.filter(int_f=1)).apply_filters('not(eq(int_f,120))')
7760

7861
assert qs.query.q == {'$nor': [{'other_int_f': 120}], 'other_int_f': 1}
62+
63+
64+
def test_db_operation(is_real_mongo):
65+
if is_real_mongo:
66+
doc = Doc.objects.create(str_f='a')
67+
Doc.objects.create(str_f='b')
68+
69+
_, qs = DocFilterClass(Doc.objects).apply_filters('str_f=a')
70+
71+
assert list(qs.all()) == [doc]

0 commit comments

Comments
 (0)