Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
thisbejim committed Oct 7, 2016
2 parents 9297a1b + 2455a35 commit ce640f8
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ pyrebase.json
__pycache__
*.pyc
pyrebase.py.orig
sseclient
<<<<<<< HEAD
=======

/secret.json
/tests/config.py
.cache
sseclient
>>>>>>> 2455a35d1e6713b14f46840a2c2ce5ca0afa341f
32 changes: 32 additions & 0 deletions DEV.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Developing
==========

Create a new virtualenv and install the required libraries for
the default pyrebase + the testing tools.

```
mkvirtualenv -p python3 pyrebase-dev # if you have virtualenvwrapper installed
pip install -r requirements.txt -r requirements.dev.txt
```

Configure a test database

```
cp ~/my_secret_firebase_service_account_key.json ./secret.json
cp ./tests/config.template.py ./tests/config.py
# Update the ./tests/config.py file with your test database.
```

Note that to make it easier to run the tests, they read/write to
`/pyrebase_tests/` on your firebase database. They should not mess
up the rest of the database.


Run the tests:

```
pytest -s tests
```

On MacOS you may need to fix the shebang in the pytest executable
to make it point to the correct python binary.
1 change: 1 addition & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
Empty file added tests/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions tests/config.template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SIMPLE_CONFIG = {
"apiKey": "",
"authDomain": "",
"databaseURL": "",
"storageBucket": "",
}

SERVICE_ACCOUNT_PATH = "secret.json"

SERVICE_CONFIG = dict(SIMPLE_CONFIG, serviceAccount=SERVICE_ACCOUNT_PATH)
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from tests.tools import make_db


@pytest.fixture(scope='session')
def db():
# To make it easier to test, we keep the test restricted to firebase_tests
# Because of the current mutations on calls, we return it as a function.
try:
yield lambda: make_db(service_account=True).child('pyrebase_tests')
finally:
make_db(service_account=True).child('pyrebase_tests').remove()
105 changes: 105 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import random
import time
from contextlib import contextmanager

import pytest


@pytest.fixture(scope='function')
def db_sa(db):
# To make it easier to test, we keep the test restricted to firebase_tests
# Because of the current mutations on calls, we return it as a function.
name = 'test_%05d' % random.randint(0, 99999)
yield lambda: db().child(name)


@contextmanager
def make_stream(db, cbk):
s = db.stream(cbk)
try:
yield s
finally:
s.close()


@contextmanager
def make_append_stream(db):
l = []

def cbk(event):
l.append(event)

with make_stream(db, cbk) as s:
yield s, l


class TestSimpleGetAndPut:
def test_simple_get(self, db_sa):
assert db_sa().get().val() is None

def test_put_succeed(self, db_sa):
assert db_sa().set(True)

def test_put_then_get_keeps_value(self, db_sa):
db_sa().set("some_value")
assert db_sa().get().val() == "some_value"

def test_put_dictionary(self, db_sa):
v = dict(a=1, b="2", c=dict(x=3.1, y="3.2"))
db_sa().set(v)

assert db_sa().get().val() == v

@pytest.mark.skip
def test_put_deeper_dictionnary(self, db_sa):
v = {'1': {'11': {'111': 42}}}
db_sa().set(v)

# gives: assert [None, {'11': {'111': 42}}] == {'1': {'11': {'111': 42}}}
assert db_sa().get().val() == v


class TestChildNavigation:
def test_get_child_none(self, db_sa):
assert db_sa().child('lorem').get().val() is None

def test_get_child_after_pushing_data(self, db_sa):
db_sa().set({'lorem': "a", 'ipsum': 2})

assert db_sa().child('lorem').get().val() == "a"
assert db_sa().child('ipsum').get().val() == 2

def test_update_child(self, db_sa):
db_sa().child('child').update({'c1/c11': 1, 'c1/c12': 2, 'c2': 3})

assert db_sa().child('child').child('c1').get().val() == {'c11': 1, 'c12': 2}
assert db_sa().child('child').child('c2').get().val() == 3

def test_path_equivalence(self, db_sa):
db_sa().set({'1': {'11': {'111': 42}}})

assert db_sa().child('1').child('11').child('111').get().val() == 42
assert db_sa().child('1/11/111').get().val() == 42
assert db_sa().child('1', '11', '111').get().val() == 42
assert db_sa().child(1, '11', '111').get().val() == 42


class TestStreaming:
def test_create_stream_succeed(self, db_sa):
with make_append_stream(db_sa()) as (stream, l):
assert stream is not None

def test_does_initial_call(self, db_sa):
with make_append_stream(db_sa()) as (stream, l):
time.sleep(2)
assert len(l) == 1

def test_responds_to_update_calls(self, db_sa):
with make_append_stream(db_sa()) as (stream, l):
db_sa().set({"1": "a", "1_2": "b"})
db_sa().update({"2": "c"})
db_sa().push("3")

time.sleep(2)

assert len(l) == 3
6 changes: 6 additions & 0 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from tests.tools import make_db


def test_setup():
db = make_db(True)
assert db.get()
11 changes: 11 additions & 0 deletions tests/tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pyrebase import pyrebase
from tests import config


def make_db(service_account=False):
if service_account:
c = config.SERVICE_CONFIG
else:
c = config.SIMPLE_CONFIG

return pyrebase.initialize_app(c).database()

0 comments on commit ce640f8

Please sign in to comment.