def test_duid_init_fail():
    with raises(Exception) as got:
        DataUnitID(None)
    assert_exception_correct(got.value, ValueError(
        'upa cannot be a value that evaluates to false'))

    with raises(Exception) as got:
        DataUnitID(UPA('1/1/1'), 'a' * 257)
    assert_exception_correct(got.value, IllegalParameterError(
        'dataid exceeds maximum length of 256'))
def test_duid_hash():
    # string hashes will change from instance to instance of the python interpreter, and therefore
    # tests can't be written that directly test the hash value. See
    # https://docs.python.org/3/reference/datamodel.html#object.__hash__
    assert hash(DataUnitID(UPA('1/1/1'))) == hash(DataUnitID(UPA('1/1/1')))
    assert hash(DataUnitID(UPA('1/1/1'), 'foo')) == hash(DataUnitID(UPA('1/1/1'), 'foo'))

    assert hash(DataUnitID(UPA('1/1/1'))) != hash(DataUnitID(UPA('2/1/1')))
    assert hash(DataUnitID(UPA('1/1/1'), 'foo')) != hash(DataUnitID(UPA('2/1/1'), 'foo'))
    assert hash(DataUnitID(UPA('1/1/1'), 'foo')) != hash(DataUnitID(UPA('1/1/1'), 'fooo'))
def test_hash():
    # hashes will change from instance to instance of the python interpreter, and therefore
    # tests can't be written that directly test the hash value. See
    # https://docs.python.org/3/reference/datamodel.html#object.__hash__
    lid1 = uuid.UUID('1234567890abcdef1234567890abcdee')
    lid1a = uuid.UUID('1234567890abcdef1234567890abcdee')
    lid2 = uuid.UUID('1234567890abcdef1234567890abcdec')
    lid2a = uuid.UUID('1234567890abcdef1234567890abcdec')
    d1 = DataUnitID(UPA('1/1/1'))
    d1a = DataUnitID(UPA('1/1/1'))
    d2 = DataUnitID(UPA('1/1/2'))
    d2a = DataUnitID(UPA('1/1/2'))
    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    s1 = SampleNodeAddress(SampleAddress(id_, 1), 'foo')
    s1a = SampleNodeAddress(SampleAddress(id_, 1), 'foo')
    s2 = SampleNodeAddress(SampleAddress(id_, 2), 'foo')
    s2a = SampleNodeAddress(SampleAddress(id_, 2), 'foo')
    t1 = dt(500)
    t1a = dt(500)
    t2 = dt(600)
    t2a = dt(600)
    u1 = UserID('u')
    u1a = UserID('u')
    u2 = UserID('y')
    u2a = UserID('y')

    assert hash(DataLink(lid1, d1, s1, t1,
                         u1)) == hash(DataLink(lid1a, d1a, s1a, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1, None)) == hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a, None))
    assert hash(DataLink(lid2, d2, s2, t1, u2, t2, u1)) == hash(
        DataLink(lid2a, d2a, s2a, t1a, u2a, t2a, u1a))

    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid2, d1a, s1a, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d2, s1a, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d1a, s2, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d1a, s1a, t2, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u2))
    assert hash(DataLink(lid1, d1, s1, t1, u1, t2, u2)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a, t1, u2a))
    assert hash(DataLink(lid1, d1, s1, t1, u1, t2, u2)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a, t2a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1, t1, u2)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a, t1a, u2a))
def test_init_fail():
    lid = uuid.UUID('1234567890abcdef1234567890abcdee')
    d = DataUnitID(UPA('1/1/1'))
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')
    s = SampleNodeAddress(SampleAddress(sid, 1), 'a')
    t = dt(1)
    u = UserID('u')
    bt = datetime.datetime.now()
    n = None

    _init_fail(None, d, s, t, u, n, n,
               ValueError('id_ cannot be a value that evaluates to false'))
    _init_fail(lid, None, s, t, u, n, n,
               ValueError('duid cannot be a value that evaluates to false'))
    _init_fail(
        lid, d, None, t, u, n, n,
        ValueError(
            'sample_node_address cannot be a value that evaluates to false'))
    _init_fail(lid, d, s, None, u, n, n,
               ValueError('created cannot be a value that evaluates to false'))
    _init_fail(lid, d, s, bt, u, n, n,
               ValueError('created cannot be a naive datetime'))
    _init_fail(
        lid, d, s, t, None, n, n,
        ValueError('created_by cannot be a value that evaluates to false'))
    _init_fail(lid, d, s, t, u, bt, u,
               ValueError('expired cannot be a naive datetime'))
    _init_fail(
        lid, d, s, t, u, t, None,
        ValueError('expired_by cannot be a value that evaluates to false'))
    _init_fail(lid, d, s, dt(100), u, dt(99), u,
               ValueError('link cannot expire before it is created'))
