Beispiel #1
0
def test_round_trip_annotations():
    """Test that annotations can make the round trip from a simple dictionary to the synapse format and back"""
    a = dict(foo=1234, zoo=[123.1, 456.2, 789.3], species='Moose',
             birthdays=[Datetime(1969, 4, 28), Datetime(1973, 12, 8), Datetime(2008, 1, 3), Datetime(2013, 3, 15)])
    sa = to_synapse_annotations(a)
    a2 = from_synapse_annotations(sa)
    a = a2
Beispiel #2
0
def test_timestamp_mismatch(is_java, con):
    if not is_java:
        mst = pytz.timezone("America/Edmonton")
        con.run("SET SESSION TIME ZONE 'America/Edmonton'")
        try:
            con.run("CREATE TEMPORARY TABLE TestTz "
                    "(f1 timestamp with time zone, "
                    "f2 timestamp without time zone)")
            con.run(
                "INSERT INTO TestTz (f1, f2) VALUES (:v1, :v2)",
                # insert timestamp into timestamptz field (v1)
                v1=Datetime(2001, 2, 3, 4, 5, 6, 170000),
                # insert timestamptz into timestamp field (v2)
                v2=mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000)))
            retval = con.run("SELECT f1, f2 FROM TestTz")

            # when inserting a timestamp into a timestamptz field,
            # postgresql assumes that it is in local time. So the value
            # that comes out will be the server's local time interpretation
            # of v1. We've set the server's TZ to MST, the time should
            # be...
            f1 = retval[0][0]
            assert f1 == Datetime(2001, 2, 3, 11, 5, 6, 170000, Timezone.utc)

            # inserting the timestamptz into a timestamp field, pg8000
            # converts the value into UTC, and then the PG server converts
            # it into local time for insertion into the field. When we
            # query for it, we get the same time back, like the tz was
            # dropped.
            f2 = retval[0][1]
            assert f2 == Datetime(2001, 2, 3, 4, 5, 6, 170000)
        finally:
            con.run("SET SESSION TIME ZONE DEFAULT")
def test_executemany(db_table):
    with db_table.cursor() as cursor:
        cursor.executemany("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)",
                           ((1, 1, "Avast ye!"), (2, 1, None)))

        cursor.executemany("select %s",
                           ((Datetime(2014, 5, 7, tzinfo=Timezone.utc), ),
                            (Datetime(2014, 5, 7), )))
Beispiel #4
0
def test_executemany(db_table):
    cursor = db_table.cursor()
    cursor.executemany(
        "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)",
        ((1, 1, "Avast ye!"), (2, 1, None)),
    )

    cursor.executemany(
        "select CAST(%s AS TIMESTAMP)",
        ((Datetime(2014, 5, 7, tzinfo=Timezone.utc),), (Datetime(2014, 5, 7),)),
    )
def test_more_annotations():
    """Test long, float and data annotations"""
    a = dict(foo=1234, zoo=[123.1, 456.2, 789.3], species='Platypus', birthdays=[Datetime(1969,4,28), Datetime(1973,12,8), Datetime(2008,1,3)])
    sa = to_synapse_annotations(a)
    # print sa
    assert sa['longAnnotations']['foo'] == [1234]
    assert sa['doubleAnnotations']['zoo'] == [123.1, 456.2, 789.3]
    assert sa['stringAnnotations']['species'] == ['Platypus']

    ## this part of the test is kinda fragile. It it breaks again, it should be removed
    bdays = [utils.from_unix_epoch_time(t) for t in sa['dateAnnotations']['birthdays']]
    assert all([t in bdays for t in [Datetime(1969,4,28), Datetime(1973,12,8), Datetime(2008,1,3)]])
