rochacbruno / talkshow

A Call 4 Papers System - A simple base app as example of Flask Architecture
http://bit.ly/tutorialflaskconf
65 stars 26 forks source link

Ajustes no pytest da branch master #16

Open rochacbruno opened 6 years ago

rochacbruno commented 6 years ago

Ajustar pytest conforme a branch extended

atualizar PDF

Riverfount commented 6 years ago

Oi @rochacbruno seria possível explicitar melhor o que precisa ser ajustado no pytest?

rochacbruno commented 5 years ago

diff --git a/tests/conftest.py b/tests/conftest.py
index 26d6c00..8fd0911 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,15 +1,23 @@
-import pytest
 from base64 import b64encode
+from contextlib import suppress
+
+import pytest
+
 from talkshow.app import create_app
+from talkshow.ext.login import create_user

-@pytest.fixture(scope="module")
+@pytest.fixture(scope='module')
 def app():
     """Flask Pytest uses it"""
-    return create_app()
+    _app = create_app(MONGODB_NAME='test_db')
+    with _app.app_context(), suppress(RuntimeError):
+        # creates new admin user to be used in tests if it does not exist
+        create_user("admin", "1234")
+    return _app

-@pytest.fixture(scope="module")
+@pytest.fixture(scope='module')
 def auth():
     """The Basic Auth credentials for testing"""
     credentials = b64encode(bytes("admin:1234", 'ascii')).decode('ascii')
diff --git a/tests/test_api.py b/tests/test_api.py
new file mode 100644
index 0000000..7a8f1b6
--- /dev/null
+++ b/tests/test_api.py
@@ -0,0 +1,29 @@
+def test_can_create_event(client, auth):
+    """Asserts an event can be created via API"""
+    event_data = {"name": "Puggies Convention", "date": "2018-09-09"}
+    created = client.post('/api/v1/event/', json=event_data, headers=auth)
+    assert created.status_code == 201
+
+
+def test_can_list_event(client):
+    """Asserts event can be listed"""
+    events = client.get('/api/v1/event/')
+    assert events.status_code == 200
+    assert 'events' in events.json
+
+
+def test_can_get_event_create(client):
+    events = client.get('/api/v1/event/puggies-convention')
+    assert events.status_code == 200
+
+
+def test_can_create_proposal_in_event(client, app):
+    proposal_event_data = {"name": "Emma Smith ",
+                           "email": "emma_smith@mail.com",
+                           "title": "Tutorial Flask",
+                           "description": "A simple base app as "
+                                          "example of Flask Architecture"}
+    events = client.post(f"/api/v1/event/{app.db['events'].find_one()['_id']}",
+                         json=proposal_event_data)
+    assert events.status_code == 201
+    assert 'proposal created' in events.json
diff --git a/tests/test_apy.py b/tests/test_apy.py
deleted file mode 100644
index 2a51ccf..0000000
--- a/tests/test_apy.py
+++ /dev/null
@@ -1,16 +0,0 @@
-
-
-def test_can_create_event(app, auth):
-    """Asserts an event can be created via API"""
-    event_data = {"name": "Puggies Convention", "date": "2018-09-09"}
-    with app.test_client() as client:
-        created = client.post('/api/v1/event/', json=event_data, headers=auth)
-        assert created.status_code == 201
-
-
-def test_can_list_event(app):
-    """Asserts event can be listed"""
-    with app.test_client() as client:
-        events = client.get('/api/v1/event/')
-        assert events.status_code == 200
-        assert 'events' in events.json