示例#1
0
def main():
    cfg = env_config()
    cfg.disable_collection_delete = False
    db = cfg.database('arango_crud_examples')
    coll = db.collection('arango_as_cache')

    db.create_if_not_exists()
    coll.create_if_not_exists()

    def triangle(n):
        if n == 0:
            return 1

        doc = coll.document(str(n))
        if not doc.read():
            doc.body['value'] = n + triangle(n - 1)
            assert doc.create() is True
        return doc.body['value']

    print('Calculating triangle(19):')
    print(triangle(19))

    print('Calculating triangle(20):')
    print(triangle(20))

    assert coll.force_delete() is True
def main():
    from arango_crud import env_config

    page_size = NUM_FLOATS_IN_SORTER_MEMORY // 4
    assert page_size * 4 == NUM_FLOATS_IN_SORTER_MEMORY
    num_pages = NUM_FLOATS_IN_DATA_ARRAY // page_size
    assert num_pages * page_size == NUM_FLOATS_IN_DATA_ARRAY

    cfg = env_config()
    cfg.disable_collection_delete = False

    db = cfg.database('arango_crud_examples')
    coll = db.collection('arango_as_working_space')

    db.create_if_not_exists()
    coll.force_delete()
    coll.create_if_not_exists()

    print('Writing random array')
    for i in range(num_pages):
        arr = np.random.randint(10001, size=page_size)
        coll.create_or_overwrite_doc(f'page-{i}', arr.tolist())

    print('Done! Beginning sort')
    started_at = time.time()
    sort(coll, page_size, num_pages)
    total_seconds = time.time() - started_at

    print(
        f'Sorted {NUM_FLOATS_IN_DATA_ARRAY} numbers in {total_seconds} seconds'
    )
    assert coll.force_delete() is True
示例#3
0
def kvstore():
    """
    This doesn't actually do anything, since Arango operates on a rest API,
    but it returns the convienent object for using that rest api

    @return [Config] The ArangoDB rest API wrapper
    """
    return env_config()
 def test_disable_db_delete(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD,
             'ARANGO_DISABLE_DATABASE_DELETE': 'false'
         })
     self.assertFalse(cfg.disable_database_delete)
 def test_timeout(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_AUTH': 'basic',
             'ARANGO_TIMEOUT_SECONDS': '20',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD
         })
     self.assertEqual(cfg.timeout_seconds, 20.0)
 def test_ttl(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_TTL_SECONDS': '12000',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD
         })
     self.assertEqual(cfg.ttl_seconds, 12000)
 def test_disable_coll_delete(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD,
             'ARANGO_DISABLE_COLLECTION_DELETE': 'false'
         })
     self.assertFalse(cfg.disable_collection_delete)
 def test_verify(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_VERIFY': 'my/verify/certfile',
             'ARANGO_CLUSTER_STYLE': 'random',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD
         })
     self.assertEqual(cfg.verify, 'my/verify/certfile')
 def test_basic_auth(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD
         })
     self.assertIsInstance(cfg.auth, BasicAuth)
     self.assertEqual(cfg.auth.username, helper.TEST_USERNAME)
     self.assertEqual(cfg.auth.password, helper.TEST_PASSWORD)
 def test_back_off_step(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_BACK_OFF': 'step',
             'ARANGO_BACK_OFF_STEPS': '1,2.5,0.1',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD
         })
     self.assertIsInstance(cfg.back_off, StepBackOffStrategy)
     self.assertEqual(cfg.back_off.steps, [1.0, 2.5, 0.1])
 def test_protect_db(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD,
             'ARANGO_DISABLE_DATABASE_DELETE': 'false',
             'ARANGO_PROTECTED_DATABASES': 'test_db,test_db2'
         })
     self.assertFalse(cfg.disable_database_delete)
     self.assertEqual(cfg.protected_databases, ['test_db', 'test_db2'])
 def test_protect_coll(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD,
             'ARANGO_DISABLE_COLLECTION_DELETE': 'false',
             'ARANGO_PROTECTED_COLLECTIONS': 'test_coll,test_coll2'
         })
     self.assertFalse(cfg.disable_collection_delete)
     self.assertEqual(cfg.protected_collections,
                      ['test_coll', 'test_coll2'])
