Esempio n. 1
0
 def _check_reports(self, committee_type, factory, schema):
     committee = factories.CommitteeFactory(committee_type=committee_type)
     factories.CommitteeHistoryFactory(
         committee_id=committee.committee_id,
         committee_key=committee.committee_key,
         committee_type=committee_type,
         cycle=2012,
     )
     end_dates = [datetime.datetime(2012, 1, 1), datetime.datetime(2008, 1, 1)]
     committee_id = committee.committee_id
     committee_key = committee.committee_key
     db.session.flush()
     [
         factory(
             committee_id=committee_id,
             committee_key=committee_key,
             coverage_end_date=end_date,
             report_year=2011,
         )
         for end_date in end_dates
     ]
     response = self._results(api.url_for(ReportsView, committee_id=committee_id))
     self.assertEqual(len(response), 2)
     self.assertEqual(response[0]['coverage_end_date'], isoformat(end_dates[0]))
     self.assertEqual(response[1]['coverage_end_date'], isoformat(end_dates[1]))
     assert response[0].keys() == schema().fields.keys()
Esempio n. 2
0
 def test_committee_date_filters(self):
     [
         factories.CommitteeFactory(first_file_date=datetime.datetime(2015, 1, 1)),
         factories.CommitteeFactory(first_file_date=datetime.datetime(2015, 2, 1)),
         factories.CommitteeFactory(first_file_date=datetime.datetime(2015, 3, 1)),
         factories.CommitteeFactory(first_file_date=datetime.datetime(2015, 4, 1)),
     ]
     results = self._results(api.url_for(CommitteeList, min_first_file_date='02/01/2015'))
     self.assertTrue(all(each['first_file_date'] >= isoformat(datetime.datetime(2015, 2, 1)) for each in results))
     results = self._results(api.url_for(CommitteeList, max_first_file_date='02/03/2015'))
     self.assertTrue(all(each['first_file_date'] <= isoformat(datetime.datetime(2015, 3, 1)) for each in results))
     results = self._results(
         api.url_for(
             CommitteeList,
             min_first_file_date='02/01/2015',
             max_first_file_date='02/03/2015',
         )
     )
     self.assertTrue(
         all(
             isoformat(datetime.datetime(2015, 2, 1))
             <= each['first_file_date']
             <= isoformat(datetime.datetime(2015, 3, 1))
             for each in results
         )
     )
Esempio n. 3
0
def test_meta_serializer_fields():
    u = User("John", age=42.3, email="*****@*****.**",
             homepage="http://john.com")
    s = UserMetaSchema().dump(u)
    assert s.data['name'] == u.name
    assert s.data['balance'] == "100.00"
    assert s.data['uppername'] == "JOHN"
    assert s.data['is_old'] is False
    assert s.data['created'] == utils.isoformat(u.created)
    assert s.data['updated_local'] == utils.isoformat(u.updated, localtime=True)
    assert s.data['finger_count'] == 10
Esempio n. 4
0
    def get(self, task_id=None):
        user = self.get_current_user()
        c = self.db.cursor()
        if task_id:
            # Get one task
            c.execute(
                'SELECT * FROM tasks WHERE id = ? AND user_id = ?',
                (task_id, user['id'],)
            )
        else:
            # List of user's tasks
            c.execute(
                'SELECT * FROM tasks WHERE user_id = ? ORDER BY created DESC',
                (user['id'], )
            )

        tasks = [dict(zip(t.keys(), t)) for t in c.fetchall()]

        if task_id and len(tasks) > 1:
            self.send_error(500,
                            message='Founded multiple tasks with the same ID.')
            return
        elif task_id and not tasks:
            self.send_error(400, message='Task is not found.')
            return

        for task in tasks:
            task['result'] = json_decode(task['result'])
            task['status'] = TaskStatus(task['status']).name
            for date_field in ('created', 'started', 'finished'):
                if task[date_field]:
                    task[date_field] = isoformat(task[date_field])
        self.finish(json_encode(tasks[0] if task_id else tasks))