def test_equals():
    lid1 = uuid.UUID('1234567890abcdef1234567890abcdee')
    lid1a = uuid.UUID('1234567890abcdef1234567890abcdee')
    lid2 = uuid.UUID('1234567890abcdef1234567890abcdec')
    lid2a = uuid.UUID('1234567890abcdef1234567890abcdec')
    d1 = DataUnitID(UPA('1/1/1'))
    d1a = DataUnitID(UPA('1/1/1'))
    d2 = DataUnitID(UPA('1/1/2'))
    d2a = DataUnitID(UPA('1/1/2'))
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')
    s1 = SampleNodeAddress(SampleAddress(sid, 1), 'foo')
    s1a = SampleNodeAddress(SampleAddress(sid, 1), 'foo')
    s2 = SampleNodeAddress(SampleAddress(sid, 2), 'foo')
    s2a = SampleNodeAddress(SampleAddress(sid, 2), 'foo')
    t1 = dt(500)
    t1a = dt(500)
    t2 = dt(600)
    t2a = dt(600)
    u1 = UserID('u')
    u1a = UserID('u')
    u2 = UserID('y')
    u2a = UserID('y')

    assert DataLink(lid1, d1, s1, t1, u1) == DataLink(lid1a, d1a, s1a, t1a,
                                                      u1a)
    assert DataLink(lid1, d1, s1, t1, u1,
                    None) == DataLink(lid1a, d1a, s1a, t1a, u1a, None)
    assert DataLink(lid2, d2, s2, t1, u2, t2,
                    u1) == DataLink(lid2a, d2a, s2a, t1a, u2a, t2a, u1a)

    assert DataLink(lid1, d1, s1, t1, u1) != (lid1, d1, s1, t1, u1)
    assert DataLink(lid1, d1, s1, t1, u1, t2,
                    u2) != (lid1, d1, s1, t1, u1, t2, u2)

    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid2, d1a, s1a, t1a, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d2, s1a, t1a, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d1a, s2, t1a, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d1a, s1a, t2, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d1a, s1a, t1, u2)
    assert DataLink(lid1, d1, s1, t1, u1, t2, u2) != DataLink(
        lid1a, d1a, s1a, t1a, u1a, t1, u2a)
    assert DataLink(lid1, d1, s1, t1, u1, t2, u2) != DataLink(
        lid1a, d1a, s1a, t1a, u1a, t2a, u1)
    assert DataLink(lid1, d1, s1, t1, u1, t1, u1) != DataLink(
        lid1a, d1a, s1a, t1a, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d1a, s1a, t1a,
                                                      u1a, t1a, u1a)
def test_links_to_dicts():
    links = [
        DataLink(
            UUID('f5bd78c3-823e-40b2-9f93-20e78680e41e'),
            DataUnitID(UPA('1/2/3'), 'foo'),
            SampleNodeAddress(
                SampleAddress(UUID('f5bd78c3-823e-40b2-9f93-20e78680e41f'), 6), 'foo'),
            dt(0.067),
            UserID('usera'),
            dt(89),
            UserID('userb')
        ),
        DataLink(
            UUID('f5bd78c3-823e-40b2-9f93-20e78680e41a'),
            DataUnitID(UPA('4/9/10')),
            SampleNodeAddress(
                SampleAddress(UUID('f5bd78c3-823e-40b2-9f93-20e78680e41b'), 4), 'bar'),
            dt(1),
            UserID('userc'),
        ),
    ]
    assert links_to_dicts(links) == [
        {
            'upa': '1/2/3',
            'dataid': 'foo',
            'id': 'f5bd78c3-823e-40b2-9f93-20e78680e41f',
            'version': 6,
            'node': 'foo',
            'created': 67,
            'createdby': 'usera',
            'expired': 89000,
            'expiredby': 'userb'
            },
        {
            'upa': '4/9/10',
            'dataid': None,
            'id': 'f5bd78c3-823e-40b2-9f93-20e78680e41b',
            'version': 4,
            'node': 'bar',
            'created': 1000,
            'createdby': 'userc',
            'expired': None,
            'expiredby': None
            }
    ]
