def test_cannot_add_legislator_events_twice(self): legislator = LegislatorFactory.create() legislator_events_type = LegislatorEventsTypeFactory.create() date = date_to_timestamp(datetime.utcnow().date()) yield self.anonymous_fetch( '/legislator-events/', method='POST', body=dumps({ 'date': date, 'legislator_id': legislator.id, 'legislator_events_type_id': legislator_events_type.id }) ) try: yield self.anonymous_fetch( '/legislator-events/', method='POST', body=dumps({ 'date': date, 'legislator_id': legislator.id, 'legislator_events_type_id': legislator_events_type.id }) ) except HTTPError as e: expect(e).not_to_be_null() expect(e.code).to_equal(409) expect(e.response.reason).to_be_like( 'Legislator Events already exists' ) expect(loads(e.response.body)).to_equal({ 'message': 'Legislator Events already exists' })
def test_can_add_mandate(self): political_office = PoliticalOfficeFactory.create() legislator = LegislatorFactory.create() now = datetime.utcnow() date_start = date_to_timestamp(now.date()) date_end = date_to_timestamp((now + timedelta(days=10)).date()) response = yield self.anonymous_fetch( '/mandates/', method='POST', body=dumps({ 'date_start': date_start, 'date_end': date_end, 'political_office_id': political_office.id, 'legislator_id': legislator.id, }) ) expect(response.code).to_equal(200) mandate = loads(response.body) expect(mandate.get('date_start')).to_equal(date_start) expect(mandate.get('date_end')).to_equal(date_end) expect(mandate.get('political_office')).to_equal( political_office.to_dict() ) expect(mandate.get('legislator')).to_equal(legislator.to_dict())
def test_can_add_mandate(self, logging_mock): political_office = PoliticalOfficeFactory.create() legislator = LegislatorFactory.create() now = datetime.utcnow() d1 = now.date() date_start = date_to_timestamp(d1) d2 = (now + timedelta(days=10)).date() date_end = date_to_timestamp(d2) data = { 'legislator_id': legislator.id, 'political_office_id': political_office.id, 'date_start': date_start, 'date_end': date_end, } mandate = Mandate.add_mandate(self.db, data) expect(mandate.legislator).to_equal(legislator) expect(mandate.political_office).to_equal(political_office) expect(mandate.date_start).to_equal(d1) expect(mandate.date_end).to_equal(d2) expect(logging_mock.mock_calls).to_include( call.debug('Added mandate: "%s"', str(mandate)) )
def test_can_get_all_legislators(self): legislators = [] for x in range(5): legislator = LegislatorFactory.create() legislators.append(legislator.to_dict()) response = yield self.anonymous_fetch( '/legislators/', method='GET' ) expect(response.code).to_equal(200) legislators_loaded = loads(response.body) expect(legislators_loaded).to_length(5) expect(legislators_loaded).to_be_like(legislators)
def test_can_create_mandate(self): date_start = datetime.utcnow().date() date_end = (datetime.utcnow() + timedelta(days=10)).date() political_office = PoliticalOfficeFactory.create() legislator = LegislatorFactory.create() mandate = MandateFactory.create( legislator=legislator, political_office=political_office, date_start=date_start, date_end=date_end ) expect(mandate.id).not_to_be_null() expect(mandate.date_start).to_equal(date_start) expect(mandate.date_end).to_equal(date_end) expect(mandate.political_office).to_equal(political_office)
def test_cannot_add_legislator_events_without_legislator_events_type_id(self): legislator = LegislatorFactory.create() date = date_to_timestamp(datetime.utcnow().date()) try: yield self.anonymous_fetch( '/legislator-events/', method='POST', body=dumps({ 'date': date, 'legislator_id': legislator.id, }) ) except HTTPError as e: expect(e).not_to_be_null() expect(e.code).to_equal(400) expect(e.response.reason).to_be_like('Invalid Legislator Events')
def test_cannot_add_legislator_events_without_date(self): legislator = LegislatorFactory.create() legislator_events_type = LegislatorEventsTypeFactory.create() try: yield self.anonymous_fetch( '/legislator-events/', method='POST', body=dumps({ 'legislator_id': legislator.id, 'legislator_events_type_id': legislator_events_type.id }) ) except HTTPError as e: expect(e).not_to_be_null() expect(e.code).to_equal(400) expect(e.response.reason).to_be_like('Invalid Legislator Events')
def test_cannot_add_mandate_twice(self): date_start = datetime.utcnow().date() date_end = (datetime.utcnow() + timedelta(days=10)).date() political_office = PoliticalOfficeFactory.create() legislator = LegislatorFactory.create() MandateFactory.create( legislator=legislator, political_office=political_office, date_start=date_start, date_end=date_end ) with expect.error_to_happen(IntegrityError): MandateFactory.create( legislator=legislator, political_office=political_office, date_start=date_start, date_end=date_end )
def test_can_convert_to_dict(self): legislator = LegislatorFactory.create() legislator_dict = legislator.to_dict() expect(legislator_dict.keys()).to_length(7) expect(legislator_dict.keys()).to_be_like([ 'name', 'picture', 'website', 'email', 'gender', 'date_of_birth', 'about', ]) date_of_birth = date_to_timestamp(legislator.date_of_birth) expect(legislator_dict['name']).to_equal(legislator.name) expect(legislator_dict['picture']).to_equal(legislator.picture) expect(legislator_dict['website']).to_equal(legislator.website) expect(legislator_dict['email']).to_equal(legislator.email) expect(legislator_dict['gender']).to_equal(legislator.gender) expect(legislator_dict['date_of_birth']).to_equal(date_of_birth) expect(legislator_dict['about']).to_equal(legislator.about)
def test_can_create_legislator(self): date = datetime.utcnow().date() legislator = LegislatorFactory.create( name='Marcelo Jorge Vieira', picture='http://domain.com/picture.png', website='http://domain.com/', email='*****@*****.**', gender='M', date_of_birth=date, about='Heavy Metal', ) expect(legislator.id).not_to_be_null() expect(legislator.name).to_equal('Marcelo Jorge Vieira') expect(legislator.picture).to_equal('http://domain.com/picture.png') expect(legislator.website).to_equal('http://domain.com/') expect(legislator.email).to_equal('*****@*****.**') expect(legislator.gender).to_equal('M') expect(legislator.date_of_birth).to_equal(date) expect(legislator.about).to_equal('Heavy Metal')
def test_can_add_legislator_events(self): legislator = LegislatorFactory.create() legislator_events_type = LegislatorEventsTypeFactory.create() date = date_to_timestamp(datetime.utcnow().date()) response = yield self.anonymous_fetch( '/legislator-events/', method='POST', body=dumps({ 'date': date, 'legislator_id': legislator.id, 'legislator_events_type_id': legislator_events_type.id }) ) expect(response.code).to_equal(200) legislator_events = loads(response.body) expect(legislator_events.get('date')).to_equal(date) expect(legislator_events.get('legislator')).to_equal(legislator.to_dict()) expect(legislator_events.get('legislator_events_type')).to_equal( legislator_events_type.to_dict() )
def test_cannot_add_mandate_twice(self): political_office = PoliticalOfficeFactory.create() legislator = LegislatorFactory.create() now = datetime.utcnow() date_start = date_to_timestamp(now.date()) date_end = date_to_timestamp((now + timedelta(days=10)).date()) yield self.anonymous_fetch( '/mandates/', method='POST', body=dumps({ 'date_start': date_start, 'date_end': date_end, 'political_office_id': political_office.id, 'legislator_id': legislator.id, }) ) try: yield self.anonymous_fetch( '/mandates/', method='POST', body=dumps({ 'date_start': date_start, 'date_end': date_end, 'political_office_id': political_office.id, 'legislator_id': legislator.id, }) ) except HTTPError as e: expect(e).not_to_be_null() expect(e.code).to_equal(409) expect(e.response.reason).to_be_like('Mandate already exists') expect(loads(e.response.body)).to_equal({ 'message': 'Mandate already exists' })
def test_cannot_add_mandate_without_date_end(self): political_office = PoliticalOfficeFactory.create() legislator = LegislatorFactory.create() now = datetime.utcnow() date_start = date_to_timestamp(now.date()) try: yield self.anonymous_fetch( '/mandates/', method='POST', body=dumps({ 'date_start': date_start, 'political_office_id': political_office.id, 'legislator_id': legislator.id, }) ) except HTTPError as e: expect(e).not_to_be_null() expect(e.code).to_equal(422) expect(e.response.reason).to_be_like('Invalid Mandate') expect(loads(e.response.body)).to_equal({ 'message': 'Invalid Mandate' })
def test_can_convert_to_dict_with_none_date_of_birth(self): legislator = LegislatorFactory.create() legislator.date_of_birth = None legislator_dict = legislator.to_dict() expect(legislator_dict['date_of_birth']).to_be_null()