Exemple #1
0
async def test_descriptor_query(tables):
    await create_user_notes()
    await create_relationship()

    charlie, huey, mickey, zaizee = await User.select().order_by(User.username)

    with assert_query_count(1):
        await assert_notes(charlie.notes, [1, 2])

    with assert_query_count(1):
        await assert_notes(zaizee.notes, [4, 5])

    u = await User.create(username='******')
    await assert_notes(u.notes, [])

    n1, n2, n3, n4, n5 = await Note.select().order_by(Note.text)
    with assert_query_count(1):
        await assert_users(n1.users, ['charlie'])

    with assert_query_count(1):
        await assert_users(n2.users, ['charlie', 'huey'])

    with assert_query_count(1):
        await assert_users(n5.users, ['zaizee'])

    n6 = await Note.create(text='note-6')
    await assert_users(n6.users, [])
Exemple #2
0
async def test_remove(tables):
    await create_user_notes()
    await create_relationship()

    charlie, huey, mickey, zaizee = await User.select().order_by(User.username)
    n1, n2, n3, n4, n5 = await Note.select().order_by(Note.text)

    with assert_query_count(1):
        await charlie.notes.remove([n1, n2, n3])

    await assert_notes(charlie.notes, [])
    await assert_notes(huey.notes, [2, 3])

    with assert_query_count(1):
        await huey.notes.remove(Note.select().where(
            Note.text << ['note-2', 'note-4', 'note-5']))

    await assert_notes(huey.notes, [3])
    await assert_notes(mickey.notes, [3, 4])
    await assert_notes(zaizee.notes, [4, 5])

    with assert_query_count(1):
        await n4.users.remove([charlie, mickey])
    await assert_users(n4.users, ['zaizee'])

    with assert_query_count(1):
        await n5.users.remove(User.select())
    await assert_users(n5.users, [])
Exemple #3
0
async def test_add(tables):
    await create_user_notes()

    charlie = await User.get(User.username == 'charlie')
    huey = await User.get(User.username == 'huey')
    n1, n2, n3, n4, n5 = await Note.select().order_by(Note.text)

    await charlie.notes.add([n1, n2])
    await assert_notes(charlie.notes, [1, 2])
    await assert_users(n1.users, ['charlie'])
    await assert_users(n2.users, ['charlie'])
    others = [n3, n4, n5]
    for note in others:
        await assert_users(note.users, [])

    with assert_query_count(1):
        await huey.notes.add(Note.select().where(
            fn.substr(Note.text, 6, 1) << ['1', '3', '5']))
    await assert_notes(huey.notes, [1, 3, 5])
    await assert_users(n1.users, ['charlie', 'huey'])
    await assert_users(n2.users, ['charlie'])
    await assert_users(n3.users, ['huey'])
    await assert_users(n4.users, [])
    await assert_users(n5.users, ['huey'])

    with assert_query_count(1):
        await charlie.notes.add(n4)
    await assert_notes(charlie.notes, [1, 2, 4])

    with assert_query_count(2):
        await n3.users.add(
            User.select().where(User.username != 'charlie'),
            clear_existing=True)
    await assert_users(n3.users, ['huey', 'mickey', 'zaizee'])
Exemple #4
0
async def test_simple_backref(tables):
    user = await User.create(username='******')
    with assert_query_count(1):
        assert await model_to_dict(user, backrefs=True) == {
            'id': user.id,
            'notes': [],
            'username': user.username}

    # Create a note to populate backrefs list.
    note = await Note.create(user=user, text='note-1')

    expected = {
        'id': user.id,
        'notes': [
            {'id': note.id, 'notetag_set': [], 'text': note.text},
        ],
        'username': user.username}

    # Two queries: one to get related notes, one to get related notetags.
    with assert_query_count(2):
        assert await model_to_dict(user, backrefs=True) == expected

    query = (User
             .select(User, Note, NoteTag)
             .join(Note, JOIN.LEFT_OUTER)
             .join(NoteTag, JOIN.LEFT_OUTER)
             .aggregate_rows())
    user = await query.get()

    with assert_query_count(0):
        assert await model_to_dict(user, backrefs=True) == expected
