def test_sample_eq():
    sn = SampleNode('foo')
    sn2 = SampleNode('bar')

    assert Sample([sn], 'yay') == Sample([sn], 'yay')
    assert Sample([sn], 'yay') != Sample([sn2], 'yay')
    assert Sample([sn], 'yay') != Sample([sn], 'yooo')

    id1 = uuid.UUID('1234567890abcdef1234567890abcdef')
    id2 = uuid.UUID('1234567890abcdef1234567890abcdea')
    dt1 = dt(5)
    dt2 = dt(8)
    u = UserID('u')
    u2 = UserID('u2')

    assert SavedSample(id1, u, [sn], dt1) == SavedSample(id1, u, [sn], dt(5))
    assert SavedSample(id1, u, [sn], dt1) != SavedSample(id2, u, [sn], dt1)
    assert SavedSample(id1, u, [sn], dt1) != SavedSample(id1, u2, [sn], dt1)
    assert SavedSample(id1, u, [sn], dt1) != SavedSample(id1, u, [sn2], dt1)
    assert SavedSample(id1, u, [sn], dt1) != SavedSample(id1, u, [sn], dt2)

    assert SavedSample(id1, u, [sn], dt1, 'yay') == SavedSample(id1, u, [sn], dt1, 'yay')
    assert SavedSample(id1, u, [sn], dt1, 'yay') != SavedSample(id1, u, [sn], dt1, 'yooo')

    assert SavedSample(id1, u, [sn], dt2, 'yay', 6) == SavedSample(id1, u, [sn], dt2, 'yay', 6)
    assert SavedSample(id1, u, [sn], dt1, 'yay', 6) != SavedSample(id1, u, [sn], dt1, 'yay', 7)

    assert SavedSample(id1, u, [sn], dt1, 'yay') != Sample([sn], 'yay')
    assert Sample([sn], 'yay') != SavedSample(id1, u, [sn], dt1, 'yay')
def _sample_node_build_fail_metadata(meta, expected):
    with raises(Exception) as got:
        SampleNode('n', SubSampleType.BIOLOGICAL_REPLICATE, controlled_metadata=meta)
    assert_exception_correct(got.value, IllegalParameterError(expected.format('Controlled')))
    with raises(Exception) as got:
        SampleNode('n', SubSampleType.BIOLOGICAL_REPLICATE, user_metadata=meta)
    assert_exception_correct(got.value, IllegalParameterError(expected.format('User')))
def test_sample_build_fail():
    # not testing every permutation of failing check_string here, just one test to make sure
    # it's there

    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    u = UserID('user')
    sn = SampleNode('foo')
    tn = SampleNode('bar', SubSampleType.TECHNICAL_REPLICATE, 'foo')
    sn2 = SampleNode('baz')
    dup = SampleNode('foo')
    d = dt(8)

    _sample_build_fail(
        [sn], 'a' * 257, IllegalParameterError('name exceeds maximum length of 256'))
    _sample_build_fail([], None, MissingParameterError('At least one node per sample is required'))
    _sample_build_fail(
        [tn, sn], 'a', IllegalParameterError('The first node in a sample must be a BioReplicate'))
    _sample_build_fail([sn, tn, sn2], 'a', IllegalParameterError(
                       'BioReplicates must be the first nodes in the list of sample nodes.'))
    _sample_build_fail([sn, sn2, dup], 'a', IllegalParameterError(
                       'Duplicate sample node name: foo'))
    _sample_build_fail([sn2, tn], 'a', IllegalParameterError(
                        'Parent foo of node bar does not appear in node list prior to node.'))

    _sample_with_id_build_fail(None, u, [sn], d, None, None,
                               ValueError('id_ cannot be a value that evaluates to false'))
    _sample_with_id_build_fail(id_, None, [sn], d, None, None,
                               ValueError('user cannot be a value that evaluates to false'))
    _sample_with_id_build_fail(id_, u, [sn], None, None, None, ValueError(
                               'savetime cannot be a value that evaluates to false'))
    _sample_with_id_build_fail(id_, u, [sn], datetime.datetime.now(), None, None, ValueError(
                               'savetime cannot be a naive datetime'))
    _sample_with_id_build_fail(id_, u, [sn], d, None, 0, ValueError('version must be > 0'))