def test_duid_init():
    duid = DataUnitID(UPA('1/2/3'))
    assert duid.upa == UPA('1/2/3')
    assert duid.dataid is None
    assert str(duid) == '1/2/3'

    duid = DataUnitID(UPA('1/2/3'), '')
    assert duid.upa == UPA('1/2/3')
    assert duid.dataid is None
    assert str(duid) == '1/2/3'

    duid = DataUnitID(UPA('1/2/3'), 'foo')
    assert duid.upa == UPA('1/2/3')
    assert duid.dataid == 'foo'
    assert str(duid) == '1/2/3:foo'

    duid = DataUnitID(UPA('1/2/3'), 'f' * 256)
    assert duid.upa == UPA('1/2/3')
    assert duid.dataid == 'f' * 256
    assert str(duid) == '1/2/3:' + 'f' * 256
def test_init_with_expire1():
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')

    dl = DataLink(uuid.UUID('1234567890abcdef1234567890abcdee'),
                  DataUnitID(UPA('2/6/4'), 'whee'),
                  SampleNodeAddress(SampleAddress(sid, 7), 'bar'), dt(400),
                  UserID('u'), dt(800), UserID('gotdam'))

    assert dl.id == uuid.UUID('1234567890abcdef1234567890abcdee')
    assert dl.duid == DataUnitID(UPA('2/6/4'), 'whee')
    assert dl.sample_node_address == SampleNodeAddress(SampleAddress(sid, 7),
                                                       'bar')
    assert dl.created == dt(400)
    assert dl.created_by == UserID('u')
    assert dl.expired == dt(800)
    assert dl.expired_by == UserID('gotdam')
    assert str(dl) == (
        'id=12345678-90ab-cdef-1234-567890abcdee ' + 'duid=[2/6/4:whee] ' +
        'sample_node_address=[12345678-90ab-cdef-1234-567890abcdef:7:bar] ' +
        'created=400.0 created_by=u expired=800.0 expired_by=gotdam')
def test_is_equivalent_fail():
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')
    dl1 = DataLink(uuid.UUID('1234567890abcdef1234567890abcdee'),
                   DataUnitID(UPA('2/6/4'), 'whee'),
                   SampleNodeAddress(SampleAddress(sid, 8), 'bar'), dt(400),
                   UserID('myuserᚥnameisHank'), dt(400), UserID('yay'))
    with raises(Exception) as got:
        dl1.is_equivalent(None)
    assert_exception_correct(
        got.value,
        ValueError('link cannot be a value that evaluates to false'))
Exemple #10
0
def get_data_unit_id_from_object(params: Dict[str, Any]) -> DataUnitID:
    '''
    Get a Data Unit ID from a parameter object. Expects an UPA in the key 'upa' and a data unit
    ID, if any, in the key dataID.

    :param params: the parameters.
    :returns: the DUID.
    :raises MissingParameterError: if the UPA is missing.
    :raises IllegalParameterError: if the UPA or data ID are illegal.
    '''
    _check_params(params)
    return DataUnitID(get_upa_from_object(params), _check_string_int(params, 'dataid'))
def test_links_to_dicts_fail_bad_args():
    dl = DataLink(
            UUID('f5bd78c3-823e-40b2-9f93-20e78680e41e'),
            DataUnitID(UPA('1/2/3'), 'foo'),
            SampleNodeAddress(
                SampleAddress(UUID('f5bd78c3-823e-40b2-9f93-20e78680e41f'), 6), 'foo'),
            dt(0.067),
            UserID('usera'),
            dt(89),
            UserID('userb')
        )

    _links_to_dicts_fail(None, ValueError('links cannot be None'))
    _links_to_dicts_fail([dl, None], ValueError(
        'Index 1 of iterable links cannot be a value that evaluates to false'))