示例#13
0
 def test_envvar_basic_auth(self):
     config = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:8529',
             'ARANGO_CLUSTER_STYLE': 'random',
             'ARANGO_TIMEOUT_SECONDS': 3,
             'ARANGO_BACK_OFF': 'step',
             'ARANGO_BACK_OFF_STEPS': '0.1,0.5,1,1,1',
             'ARANGO_TTL_SECONDS': '31622400',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': '******',
             'ARANGO_AUTH_PASSWORD': ''
         })
     self.assertIsNotNone(config)
 def test_jwt_auth_no_cache(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_AUTH': 'jwt',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD,
             'ARANGO_AUTH_CACHE': 'none'
         })
     self.assertIsInstance(cfg.auth, StatefulAuthWrapper)
     self.assertIsInstance(cfg.auth.delegate, JWTAuth)
     self.assertEqual(cfg.auth.delegate.username, helper.TEST_USERNAME)
     self.assertEqual(cfg.auth.delegate.password, helper.TEST_PASSWORD)
     self.assertIsNone(cfg.auth.delegate.cache)
    def test_cluster_random(self):
        cfg = env_config(
            cfg={
                'ARANGO_CLUSTER': 'http://localhost:5289',
                'ARANGO_CLUSTER_STYLE': 'random',
                'ARANGO_AUTH': 'basic',
                'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
                'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD
            })
        self.assertIsInstance(cfg.cluster, RandomCluster)
        self.assertEqual(cfg.cluster.urls, ['http://localhost:5289'])

        cfg = env_config(
            cfg={
                'ARANGO_CLUSTER':
                'http://localhost:5289,http://localhost:5290',
                'ARANGO_CLUSTER_STYLE': 'random',
                'ARANGO_AUTH': 'basic',
                'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
                'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD
            })
        self.assertIsInstance(cfg.cluster, RandomCluster)
        self.assertEqual(cfg.cluster.urls,
                         ['http://localhost:5289', 'http://localhost:5290'])
 def test_cluster_weighted_random(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER':
             'http://localhost:5289,http://localhost:5290',
             'ARANGO_CLUSTER_STYLE': 'weighted-random',
             'ARANGO_CLUSTER_WEIGHTS': '1,2.1',
             'ARANGO_AUTH': 'basic',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD
         })
     self.assertIsInstance(cfg.cluster, WeightedRandomCluster)
     self.assertEqual(cfg.cluster.urls,
                      ['http://localhost:5289', 'http://localhost:5290'])
     self.assertEqual(cfg.cluster.weights, [1.0, 2.1])
示例#17
0
 def test_envvar_jwt(self):
     config = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:8529',
             'ARANGO_CLUSTER_STYLE': 'random',
             'ARANGO_TIMEOUT_SECONDS': 3,
             'ARANGO_BACK_OFF': 'step',
             'ARANGO_BACK_OFF_STEPS': '0.1,0.5,1,1,1',
             'ARANGO_TTL_SECONDS': '31622400',
             'ARANGO_AUTH': 'jwt',
             'ARANGO_AUTH_USERNAME': '******',
             'ARANGO_AUTH_PASSWORD': '',
             'ARANGO_AUTH_CACHE': 'disk',
             'ARANGO_AUTH_CACHE_LOCK_FILE': '.arango_jwt.lock',
             'ARANGO_AUTH_CACHE_LOCK_TIME_SECONDS': '10',
             'ARANGO_AUTH_CACHE_STORE_FILE': '.arango_jwt'
         })
     self.assertIsNotNone(config)
示例#18
0
 def test_weighted_random_cluster_envvar(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER':
             ('http://localhost:8529,http://localhost:8530,http://localhost:8531'
              ),
             'ARANGO_CLUSTER_STYLE':
             'weighted-random',
             'ARANGO_CLUSTER_WEIGHTS':
             '1,2,1',
             'ARANGO_AUTH':
             'basic',
             'ARANGO_AUTH_USERNAME':
             helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD':
             helper.TEST_PASSWORD
         })
     self.assertIsNotNone(cfg)
 def test_jwt_auth_disk_cache(self):
     cfg = env_config(
         cfg={
             'ARANGO_CLUSTER': 'http://localhost:5289',
             'ARANGO_AUTH': 'jwt',
             'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
             'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD,
             'ARANGO_AUTH_CACHE': 'disk',
             'ARANGO_AUTH_CACHE_LOCK_FILE': 'foo.lock',
             'ARANGO_AUTH_CACHE_LOCK_TIME_SECONDS': '10',
             'ARANGO_AUTH_CACHE_STORE_FILE': 'foo.jwt'
         })
     self.assertIsInstance(cfg.auth, StatefulAuthWrapper)
     self.assertIsInstance(cfg.auth.delegate, JWTAuth)
     self.assertEqual(cfg.auth.delegate.username, helper.TEST_USERNAME)
     self.assertEqual(cfg.auth.delegate.password, helper.TEST_PASSWORD)
     self.assertIsInstance(cfg.auth.delegate.cache, JWTDiskCache)
     self.assertEqual(cfg.auth.delegate.cache.lock_file, 'foo.lock')
     self.assertEqual(cfg.auth.delegate.cache.lock_time_seconds, 10.0)
     self.assertEqual(cfg.auth.delegate.cache.store_file, 'foo.jwt')