Esempio n. 5
0
    def test_candidate(self):
        """Compare results to expected fields."""
        candidate_old = factories.CandidateDetailFactory(
            load_date=datetime.datetime(2014, 1, 2),
            **fields
        )
        candidate = factories.CandidateDetailFactory(
            load_date=datetime.datetime(2014, 1, 3),
            **fields
        )
        response = self._response(
            api.url_for(CandidateView, candidate_id=candidate.candidate_id)
        )
        self.assertResultsEqual(
            response['pagination'],
            {'count': 1, 'page': 1, 'pages': 1, 'per_page': 20})
        # we are showing the full history rather than one result
        self.assertEqual(len(response['results']), 1)

        result = response['results'][0]
        self.assertEqual(result['candidate_id'], candidate.candidate_id)
        self.assertEqual(result['form_type'], 'F2')
        # @todo - check for a value for expire_data
        self.assertEqual(result['expire_date'], None)
        # # most recent record should be first
        self.assertEqual(result['load_date'], isoformat(candidate.load_date))
        self.assertNotEqual(result['load_date'], isoformat(candidate_old.load_date))
        self.assertResultsEqual(result['name'], candidate.name)
        # #address
        self.assertEqual(result['address_city'], candidate.address_city)
        self.assertEqual(result['address_state'], candidate.address_state)
        self.assertEqual(result['address_street_1'], candidate.address_street_1)
        self.assertEqual(result['address_zip'], candidate.address_zip)
        # # office
        self.assertResultsEqual(result['office'], candidate.office)
        self.assertResultsEqual(result['district'], candidate.district)
        self.assertResultsEqual(result['state'], candidate.state)
        self.assertResultsEqual(result['office_full'], candidate.office_full)
        # # From party_mapping
        self.assertResultsEqual(result['party'], candidate.party)
        self.assertResultsEqual(result['party_full'], candidate.party_full)
        # From status_mapping
        self.assertResultsEqual(result['active_through'], candidate.active_through)
        self.assertResultsEqual(result['candidate_inactive'], candidate.candidate_inactive)
        self.assertResultsEqual(result['candidate_status'], candidate.candidate_status)
        self.assertResultsEqual(result['incumbent_challenge'], candidate.incumbent_challenge)
        self.assertResultsEqual(result['candidate_status_full'], candidate.candidate_status_full)
def isoformat(dt, localtime=False, *args, **kwargs):
    """ISO8601-formatted UTC representation of a datetime object.
    The timezone offset portion of the string will be replaced with a "Z" if the UTC offset is 0.
    """
    value = ma_utils.isoformat(dt, localtime=localtime, *args, **kwargs)
    if value.endswith('+00:00'):
        return "{}Z".format(value[:-6])
    return value
Esempio n. 7
0
 def __repr__(self):
     return ('<User id={id}, name={name}, created_at={created_at}, '
             'creator_email={creator_email}, contacts=[{contacts}]>').format(
                 id=self.id,
                 name=self.name,
                 created_at=utils.isoformat(self.created_at),
                 creator_email=self.creator_email,
                 contacts=', '.join((repr(c) for c in self.contacts)))
Esempio n. 8
0
 def __repr__(self):
     return ('<Contact id={id}, phone_no={phone}, email={email}, '
             'type={type}, created_at={created_at}>').format(
                 id=self.id,
                 phone=self.phone_no,
                 email=self.email,
                 type=self.TYPES_TO_NAMES[self.type],
                 created_at=utils.isoformat(self.created_at))
Esempio n. 9
0
def test_default_dateformat(user):
    class DateFormatSchema(Schema):
        updated = fields.DateTime(format="%m-%d")

        class Meta:
            fields = ('created', 'updated')
    serialized = DateFormatSchema().dump(user)
    assert serialized.data['created'] == utils.isoformat(user.created)
    assert serialized.data['updated'] == user.updated.strftime("%m-%d")
Esempio n. 10
0
 def test_get(self, wt):
     author = AuthorFactory()
     url = url_for('books.AuthorDetail:get', id=author.id)
     res = wt.get(url)
     assert res.status_code == http.OK
     data = res.json['result']
     assert data['first'] == author.first
     assert data['last'] == author.last
     assert data['created'] == isoformat(author.date_created)
     assert 'books' in data