def _create_data_link_params_with_update(update, expected):
    params = {
        'id': '706fe9e1-70ef-4feb-bbd9-32295104a119',
        'version': 1,
        'node': 'm',
        'upa': '1/1/1',
        'update': update
    }

    assert create_data_link_params(params) == (
        DataUnitID(UPA('1/1/1')),
        SampleNodeAddress(
            SampleAddress(UUID('706fe9e1-70ef-4feb-bbd9-32295104a119'), 1), 'm'),
        expected
    )
def test_init_no_expire():
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')

    dl = DataLink(
        uuid.UUID('1234567890abcdef1234567890abcdee'),
        DataUnitID(UPA('2/3/4')),
        SampleNodeAddress(SampleAddress(sid, 5), 'foo'),
        dt(500),
        UserID('usera'),
        expired_by=UserID('u')  # should be ignored
    )

    assert dl.id == uuid.UUID('1234567890abcdef1234567890abcdee')
    assert dl.duid == DataUnitID(UPA('2/3/4'))
    assert dl.sample_node_address == SampleNodeAddress(SampleAddress(sid, 5),
                                                       'foo')
    assert dl.created == dt(500)
    assert dl.created_by == UserID('usera')
    assert dl.expired is None
    assert dl.expired_by is None
    assert str(dl) == (
        'id=12345678-90ab-cdef-1234-567890abcdee ' + 'duid=[2/3/4] ' +
        'sample_node_address=[12345678-90ab-cdef-1234-567890abcdef:5:foo] ' +
        'created=500.0 created_by=usera expired=None expired_by=None')
def test_create_data_link_params_missing_update_key():
    params = {
        'id': '706fe9e1-70ef-4feb-bbd9-32295104a119',
        'version': 78,
        'node': 'mynode',
        'upa': '6/7/29',
        'dataid': 'mydata'
    }

    assert create_data_link_params(params) == (
        DataUnitID(UPA('6/7/29'), 'mydata'),
        SampleNodeAddress(
            SampleAddress(UUID('706fe9e1-70ef-4feb-bbd9-32295104a119'), 78), 'mynode'),
        False
    )
def test_is_equivalent_link():
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')

    _is_equivalent(DataUnitID(UPA('2/6/4'), 'whee'),
                   SampleNodeAddress(SampleAddress(sid, 7), 'bar'),
                   DataUnitID(UPA('2/6/4'), 'whee'),
                   SampleNodeAddress(SampleAddress(sid, 7), 'bar'), True)

    _is_equivalent(DataUnitID(UPA('2/6/4'), 'wheo'),
                   SampleNodeAddress(SampleAddress(sid, 7), 'bar'),
                   DataUnitID(UPA('2/6/4'), 'whee'),
                   SampleNodeAddress(SampleAddress(sid, 7), 'bar'), False)

    _is_equivalent(DataUnitID(UPA('2/6/4'), 'whee'),
                   SampleNodeAddress(SampleAddress(sid, 8), 'bar'),
                   DataUnitID(UPA('2/6/4'), 'whee'),
                   SampleNodeAddress(SampleAddress(sid, 7), 'bar'), False)