def test_sample_build():
    sn = SampleNode('foo')
    sn2 = SampleNode('bat')
    sn3 = SampleNode('bar', type_=SubSampleType.TECHNICAL_REPLICATE, parent='foo')
    sn4 = SampleNode('baz', type_=SubSampleType.SUB_SAMPLE, parent='foo')
    sndup = SampleNode('foo')

    s = Sample([sn])
    assert s.nodes == (sndup,)
    assert s.name is None

    s = Sample([sn, sn2, sn4, sn3], '   \t   foo    ')
    assert s.nodes == (sndup, sn2, sn4, sn3)
    assert s.name == 'foo'

    s = Sample([sn], 'a' * 256)
    assert s.nodes == (sndup,)
    assert s.name == 'a' * 256

    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    s = SavedSample(id_, UserID('user'), [sn], dt(6))
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name is None
    assert s.version is None

    s = SavedSample(id_, UserID('user2'), [sn], dt(6), 'foo')
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user2')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name == 'foo'
    assert s.version is None

    s = SavedSample(id_, UserID('user'), [sn], dt(6), 'foo', 1)
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name == 'foo'
    assert s.version == 1

    s = SavedSample(id_, UserID('user'), [sn], dt(6), 'foo', 8)
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name == 'foo'
    assert s.version == 8

    s = SavedSample(id_, UserID('user'), [sn], dt(6), version=8)
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name is None
    assert s.version == 8
def test_sample_node_build():
    sn = SampleNode('foo', SubSampleType.BIOLOGICAL_REPLICATE)
    assert sn.name == 'foo'
    assert sn.type == SubSampleType.BIOLOGICAL_REPLICATE
    assert sn.parent is None
    assert sn.controlled_metadata == {}
    assert sn.user_metadata == {}
    assert sn.source_metadata == ()

    sn = SampleNode('a' * 256, SubSampleType.TECHNICAL_REPLICATE, 'b' * 256)
    assert sn.name == 'a' * 256
    assert sn.type == SubSampleType.TECHNICAL_REPLICATE
    assert sn.parent == 'b' * 256
    assert sn.controlled_metadata == {}
    assert sn.user_metadata == {}
    assert sn.source_metadata == ()

    sn = SampleNode('a' * 256, SubSampleType.TECHNICAL_REPLICATE, 'b' * 256,
                    {'a' * 256: {'bar': 'baz', 'bat': 'wh\tee'},
                     'wugga': {'a': 'b' * 1024},
                     # tests that having a controlled key doesn't force a source key
                     'z': {'u': 'v'}},
                    {'a': {'b' * 256: 'fo\no', 'c': 1, 'd': 1.5, 'e': False}},
                    [SourceMetadata('a' * 256, 'sk', {'a': 'b'}),
                     SourceMetadata('wugga', 'sk', {'a': 'b'})])
    assert sn.name == 'a' * 256
    assert sn.type == SubSampleType.TECHNICAL_REPLICATE
    assert sn.parent == 'b' * 256
    assert sn.controlled_metadata == {'a' * 256: {'bar': 'baz', 'bat': 'wh\tee'},
                                      'wugga': {'a': 'b' * 1024},
                                      'z': {'u': 'v'}}
    assert sn.user_metadata == {'a': {'b' * 256: 'fo\no', 'c': 1, 'd': 1.5, 'e': False}}
    assert sn.source_metadata == (SourceMetadata('a' * 256, 'sk', {'a': 'b'}),
                                  SourceMetadata('wugga', 'sk', {'a': 'b'}))

    # 100KB when serialized to json
    meta = {str(i): {'b': '𐎦' * 25} for i in range(848)}
    meta['a'] = {'b': 'c' * 30}

    # Also 100KB when the size calculation routine is run
    smeta = [SourceMetadata(str(i), 'sksksk', {'x': '𐎦' * 25}) for i in range(848)]
    smeta.append(SourceMetadata('a', 'b' * 35, {'u': 'v'}))

    sn = SampleNode('a', SubSampleType.SUB_SAMPLE, 'b', meta, meta, smeta)
    assert sn.name == 'a'
    assert sn.type == SubSampleType.SUB_SAMPLE
    assert sn.parent == 'b'
    assert sn.controlled_metadata == meta
    assert sn.user_metadata == meta
    assert sn.source_metadata == tuple(smeta)