Esempio n. 11
0
 def test_filter_date(self):
     [
         factories.FilingsFactory(receipt_date=datetime.datetime(2012, 1, 1)),
         factories.FilingsFactory(receipt_date=datetime.datetime(2013, 1, 1)),
         factories.FilingsFactory(receipt_date=datetime.datetime(2014, 1, 1)),
         factories.FilingsFactory(receipt_date=datetime.datetime(2015, 1, 1)),
     ]
     min_date = datetime.datetime(2013, 1, 1)
     results = self._results(api.url_for(FilingsList, min_receipt_date=min_date))
     self.assertTrue(all(each for each in results if each['receipt_date'] >= isoformat(min_date)))
     max_date = datetime.datetime(2014, 1, 1)
     results = self._results(api.url_for(FilingsList, max_receipt_date=max_date))
     self.assertTrue(all(each for each in results if each['receipt_date'] <= isoformat(max_date)))
     results = self._results(api.url_for(FilingsList, min_receipt_date=min_date, max_receipt_date=max_date))
     self.assertTrue(
         all(
             each for each in results
             if isoformat(min_date) <= each['receipt_date'] <= isoformat(max_date)
         )
     )
Esempio n. 12
0
 def test_committee_detail_fields(self):
     committee = factories.CommitteeDetailFactory(
         first_file_date=datetime.datetime(1982, 12, 31),
         committee_type="P",
         treasurer_name="Robert J. Lipshutz",
         party="DEM",
         form_type="F1Z",
         load_date=datetime.datetime(1982, 12, 31),
         street_1="1795 Peachtree Road",
         zip="30309",
     )
     response = self._response(api.url_for(CommitteeView, committee_id=committee.committee_id))
     result = response["results"][0]
     # main fields
     self.assertEqual(result["first_file_date"], isoformat(committee.first_file_date))
     self.assertEqual(result["committee_type"], committee.committee_type)
     self.assertEqual(result["treasurer_name"], committee.treasurer_name)
     self.assertEqual(result["party"], committee.party)
     # Things on the detailed view
     self.assertEqual(result["form_type"], committee.form_type)
     self.assertEqual(result["load_date"], isoformat(committee.load_date))
     self.assertEqual(result["street_1"], committee.street_1)
     self.assertEqual(result["zip"], committee.zip)
Esempio n. 13
0
 def test_committee_detail_fields(self):
     committee = factories.CommitteeDetailFactory(
         first_file_date=datetime.datetime(1982, 12, 31),
         committee_type='P',
         treasurer_name='Robert J. Lipshutz',
         party='DEM',
         form_type='F1Z',
         load_date=datetime.datetime(1982, 12, 31),
         street_1='1795 Peachtree Road',
         zip='30309',
     )
     response = self._response(api.url_for(CommitteeView, committee_id=committee.committee_id))
     result = response['results'][0]
     # main fields
     self.assertEqual(result['first_file_date'], isoformat(committee.first_file_date))
     self.assertEqual(result['committee_type'], committee.committee_type)
     self.assertEqual(result['treasurer_name'], committee.treasurer_name)
     self.assertEqual(result['party'], committee.party)
     # Things on the detailed view
     self.assertEqual(result['form_type'], committee.form_type)
     self.assertEqual(result['load_date'], isoformat(committee.load_date))
     self.assertEqual(result['street_1'], committee.street_1)
     self.assertEqual(result['zip'], committee.zip)
Esempio n. 14
0
 def test_committee_list_fields(self):
     committee = factories.CommitteeFactory(
         first_file_date=datetime.datetime(1982, 12, 31),
         committee_type='P',
         treasurer_name='Robert J. Lipshutz',
         party='DEM',
     )
     response = self._response(api.url_for(CommitteeView, committee_id=committee.committee_id))
     result = response['results'][0]
     # main fields
     # original registration date doesn't make sense in this example, need to look into this more
     self.assertEqual(result['first_file_date'], isoformat(committee.first_file_date))
     self.assertEqual(result['committee_type'], committee.committee_type)
     self.assertEqual(result['treasurer_name'], committee.treasurer_name)
     self.assertEqual(result['party'], committee.party)