Beispiel #6
0
def test_more_annotations():
    """Test long, float and data annotations"""
    a = Annotations(
        'syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', {
            'foo':
            1234,
            'zoo': [123.1, 456.2, 789.3],
            'species':
            'Platypus',
            'birthdays': [
                Datetime(1969, 4, 28),
                Datetime(1973, 12, 8),
                Datetime(2008, 1, 3)
            ],
            'test_boolean':
            True,
            'test_mo_booleans': [False, True, True, False]
        })
    sa = to_synapse_annotations(a)

    expected = {
        'id': 'syn123',
        'etag': '7bdb83e9-a50a-46e4-987a-4962559f090f',
        'annotations': {
            'foo': {
                'value': ['1234'],
                'type': 'LONG'
            },
            'zoo': {
                'value': ['123.1', '456.2', '789.3'],
                'type': 'DOUBLE'
            },
            'species': {
                'value': ['Platypus'],
                'type': 'STRING'
            },
            'birthdays': {
                'value': ['-21427200000', '124156800000', '1199318400000'],
                'type': 'TIMESTAMP_MS'
            },
            'test_boolean': {
                'value': ['true'],
                'type': 'STRING'
            },
            'test_mo_booleans': {
                'value': ['false', 'true', 'true', 'false'],
                'type': 'STRING'
            }
        }
    }
    assert expected == sa
Beispiel #7
0
    def testExecutemany(self):
        try:
            cursor = self.db.cursor()
            cursor.executemany(
                "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)",
                ((1, 1, 'Avast ye!'), (2, 1, None)))

            cursor.executemany(
                "select %s",
                (
                    (Datetime(2014, 5, 7, tzinfo=Timezone.utc), ),
                    (Datetime(2014, 5, 7),)))
        finally:
            cursor.close()
            self.db.commit()
Beispiel #8
0
def create_ical_download(open_day, end_day, data):

    semester_open_day = Datetime.strptime(open_day,
                                          "%Y-%m-%d").strftime("%Y%m%d")
    semester_end_day = (Datetime.strptime(end_day, "%Y-%m-%d") +
                        timedelta(1)).strftime("%Y%m%d")

    # Add header of iCalendar
    cal = Calendar()
    cal.add('PRODID', '-//Google Inc//Google Calendar 70.9054//EN')
    cal.add('VERSION', '2.0')
    cal.add('TZID', 'Asia/Bangkok')
    cal.add('X-LIC-LOCATION', 'Asia/Bangkok')
    cal.add('TZOFFSETFROM', timedelta(hours=7))
    cal.add('TZOFFSETTO', timedelta(hours=7))
    cal.add('TZNAME', 'ICT')
    cal.add('DTSTART', Datetime(1970, 1, 1))

    subjects = getSubject(semester_open_day, semester_end_day, data)

    for subject in subjects:
        event = Event()
        event['summary'] = subject['subject']
        event['dtstart'] = subject['date_start'] + subject['time_start']
        event['dtend'] = subject['date_start'] + subject['time_end']
        event[
            'rrule'] = 'FREQ=WEEKLY;UNTIL=' + semester_end_day + ';BYDAY=' + subject[
                'day_of_week']
        event['description'] = subject['description']
        event['location'] = subject['location']
        cal.add_component(event)

    return display(cal.to_ical())
Beispiel #9
0
def do_get(sess):

    base_name = []

    if 'scenario_id' in request.values:
        scenario_id = req_int('scenario_id')
        scenario_props = None
    else:
        year = req_int("finish_year")
        month = req_int("finish_month")
        months = req_int("months")
        start_date = Datetime(year, month, 1) - \
            relativedelta(months=months - 1)
        scenario_props = {
            'scenario_start': start_date,
            'scenario_duration': months
        }
        scenario_id = None
        base_name.append('monthly_duration')

    site_id = req_int('site_id') if 'site_id' in request.values else None
    supply_id = req_int('supply_id') if 'supply_id' in request.values else None
    if 'compression' in request.values:
        compression = req_bool('compression')
    else:
        compression = True
    user = g.user

    threading.Thread(target=content,
                     args=(scenario_props, scenario_id, base_name, site_id,
                           supply_id, user, compression)).start()
    return chellow_redirect("/downloads", 303)
Beispiel #10
0
def test_dumps():
    book = OrderedDict(
        (('title', 'A Hero of Our Time'), ('read_date',
                                           Datetime(2017,
                                                    7,
                                                    16,
                                                    14,
                                                    5,
                                                    tzinfo=Timezone.utc)),
         ('would_recommend', True), ('description', None),
         ('number_of_novellas', 5), ('price', Decimal('7.99')),
         ('weight', 6.88), ('key', bytearray(b'kshhgrl')),
         ('tags', ['russian', 'novel', '19th centuary'])))

    ion_str = """{
  'description': null,
  'key': {{ a3NoaGdybA== }},
  'number_of_novellas': 5,
  'price': 7.99,
  'read_date': 2017-07-16T14:05:00Z,
  'tags': [
    "russian",
    "novel",
    "19th centuary"],
  'title': "A Hero of Our Time",
  'weight': 6.88,
  'would_recommend': true}"""

    assert dumps(book) == ion_str