def test_create_sample_params_maximal():
    params = {'sample': {'version': 7,      # should be ignored
                         'save_date': 9,    # should be ignored
                         'id': '706fe9e1-70ef-4feb-bbd9-32295104a119',
                         'name': 'myname',
                         'node_tree': [{'id': 'foo',
                                        'type': 'BioReplicate'},
                                       {'id': 'bar',
                                        'parent': 'foo',
                                        'type': 'TechReplicate',
                                        'meta_controlled':
                                            {'concentration/NO2':
                                                {'species': 'NO2',
                                                 'units': 'ppm',
                                                 'value': 78.91,
                                                 'protocol_id': 782,
                                                 'some_boolean_or_other': True
                                                 }
                                             },
                                        'meta_user': {'location_name': {'name': 'my_buttocks'}}
                                        }
                                       ]
                         },
              'prior_version': 1}

    assert create_sample_params(params) == (
        Sample([
            SampleNode('foo'),
            SampleNode(
                'bar',
                SubSampleType.TECHNICAL_REPLICATE,
                'foo',
                {'concentration/NO2':
                    {'species': 'NO2',
                     'units': 'ppm',
                     'value': 78.91,
                     'protocol_id': 782,
                     'some_boolean_or_other': True
                     }
                 },
                {'location_name': {'name': 'my_buttocks'}}
                )
            ],
            'myname'
        ),
        UUID('706fe9e1-70ef-4feb-bbd9-32295104a119'),
        1)
def test_create_sample_params_minimal():
    params = {'sample': {'version': 7,      # should be ignored
                         'save_date': 9,    # should be ignored
                         'node_tree': [{'id': 'foo',
                                        'type': 'BioReplicate'}]
                         }}
    expected = Sample([SampleNode('foo')])

    assert create_sample_params(params) == (expected, None, None)
def test_sample_build_fail_sample_count():
    nodes = [SampleNode('s' + str(i)) for i in range(10000)]

    s = Sample(nodes)
    assert s.nodes == tuple(nodes)
    assert s.name is None

    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    s = SavedSample(id_, UserID('u'), nodes, dt(8))
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('u')
    assert s.nodes == tuple(nodes)
    assert s.savetime == dt(8)
    assert s.name is None
    assert s.version is None

    nodes.append(SampleNode('s10000'))
    _sample_build_fail(nodes, None, IllegalParameterError(
                       'At most 10000 nodes are allowed per sample'))
def _sample_node_build_fail_source_metadata(meta, expected, cmeta=None):
    if cmeta is None:
        cmeta = {sm.key: {'x': 'y'} for sm in meta if sm is not None}
    with raises(Exception) as got:
        SampleNode(
            'n',
            SubSampleType.BIOLOGICAL_REPLICATE,
            controlled_metadata=cmeta,
            source_metadata=meta)
    assert_exception_correct(got.value, expected)
def test_sample_to_dict_maximal():
    expected = {'node_tree': [{'id': 'foo',
                               'type': 'BioReplicate',
                               'meta_controlled': {},
                               'meta_user': {},
                               'parent': None
                               },
                              {'id': 'bar',
                               'type': 'TechReplicate',
                               'meta_controlled': {'a': {'b': 'c', 'm': 6.7}},
                               'meta_user': {'d': {'e': True}, 'g': {'h': 1}},
                               'parent': 'foo'
                               }],
                'id': 'f5bd78c3-823e-40b2-9f93-20e78680e41e',
                'user': '******',
                'save_date': 87897,
                'name': 'myname',
                'version': 23,
                }

    id_ = UUID('f5bd78c3-823e-40b2-9f93-20e78680e41e')

    s = sample_to_dict(
        SavedSample(
            id_,
            UserID('user3'),
            [SampleNode('foo'),
             SampleNode(
                 'bar',
                 SubSampleType.TECHNICAL_REPLICATE,
                 'foo',
                 {'a': {'b': 'c', 'm': 6.7}},
                 {'d': {'e': True}, 'g': {'h': 1}})
             ],
            dt(87.8971),
            'myname',
            23))

    assert s == expected

    # ensure that the result is jsonable. The data structure includes frozen maps which are not
    json.dumps(s)
def test_sample_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__

    sn = SampleNode('foo')
    sn2 = SampleNode('bar')
    id1 = uuid.UUID('1234567890abcdef1234567890abcdef')
    id2 = uuid.UUID('1234567890abcdef1234567890abcdea')
    dt1 = dt(5)
    dt2 = dt(8)
    u = UserID('u')
    u2 = UserID('u2')

    assert hash(Sample([sn], 'yay')) == hash(Sample([sn], 'yay'))
    assert hash(Sample([sn], 'foo')) == hash(Sample([sn], 'foo'))
    assert hash(Sample([sn], 'yay')) != hash(Sample([sn2], 'yay'))
    assert hash(Sample([sn], 'yay')) != hash(Sample([sn], 'yo'))

    assert hash(SavedSample(id1, u, [sn], dt1, 'yay')) == hash(SavedSample(
                                                                 id1, u, [sn], dt(5), 'yay'))
    assert hash(SavedSample(id2, u, [sn], dt1, 'foo')) == hash(SavedSample(
                                                                 id2, u, [sn], dt1, 'foo'))
    assert hash(SavedSample(id1, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id2, u, [sn], dt1, 'foo'))
    assert hash(SavedSample(id1, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id1, u2, [sn], dt1, 'foo'))
    assert hash(SavedSample(id2, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id2, u, [sn2], dt1, 'foo'))
    assert hash(SavedSample(id2, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id2, u, [sn], dt2, 'foo'))
    assert hash(SavedSample(id2, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id2, u, [sn], dt1, 'bar'))
    assert hash(SavedSample(id1, u, [sn], dt1, 'foo', 6)) == hash(SavedSample(
                                                                    id1, u, [sn], dt1, 'foo', 6))
    assert hash(SavedSample(id1, u, [sn], dt1, 'foo', 6)) != hash(SavedSample(
                                                                    id1, u, [sn], dt1, 'foo', 7))