Esempio n. 15
0
 def test_committee_list_fields(self):
     committee = factories.CommitteeFactory(
         first_file_date=datetime.datetime(1982, 12, 31),
         committee_type="P",
         treasurer_name="Robert J. Lipshutz",
         party="DEM",
     )
     response = self._response(api.url_for(CommitteeView, committee_id=committee.committee_id))
     result = response["results"][0]
     # main fields
     # original registration date doesn't make sense in this example, need to look into this more
     self.assertEqual(result["first_file_date"], isoformat(committee.first_file_date))
     self.assertEqual(result["committee_type"], committee.committee_type)
     self.assertEqual(result["treasurer_name"], committee.treasurer_name)
     self.assertEqual(result["party"], committee.party)
Esempio n. 16
0
 def test_reports_sort_default(self):
     committee = factories.CommitteeFactory(committee_type='H')
     committee_id = committee.committee_id
     factories.CommitteeHistoryFactory(
         committee_id=committee_id,
         committee_type='H',
     )
     dates = [
         datetime.datetime(2015, 7, 4),
         datetime.datetime(2015, 7, 5),
     ]
     dates_formatted = [isoformat(each) for each in dates]
     factories.ReportsHouseSenateFactory(committee_id=committee_id, coverage_end_date=dates[0])
     factories.ReportsHouseSenateFactory(committee_id=committee_id, coverage_end_date=dates[1])
     results = self._results(api.url_for(ReportsView, committee_id=committee_id))
     self.assertEqual([each['coverage_end_date'] for each in results], dates_formatted[::-1])
Esempio n. 17
0
def test_datetime_iso_field(user, serialized_user):
    assert serialized_user.data['created_iso'] == utils.isoformat(user.created)
Esempio n. 18
0
def test_isoformat_localtime():
    d = central.localize(dt.datetime(2013, 11, 10, 1, 23, 45), is_dst=False)
    assert utils.isoformat(d, localtime=True) == "2013-11-10T01:23:45-06:00"
Esempio n. 19
0
def test_isoformat_tzaware():
    d = central.localize(dt.datetime(2013, 11, 10, 1, 23, 45), is_dst=False)
    assert utils.isoformat(d) == "2013-11-10T07:23:45+00:00"
Esempio n. 20
0
def _format_value(value):
    if isinstance(value, datetime.datetime):
        return isoformat(value)
    if isinstance(value, datetime.date):
        return value.isoformat()
    return value
Esempio n. 21
0
 def test_datetime_serializes_to_iso_by_default(self, user):
     field = fields.DateTime()  # No format specified
     expected = utils.isoformat(user.created, localtime=False)
     assert field.serialize('created', user) == expected
Esempio n. 22
0
def test_isoformat_localtime():
    d = central.localize(dt.datetime(2013, 11, 10, 1, 23, 45), is_dst=False)
    assert utils.isoformat(d, localtime=True) == "2013-11-10T01:23:45-06:00"
Esempio n. 23
0
 def test_datetime_deserializes_to_iso_by_default(self, user):
     field = fields.DateTime()  # No format specified
     expected = utils.isoformat(user.created, localtime=False)
     assert field.serialize("created", user) == expected
Esempio n. 24
0
 def test_datetime_iso8601(self):
     field = fields.DateTime(format="iso")
     expected = utils.isoformat(self.user.created, localtime=False)
     assert field.output("created", self.user) == expected
Esempio n. 25
0
def test_isoformat():
    d = dt.datetime(2013, 11, 10, 1, 23, 45)
    assert utils.isoformat(d) == '2013-11-10T01:23:45+00:00'
Esempio n. 26
0
 def test_localdatetime_iso(self):
     field = fields.LocalDateTime(format="iso")
     expected = utils.isoformat(self.user.created, localtime=True)
     assert field.output("created", self.user) == expected
Esempio n. 27
0
 def test_localdatetime_iso(self):
     field = fields.LocalDateTime(format="iso")
     expected = utils.isoformat(self.user.created, localtime=True)
     assert field.output("created", self.user) == expected
Esempio n. 28
0
 def test_datetime_iso8601(self):
     field = fields.DateTime(format="iso")
     expected = utils.isoformat(self.user.created, localtime=False)
     assert field.output("created", self.user) == expected
