简体   繁体   English

Python 方法运行后 ArangoDB 插入对象未完成

[英]Python ArangoDB insertion of objects not completed after method is run

I use arango-orm (which uses python-arango in the background) in my Python/ArangoDB back-end.我在我的 Python/ArangoDB 后端使用arango-orm (在后台使用python-arango )。 I have set up a small testing util that uses a remote database to insert test data, execute the unit tests and remove the test data again.我设置了一个小型测试工具,它使用远程数据库插入测试数据、执行单元测试并再次删除测试数据。 I insert my test data with a Python for loop.我使用 Python 循环插入我的测试数据。 Each iteration, a small piece of information changes based on a generic object and then I insert that modified generic object into ArangoDB until I have 10 test objects.每次迭代,一小部分信息都会根据通用 object 更改,然后我将修改后的通用 object 插入 ArangoDB,直到我有 10 个测试对象。 However, after that code is run, my test assertions tell me I don't have 10 objects stored inside my db, but only 8 (or sometimes 3, 7 or 9).但是,在运行该代码之后,我的测试断言告诉我我的数据库中没有存储 10 个对象,而只有 8 个(有时是 3、7 或 9 个)。 It looks like pythong-arango runs these queries asynchronously or that ArangoDB already replies with an OK before the data is actually inserted.看起来pythong-arango异步运行这些查询,或者ArangoDB在实际插入数据之前已经回复了 OK。 Anyone has an idea of what is going on?任何人都知道发生了什么? When I put in a sleep of 1 second after all data is inserted, my tests run green.当我在插入所有数据后休眠 1 秒时,我的测试运行绿色。 This obviously is no solution.这显然不是解决办法。

This is a little piece of example code I use:这是我使用的一小段示例代码:

def load_test_data(self) -> None:
    # This method is called from the setUp() method.
    logging.info("Loading test data...")
    for i in range(1, 11):
        # insertion with data object (ORM)
        user = test_utils.get_default_test_user()
        user.id = i
        user.username += str(i)
        user.name += str(i)
        db.add(user)

        # insertion with dictionary
        project = test_utils.get_default_test_project()
        project['id'] = i
        project['name'] += str(i)
        project['description'] = f"Description for project with the id {i}"
        db.insert_document("projects", project)
    # TODO: solve this dirty hack
    sleep(1)

def test_search_by_user_username(self) -> None:
    actual = dao.search("TestUser3")
    self.assertEqual(1, len(actual))
    self.assertEqual(3, actual[0].id)

Then my db is created like this in a separate module:然后我的数据库在一个单独的模块中像这样创建:

client = ArangoClient(hosts=f"http://{arango_host}:{arango_port}")
test_db = client.db(arango_db, arango_user, arango_password)
db = Database(test_db)

EDIT: I had not put the sync property to true upon collection creation, but after changing the collection and setting it to true , the behaviour stays exactly the same.编辑:我没有在创建集合时将sync属性设置为true ,但是在更改集合并将其设置为true之后,行为保持完全相同。

After getting in touch with the people of ArangoDB, I learned that views are not updatet as quickly as collections.在与 ArangoDB 的人员取得联系后,我了解到视图的更新速度不如 collections。 Thye have given me an internal SEARCH option which also waits for synching views.你给了我一个内部SEARCH option ,它也等待同步视图。 Since it's an internal option, only used for unit testing, they high discourage the use of it.由于它是一个内部选项,仅用于单元测试,因此他们强烈反对使用它。 For me, I only use it for unit testing.对我来说,我只将它用于单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM