简体   繁体   中英

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. 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. 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. 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). It looks like pythong-arango runs these queries asynchronously or that ArangoDB already replies with an OK before the data is actually inserted. 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. 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.

After getting in touch with the people of ArangoDB, I learned that views are not updatet as quickly as collections. Thye have given me an internal SEARCH option which also waits for synching views. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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