Exemple #5
0
async def test_desciptor_filtering(tables):
    await create_user_notes()
    await create_relationship()

    charlie, huey, mickey, zaizee = await User.select().order_by(User.username)

    with assert_query_count(1):
        notes = charlie.notes.order_by(Note.text.desc())
        await assert_notes(notes, [2, 1])

    with assert_query_count(1):
        notes = huey.notes.where(Note.text != 'note-3')
        await assert_notes(notes, [2])
Exemple #6
0
async def test_extra_attrs(tables):
    user = await User.create(username='******')
    with assert_query_count(0):
        extra = ['name_hash', 'title']
        assert await model_to_dict(user, extra_attrs=extra) == {
            'id': user.id,
            'username': user.username,
            'name_hash': 5,
            'title': 'Peewee',
        }

    with assert_query_count(0):
        # Unknown attr causes AttributeError.
        with pytest.raises(AttributeError):
            await model_to_dict(user, extra_attrs=['xx'])
Exemple #7
0
async def test_many_to_many(tables):
    user = await User.create(username='******')
    note = await Note.create(user=user, text='note-1')
    t1 = await Tag.create(tag='t1')
    t2 = await Tag.create(tag='t2')
    await Tag.create(tag='tx')  # Note used on any notes.
    nt1 = await NoteTag.create(note=note, tag=t1)
    nt2 = await NoteTag.create(note=note, tag=t2)

    expected = {
        'id': user.id,
        'notes': [{
            'id': note.id,
            'notetag_set': [
                {'tag': {'id': t1.id, 'tag': t1.tag}},
                {'tag': {'id': t2.id, 'tag': t2.tag}},
            ],
            'text': note.text,
        }],
        'username': user.username,
    }

    # Query to retrieve notes, note-tags, and 2 tag queries.
    with assert_query_count(4):
        assert await model_to_dict(user, backrefs=True) == expected
Exemple #8
0
async def test_recursive_fk(tables):
    root = await Category.create(name='root')
    child = await Category.create(name='child', parent=root)
    grandchild = await Category.create(name='grandchild', parent=child)

    with assert_query_count(0):
        assert await model_to_dict(root) == {
            'id': root.id,
            'name': root.name,
            'parent': None,
        }

    with assert_query_count(0):
        assert await model_to_dict(root, recurse=False) == {
            'id': root.id,
            'name': root.name,
            'parent': None,
        }

    with assert_query_count(1):
        assert await model_to_dict(root, backrefs=True) == {
            'children': [{'id': child.id, 'name': child.name}],
            'id': root.id,
            'name': root.name,
            'parent': None,
        }

    with assert_query_count(1):
        assert await model_to_dict(child, backrefs=True) == {
            'children': [{'id': grandchild.id, 'name': grandchild.name}],
            'id': child.id,
            'name': child.name,
            'parent': {
                'id': root.id,
                'name': root.name,
            },
        }

    with assert_query_count(0):
        assert await model_to_dict(child, backrefs=False) == {
            'id': child.id,
            'name': child.name,
            'parent': {
                'id': root.id,
                'name': root.name,
            },
        }
Exemple #9
0
async def test_simple_recurse(tables):
    user = await User.create(username='******')
    note = await Note.create(user=user, text='note-1')

    with assert_query_count(0):
        assert await model_to_dict(note) == {
            'id': note.id,
            'text': note.text,
            'user': {
                'id': user.id,
                'username': user.username}}

    with assert_query_count(0):
        assert await model_to_dict(note, recurse=False) == {
            'id': note.id,
            'text': note.text,
            'user': user.id,
        }
Exemple #10
0
async def test_set_values(tables):
    await create_user_notes()

    charlie = await User.get(User.username == 'charlie')
    huey = await User.get(User.username == 'huey')
    n1, n2, n3, n4, n5 = await Note.select().order_by(Note.text)

    with assert_query_count(2):
        await charlie.notes.set(n1)
    await assert_notes(charlie.notes, [1])
    await assert_users(n1.users, ['charlie'])

    await charlie.notes.set([n2, n3])
    await assert_notes(charlie.notes, [2, 3])
    await assert_users(n1.users, [])
    await assert_users(n2.users, ['charlie'])
    await assert_users(n3.users, ['charlie'])

    with assert_query_count(2):
        await huey.notes.set(Note.select().where(~(Note.text.endswith('4'))))
    await assert_notes(huey.notes, [1, 2, 3, 5])