Beispiel #11
0
class SSD():
    __tablename__ = 'ssd'
    id = Column(Integer, primary_key=True)
    model = Column('model', String(32))
    maker = Column('model', String(20))
    capacity = Column('capacity', Integer, default=1)
    created_on = Column('created_on', Datetime, default=Datetime.now(tz=timezone('America/Montreal')))
Beispiel #12
0
class Gpu():
    __tablename__ = 'gpu'
    id = Column(Integer, primary_key=True)
    model = Column('model', String(32))
    ram = Column('ram', String(10), default=1)
    maker = Column('maker', String(20))
    created_on = Column('created_on', Datetime, default=Datetime.now(tz=timezone('America/Montreal')))
Beispiel #13
0
    def quantize(self, duration, snap_out=False, timezone='UTC'):
        """Returns a quanitzed interval."""
        # Convert seconds to timedelta, if appropriate.
        duration = _seconds_or_timedelta(duration)
        timezone = pytz.timezone(timezone)
        if duration <= timedelta(seconds=0):
            raise ValueError('cannot quantize by non-positive timedelta')

        epoch = timezone.localize(Datetime(1970, 1, 1))
        seconds = int(duration.total_seconds())
        start_seconds = int(
            (self.start.datetime(naive=False) - epoch).total_seconds()
        )
        end_seconds = int(
            (self.end.datetime(naive=False) - epoch).total_seconds()
        )
        if start_seconds % seconds and not snap_out:
            start_seconds += seconds
        if end_seconds % seconds and snap_out:
            end_seconds += seconds
        start_seconds -= start_seconds % seconds
        end_seconds -= end_seconds % seconds
        if start_seconds > end_seconds:
            start_seconds = end_seconds
        return MayaInterval(
            start=MayaDT.from_datetime(epoch).add(seconds=start_seconds),
            end=MayaDT.from_datetime(epoch).add(seconds=end_seconds),
        )
Beispiel #14
0
class Memory():
    __tablename__ = 'memory'
    id = Column(Integer, primary_key=True)
    model = Column('model', String(32), unique=True)
    frequency = Column('frequency', Integer, default=2133)
    maker = Column('maker', String(20))
    created_on = Column('created_on', Datetime, default=Datetime.now(tz=timezone('America/Montreal')))
Beispiel #15
0
 def __dt_to_epoch(dt):
     """Converts a datetime into an epoch."""
     # Assume UTC if no datetime is provided.
     if dt.tzinfo is None:
         dt = dt.replace(tzinfo=pytz.utc)
     epoch_start = Datetime(*MayaDT.__EPOCH_START, tzinfo=pytz.timezone('UTC'))
     return (dt - epoch_start).total_seconds()
Beispiel #16
0
def test_timestamp_tz_roundtrip(is_java, cursor):
    if not is_java:
        mst = pytz.timezone("America/Edmonton")
        v1 = mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000))
        cursor.execute("SELECT cast(%s as timestamptz)", (v1,))
        v2 = cursor.fetchall()[0][0]
        assert v2.tzinfo is not None
        assert v1 == v2
Beispiel #17
0
class Cpu():
    __tablename__ = 'cpu'
    id = Column(Integer, primary_key=True)
    model = Column('model', String(32), unique=True)
    frequency = Column('frequency', Float, default=2.0)
    nb_cores = Colum('nb_cores', Integer, default=1)
    maker = Column('maker', String(20))
    created_on = Column('created_on', Datetime, default=Datetime.now(tz=timezone('America/Montreal')))
Beispiel #18
0
class Motherboard():
    __tablename__ = 'motherboard'
    id = Column(Integer, primary_key=True)
    model = Column('model', String(32))
    pcie_qty = Column('pcie_qty', Integer, default=1)
    sata_qty = Column('sata_qty', Integer, default=1)
    fans_qty = Column('fans_qty', Integer, default=1)
    created_on = Column('created_on', Datetime, default=Datetime.now(tz=timezone('America/Montreal')))
