Esempio n. 1
0
def test():
    # access the packages
    import bizbook


    # build a simple projection
    class titles(bizbook.db.query, book=bizbook.schema.Book):
        """A short query on the book table"""
        id = book.id
        title = book.title
        category = book.category
        price = book.price

    # build datastore
    db = bizbook.pg()
    # check that we are connected to the right database
    assert db.database == 'bizbook'

    # run the query
    for record in db.select(titles):
        # check the length
        assert len(record) == 4
        # check the fields
        assert hasattr(record, 'id')
        assert hasattr(record, 'title')
        assert hasattr(record, 'category')
        assert not hasattr(record, 'publisher')
        assert not hasattr(record, 'date')
        assert not hasattr(record, 'advance')
        assert hasattr(record, 'price')
        assert not hasattr(record, 'description')

    return db, titles
Esempio n. 2
0
def test():
    # access the packages
    import bizbook

    # build a simple projection
    class titles(bizbook.db.query, book=bizbook.schema.Book):
        """A short query on the book table"""
        id = book.id
        title = book.title
        category = book.category
        price = book.price
        # restrict the results
        where = book.category == "cookbook"

    # build datastore
    db = bizbook.pg()
    # check that we are connected to the right database
    assert db.database == 'bizbook'

    # run the query
    result = tuple(db.select(titles))
    # check
    # there are five cookbooks in the dataset
    assert len(result) == 5
    # make sure that only cookbooks got through
    for record in result:
        assert record.category == 'cookbook'

    return db, titles
Esempio n. 3
0
def test():
    # import journal
    # journal.debug("postgres.init").active = True
    # journal.debug("postgres.execute").active = True
    # journal.debug("postgres.connection").active = True

    # access the bizbook package
    import bizbook

    # build a database component
    db = bizbook.pg()
    # check that we are connected to the right database
    assert db.database == 'bizbook'
    # tell postgres to shut up
    db.execute("SET client_min_messages = warning;")

    # build the tables
    db.createTable(bizbook.schema.Location)
    db.createTable(bizbook.schema.Person)
    db.createTable(bizbook.schema.Publisher)
    db.createTable(bizbook.schema.Address)
    db.createTable(bizbook.schema.ContactMethod)
    db.createTable(bizbook.schema.Staff)
    db.createTable(bizbook.schema.Book)
    db.createTable(bizbook.schema.Author)
    db.createTable(bizbook.schema.Editor)
    db.createTable(bizbook.schema.Invoice)
    db.createTable(bizbook.schema.InvoiceItem)

    # and return the component
    return db
Esempio n. 4
0
def test():
    # import journal
    # journal.debug("postgres.init").active = True
    # journal.debug("postgres.execute").active = True
    # journal.debug("postgres.connection").active = True

    # access bizbook
    import bizbook

    # build a database component
    db = bizbook.pg()
    # check that we are connected to the right database
    assert db.database == 'bizbook'

    # drop the tables
    db.dropTable(bizbook.schema.InvoiceItem)
    db.dropTable(bizbook.schema.Invoice)
    db.dropTable(bizbook.schema.Editor)
    db.dropTable(bizbook.schema.Author)
    db.dropTable(bizbook.schema.Book)
    db.dropTable(bizbook.schema.Staff)
    db.dropTable(bizbook.schema.ContactMethod)
    db.dropTable(bizbook.schema.Address)
    db.dropTable(bizbook.schema.Publisher)
    db.dropTable(bizbook.schema.Person)
    db.dropTable(bizbook.schema.Location)

    # and return the component
    return db
Esempio n. 5
0
def test():
    # access the packages
    import bizbook

    # build a simple projection
    class titles(bizbook.db.query, book=bizbook.schema.Book):
        """A short query on the book table"""
        id = book.id
        title = book.title
        category = book.category
        price = book.price
        # restrict the results
        where = book.category == "cookbook"

    # build datastore
    db = bizbook.pg()
    # check that we are connected to the right database
    assert db.database == 'bizbook'

    # run the query
    result = tuple(db.select(titles))
    # check
    # there are five cookbooks in the dataset
    assert len(result) == 5
    # make sure that only cookbooks got through
    for record in result:
        assert record.category == 'cookbook'

    return db, titles
Esempio n. 6
0
def test():
    # import journal
    # journal.debug("postgres.init").active = True
    # journal.debug("postgres.execute").active = True
    # journal.debug("postgres.connection").active = True

    # access bizbook
    import bizbook

    # build a database component
    db = bizbook.pg()
    # check that we are connected to the right database
    assert db.database == 'bizbook'

    # drop the tables
    db.dropTable(bizbook.schema.InvoiceItem)
    db.dropTable(bizbook.schema.Invoice)
    db.dropTable(bizbook.schema.Editor)
    db.dropTable(bizbook.schema.Author)
    db.dropTable(bizbook.schema.Book)
    db.dropTable(bizbook.schema.Staff)
    db.dropTable(bizbook.schema.ContactMethod)
    db.dropTable(bizbook.schema.Address)
    db.dropTable(bizbook.schema.Publisher)
    db.dropTable(bizbook.schema.Person)
    db.dropTable(bizbook.schema.Location)

    # and return the component
    return db
Esempio n. 7
0
def test():
    # access the packages
    import bizbook
    import operator

    # build a simple projection
    class titles(bizbook.db.query, book=bizbook.schema.Book):
        """A short query on the book table"""
        id = book.id
        title = book.title
        category = book.category
        price = book.price

    # derive a query from it that specifies a collation order
    class collated(titles):
        """Extend the 'titles' query with a collation order"""
        # collation
        order = (titles.category, bizbook.db.descending(titles.price),
                 titles.title)

    # build datastore
    db = bizbook.pg()
    # check that we are connected to the right database
    assert db.database == 'bizbook'

    # run the query
    report = list(db.select(collated))
    # here is what we expect
    correct = list(
        sorted(sorted(sorted(db.select(titles),
                             key=operator.attrgetter('title')),
                      key=operator.attrgetter('price'),
                      reverse=True),
               key=operator.attrgetter('category')))
    # check
    assert report == correct

    # grant access to the test parts
    return db, titles
Esempio n. 8
0
def test():
    # access the packages
    import bizbook
    import operator

    # build a simple projection
    class titles(bizbook.db.query, book=bizbook.schema.Book):
        """A short query on the book table"""
        id = book.id
        title = book.title
        category = book.category
        price = book.price

    # derive a query from it that specifies a collation order
    class collated(titles):
        """Extend the 'titles' query with a collation order"""
        # collation
        order = (titles.category, bizbook.db.descending(titles.price), titles.title)

    # build datastore
    db = bizbook.pg()
    # check that we are connected to the right database
    assert db.database == 'bizbook'

    # run the query
    report = list(db.select(collated))
    # here is what we expect
    correct = list(sorted(
        sorted(
            sorted(db.select(titles), key=operator.attrgetter('title')),
            key=operator.attrgetter('price'), reverse=True),
        key=operator.attrgetter('category')))
    # check
    assert report == correct

    # grant access to the test parts
    return db, titles