Esempio n. 29
0
 def test_datetime_iso8601(self, fmt, user):
     field = fields.DateTime(format=fmt)
     expected = utils.isoformat(user.created, localtime=False)
     assert field.serialize('created', user) == expected
Esempio n. 30
0
 def test_serialize_single(self):
     book = BookFactory()
     data = BookMarshal(book, strict=True).data
     assert data['created'] == isoformat(book.date_created)
     assert 'author' in data
Esempio n. 31
0
 def test_localdatetime_iso(self, user):
     field = fields.LocalDateTime(format='iso')
     expected = utils.isoformat(user.created, localtime=True)
     assert field.serialize('created', user) == expected
Esempio n. 32
0
 def test_localdatetime_iso(self):
     field = fields.LocalDateTime(format="iso")
     assert_equal(
         field.output("created", self.user),
         utils.isoformat(self.user.created, localtime=True),
     )
Esempio n. 33
0
def iso_datetime():
    return isoformat(datetime.utcnow().replace(microsecond=0))
Esempio n. 34
0
def test_isoformat():
    d = dt.datetime(2013, 11, 10, 1, 23, 45)
    assert utils.isoformat(d) == '2013-11-10T01:23:45+00:00'
Esempio n. 35
0
def test_datetime_iso_field(user, serialized_user):
    assert serialized_user.data['created_iso'] == utils.isoformat(user.created)
Esempio n. 36
0
        "series": [{
            "name": "First Stars",
            "number": 4
        }],
        "titles": [{
            "subtitle": "From Hayashi to the Future",
            "title": "First Stars IV"
        }],
        "urls": [{
            "description": "web page",
            "value": "http://tpweb2.phys.konan-u.ac.jp/~FirstStar4/",
        }],
        "number_of_contributions":
        0,
    }
    expected_created = utils.isoformat(record.created)
    expected_updated = utils.isoformat(record.updated)
    with inspire_app.test_client() as client:
        response = client.get(f"/conferences/{record_control_number}",
                              headers=headers)

    response_data = json.loads(response.data)
    response_data_metadata = response_data["metadata"]
    response_created = response_data["created"]
    response_updated = response_data["updated"]

    assert expected_metadata == response_data_metadata
    assert expected_created == response_created
    assert expected_updated == response_updated

Esempio n. 37
0
 def default(self, obj):
     if isinstance(obj, datetime):
         return isoformat(obj)
     return JSONEncoder.default(self, obj)
def test_isoformat(value, expected):
    assert utils.isoformat(value) == expected
Esempio n. 39
0
def _format_value(value):
    if isinstance(value, datetime.datetime):
        return isoformat(value)
    if isinstance(value, datetime.date):
        return value.isoformat()
    return value
Esempio n. 40
0
 def test_isoformat(self):
     d = dt.datetime(2013, 11, 10, 1, 23, 45)
     assert_equal(utils.isoformat(d), '2013-11-10T01:23:45+00:00')
Esempio n. 41
0
def test_isoformat_tzaware():
    d = central.localize(dt.datetime(2013, 11, 10, 1, 23, 45), is_dst=False)
    assert utils.isoformat(d) == "2013-11-10T07:23:45+00:00"
Esempio n. 42
0
 def test_datetime_iso_field(self):
     assert_equal(
         self.serialized.data['created_iso'],
         utils.isoformat(self.obj.created)
     )
Esempio n. 43
0
 def test_localdatetime_iso(self, user):
     field = fields.LocalDateTime(format="iso")
     expected = utils.isoformat(user.created, localtime=True)
     assert field.serialize("created", user) == expected
Esempio n. 44
0
 def test_datetime_iso8601(self):
     field = fields.DateTime(format="iso")
     assert_equal(
         field.output("created", self.user),
         utils.isoformat(self.user.created, localtime=False),
     )
Esempio n. 45
0
 def test_datetime_iso8601(self, fmt, user):
     field = fields.DateTime(format=fmt)
     expected = utils.isoformat(user.created, localtime=False)
     assert field.serialize("created", user) == expected
Esempio n. 46
0
 def test_serialize_single(self):
     author = AuthorFactory()
     data = AuthorMarshal(author, strict=True).data
     assert data['first'] == author.first
     assert data['last'] == author.last
     assert data['created'] == isoformat(author.date_created)