示例#20
0
    def test_crud(self):
        config = env_config(
            cfg={
                'ARANGO_CLUSTER': ','.join(helper.TEST_CLUSTER_URLS),
                'ARANGO_CLUSTER_STYLE': 'random',
                'ARANGO_TIMEOUT_SECONDS': 3,
                'ARANGO_BACK_OFF': 'step',
                'ARANGO_BACK_OFF_STEPS': '0.1,0.5,1,1,1',
                'ARANGO_TTL_SECONDS': '31622400',
                'ARANGO_AUTH': 'basic',
                'ARANGO_AUTH_USERNAME': helper.TEST_USERNAME,
                'ARANGO_AUTH_PASSWORD': helper.TEST_PASSWORD,
                'ARANGO_DISABLE_DATABASE_DELETE': 'false',
                'ARANGO_DISABLE_COLLECTION_DELETE': 'false'
            })
        config.prepare()

        db = config.database(helper.TEST_ARANGO_DB)
        self.assertTrue(db.create_if_not_exists())
        coll = db.collection('users')
        self.assertTrue(coll.create_if_not_exists())

        # The simplest interface
        self.assertIsNone(coll.create_or_overwrite_doc('tj', {'name': 'TJ'}))
        self.assertEqual(coll.read_doc('tj'), {'name': 'TJ'})
        self.assertTrue(coll.force_delete_doc('tj'))

        # non-expiring
        coll.create_or_overwrite_doc('tj', {'name': 'TJ'}, ttl=None)
        self.assertTrue(coll.force_delete_doc('tj'))

        # custom expirations with touching. Note that touching a document is not
        # a supported atomic operation on ArangoDB and is hence faked with
        # read -> compare_and_swap. Presumably if the CAS fails the document was
        # touched recently anyway.
        coll.create_or_overwrite_doc('tj', {'name': 'TJ'}, ttl=30)
        self.assertTrue(coll.touch_doc('tj', ttl=60))
        self.assertTrue(coll.force_delete_doc('tj'))

        # Alternative interface. For anything except one-liners, usually nicer.
        doc = coll.document('tj')
        doc.body['name'] = 'TJ'
        self.assertTrue(doc.create())
        doc.body['note'] = 'Pretty cool'
        self.assertTrue(doc.compare_and_swap())

        # We may use etags to avoid redownloading an unchanged document, but be careful
        # if you are modifying the body.

        # Happy case:
        doc2 = coll.document('tj')
        self.assertTrue(doc2.read())
        self.assertEqual(doc2.body, {'name': 'TJ', 'note': 'Pretty cool'})

        self.assertFalse(doc.read_if_remote_newer())
        self.assertFalse(doc2.read_if_remote_newer())

        doc.body['note'] = 'bar'
        self.assertTrue(doc.compare_and_swap())
        self.assertFalse(doc.read_if_remote_newer())
        self.assertTrue(doc2.read_if_remote_newer())
        self.assertEqual(doc2.body, {'name': 'TJ', 'note': 'bar'})

        # Where it can get dangerous
        doc.body['note'] = 'foo'
        self.assertEqual(doc.body, {'name': 'TJ', 'note': 'foo'})
        self.assertTrue(doc.read())
        self.assertEqual(doc.body, {'name': 'TJ', 'note': 'bar'})
        self.assertFalse(doc.read_if_remote_newer())
        self.assertEqual(doc.body, {'name': 'TJ', 'note': 'bar'})
        doc.body['note'] = 'foo'
        self.assertEqual(doc.body, {'name': 'TJ', 'note': 'foo'})
        self.assertFalse(doc.read_if_remote_newer())
        self.assertEqual(doc.body, {'name': 'TJ', 'note': 'foo'})
        self.assertTrue(doc.read())
        self.assertEqual(doc.body, {'name': 'TJ', 'note': 'bar'})

        self.assertTrue(doc.compare_and_delete())

        # Simple caching
        for i in range(2):
            doc = coll.document('tj')
            hit = doc.read()
            if hit:
                doc.compare_and_swap()
            else:
                doc.body = {'name': 'TJ', 'note': 'Pretty cool'}
                doc.create_or_overwrite()

            self.assertEqual(hit, i == 1)
            self.assertEqual(doc.body, {'name': 'TJ', 'note': 'Pretty cool'})

        coll.force_delete()
        self.assertTrue(db.force_delete())