Beispiel #19
0
def test_idempotent_annotations():
    """test that to_synapse_annotations won't mess up a dictionary that's already in the synapse format"""
    a = dict(species='Moose', n=42, birthday=Datetime(1969, 4, 28))
    sa = to_synapse_annotations(a)
    a2 = dict()
    a2.update(sa)
    sa2 = to_synapse_annotations(a2)
    assert_equals(sa, sa2)
Beispiel #20
0
def test_timestamp_tz_roundtrip(is_java, con):
    if not is_java:
        mst = pytz.timezone("America/Edmonton")
        v1 = mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000))
        retval = con.run("SELECT cast(:v as timestamptz)", v=v1)
        v2 = retval[0][0]
        assert v2.tzinfo is not None
        assert v1 == v2
def test_mixed_annotations():
    """test that to_synapse_annotations will coerce a list of mixed types to strings"""
    a = dict(foo=[1, 'a', Datetime(1969, 4, 28, 11, 47)])
    sa = to_synapse_annotations(a)
    a2 = from_synapse_annotations(sa)
    assert a2['foo'][0] == '1'
    assert a2['foo'][1] == 'a'
    assert a2['foo'][2].find('1969') > -1
Beispiel #22
0
def test_timestamp_tz_out(con):
    retval = con.run("SELECT '2001-02-03 04:05:06.17 America/Edmonton'"
                     "::timestamp with time zone")
    dt = retval[0][0]
    assert dt.tzinfo is not None, "no tzinfo returned"
    assert dt.astimezone(Timezone.utc) == Datetime(
        2001, 2, 3, 11, 5, 6, 170000, Timezone.utc), \
        "retrieved value match failed"
Beispiel #23
0
 def testTimestampTzOut(self):
     self.cursor.execute("SELECT '2001-02-03 04:05:06.17 America/Edmonton'"
                         "::timestamp with time zone")
     retval = self.cursor.fetchall()
     dt = retval[0][0]
     self.assertEqual(dt.tzinfo is not None, True, "no tzinfo returned")
     self.assertEqual(dt.astimezone(Timezone.utc),
                      Datetime(2001, 2, 3, 11, 5, 6, 170000, Timezone.utc),
                      "retrieved value match failed")
Beispiel #24
0
def Timestamp(year, month, day, hour, minute, second):
    """Construct an object holding a timestamp value.

    This function is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.

    :rtype: :class:`datetime.datetime`
    """
    return Datetime(year, month, day, hour, minute, second)
Beispiel #25
0
def test_more_annotations():
    """Test long, float and data annotations"""
    a = dict(foo=1234,
             zoo=[123.1, 456.2, 789.3],
             species='Platypus',
             birthdays=[Datetime(1969, 4, 28), Datetime(1973, 12, 8), Datetime(2008, 1, 3)],
             test_boolean=True,
             test_mo_booleans=[False, True, True, False])
    sa = to_synapse_annotations(a)
    assert_equals(sa['longAnnotations']['foo'], [1234])
    assert_equals(sa['doubleAnnotations']['zoo'], [123.1, 456.2, 789.3])
    assert_equals(sa['stringAnnotations']['species'], ['Platypus'])
    assert_equals(sa['stringAnnotations']['test_boolean'], ['true'])
    assert_equals(sa['stringAnnotations']['test_mo_booleans'], ['false', 'true', 'true', 'false'])

    # this part of the test is kinda fragile. It it breaks again, it should be removed
    bdays = [utils.from_unix_epoch_time(t) for t in sa['dateAnnotations']['birthdays']]
    assert_true(all([t in bdays for t in [Datetime(1969, 4, 28), Datetime(1973, 12, 8), Datetime(2008, 1, 3)]]))
Beispiel #26
0
 def testTimestampTzRoundtrip(self):
     if not IS_JYTHON:
         mst = pytz.timezone("America/Edmonton")
         v1 = mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000))
         self.cursor.execute("SELECT %s as f1", (v1, ))
         retval = self.cursor.fetchall()
         v2 = retval[0][0]
         self.assertNotEqual(v2.tzinfo, None)
         self.assertEqual(v1, v2)