Exemple #11
0
async def test_remove_by_id(tables):
    await create_user_notes()
    await create_relationship()

    charlie, huey, mickey, zaizee = await User.select().order_by(User.username)
    n1, n2, n3, n4, n5 = await Note.select().order_by(Note.text)
    await charlie.notes.add([n3, n4])

    with assert_query_count(1):
        await charlie.notes.remove([n1.id, n3.id])

    await assert_notes(charlie.notes, [2, 4])
    await assert_notes(huey.notes, [2, 3])
Exemple #12
0
async def test_recurse_backrefs(tables):
    user = await User.create(username='******')
    note = await Note.create(user=user, text='note-1')

    # One query to retrieve the note-tag set.
    with assert_query_count(1):
        assert await model_to_dict(note, backrefs=True) == {
            'id': note.id,
            'notetag_set': [],
            'text': note.text,
            'user': {
                'id': user.id,
                'username': user.username,
            },
        }
Exemple #13
0
async def test_manual_through(tables):
    await create_user_notes()

    charlie, huey, mickey, zaizee = await User.select().order_by(User.username)
    alt_notes = []
    for i in range(5):
        alt_notes.append(await AltNote.create(text='note-%s' % (i + 1)))

    await assert_notes(charlie.altnotes, [])
    for alt_note in alt_notes:
        await assert_users(alt_note.users, [])

    n1, n2, n3, n4, n5 = alt_notes

    # Test adding relationships by setting the descriptor.
    await charlie.altnotes.set([n1, n2])

    with assert_query_count(2):
        await huey.altnotes.set(AltNote.select().where(
            fn.substr(AltNote.text, 6, 1) << ['1', '3', '5']))

    await mickey.altnotes.add([n1, n4])

    with assert_query_count(2):
        await zaizee.altnotes.set(AltNote.select())

    # Test that the notes were added correctly.
    with assert_query_count(1):
        await assert_notes(charlie.altnotes, [1, 2])

    with assert_query_count(1):
        await assert_notes(huey.altnotes, [1, 3, 5])

    with assert_query_count(1):
        await assert_notes(mickey.altnotes, [1, 4])

    with assert_query_count(1):
        await assert_notes(zaizee.altnotes, [1, 2, 3, 4, 5])

    # Test removing notes.
    with assert_query_count(1):
        await charlie.altnotes.remove(n1)
    await assert_notes(charlie.altnotes, [2])

    with assert_query_count(1):
        await huey.altnotes.remove([n1, n2, n3])
    await assert_notes(huey.altnotes, [5])

    with assert_query_count(1):
        await zaizee.altnotes.remove(
            AltNote.select().where(
                fn.substr(AltNote.text, 6, 1) << ['1', '2', '4']))
    await assert_notes(zaizee.altnotes, [3, 5])

    # Test the backside of the relationship.
    await n1.users.set(User.select().where(User.username != 'charlie'))

    with assert_query_count(1):
        await assert_users(n1.users, ['huey', 'mickey', 'zaizee'])
    with assert_query_count(1):
        await assert_users(n2.users, ['charlie'])
    with assert_query_count(1):
        await assert_users(n3.users, ['zaizee'])
    with assert_query_count(1):
        await assert_users(n4.users, ['mickey'])
    with assert_query_count(1):
        await assert_users(n5.users, ['huey', 'zaizee'])

    with assert_query_count(1):
        await n1.users.remove(User.select())
    with assert_query_count(1):
        await n5.users.remove([charlie, huey])

    with assert_query_count(1):
        await assert_users(n1.users, [])
    with assert_query_count(1):
        await assert_users(n5.users, ['zaizee'])
Exemple #14
0
 async def add_user(note, user):
     with assert_query_count(1):
         await note.users.add(user)
Exemple #15
0
async def test_simple(tables):
    user = await User.create(username='******')
    with assert_query_count(0):
        expected = {'id': user.id, 'username': user.username}
        assert await model_to_dict(user) == expected