forked from thisbejim/Pyrebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |