def test_past_events(self): ''' Only return events that occurred in the past Make sure they are ordered from most recent to furthest in the past ''' # Assuming today is Christmas... organization = OrganizationFactory( name="International Cat Association") db.session.flush() # Create multiple events, one in the future, some in the past EventFactory(organization_name=organization.name, name="Thanksgiving", start_time_notz=datetime.now() - timedelta(30)) EventFactory(organization_name=organization.name, name="Christmas Eve", start_time_notz=datetime.now() - timedelta(1)) EventFactory(organization_name=organization.name, name="New Years", start_time_notz=datetime.now() + timedelta(7)) db.session.flush() # Check that past events are returned in the correct order response = self.app.get( '/api/organizations/International Cat Association/past_events') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response['total'], 2) self.assertEqual(response['objects'][0]['name'], 'Christmas Eve') self.assertEqual(response['objects'][1]['name'], 'Thanksgiving')
def test_current_events(self): ''' The three soonest upcoming events should be returned. If there are no events in the future, no events will be returned ''' # Assuming today is Christmas... organization = OrganizationFactory(name='Collective of Ericas') db.session.flush() # Create multiple events, some in the future, one in the past EventFactory(organization_name=organization.name, name="Christmas Eve", start_time_notz=datetime.now() - timedelta(1)) EventFactory(organization_name=organization.name, name="New Years", start_time_notz=datetime.now() + timedelta(7)) EventFactory(organization_name=organization.name, name="MLK Day", start_time_notz=datetime.now() + timedelta(25)) EventFactory(organization_name=organization.name, name="Cesar Chavez Day", start_time_notz=datetime.now() + timedelta(37)) db.session.flush() response = self.app.get('/api/organizations/Collective%20of%20Ericas') response_json = json.loads(response.data) self.assertEqual(len(response_json['current_events']), 2) self.assertEqual(response_json['current_events'][0]['name'], "New Years") self.assertEqual(response_json['current_events'][1]['name'], "MLK Day") self.assertEqual( response_json['current_events'][0]['organization_name'], "Collective of Ericas")
def test_all_past_events(self): ''' Test the /events/past_events end point. ''' # World Cup teams organization = OrganizationFactory(name='USA USA USA', type='Code for All') db.session.flush() # Create multiple events, some in the future, one in the past EventFactory(organization_name=organization.name, name="Past Event", start_time_notz=datetime.now() - timedelta(1000)) EventFactory(organization_name=organization.name, name="Event One", start_time_notz=datetime.now() + timedelta(10)) db.session.flush() # World Cup teams organization = OrganizationFactory(name='Brazil') db.session.flush() # Create multiple events, some in the future, one in the past EventFactory(organization_name=organization.name, name="Past Event", start_time_notz=datetime.now() - timedelta(2000)) db.session.flush() # World Cup teams organization = OrganizationFactory(name='GER', type='Code for All') db.session.flush() # Create multiple events, some in the future, one in the past EventFactory(organization_name=organization.name, name="Past Event", start_time_notz=datetime.now() - timedelta(3000)) EventFactory(organization_name=organization.name, name="Event Three", start_time_notz=datetime.now() + timedelta(30)) db.session.flush() response = self.app.get('/api/events/past_events?organization_type=Code for All') response_json = json.loads(response.data) self.assertEqual(len(response_json['objects']), 2)
def test_cascading_delete(self): ''' Test that when an organization is deleted, all of it's projects, issues, stories, events are deleted ''' # Create an organization organization = OrganizationFactory() db.session.flush() # Create a project, an event and a story project = ProjectFactory(organization_name=organization.name) EventFactory(organization_name=organization.name) StoryFactory(organization_name=organization.name) db.session.flush() # Create an issue and give it a label issue = IssueFactory(project_id=project.id) db.session.flush() label = LabelFactory() issue.labels = [label] db.session.flush() # Get all of the stuff orgs = Organization.query.all() eve = Event.query.all() sto = Story.query.all() proj = Project.query.all() iss = Issue.query.all() lab = Label.query.all() # Verify they are there self.assertEqual(len(orgs), 1) self.assertEqual(len(eve), 1) self.assertEqual(len(sto), 1) self.assertEqual(len(proj), 1) self.assertEqual(len(iss), 1) self.assertEqual(len(lab), 1) # Delete the one organization db.session.delete(orgs[0]) db.session.commit() # Get all the stuff again orgs = Organization.query.all() eve = Event.query.all() sto = Story.query.all() proj = Project.query.all() iss = Issue.query.all() lab = Label.query.all() # Make sure they are all gone self.assertEqual(len(orgs), 0) self.assertEqual(len(eve), 0) self.assertEqual(len(sto), 0) self.assertEqual(len(proj), 0) self.assertEqual(len(iss), 0) self.assertEqual(len(lab), 0)
def test_dashes_in_slugs(self): organization = OrganizationFactory(name="Code for America") event = EventFactory(organization_name="Code for America") db.session.flush() response = self.app.get('/api/organizations/Code-for-America') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response["name"], "Code for America")
def test_orgs_events(self): organization = OrganizationFactory(name="Code for America") event = EventFactory(organization_name="Code for America") db.session.flush() response = self.app.get('/api/organizations/Code for America/events') self.assertEqual(response.status_code, 200) response = json.loads(response.data) assert isinstance(response, dict)
def test_events_query_filter(self): org = OrganizationFactory(type="Brigade") another_org = OrganizationFactory(type="Code for All") awesome_event = EventFactory(name="Awesome event") sad_event = EventFactory(name="Sad event", description="sad stuff will happen") awesome_event.organization = org sad_event.organization = another_org db.session.commit() # Make sure total number of stories is 2 response = self.app.get('/api/events') response = json.loads(response.data) self.assertEqual(response['total'], 2) # Filter by name should return only 1 response = self.app.get('/api/events?name=awesome') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response['total'], 1) self.assertEqual(response['objects'][0]['name'], "Awesome event") # Filter by description should return only 1 response = self.app.get('/api/events?description=sad%20stuff') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response['total'], 1) self.assertEqual(response['objects'][0]['name'], "Sad event") # Filter by deep searching organization type should return 1 response = self.app.get('/api/events?organization_type=brigade') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response['total'], 1) self.assertEqual(response['objects'][0]['name'], "Awesome event")
def test_events(self): ''' Return all events past/future ordered by oldest to newest ''' EventFactory() db.session.flush() response = self.app.get('/api/events') response = json.loads(response.data) assert isinstance(response, dict) assert isinstance(response['pages'], dict) assert isinstance(response['total'], int) assert isinstance(response['objects'], list) assert isinstance(response['objects'][0]['description'], unicode) assert isinstance(response['objects'][0]['end_time'], unicode) assert isinstance(response['objects'][0]['event_url'], unicode) assert isinstance(response['objects'][0]['api_url'], unicode) assert isinstance(response['objects'][0]['id'], int) assert isinstance(response['objects'][0]['location'], unicode) assert isinstance(response['objects'][0]['name'], unicode) assert isinstance(response['objects'][0]['organization'], dict) assert isinstance(response['objects'][0]['organization_name'], unicode) assert isinstance(response['objects'][0]['start_time'], unicode)
def test_underscores_and_spaces(self): organization = OrganizationFactory(name="Code for America") db.session.add(organization) db.session.commit() response = self.app.get('/api/organizations/Code for America') self.assertEqual(response.status_code, 200) response = json.loads(response.data) scheme, netloc, path, _, _, _ = urlparse(response["all_events"]) self.assertTrue("-" in path) self.assertFalse("_" in path) self.assertFalse(" " in path) scheme, netloc, path, _, _, _ = urlparse(response["all_stories"]) self.assertTrue("-" in path) self.assertFalse("_" in path) self.assertFalse(" " in path) scheme, netloc, path, _, _, _ = urlparse(response["all_projects"]) self.assertTrue("-" in path) self.assertFalse("_" in path) self.assertFalse(" " in path) response = self.app.get('/api/organizations/Code-for-America') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response["name"], "Code for America") response = self.app.get('/api/organizations/Code_for_America') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response["name"], "Code for America") project = ProjectFactory(organization_name="Code for America") db.session.add(project) db.session.commit() response = self.app.get('/api/organizations/Code_for_America/projects') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response["objects"][0]["organization_name"], "Code for America") response = self.app.get('/api/organizations/Code_for_America/projects') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response["objects"][0]["organization_name"], "Code for America") event = EventFactory(organization_name="Code for America") db.session.add(event) db.session.commit() response = self.app.get('/api/organizations/Code for America/events') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response["objects"][0]["organization_name"], "Code for America") response = self.app.get('/api/organizations/Code_for_America/events') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response["objects"][0]["organization_name"], "Code for America") story = StoryFactory(organization_name="Code for America") db.session.add(story) db.session.commit() response = self.app.get('/api/organizations/Code for America/stories') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response["objects"][0]["organization_name"], "Code for America") response = self.app.get('/api/organizations/Code_for_America/stories') self.assertEqual(response.status_code, 200) response = json.loads(response.data) self.assertEqual(response["objects"][0]["organization_name"], "Code for America")
def test_all_upcoming_events_with_params(self): ''' Test the /events/upcoming_events end point with params. ''' # World Cup teams organization = OrganizationFactory(name='USA USA USA', type='Code for All') db.session.flush() # Create multiple events, some in the future, one in the past EventFactory(organization_name=organization.name, name="Past Event", start_time_notz=datetime.now() - timedelta(1000)) EventFactory(organization_name=organization.name, name="Event One", start_time_notz=datetime.now() + timedelta(10)) EventFactory(organization_name=organization.name, name="Event Four", start_time_notz=datetime.now() + timedelta(100)) EventFactory(organization_name=organization.name, name="Event Seven", start_time_notz=datetime.now() + timedelta(1000)) db.session.flush() # World Cup teams organization = OrganizationFactory(name='Brazil') db.session.flush() # Create multiple events, some in the future, one in the past EventFactory(organization_name=organization.name, name="Past Event", start_time_notz=datetime.now() - timedelta(2000)) EventFactory(organization_name=organization.name, name="Event Two", start_time_notz=datetime.now() + timedelta(20)) EventFactory(organization_name=organization.name, name="Event Five", start_time_notz=datetime.now() + timedelta(200)) EventFactory(organization_name=organization.name, name="Event Eight", start_time_notz=datetime.now() + timedelta(2000)) db.session.flush() # World Cup teams organization = OrganizationFactory(name='GER', type='Code for All') db.session.flush() # Create multiple events, some in the future, one in the past EventFactory(organization_name=organization.name, name="Past Event", start_time_notz=datetime.now() - timedelta(3000)) EventFactory(organization_name=organization.name, name="Event Three", start_time_notz=datetime.now() + timedelta(30)) EventFactory(organization_name=organization.name, name="Event Six", start_time_notz=datetime.now() + timedelta(300)) EventFactory(organization_name=organization.name, name="Event Nine", start_time_notz=datetime.now() + timedelta(3000)) db.session.flush() response = self.app.get( '/api/events/upcoming_events?organization_type=Code for All') response_json = json.loads(response.data) self.assertEqual(len(response_json['objects']), 6) self.assertEqual(response_json['objects'][0]['name'], "Event One") self.assertEqual(response_json['objects'][1]['name'], "Event Three") self.assertEqual(response_json['objects'][5]['name'], "Event Nine")