Exemple #16
0
def test_timestamp_seconds_to_milliseconds(samplestorage):
    ts1=1614958000000 # milliseconds
    ts2=1614958000    # seconds
    ts3=1614958       # seconds
    ts4=9007199254740.991 # seconds

    id1 = uuid.UUID('1234567890abcdef1234567890abcdef')
    id2 = uuid.UUID('1234567890abcdef1234567890abcdee')
    assert samplestorage.save_sample(
        SavedSample(id1, UserID('user'), [SampleNode('mynode')], dt(ts3), 'foo')) is True
    assert samplestorage.save_sample_version(
        SavedSample(id1, UserID('user'), [SampleNode('mynode1')], dt(ts2), 'foo')) == 2
    assert samplestorage.save_sample(
        SavedSample(id2, UserID('user'), [SampleNode('mynode2')], dt(ts3), 'foo')) is True

    lid1=uuid.UUID('1234567890abcdef1234567890abcde2')
    lid2=uuid.UUID('1234567890abcdef1234567890abcde3')
    lid3=uuid.UUID('1234567890abcdef1234567890abcde4')
    samplestorage.create_data_link(DataLink(
        lid1,
        DataUnitID(UPA('42/42/42'), 'dataunit1'),
        SampleNodeAddress(SampleAddress(id1, 1), 'mynode'),
        dt(ts2),
        UserID('user'))
    )

    samplestorage.create_data_link(DataLink(
        lid2,
        DataUnitID(UPA('5/89/32'), 'dataunit2'),
        SampleNodeAddress(SampleAddress(id2, 1), 'mynode2'),
        dt(ts3),
        UserID('user'))
    )

    _create_and_expire_data_link(
        samplestorage,
        DataLink(
            lid3,
            DataUnitID(UPA('5/89/33'), 'dataunit1'),
            SampleNodeAddress(SampleAddress(id1, 1), 'mynode'),
            dt(ts3),
            UserID('user')),
        dt(ts3+100),
        UserID('user')
    )

    assert samplestorage.get_sample(id1, 1).savetime == dt(ts3)
    assert samplestorage.get_sample(id1, 2).savetime == dt(ts2)
    assert samplestorage.get_sample(id2).savetime == dt(ts3)
    assert samplestorage.get_data_link(lid1).created == dt(ts2)
    assert samplestorage.get_data_link(lid2).created == dt(ts3)
    assert samplestorage.get_data_link(lid3).created == dt(ts3)
    assert samplestorage.get_data_link(lid3).expired == dt(ts3+100)

    threshold=1000000000000 # current timestamp in milliseconds is above 1600000000000
    query="""
        FOR sample1 IN samples_nodes
            FILTER sample1.saved < @threshold
            UPDATE sample1 WITH { saved: ROUND(sample1.saved * 1000) } IN samples_nodes
        FOR sample2 IN samples_version
            FILTER sample2.saved < @threshold
            UPDATE sample2 WITH { saved: ROUND(sample2.saved * 1000) } IN samples_version
        FOR link IN samples_data_link
            FILTER link.expired < @threshold OR link.created < @threshold
            UPDATE link WITH { 
                expired: link.expired < @threshold ? ROUND(link.expired * 1000) : link.expired,
                created: link.created < @threshold ? ROUND(link.created * 1000) : link.created
            } IN samples_data_link
        """

    samplestorage._db.aql.execute(query, bind_vars={'threshold': threshold})

    assert samplestorage.get_sample(id1, 1).savetime == dt(ts2)
    assert samplestorage.get_sample(id1, 2).savetime == dt(ts2)
    assert samplestorage.get_sample(id2).savetime == dt(ts2)
    assert samplestorage.get_data_link(lid1).created == dt(ts2)
    assert samplestorage.get_data_link(lid2).created == dt(ts2)
    assert samplestorage.get_data_link(lid3).created == dt(ts2)
    assert samplestorage.get_data_link(lid3).expired == dt((ts3+100) * 1000)

    samplestorage._db.aql.execute(query, bind_vars={'threshold': threshold})

    assert samplestorage.get_sample(id1, 1).savetime == dt(ts2)
    assert samplestorage.get_sample(id1, 2).savetime == dt(ts2)
    assert samplestorage.get_sample(id2).savetime == dt(ts2)
    assert samplestorage.get_data_link(lid1).created == dt(ts2)
    assert samplestorage.get_data_link(lid2).created == dt(ts2)
    assert samplestorage.get_data_link(lid3).created == dt(ts2)
    assert samplestorage.get_data_link(lid3).expired == dt((ts3+100) * 1000)
def test_get_data_unit_id_from_object():
    assert get_data_unit_id_from_object({'upa': '1/1/1'}) == DataUnitID(UPA('1/1/1'))
    assert get_data_unit_id_from_object({'upa': '8/3/2'}) == DataUnitID(UPA('8/3/2'))
    assert get_data_unit_id_from_object(
        {'upa': '8/3/2', 'dataid': 'a'}) == DataUnitID(UPA('8/3/2'), 'a')
def test_duid_equals():
    assert DataUnitID(UPA('1/1/1')) == DataUnitID(UPA('1/1/1'))
    assert DataUnitID(UPA('1/1/1'), 'foo') == DataUnitID(UPA('1/1/1'), 'foo')

    assert DataUnitID(UPA('1/1/1')) != UPA('1/1/1')

    assert DataUnitID(UPA('1/1/1')) != DataUnitID(UPA('2/1/1'))
    assert DataUnitID(UPA('1/1/1'), 'foo') != DataUnitID(UPA('2/1/1'), 'foo')
    assert DataUnitID(UPA('1/1/1'), 'foo') != DataUnitID(UPA('1/1/1'), 'fooo')