def test_sample_to_dict_minimal():

    expected = {'node_tree': [{'id': 'foo',
                               'type': 'BioReplicate',
                               'meta_controlled': {},
                               'meta_user': {},
                               'parent': None
                               }],
                'id': 'f5bd78c3-823e-40b2-9f93-20e78680e41e',
                'user': '******',
                'save_date': 87897,
                'name': None,
                'version': None,
                }

    id_ = UUID('f5bd78c3-823e-40b2-9f93-20e78680e41e')

    s = sample_to_dict(SavedSample(id_, UserID('user2'), [SampleNode('foo')], dt(87.8971)))

    assert s == expected

    # ensure that the result is jsonable. The data structure includes frozen maps which are not
    json.dumps(s)
Esempio n. 13
0
    SampleNodeAddress,
    SampleAddress,
    SourceMetadata,
)
from SampleService.core.errors import (
    MissingParameterError, NoSuchSampleError, ConcurrencyError, UnauthorizedError,
    NoSuchSampleVersionError, DataLinkExistsError, TooManyDataLinksError, NoSuchLinkError,
    NoSuchSampleNodeError
)
from SampleService.core.storage.arango_sample_storage import ArangoSampleStorage
from SampleService.core.storage.errors import SampleStorageError, StorageInitError
from SampleService.core.storage.errors import OwnerChangedError
from SampleService.core.user import UserID
from SampleService.core.workspace import UPA, DataUnitID

TEST_NODE = SampleNode('foo')

TEST_DB_NAME = 'test_sample_service'
TEST_COL_SAMPLE = 'samples'
TEST_COL_VERSION = 'samples_version'
TEST_COL_VER_EDGE = 'ver_to_sample'
TEST_COL_NODES = 'samples_nodes'
TEST_COL_NODE_EDGE = 'node_edges'
TEST_COL_WS_OBJ_VER = 'ws_obj_ver'
TEST_COL_DATA_LINK = 'samples_data_link'
TEST_COL_SCHEMA = 'schema'
TEST_USER = '******'
TEST_PWD = 'password1'


@fixture(scope='module')
def _sample_node_build_fail(name, type_, parent, expected):
    with raises(Exception) as got:
        SampleNode(name, type_, parent)
    assert_exception_correct(got.value, expected)
def test_sample_node_eq():
    t = SubSampleType.TECHNICAL_REPLICATE
    s = SubSampleType.SUB_SAMPLE
    r = SubSampleType.BIOLOGICAL_REPLICATE

    assert SampleNode('foo') == SampleNode('foo')
    assert SampleNode('foo') != SampleNode('bar')

    assert SampleNode('foo', r) == SampleNode('foo', r)
    assert SampleNode('foo', r) != SampleNode('foo', s, 'baz')

    assert SampleNode('foo', s, 'bar') == SampleNode('foo', s, 'bar')
    assert SampleNode('foo', s, 'bar') != SampleNode('foo', t, 'bar')
    assert SampleNode('foo', s, 'bar') != SampleNode('foo', s, 'bat')

    assert SampleNode('foo', s, 'bar', {'foo': {'a': 'b'}}) == SampleNode(
        'foo', s, 'bar', {'foo': {'a': 'b'}})
    assert SampleNode('foo', s, 'bar', {'foo': {'a': 'b'}}) != SampleNode(
        'foo', s, 'bar', {'foo': {'a': 'c'}})
    assert SampleNode('foo', s, 'bar', {'foo': {'a': 'b'}}) != SampleNode(
        'foo', s, 'bar', {'foo': {'z': 'b'}})
    assert SampleNode('foo', s, 'bar', {'foo': {'a': 'b'}}) != SampleNode(
        'foo', s, 'bar', {'fo': {'a': 'b'}})

    assert SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'b'}}) == SampleNode(
        'foo', s, 'bar', user_metadata={'foo': {'a': 'b'}})
    assert SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'b'}}) != SampleNode(
        'foo', s, 'bar', user_metadata={'foo': {'a': 'c'}})
    assert SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'b'}}) != SampleNode(
        'foo', s, 'bar', user_metadata={'foo': {'z': 'b'}})
    assert SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'b'}}) != SampleNode(
        'foo', s, 'bar', user_metadata={'fo': {'a': 'b'}})

    assert SampleNode(
        'foo',
        s,
        'bar',
        controlled_metadata={'k': {'a': 'b'}},
        source_metadata=[SourceMetadata('k', 'k', {'c': 'd'})]) == SampleNode(
            'foo',
            s,
            'bar',
            controlled_metadata={'k': {'a': 'b'}},
            source_metadata=[SourceMetadata('k', 'k', {'c': 'd'})])
    assert SampleNode(
        'foo',
        s,
        'bar',
        controlled_metadata={'k': {'a': 'b'}},
        source_metadata=[SourceMetadata('k', 'k', {'c': 'd'})]) != SampleNode(
            'foo',
            s,
            'bar',
            controlled_metadata={'k': {'a': 'b'}},
            source_metadata=[SourceMetadata('k', 'v', {'c': 'd'})])

    assert SampleNode('foo') != 'foo'
    assert 'foo' != SampleNode('foo')
