Exemplo n.º 1
0
def test_datastore_client_uses_the_default_namespace(adapter):
    # Given that I've created some entities in the "anom" namespace
    with anom.namespace("anom"):
        user_1 = models.User(email="*****@*****.**",
                             password="******").put()
        user_2 = models.User(email="*****@*****.**",
                             password="******").put()

    # And I've set the default namespace to "anom"
    anom.set_default_namespace("anom")

    # When I try to get those users by creating new keys
    user_1_2, user_2_2 = anom.get_multi([
        anom.Key(models.User, user_1.key.int_id),
        anom.Key(models.User, user_2.key.int_id),
    ])

    # Then I should get back the same users
    assert user_1 == user_1_2
    assert user_2 == user_2_2

    # When I change the default namespace and try to get the same users
    anom.set_default_namespace(None)
    users = anom.get_multi([
        anom.Key(models.User, user_1.key.int_id),
        anom.Key(models.User, user_2.key.int_id),
    ])

    # Then I shuold get nothing back
    assert users == [None, None]
Exemplo n.º 2
0
def test_get_multi_retrieves_cached_and_non_cached_entities(memcache_adapter):
    person_1 = models.Person(email="*****@*****.**",
                             first_name="Person 1").put()
    person_2 = models.Person(email="*****@*****.**",
                             first_name="Person 2").put()

    for _ in range(2):
        assert get_multi([person_1.key]) == [person_1]
        assert get_multi([person_1.key, person_2.key]) == [person_1, person_2]
Exemplo n.º 3
0
def test_transactions_are_serializable(adapter):
    @transactional(retries=128)
    def transfer_money(source_account_key, target_account_key, amount):
        source_account, target_account = get_multi(
            [source_account_key, target_account_key])
        source_account.balance -= amount
        target_account.balance += amount
        put_multi([source_account, target_account])

    source = BankAccount(balance=100).put()
    target = BankAccount(balance=0).put()

    futures = []
    with ThreadPoolExecutor(max_workers=10) as e:
        for _ in range(10):
            futures.append(e.submit(transfer_money, source.key, target.key,
                                    10))

    for future in futures:
        future.result()

    source, target = get_multi([source.key, target.key])
    assert source.balance == 0
    assert target.balance == 100
Exemplo n.º 4
0
def test_keys_get_multi_fails_unknown_kind():
    with pytest.raises(RuntimeError):
        get_multi([Key("UnknownKind")])
Exemplo n.º 5
0
def test_keys_get_multi_fails_given_partial_keys():
    with pytest.raises(RuntimeError):
        get_multi([Key("Person")])
Exemplo n.º 6
0
def test_keys_can_get_multiple_entities_at_once(person):
    entities = get_multi([person.key, Key("Person", "nonexistent")])
    assert entities == [person, None]
Exemplo n.º 7
0
 def transfer_money(source_account_key, target_account_key, amount):
     source_account, target_account = get_multi(
         [source_account_key, target_account_key])
     source_account.balance -= amount
     target_account.balance += amount
     put_multi([source_account, target_account])