Beispiel #27
0
def test_idempotent_annotations():
    """test that to_synapse_annotations won't mess up a dictionary that's already in the synapse format"""
    a = Annotations('syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f',
                    {'species': 'Moose', 'n': 42, 'birthday': Datetime(1969, 4, 28), 'fact': True})
    sa = to_synapse_annotations(a)
    a2 = {}
    a2.update(sa)
    sa2 = to_synapse_annotations(a2)
    assert sa == sa2
Beispiel #28
0
def get_initialization_data(taxpayer, logger=None, process_params=None):
    try:
        initialization_data_backup = taxpayer['data'][
            'initialization'] if 'data' in taxpayer and 'initialization' in taxpayer[
                'data'] else {}
        new_initialization_data = dict(initialization_data_backup)
        year = None
        month = None
        for year in initialization_data_backup:
            months_of_this_year_to_initialize = new_initialization_data[year]
            if len(months_of_this_year_to_initialize) > 0:
                month = months_of_this_year_to_initialize.pop()
                new_initialization_data[
                    year] = months_of_this_year_to_initialize
                break
            else:
                del new_initialization_data[year]
                year = None
        if 'year' in process_params:
            year = process_params['year']
            month = process_params['month']
        if year is None and month is None:
            initialization_data = {
                'initialized': True,
                'new_initialization_data': new_initialization_data
            }  # End of initialization_data
        else:
            begin_date = Datetime(int(year), int(month), 1)
            end_date = Datetime(int(year), int(month),
                                _Utilities.get_month_days(month))
            initialization_data = {
                'initialized': False,
                'new_initialization_data': new_initialization_data,
                'year': year,
                'month': month,
                'begin_date': begin_date,
                'end_date': end_date
            }  # End of initialization_data
        return initialization_data
    except Exception as e:
        if logger is not None:
            logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Beispiel #29
0
def test_round_trip_annotations():
    """Test that annotations can make the round trip from a simple dictionary to the synapse format and back"""
    a = Annotations(
        'syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', {
            'foo': [1234],
            'zoo': [123.1, 456.2, 789.3],
            'species': ['Moose'],
            'birthdays': [
                Datetime(1969, 4, 28),
                Datetime(1973, 12, 8),
                Datetime(2008, 1, 3),
                Datetime(2013, 3, 15)
            ]
        })
    sa = to_synapse_annotations(a)
    a2 = from_synapse_annotations(sa)
    assert a == a2
    assert a.id == a2.id
    assert a.etag == a2.etag
Beispiel #30
0
def test__convert_to_annotations_list():
    """Test long, float and data annotations"""
    a = {'foo': 1234,
         'zoo': [123.1, 456.2, 789.3],
         'species': 'Platypus',
         'birthdays': [Datetime(1969, 4, 28), Datetime(1973, 12, 8), Datetime(2008, 1, 3)],
         'test_boolean': True,
         'test_mo_booleans': [False, True, True, False]}
    actual_annos = _convert_to_annotations_list(a)

    expected_annos = {'foo': {'value': ['1234'],
                              'type': 'LONG'},
                      'zoo': {'value': ['123.1', '456.2', '789.3'],
                              'type': 'DOUBLE'},
                      'species': {'value': ['Platypus'],
                                  'type': 'STRING'},
                      'birthdays': {'value': ['-21427200000', '124156800000', '1199318400000'],
                                    'type': 'TIMESTAMP_MS'},
                      'test_boolean': {'value': ['true'],
                                       'type': 'BOOLEAN'},
                      'test_mo_booleans': {'value': ['false', 'true', 'true', 'false'],
                                           'type': 'BOOLEAN'}}
    assert expected_annos == actual_annos

    a_with_single_value = {'foo': 1234,
                           'zoo': 123.1,
                           'species': 'Platypus',
                           'birthdays': Datetime(1969, 4, 28),
                           'test_boolean': True}
    actual_annos = _convert_to_annotations_list(a_with_single_value)

    expected_annos = {'foo': {'value': ['1234'],
                              'type': 'LONG'},
                      'zoo': {'value': ['123.1'],
                              'type': 'DOUBLE'},
                      'species': {'value': ['Platypus'],
                                  'type': 'STRING'},
                      'birthdays': {'value': ['-21427200000'],
                                    'type': 'TIMESTAMP_MS'},
                      'test_boolean': {'value': ['true'],
                                       'type': 'BOOLEAN'}}
    assert expected_annos == actual_annos