def test_sample_node_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__

    t = SubSampleType.TECHNICAL_REPLICATE
    s = SubSampleType.SUB_SAMPLE
    r = SubSampleType.BIOLOGICAL_REPLICATE

    assert hash(SampleNode('foo')) == hash(SampleNode('foo'))
    assert hash(SampleNode('bar')) == hash(SampleNode('bar'))
    assert hash(SampleNode('foo')) != hash(SampleNode('bar'))
    assert hash(SampleNode('foo', r)) == hash(SampleNode('foo', r))
    assert hash(SampleNode('foo', r)) != hash(SampleNode('foo', s, 'baz'))
    assert hash(SampleNode('foo', s, 'bar')) == hash(SampleNode('foo', s, 'bar'))
    assert hash(SampleNode('foo', t, 'bat')) == hash(SampleNode('foo', t, 'bat'))
    assert hash(SampleNode('foo', s, 'bar')) != hash(SampleNode('foo', t, 'bar'))
    assert hash(SampleNode('foo', s, 'bar')) != hash(SampleNode('foo', s, 'bat'))

    assert hash(SampleNode('foo', s, 'bar', {'foo': {'a': 'b'}})) == hash(SampleNode(
        'foo', s, 'bar', {'foo': {'a': 'b'}}))
    assert hash(SampleNode('foo', s, 'bar', {'foo': {'a': 'b'}})) != hash(SampleNode(
        'foo', s, 'bar', {'foo': {'a': 'c'}}))
    assert hash(SampleNode('foo', s, 'bar', {'foo': {'a': 'b'}})) != hash(SampleNode(
        'foo', s, 'bar', {'foo': {'z': 'b'}}))
    assert hash(SampleNode('foo', s, 'bar', {'foo': {'a': 'b'}})) != hash(SampleNode(
        'foo', s, 'bar', {'fo': {'a': 'b'}}))

    assert hash(SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'b'}})) == hash(
        SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'b'}}))
    assert hash(SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'b'}})) != hash(
        SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'c'}}))
    assert hash(SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'b'}})) != hash(
        SampleNode('foo', s, 'bar', user_metadata={'foo': {'z': 'b'}}))
    assert hash(SampleNode('foo', s, 'bar', user_metadata={'foo': {'a': 'b'}})) != hash(
        SampleNode('foo', s, 'bar', user_metadata={'fo': {'a': 'b'}}))

    assert hash(SampleNode(
        'foo',
        s,
        'bar',
        controlled_metadata={'k': {'a': 'b'}},
        source_metadata=[SourceMetadata('k', 'k', {'c': 'd'})])) == hash(SampleNode(
            'foo',
            s,
            'bar',
            controlled_metadata={'k': {'a': 'b'}},
            source_metadata=[SourceMetadata('k', 'k', {'c': 'd'})]))
    assert hash(SampleNode(
        'foo',
        s,
        'bar',
        controlled_metadata={'k': {'a': 'b'}},
        source_metadata=[SourceMetadata('k', 'k', {'c': 'd'})])) != hash(SampleNode(
            'foo',
            s,
            'bar',
            controlled_metadata={'k': {'a': 'b'}},
            source_metadata=[SourceMetadata('k', 'v', {'c': 'd'})]))
Esempio n. 17
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)