/
test_api.py
97 lines (78 loc) 路 3.08 KB
/
test_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
"""API tests using WebTest.
"""
import pytest
import json
from dribdat.aggregation import ProjectActivity
from dribdat.user.models import Event
from dribdat.public.api import *
from .factories import EventFactory, ProjectFactory, UserFactory
@pytest.mark.usefixtures('db')
class TestApi:
"""API and data import/export tests."""
def test_api_events(self):
"""Test event API functions."""
event = EventFactory(name="hello")
event.save()
# Test basic event metadata
jsondata = json.loads(info_current_event_json().data)
assert "hello" == jsondata["event"]["name"]
jsondata = json.loads(info_event_json(event.id).data)
assert "hello" == jsondata["event"]["name"]
# Test Schema.org compliance
jsondata = json.loads(info_event_hackathon_json(event.id).data)
# TODO: use https://github.com/schemaorg/schemarama for full validation
assert "hello" == jsondata["name"]
assert "Hackathon" == jsondata["@type"]
assert "http://schema.org" == jsondata["@context"]
def test_get_event_data(self, testapp):
"""Get basic event data."""
event = Event(name="test")
event.save()
# Check event in JSON format
res = testapp.get('/api/events.json')
assert res.status_code == 200
assert len(res.json['events']) == 1
assert res.json['events'][0]['name'] == "test"
# Test the same in CSV format
res = testapp.get('/api/events.csv')
assert '"test"' in res
# Test participants
user1 = UserFactory()
user2 = UserFactory()
project = ProjectFactory()
project.user = user1
project.event = event
project.save()
ProjectActivity(project, 'star', user1)
ProjectActivity(project, 'star', user2)
assert project in user1.joined_projects()
assert project not in user1.joined_projects(False) # no challenges
# Test event API
userlist = get_users_for_event(event)
assert user1.username in [ u['username'] for u in userlist ]
assert user2.username in [ u['username'] for u in userlist ]
def test_get_platform_data(self):
"""More global data types."""
event = EventFactory(name="hello")
user = UserFactory()
project = ProjectFactory()
project.user = user
project.event = event
project.longtext = "Smee"
project.save()
ProjectActivity(project, 'update', user)
# Test all Event data
csvdata = project_list_all_events_csv()
csvdata = str(csvdata.get_data())
assert "hello" in csvdata
# Test Activities data
csvdata = event_activity_csv(event.id)
csvdata = str(csvdata.get_data())
assert "update" in csvdata
# Test project Activities
jsondata = json.loads(projects_activity_json().get_data())
assert len(jsondata['activities']) == 1
# Test Project search
ppj = json.loads(projects_top_json().get_data())
assert len(ppj['projects']) == 1