Beispiel #1
0
def test_BulkLoader():
    args = []
    db = database.Database()
    db.execute = lambda s, vs: args.append((s, vs.copy()))
    ldr = BulkLoader(db,
                     'foos', ('id', 'foo'),
                     per=2,
                     suffix='ON DUPLICATE KEY UPDATE foo = VALUES(foo)')

    sql = ("INSERT INTO foos (id, foo) VALUES\n"
           "(%s, %s),\n"
           "(%s, %s)\n"
           "ON DUPLICATE KEY UPDATE foo = VALUES(foo)")
    ldr.add(1, 'A')
    assert not args
    ldr.add(2, 'B')
    assert args == [(sql, [1, 'A', 2, 'B'])]
    args.clear()
    ldr.extend(((3, 'a'), (4, 'b')))
    assert args == [(sql, [3, 'a', 4, 'b'])]

    ldr.per = 3
    ldr.extend(((3, 'a'), (4, 'b')))
    assert ldr.sql() == SQL(sql, 3, 'a', 4, 'b')

    ldr = BulkLoader(db, 'foos', dict(id='%s', created_at='NOW()'))
    assert ldr._cs == ('(%s, NOW())', )
    assert ldr._nc == 1
    ldr.add(1)
    assert ldr.sql() == SQL(
        "INSERT INTO foos (id, created_at) VALUES\n"
        "(%s, NOW())", 1)
Beispiel #2
0
 class Base(model.Model):
     DB = database.Database()