def data(self, sync, book_cls, publisher_cls):

        session = sync.session

        books = [
            book_cls(
                isbn='abc',
                title='The Tiger Club',
                description='Tigers are fierce creatures',
                publisher=publisher_cls(id=1, name='Tiger publishing'),
            ),
            book_cls(
                isbn='def',
                title='The Lion Club',
                description='Lion and the mouse',
                publisher=publisher_cls(id=2, name='Lion publishing'),
            ),
            book_cls(
                isbn='ghi',
                title='The Rabbit Club',
                description='Rabbits on the run',
                publisher=publisher_cls(id=3, name='Hop Bunny publishing'),
            )
        ]

        with subtransactions(session):
            session.add_all(books)

        sync.logical_slot_get_changes(
            f'{sync.database}_testdb',
            upto_nchanges=None,
        )

        yield books

        with subtransactions(session):
            conn = session.connection().engine.connect().connection
            conn.set_isolation_level(
                psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
            cursor = conn.cursor()
            channel = session.connection().engine.url.database
            cursor.execute(f'UNLISTEN {channel}')

        with subtransactions(session):
            sync.truncate_tables(
                [book_cls.__table__.name, publisher_cls.__table__.name])

        sync.logical_slot_get_changes(
            f'{sync.database}_testdb',
            upto_nchanges=None,
        )

        try:
            sync.es.teardown(index='testdb')
        except Exception:
            raise

        sync.redis._delete()
        session.connection().engine.connect().close()
        session.connection().engine.dispose()
Beispiel #2
0
 def poll_db():
     with subtransactions(session):
         session.execute(
             rating_cls.__table__.update().where(
                 rating_cls.__table__.c.id == 3
             ).values(value=4.4)
         )
         session.commit()
Beispiel #3
0
 def poll_db():
     with subtransactions(session):
         session.execute(
             book_cls.__table__.delete().where(
                 book_cls.__table__.c.isbn == 'abc'
             )
         )
         session.commit()
Beispiel #4
0
 def poll_db():
     with subtransactions(session):
         session.execute(
             book_cls.__table__.update().where(
                 book_cls.__table__.c.isbn == 'abc'
             ).values(title='Tiger Club')
         )
         session.commit()
Beispiel #5
0
    def test_update_non_concurrent(self, data, book_cls):
        """Test sync update and then sync in non-concurrent mode."""
        document = {
            'index': 'testdb',
            'nodes': [{
                'table': 'book',
                'columns': ['isbn', 'title']
            }]
        }
        sync = Sync(document)
        sync.sync()
        sync.es.refresh('testdb')

        session = sync.session

        docs = search(sync.es, 'testdb')

        assert docs == [
            {
                u'_meta': {},
                u'isbn': u'abc',
                u'title': u'The Tiger Club'
            },
            {
                u'_meta': {},
                u'isbn': u'def',
                u'title': u'The Lion Club'
            },
            {
                u'_meta': {},
                u'isbn': u'ghi',
                u'title': u'The Rabbit Club'
            }
        ]

        with subtransactions(session):
            session.execute(
                book_cls.__table__.update().where(
                    book_cls.__table__.c.isbn == 'abc'
                ).values(title='Tiger Club')
            )

        sync.sync()
        sync.es.refresh('testdb')

        docs = search(sync.es, 'testdb')

        assert docs == [
            {
                u'_meta': {},
                u'isbn': u'abc',
                u'title': u'Tiger Club'
            },
            {
                u'_meta': {},
                u'isbn': u'def',
                u'title': u'The Lion Club'
            },
            {
                u'_meta': {},
                u'isbn': u'ghi',
                u'title': u'The Rabbit Club'
            }
        ]
        assert_resync_empty(
            sync,
            document.get('nodes', []),
            document.get('index'),
        )
Beispiel #6
0
    def test_update_primary_key_non_concurrent(self, data, book_cls):
        """
        Test sync updates primary_key and then sync in non-concurrent mode.
        TODO: Note this test highlights a potential undesired bahaviour
        i.e we have a duplicate doc at a point in time.
        Note to self. I think this has been fixed. i.e we delete and then
        query ibsert
        """
        document = {
            'index': 'testdb',
            'nodes': [{
                'table': 'book',
                'columns': ['isbn', 'title']
            }]
        }
        sync = Sync(document)
        sync.sync()
        sync.es.refresh('testdb')

        docs = search(sync.es, 'testdb')

        assert docs == [
            {
                u'_meta': {},
                u'isbn': u'abc',
                u'title': u'The Tiger Club'
            },
            {
                u'_meta': {},
                u'isbn': u'def',
                u'title': u'The Lion Club'
            },
            {
                u'_meta': {},
                u'isbn': u'ghi',
                u'title': u'The Rabbit Club'
            }
        ]

        session = sync.session
        with subtransactions(session):
            session.execute(
                book_cls.__table__.update().where(
                    book_cls.__table__.c.isbn == 'abc'
                ).values(isbn='cba')
            )

        sync.sync()
        sync.es.refresh('testdb')

        docs = search(sync.es, 'testdb')

        assert docs == [
            {
                u'_meta': {},
                u'isbn': u'abc',
                u'title': u'The Tiger Club'
            },
            {
                u'_meta': {},
                u'isbn': u'cba',
                u'title': u'The Tiger Club'
            },
            {
                u'_meta': {},
                u'isbn': u'def',
                u'title': u'The Lion Club'
            },
            {
                u'_meta': {},
                u'isbn': u'ghi',
                u'title': u'The Rabbit Club'
            }
        ]
        assert_resync_empty(
            sync,
            document.get('nodes', []),
            document.get('index'),
        )
Beispiel #7
0
 def poll_db():
     with subtransactions(session):
         session.execute(publisher_cls.__table__.update().where(
             publisher_cls.__table__.c.id == 3).values(
                 name='Rabbit publishers'))
         session.commit()
Beispiel #8
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    documents = json.load(open(config))
    engine = pg_engine(
        database=documents[0].get("database", documents[0]["index"]))
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    # Bootstrap
    users = {
        "Carla Ferreira Cardoso":
        User(name="Carla Ferreira Cardoso", age=19, gender="female"),
        "Uwe Fuerst":
        User(name="Uwe Fuerst", age=58, gender="male"),
        "Otitodilinna Chigolum":
        User(name="Otitodilinna Chigolum", age=36, gender="male"),
    }
    with subtransactions(session):
        session.add_all(users.values())

    posts = {
        "Post 1": Post(slug="post_1", title="This is the first post"),
        "Post 2": Post(slug="post_2", title="This is the second post"),
        "Post 3": Post(slug="post_3", title="This is the third post"),
    }
    with subtransactions(session):
        session.add_all(posts.values())

    comments = {
        "Comment 1":
        Comment(
            title="Comment 1",
            content="This is a sample comment for comment 1",
        ),
        "Comment 2":
        Comment(
            title="Comment 2",
            content="This is a sample comment for comment 2",
        ),
        "Comment 3":
        Comment(
            title="Comment 3",
            content="This is a sample comment for comment 3",
        ),
        "Comment 4":
        Comment(
            title="Comment 4",
            content="This is a sample comment for comment 4",
        ),
        "Comment 5":
        Comment(
            title="Comment 5",
            content="This is a sample comment for comment 5",
        ),
        "Comment 6":
        Comment(
            title="Comment 6",
            content="This is a sample comment for comment 6",
        ),
    }
    with subtransactions(session):
        session.add_all(comments.values())

    tags = {
        "Economics": Tag(name="Economics"),
        "Career": Tag(name="Career"),
        "Political": Tag(name="Political"),
        "Fitness": Tag(name="Fitness"),
        "Entertainment": Tag(name="Entertainment"),
        "Education": Tag(name="Education"),
        "Technology": Tag(name="Technology"),
        "Health": Tag(name="Health"),
        "Fashion": Tag(name="Fashion"),
        "Design": Tag(name="Design"),
        "Photography": Tag(name="Photography"),
        "Lifestyle": Tag(name="Lifestyle"),
    }
    with subtransactions(session):
        session.add_all(tags.values())

    user_posts = [
        UserPost(
            user=users["Carla Ferreira Cardoso"],
            post=posts["Post 1"],
        ),
        UserPost(
            user=users["Uwe Fuerst"],
            post=posts["Post 2"],
        ),
        UserPost(
            user=users["Otitodilinna Chigolum"],
            post=posts["Post 3"],
        ),
    ]
    with subtransactions(session):
        session.add_all(user_posts)

    user_tags = [
        UserTag(
            user=users["Carla Ferreira Cardoso"],
            tag=tags["Economics"],
        ),
        UserTag(
            user=users["Carla Ferreira Cardoso"],
            tag=tags["Career"],
        ),
        UserTag(
            user=users["Carla Ferreira Cardoso"],
            tag=tags["Political"],
        ),
        UserTag(
            user=users["Carla Ferreira Cardoso"],
            tag=tags["Lifestyle"],
        ),
        UserTag(
            user=users["Carla Ferreira Cardoso"],
            tag=tags["Health"],
        ),
        UserTag(
            user=users["Uwe Fuerst"],
            tag=tags["Education"],
        ),
        UserTag(
            user=users["Uwe Fuerst"],
            tag=tags["Lifestyle"],
        ),
        UserTag(
            user=users["Otitodilinna Chigolum"],
            tag=tags["Fashion"],
        ),
    ]
    with subtransactions(session):
        session.add_all(user_tags)

    post_comments = [
        PostComment(
            post=posts["Post 1"],
            comment=comments["Comment 1"],
        ),
        PostComment(
            post=posts["Post 1"],
            comment=comments["Comment 2"],
        ),
        PostComment(
            post=posts["Post 2"],
            comment=comments["Comment 3"],
        ),
        PostComment(
            post=posts["Post 2"],
            comment=comments["Comment 4"],
        ),
        PostComment(
            post=posts["Post 3"],
            comment=comments["Comment 5"],
        ),
        PostComment(
            post=posts["Post 3"],
            comment=comments["Comment 6"],
        ),
    ]
    with subtransactions(session):
        session.add_all(post_comments)
Beispiel #9
0
def main(config, nsize):

    config = get_config(config)
    teardown(drop_db=False, config=config)

    for document in json.load(open(config)):

        engine = pg_engine(
            database=document.get('database', document['index'])
        )
        schema = document.get('schema', SCHEMA)
        connection = engine.connect().execution_options(
            schema_translate_map={
                None: schema
            }
        )
        Session = sessionmaker(bind=connection, autoflush=True)
        session = Session()

        # Bootstrap
        continents = {
            'Europe': Continent(name='Europe'),
            'North America': Continent(name='North America'),
        }
        with subtransactions(session):
            session.add_all(continents.values())

        countries = {
            'United Kingdom': Country(
                name='United Kingdom',
                continent=continents['Europe'],
            ),
            'France': Country(
                name='France',
                continent=continents['Europe'],
            ),
            'United States': Country(
                name='United States',
                continent=continents['North America'],
            ),
        }
        with subtransactions(session):
            session.add_all(countries.values())

        cities = {
            'London': City(name='London', country=countries['United Kingdom']),
            'Paris': City(name='Paris', country=countries['France']),
            'New York': City(name='New York', country=countries['United States']),
        }
        with subtransactions(session):
            session.add_all(cities.values())

        publishers = {
            'Oxford Press': Publisher(name='Oxford Press', is_active=True),
            'Penguin Books': Publisher(name='Penguin Books', is_active=False),
            'Pearson Press': Publisher(name='Pearson Press', is_active=True),
            'Reutgers Press': Publisher(name='Reutgers Press', is_active=False),
        }
        with subtransactions(session):
            session.add_all(publishers.values())

        authors = {
            'Stephen King': Author(
                name='Stephen King',
                date_of_birth=datetime.datetime(1947, 9, 21),
                city=cities['London'],
            ),
            'J. K. Rowling': Author(
                name='J. K. Rowling',
                date_of_birth=datetime.datetime(1965, 7, 31),
                city=cities['Paris'],
            ),
            'James Patterson': Author(
                name='James Patterson',
                date_of_birth=datetime.datetime(1947, 3, 22),
                city=cities['New York'],
            ),
            'Melinda Leigh': Author(
                name='Melinda Leigh',
                date_of_birth=datetime.datetime(1980, 1, 1),
                city=cities['Paris'],
            ),
            'Tolu Aina': Author(
                name='Tolu Aina',
                date_of_birth=datetime.datetime(1980, 5, 21),
                city=cities['London'],
            ),
        }
        with subtransactions(session):
            session.add_all(authors.values())

        subjects = {
            'Literature': Subject(name='Literature'),
            'Poetry': Subject(name='Poetry'),
            'Romance': Subject(name='Romance'),
            'Science Fiction & Fantasy': Subject(name='Science Fiction & Fantasy'),
            'Westerns': Subject(name='Westerns'),
        }
        with subtransactions(session):
            session.add_all(subjects.values())

        languages = {
            'en-GB': Language(code='en-GB'),
            'en-US': Language(code='en-US'),
            'de-DE': Language(code='de-DE'),
            'af-ZA': Language(code='af-ZA'),
            'es-ES': Language(code='es-ES'),
            'fr-FR': Language(code='fr-FR'),
            'it-IT': Language(code='it-IT'),
            'ja-JP': Language(code='ja-JP'),
        }
        with subtransactions(session):
            session.add_all(languages.values())

        shelves = {
            'Shelf A': Shelf(shelf='Shelf A'),
            'Shelf B': Shelf(shelf='Shelf B'),
        }
        with subtransactions(session):
            session.add_all(shelves.values())

        books = {
            '001': Book(
                isbn='001',
                title='It',
                description='Stephens Kings It',
                publisher=publishers['Oxford Press'],
                tags=['a', 'b', 'c'],
                doc={
                    "i": 73,
                    "bool": True,
                    "firstname": "Glenda",
                    "lastname": "Judye",
                    "nick_names": [
                        "Beatriz",
                        "Jean",
                        "Carilyn",
                        "Carol-Jean",
                        "Sara-Ann"
                    ],
                    "coordinates": {
                        "lat": 21.1,
                        "lon": 32.9
                    },
                    "a": {
                        "b": {
                            "c": [0, 1, 2, 3, 4]
                        }
                    },
                    "x": [
                        {
                            "y": 0,
                            "z": 5
                        },
                        {
                            "y": 1,
                            "z": 6
                        }
                    ],
                    "generation": {
                        "name": 'X'
                    }
                }
            ),
            '002': Book(
                isbn='002',
                title='The Body',
                description='Lodsdcsdrem ipsum dodscdslor sit amet',
                publisher=publishers['Oxford Press'],
                tags=['d', 'e', 'f'],
                doc={
                    "i": 99,
                    "bool": False,
                    "firstname": "Jack",
                    "lastname": "Jones",
                    "nick_names": [
                        "Jack",
                        "Jones",
                        "Jay",
                        "Jay-Jay",
                        "Jackie"
                    ],
                    "coordinates": {
                        "lat": 25.1,
                        "lon": 52.2
                    },
                    "a": {
                        "b": {
                            "c": [2, 3, 4, 5, 6]
                        }
                    },
                    "x": [
                        {
                            "y": 2,
                            "z": 3
                        },
                        {
                            "y": 7,
                            "z": 2
                        }
                    ],
                    "generation": {
                        "name": 'X'
                    }
                }
            ),
            '003': Book(
                isbn='003',
                title='Harry Potter and the Sorcerer\'s Stone',
                description='Harry Potter has never been',
                publisher=publishers['Penguin Books'],
                tags=['g', 'h', 'i'],
                doc={
                    "i": 13,
                    "bool": True,
                    "firstname": "Mary",
                    "lastname": "Jane",
                    "nick_names": [
                        "Mariane",
                        "May",
                        "Maey",
                        "M-Jane",
                        "Jane"
                    ],
                    "coordinates": {
                        "lat": 24.9,
                        "lon": 93.2
                    },
                    "a": {
                        "b": {
                            "c": [9, 8, 7, 6, 5]
                        }
                    },
                    "x": [
                        {
                            "y": 3,
                            "z": 5
                        },
                        {
                            "y": 8,
                            "z": 2
                        }
                    ],
                    "generation": {
                        "name": 'X'
                    }
                }
            ),
            '004': Book(
                isbn='004',
                title='Harry Potter and the Chamber of Secrets',
                description='The Dursleys were so mean and hideous that summer '
                            'that all Harry Potter wanted was to get back to the '
                            'Hogwarts School for Witchcraft and Wizardry',
                publisher=publishers['Penguin Books'],
                tags=['j', 'k', 'l'],
                doc={
                    "i": 43,
                    "bool": False,
                    "firstname": "Kermit",
                    "lastname": "Frog",
                    "nick_names": [
                        "Kermit",
                        "Frog",
                        "Ker",
                        "Boss",
                        "K"
                    ],
                    "coordinates": {
                        "lat": 22.7,
                        "lon": 35.2
                    },
                    "a": {
                        "b": {
                            "c": [9, 1, 2, 8, 4]
                        }
                    },
                    "x": [
                        {
                            "y": 3,
                            "z": 2
                        },
                        {
                            "y": 9,
                            "z": 2
                        }
                    ],
                    "generation": {
                        "name": 'Z'
                    }
                }
            ),
            '005': Book(
                isbn='005',
                title='The 17th Suspect',
                description='A series of shootings exposes San Francisco to a '
                            'methodical yet unpredictable killer, and a reluctant '
                            'woman decides to put her trust in Sergeant Lindsay '
                            'Boxer',
                publisher=publishers['Pearson Press'],
                tags=['m', 'n', 'o'],
            ),
            '006': Book(
                isbn='006',
                title='The President Is Missing',
                description='The publishing event of 2018: Bill Clinton and James '
                            'Patterson\'s The President Is Missing is a '
                            'superlative thriller',
                publisher=publishers['Pearson Press'],
            ),
            '007': Book(
                isbn='007',
                title='Say You\'re Sorry',
                description='deserunt mollit anim id est laborum',
                publisher=publishers['Reutgers Press'],
            ),
            '008': Book(
                isbn='008',
                title='Bones Don\'t Lie',
                description='Lorem ipsum',
                publisher=publishers['Reutgers Press'],
            ),
        }
        with subtransactions(session):
            session.add_all(books.values())

        ratings = [
            Rating(value=1.1, book=books['001']),
            Rating(value=2.1, book=books['002']),
            Rating(value=3.1, book=books['003']),
            Rating(value=4.1, book=books['004']),
            Rating(value=5.1, book=books['005']),
            Rating(value=6.1, book=books['006']),
            Rating(value=7.1, book=books['007']),
            Rating(value=8.1, book=books['008']),
        ]
        with subtransactions(session):
            session.add_all(ratings)

        book_authors = [
            BookAuthor(book=books['001'], author=authors['Stephen King']),
            BookAuthor(book=books['002'], author=authors['Stephen King']),
            BookAuthor(book=books['003'], author=authors['J. K. Rowling']),
            BookAuthor(book=books['004'], author=authors['J. K. Rowling']),
            BookAuthor(book=books['005'], author=authors['James Patterson']),
            BookAuthor(book=books['006'], author=authors['James Patterson']),
            BookAuthor(book=books['007'], author=authors['Melinda Leigh']),
            BookAuthor(book=books['008'], author=authors['Melinda Leigh']),
            BookAuthor(book=books['007'], author=authors['Tolu Aina']),
            BookAuthor(book=books['008'], author=authors['Tolu Aina']),
        ]
        with subtransactions(session):
            session.add_all(book_authors)

        book_subjects = [
            BookSubject(book=books['001'], subject=subjects['Literature']),
            BookSubject(book=books['002'], subject=subjects['Literature']),
            BookSubject(book=books['003'], subject=subjects['Poetry']),
            BookSubject(book=books['004'], subject=subjects['Poetry']),
            BookSubject(book=books['005'], subject=subjects['Romance']),
            BookSubject(book=books['006'], subject=subjects['Romance']),
            BookSubject(
                book=books['007'],
                subject=subjects['Science Fiction & Fantasy'],
            ),
            BookSubject(
                book=books['008'],
                subject=subjects['Science Fiction & Fantasy'],
            ),
        ]
        with subtransactions(session):
            session.add_all(book_subjects)

        book_languages = [
            BookLanguage(book=books['001'], language=languages['en-GB']),
            BookLanguage(book=books['002'], language=languages['en-GB']),
            BookLanguage(book=books['003'], language=languages['en-GB']),
            BookLanguage(book=books['004'], language=languages['en-GB']),
            BookLanguage(book=books['005'], language=languages['en-GB']),
            BookLanguage(book=books['006'], language=languages['en-GB']),
            BookLanguage(book=books['007'], language=languages['en-GB']),
            BookLanguage(book=books['008'], language=languages['en-GB']),
            BookLanguage(book=books['001'], language=languages['fr-FR']),
            BookLanguage(book=books['002'], language=languages['fr-FR']),
            BookLanguage(book=books['003'], language=languages['fr-FR']),
            BookLanguage(book=books['004'], language=languages['fr-FR']),
            BookLanguage(book=books['005'], language=languages['fr-FR']),
            BookLanguage(book=books['006'], language=languages['fr-FR']),
            BookLanguage(book=books['007'], language=languages['fr-FR']),
            BookLanguage(book=books['008'], language=languages['fr-FR']),
        ]
        with subtransactions(session):
            session.add_all(book_languages)

        book_shelves = [
            BookShelf(book=books['001'], shelf=shelves['Shelf A']),
            BookShelf(book=books['001'], shelf=shelves['Shelf B']),
            BookShelf(book=books['002'], shelf=shelves['Shelf A']),
            BookShelf(book=books['002'], shelf=shelves['Shelf B']),
            BookShelf(book=books['003'], shelf=shelves['Shelf A']),
            BookShelf(book=books['004'], shelf=shelves['Shelf A']),
            BookShelf(book=books['005'], shelf=shelves['Shelf A']),
            BookShelf(book=books['006'], shelf=shelves['Shelf A']),
            BookShelf(book=books['007'], shelf=shelves['Shelf A']),
            BookShelf(book=books['008'], shelf=shelves['Shelf A']),
        ]
        with subtransactions(session):
            session.add_all(book_shelves)

        # Dummy data
        if nsize > 1:
            nsamples = int(nsize)
            faker = Faker()
            print('Adding {} books'.format(nsamples))

            for _ in range(nsamples):
                book = Book(
                    isbn=faker.isbn13(),
                    title=faker.sentence(),
                    description=faker.text(),
                    publisher=random.choice(list(publishers.values())),
                )
                session.add(book)
                author = Author(
                    name='{} .{} {}'.format(
                        faker.first_name(),
                        faker.random_letter(),
                        faker.last_name(),
                    ),
                    date_of_birth=faker.date_object(),
                    city=random.choice(list(cities.values())),
                )
                session.add(author)
                book_author = BookAuthor(book=book, author=author)
                session.add(book_author)
                book_subject = BookSubject(
                    book=book,
                    subject=random.choice(list(subjects.values())),
                )
                session.add(book_subject)
                book_languge = BookLanguage(
                    book=book,
                    language=random.choice(list(languages.values())),
                )
                session.add(book_languge)
                book_shelf = BookShelf(
                    book=book,
                    shelf=random.choice(list(shelves.values())),
                )
                session.add(book_shelf)
            session.commit()
Beispiel #10
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    document = json.load(open(config))
    engine = pg_engine(
        database=document[0].get('database', document[0]['index']))
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    # Bootstrap
    users = {
        'Carla Ferreira Cardoso':
        User(name='Carla Ferreira Cardoso', age=19, gender='female'),
        'Uwe Fuerst':
        User(name='Uwe Fuerst', age=58, gender='male'),
        'Otitodilinna Chigolum':
        User(name='Otitodilinna Chigolum', age=36, gender='male'),
    }
    with subtransactions(session):
        session.add_all(users.values())

    posts = {
        'Post1': Post(slug='post_1', title='This is the first post'),
        'Post2': Post(slug='post_2', title='This is the second post'),
        'Post3': Post(slug='post_3', title='This is the third post'),
    }
    with subtransactions(session):
        session.add_all(posts.values())

    comments = {
        'Comment1':
        Comment(
            title='Comment 1',
            content='This is a sample comment for comment 1',
        ),
        'Comment2':
        Comment(
            title='Comment 2',
            content='This is a sample comment for comment 2',
        ),
        'Comment3':
        Comment(
            title='Comment 3',
            content='This is a sample comment for comment 3',
        ),
        'Comment4':
        Comment(
            title='Comment 4',
            content='This is a sample comment for comment 4',
        ),
        'Comment5':
        Comment(
            title='Comment 5',
            content='This is a sample comment for comment 5',
        ),
        'Comment6':
        Comment(
            title='Comment 6',
            content='This is a sample comment for comment 6',
        ),
    }
    with subtransactions(session):
        session.add_all(comments.values())

    tags = {
        'Economics': Tag(name='Economics'),
        'Career': Tag(name='Career'),
        'Political': Tag(name='Political'),
        'Fitness': Tag(name='Fitness'),
        'Entertainment': Tag(name='Entertainment'),
        'Education': Tag(name='Education'),
        'Technology': Tag(name='Technology'),
        'Health': Tag(name='Health'),
        'Fashion': Tag(name='Fashion'),
        'Design': Tag(name='Design'),
        'Photography': Tag(name='Photography'),
        'Lifestyle': Tag(name='Lifestyle'),
    }
    with subtransactions(session):
        session.add_all(tags.values())

    user_posts = [
        UserPost(
            user=users['Carla Ferreira Cardoso'],
            post=posts['Post1'],
        ),
        UserPost(
            user=users['Uwe Fuerst'],
            post=posts['Post2'],
        ),
        UserPost(
            user=users['Otitodilinna Chigolum'],
            post=posts['Post3'],
        ),
    ]
    with subtransactions(session):
        session.add_all(user_posts)

    user_tags = [
        UserTag(
            user=users['Carla Ferreira Cardoso'],
            tag=tags['Economics'],
        ),
        UserTag(
            user=users['Carla Ferreira Cardoso'],
            tag=tags['Career'],
        ),
        UserTag(
            user=users['Carla Ferreira Cardoso'],
            tag=tags['Political'],
        ),
        UserTag(
            user=users['Carla Ferreira Cardoso'],
            tag=tags['Lifestyle'],
        ),
        UserTag(
            user=users['Carla Ferreira Cardoso'],
            tag=tags['Health'],
        ),
        UserTag(
            user=users['Uwe Fuerst'],
            tag=tags['Education'],
        ),
        UserTag(
            user=users['Uwe Fuerst'],
            tag=tags['Lifestyle'],
        ),
        UserTag(
            user=users['Otitodilinna Chigolum'],
            tag=tags['Fashion'],
        ),
    ]
    with subtransactions(session):
        session.add_all(user_tags)

    post_comments = [
        PostComment(
            post=posts['Post1'],
            comment=comments['Comment1'],
        ),
        PostComment(
            post=posts['Post1'],
            comment=comments['Comment2'],
        ),
        PostComment(
            post=posts['Post2'],
            comment=comments['Comment3'],
        ),
        PostComment(
            post=posts['Post2'],
            comment=comments['Comment4'],
        ),
        PostComment(
            post=posts['Post3'],
            comment=comments['Comment5'],
        ),
        PostComment(
            post=posts['Post3'],
            comment=comments['Comment6'],
        ),
    ]
    with subtransactions(session):
        session.add_all(post_comments)
Beispiel #11
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    schema = json.load(open(config))
    engine = pg_engine(database=schema[0].get('index'))
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    # Bootstrap
    users = [
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
    ]

    hosts = [
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
    ]

    cities = [
        Cities(name='Manila',
               country=Countries(
                   name='Philippines',
                   country_code='PH',
               )),
        Cities(
            name='Lisbon',
            country=Countries(
                name='Portugal',
                country_code='PT',
            ),
        ),
        Cities(
            name='Havana',
            country=Countries(
                name='Cuba',
                country_code='PT',
            ),
        ),
        Cities(
            name='Copenhagen',
            country=Countries(
                name='Denmark',
                country_code='DK',
            ),
        ),
        Cities(
            name='London',
            country=Countries(
                name='United Kingdom',
                country_code='UK',
            ),
        ),
        Cities(
            name='Casablanca',
            country=Countries(
                name='Morocco',
                country_code='MA',
            ),
        ),
    ]

    places = [
        Places(
            host=hosts[0],
            city=cities[0],
            address='Quezon Boulevard',
        ),
        Places(
            host=hosts[1],
            city=cities[1],
            address='Castelo de São Jorge',
        ),
        Places(
            host=hosts[2],
            city=cities[2],
            address='Old Havana',
        ),
        Places(
            host=hosts[3],
            city=cities[3],
            address='Tivoli Gardens',
        ),
        Places(
            host=hosts[4],
            city=cities[4],
            address='Buckingham Palace',
        ),
        Places(
            host=hosts[5],
            city=cities[5],
            address='Medina',
        ),
    ]

    reviews = [
        Reviews(booking=Bookings(
            user=users[0],
            place=places[0],
            start_date=datetime.now() + timedelta(days=1),
            end_date=datetime.now() + timedelta(days=4),
            price_per_night=100,
            num_nights=4,
        ),
                rating=1,
                review_body='Neque porro quisquam est qui dolorem'),
        Reviews(booking=Bookings(
            user=users[1],
            place=places[1],
            start_date=datetime.now() + timedelta(days=2),
            end_date=datetime.now() + timedelta(days=4),
            price_per_night=150,
            num_nights=3,
        ),
                rating=2,
                review_body='Sed eget finibus massa, vel efficitur mauris'),
        Reviews(
            booking=Bookings(
                user=users[2],
                place=places[2],
                start_date=datetime.now() + timedelta(days=15),
                end_date=datetime.now() + timedelta(days=19),
                price_per_night=120,
                num_nights=4,
            ),
            rating=3,
            review_body='Suspendisse cursus ex et turpis dignissim dignissim'),
        Reviews(booking=Bookings(
            user=users[3],
            place=places[3],
            start_date=datetime.now() + timedelta(days=2),
            end_date=datetime.now() + timedelta(days=7),
            price_per_night=300,
            num_nights=5,
        ),
                rating=4,
                review_body='Suspendisse ultricies arcu lectus'),
        Reviews(booking=Bookings(
            user=users[4],
            place=places[4],
            start_date=datetime.now() + timedelta(days=1),
            end_date=datetime.now() + timedelta(days=10),
            price_per_night=800,
            num_nights=3,
        ),
                rating=5,
                review_body='Putent sententiae scribentur ne vis'),
        Reviews(booking=Bookings(
            user=users[5],
            place=places[5],
            start_date=datetime.now() + timedelta(days=2),
            end_date=datetime.now() + timedelta(days=8),
            price_per_night=80,
            num_nights=10,
        ),
                rating=3,
                review_body='Debet invenire sed ne'),
    ]

    with subtransactions(session):
        session.add_all(users)
        session.add_all(hosts)
        session.add_all(cities)
        session.add_all(places)
        session.add_all(reviews)
Beispiel #12
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    documents = json.load(open(config))
    engine = pg_engine(database=documents[0].get(
        "database",
        documents[0]["index"],
    ))
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    # Bootstrap
    categories = [
        Category(
            id=1,
            uid="c001",
            text="Colours",
        ),
        Category(
            id=2,
            uid="c002",
            text="Weather",
        ),
    ]
    with subtransactions(session):
        session.add_all(categories)

    questions = [
        Question(
            id=1,
            uid="q001",
            category_id=1,
            category_uid="c001",
            text="What is your favorite color?",
        ),
        Question(
            id=2,
            uid="q002",
            category_id=2,
            category_uid="c002",
            text="Is it raining outside?",
        ),
    ]
    with subtransactions(session):
        session.add_all(questions)

    answers = [
        Answer(id=1, uid="a001", text="Red"),
        Answer(id=2, uid="a002", text="Yes"),
        Answer(id=3, uid="a003", text="Green"),
        Answer(id=4, uid="a004", text="No"),
    ]
    with subtransactions(session):
        session.add_all(answers)

    possible_answers = [
        PossibleAnswer(
            question_id=1,
            question_uid="q001",
            answer_id=1,
            answer_uid="a001",
        ),
        PossibleAnswer(
            question_id=1,
            question_uid="q001",
            answer_id=3,
            answer_uid="a003",
        ),
        PossibleAnswer(
            question_id=2,
            question_uid="q002",
            answer_id=2,
            answer_uid="a002",
        ),
        PossibleAnswer(
            question_id=2,
            question_uid="q002",
            answer_id=4,
            answer_uid="a004",
        ),
    ]
    with subtransactions(session):
        session.add_all(possible_answers)

    real_answers = [
        RealAnswer(
            question_id=1,
            question_uid="q001",
            answer_id=1,
            answer_uid="a001",
        ),
        RealAnswer(
            question_id=2,
            question_uid="q002",
            answer_id=4,
            answer_uid="a004",
        ),
    ]
    with subtransactions(session):
        session.add_all(real_answers)
Beispiel #13
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    documents = json.load(open(config))
    engine = pg_engine(
        database=documents[0].get("database", documents[0]["index"])
    )
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    users = [
        Users(email="*****@*****.**"),
        Users(email="*****@*****.**"),
        Users(email="*****@*****.**"),
        Users(email="*****@*****.**"),
        Users(email="*****@*****.**"),
        Users(email="*****@*****.**"),
    ]

    hosts = [
        Hosts(email="*****@*****.**"),
        Hosts(email="*****@*****.**"),
        Hosts(email="*****@*****.**"),
        Hosts(email="*****@*****.**"),
        Hosts(email="*****@*****.**"),
        Hosts(email="*****@*****.**"),
        Hosts(email="*****@*****.**"),
    ]

    cities = [
        Cities(
            name="Manila",
            country=Countries(
                name="Philippines",
                country_code="PH",
            ),
        ),
        Cities(
            name="Lisbon",
            country=Countries(
                name="Portugal",
                country_code="PT",
            ),
        ),
        Cities(
            name="Havana",
            country=Countries(
                name="Cuba",
                country_code="CU",
            ),
        ),
        Cities(
            name="Copenhagen",
            country=Countries(
                name="Denmark",
                country_code="DK",
            ),
        ),
        Cities(
            name="London",
            country=Countries(
                name="United Kingdom",
                country_code="UK",
            ),
        ),
        Cities(
            name="Casablanca",
            country=Countries(
                name="Morocco",
                country_code="MA",
            ),
        ),
    ]

    places = [
        Places(
            host=hosts[0],
            city=cities[0],
            address="Quezon Boulevard",
        ),
        Places(
            host=hosts[1],
            city=cities[1],
            address="Castelo de São Jorge",
        ),
        Places(
            host=hosts[2],
            city=cities[2],
            address="Old Havana",
        ),
        Places(
            host=hosts[3],
            city=cities[3],
            address="Tivoli Gardens",
        ),
        Places(
            host=hosts[4],
            city=cities[4],
            address="Buckingham Palace",
        ),
        Places(
            host=hosts[5],
            city=cities[5],
            address="Medina",
        ),
    ]

    reviews = [
        Reviews(
            booking=Bookings(
                user=users[0],
                place=places[0],
                start_date=datetime.now() + timedelta(days=1),
                end_date=datetime.now() + timedelta(days=4),
                price_per_night=100,
                num_nights=4,
            ),
            rating=1,
            review_body="The rooms were left in a tolerable state",
        ),
        Reviews(
            booking=Bookings(
                user=users[1],
                place=places[1],
                start_date=datetime.now() + timedelta(days=2),
                end_date=datetime.now() + timedelta(days=4),
                price_per_night=150,
                num_nights=3,
            ),
            rating=2,
            review_body="I found my place wonderfully taken care of",
        ),
        Reviews(
            booking=Bookings(
                user=users[2],
                place=places[2],
                start_date=datetime.now() + timedelta(days=15),
                end_date=datetime.now() + timedelta(days=19),
                price_per_night=120,
                num_nights=4,
            ),
            rating=3,
            review_body="All of my house rules were respected",
        ),
        Reviews(
            booking=Bookings(
                user=users[3],
                place=places[3],
                start_date=datetime.now() + timedelta(days=2),
                end_date=datetime.now() + timedelta(days=7),
                price_per_night=300,
                num_nights=5,
            ),
            rating=4,
            review_body="Such a pleasure to host and welcome these guests",
        ),
        Reviews(
            booking=Bookings(
                user=users[4],
                place=places[4],
                start_date=datetime.now() + timedelta(days=1),
                end_date=datetime.now() + timedelta(days=10),
                price_per_night=800,
                num_nights=3,
            ),
            rating=5,
            review_body="We would be happy to host them again",
        ),
        Reviews(
            booking=Bookings(
                user=users[5],
                place=places[5],
                start_date=datetime.now() + timedelta(days=2),
                end_date=datetime.now() + timedelta(days=8),
                price_per_night=80,
                num_nights=10,
            ),
            rating=3,
            review_body="Please do not visit our town ever again!",
        ),
    ]

    with subtransactions(session):
        session.add_all(users)
        session.add_all(hosts)
        session.add_all(cities)
        session.add_all(places)
        session.add_all(reviews)
Beispiel #14
0
def main(config, nsize):

    config = get_config(config)
    teardown(drop_db=False, config=config)

    for document in json.load(open(config)):

        engine = pg_engine(
            database=document.get("database", document["index"]))
        schema = document.get("schema", SCHEMA)
        connection = engine.connect().execution_options(
            schema_translate_map={None: schema})
        Session = sessionmaker(bind=connection, autoflush=True)
        session = Session()

        # Bootstrap
        continents = {
            "Europe": Continent(name="Europe"),
            "North America": Continent(name="North America"),
        }
        with subtransactions(session):
            session.add_all(continents.values())

        countries = {
            "United Kingdom":
            Country(
                name="United Kingdom",
                continent=continents["Europe"],
            ),
            "France":
            Country(
                name="France",
                continent=continents["Europe"],
            ),
            "United States":
            Country(
                name="United States",
                continent=continents["North America"],
            ),
        }
        with subtransactions(session):
            session.add_all(countries.values())

        cities = {
            "London": City(name="London", country=countries["United Kingdom"]),
            "Paris": City(name="Paris", country=countries["France"]),
            "New York": City(name="New York",
                             country=countries["United States"]),
        }
        with subtransactions(session):
            session.add_all(cities.values())

        publishers = {
            "Oxford Press": Publisher(name="Oxford Press", is_active=True),
            "Penguin Books": Publisher(name="Penguin Books", is_active=False),
            "Pearson Press": Publisher(name="Pearson Press", is_active=True),
            "Reutgers Press": Publisher(name="Reutgers Press",
                                        is_active=False),
        }
        with subtransactions(session):
            session.add_all(publishers.values())

        authors = {
            "Stephen King":
            Author(
                name="Stephen King",
                date_of_birth=datetime.datetime(1947, 9, 21),
                city=cities["London"],
            ),
            "J. K. Rowling":
            Author(
                name="J. K. Rowling",
                date_of_birth=datetime.datetime(1965, 7, 31),
                city=cities["Paris"],
            ),
            "James Patterson":
            Author(
                name="James Patterson",
                date_of_birth=datetime.datetime(1947, 3, 22),
                city=cities["New York"],
            ),
            "Melinda Leigh":
            Author(
                name="Melinda Leigh",
                date_of_birth=datetime.datetime(1980, 1, 1),
                city=cities["Paris"],
            ),
            "Tolu Aina":
            Author(
                name="Tolu Aina",
                date_of_birth=datetime.datetime(1980, 5, 21),
                city=cities["London"],
            ),
        }
        with subtransactions(session):
            session.add_all(authors.values())

        subjects = {
            "Literature": Subject(name="Literature"),
            "Poetry": Subject(name="Poetry"),
            "Romance": Subject(name="Romance"),
            "Science Fiction & Fantasy":
            Subject(name="Science Fiction & Fantasy"),
            "Westerns": Subject(name="Westerns"),
        }
        with subtransactions(session):
            session.add_all(subjects.values())

        languages = {
            "en-GB": Language(code="en-GB"),
            "en-US": Language(code="en-US"),
            "de-DE": Language(code="de-DE"),
            "af-ZA": Language(code="af-ZA"),
            "es-ES": Language(code="es-ES"),
            "fr-FR": Language(code="fr-FR"),
            "it-IT": Language(code="it-IT"),
            "ja-JP": Language(code="ja-JP"),
        }
        with subtransactions(session):
            session.add_all(languages.values())

        shelves = {
            "Shelf A": Shelf(shelf="Shelf A"),
            "Shelf B": Shelf(shelf="Shelf B"),
        }
        with subtransactions(session):
            session.add_all(shelves.values())

        books = {
            "001":
            Book(
                isbn="001",
                title="It",
                description="Stephens Kings It",
                publisher=publishers["Oxford Press"],
                tags=["a", "b", "c"],
                doc={
                    "i":
                    73,
                    "bool":
                    True,
                    "firstname":
                    "Glenda",
                    "lastname":
                    "Judye",
                    "nick_names": [
                        "Beatriz",
                        "Jean",
                        "Carilyn",
                        "Carol-Jean",
                        "Sara-Ann",
                    ],
                    "coordinates": {
                        "lat": 21.1,
                        "lon": 32.9
                    },
                    "a": {
                        "b": {
                            "c": [0, 1, 2, 3, 4]
                        }
                    },
                    "x": [{
                        "y": 0,
                        "z": 5
                    }, {
                        "y": 1,
                        "z": 6
                    }],
                    "generation": {
                        "name": "X"
                    },
                },
            ),
            "002":
            Book(
                isbn="002",
                title="The Body",
                description="Lodsdcsdrem ipsum dodscdslor sit amet",
                publisher=publishers["Oxford Press"],
                tags=["d", "e", "f"],
                doc={
                    "i": 99,
                    "bool": False,
                    "firstname": "Jack",
                    "lastname": "Jones",
                    "nick_names": [
                        "Jack",
                        "Jones",
                        "Jay",
                        "Jay-Jay",
                        "Jackie",
                    ],
                    "coordinates": {
                        "lat": 25.1,
                        "lon": 52.2
                    },
                    "a": {
                        "b": {
                            "c": [2, 3, 4, 5, 6]
                        }
                    },
                    "x": [{
                        "y": 2,
                        "z": 3
                    }, {
                        "y": 7,
                        "z": 2
                    }],
                    "generation": {
                        "name": "X"
                    },
                },
            ),
            "003":
            Book(
                isbn="003",
                title="Harry Potter and the Sorcerer's Stone",
                description="Harry Potter has never been",
                publisher=publishers["Penguin Books"],
                tags=["g", "h", "i"],
                doc={
                    "i": 13,
                    "bool": True,
                    "firstname": "Mary",
                    "lastname": "Jane",
                    "nick_names": ["Mariane", "May", "Maey", "M-Jane", "Jane"],
                    "coordinates": {
                        "lat": 24.9,
                        "lon": 93.2
                    },
                    "a": {
                        "b": {
                            "c": [9, 8, 7, 6, 5]
                        }
                    },
                    "x": [{
                        "y": 3,
                        "z": 5
                    }, {
                        "y": 8,
                        "z": 2
                    }],
                    "generation": {
                        "name": "X"
                    },
                },
            ),
            "004":
            Book(
                isbn="004",
                title="Harry Potter and the Chamber of Secrets",
                description="The Dursleys were so mean and hideous that summer "
                "that all Harry Potter wanted was to get back to the "
                "Hogwarts School for Witchcraft and Wizardry",
                publisher=publishers["Penguin Books"],
                tags=["j", "k", "l"],
                doc={
                    "i": 43,
                    "bool": False,
                    "firstname": "Kermit",
                    "lastname": "Frog",
                    "nick_names": ["Kermit", "Frog", "Ker", "Boss", "K"],
                    "coordinates": {
                        "lat": 22.7,
                        "lon": 35.2
                    },
                    "a": {
                        "b": {
                            "c": [9, 1, 2, 8, 4]
                        }
                    },
                    "x": [{
                        "y": 3,
                        "z": 2
                    }, {
                        "y": 9,
                        "z": 2
                    }],
                    "generation": {
                        "name": "Z"
                    },
                },
            ),
            "005":
            Book(
                isbn="005",
                title="The 17th Suspect",
                description="A series of shootings exposes San Francisco to a "
                "methodical yet unpredictable killer, and a reluctant "
                "woman decides to put her trust in Sergeant Lindsay "
                "Boxer",
                publisher=publishers["Pearson Press"],
                tags=["m", "n", "o"],
            ),
            "006":
            Book(
                isbn="006",
                title="The President Is Missing",
                description=
                "The publishing event of 2018: Bill Clinton and James "
                "Patterson's The President Is Missing is a "
                "superlative thriller",
                publisher=publishers["Pearson Press"],
            ),
            "007":
            Book(
                isbn="007",
                title="Say You're Sorry",
                description="deserunt mollit anim id est laborum",
                publisher=publishers["Reutgers Press"],
            ),
            "008":
            Book(
                isbn="008",
                title="Bones Don't Lie",
                description="Lorem ipsum",
                publisher=publishers["Reutgers Press"],
            ),
        }
        with subtransactions(session):
            session.add_all(books.values())

        ratings = [
            Rating(value=1.1, book=books["001"]),
            Rating(value=2.1, book=books["002"]),
            Rating(value=3.1, book=books["003"]),
            Rating(value=4.1, book=books["004"]),
            Rating(value=5.1, book=books["005"]),
            Rating(value=6.1, book=books["006"]),
            Rating(value=7.1, book=books["007"]),
            Rating(value=8.1, book=books["008"]),
        ]
        with subtransactions(session):
            session.add_all(ratings)

        book_authors = [
            BookAuthor(book=books["001"], author=authors["Stephen King"]),
            BookAuthor(book=books["002"], author=authors["Stephen King"]),
            BookAuthor(book=books["003"], author=authors["J. K. Rowling"]),
            BookAuthor(book=books["004"], author=authors["J. K. Rowling"]),
            BookAuthor(book=books["005"], author=authors["James Patterson"]),
            BookAuthor(book=books["006"], author=authors["James Patterson"]),
            BookAuthor(book=books["007"], author=authors["Melinda Leigh"]),
            BookAuthor(book=books["008"], author=authors["Melinda Leigh"]),
            BookAuthor(book=books["007"], author=authors["Tolu Aina"]),
            BookAuthor(book=books["008"], author=authors["Tolu Aina"]),
        ]
        with subtransactions(session):
            session.add_all(book_authors)

        book_subjects = [
            BookSubject(book=books["001"], subject=subjects["Literature"]),
            BookSubject(book=books["002"], subject=subjects["Literature"]),
            BookSubject(book=books["003"], subject=subjects["Poetry"]),
            BookSubject(book=books["004"], subject=subjects["Poetry"]),
            BookSubject(book=books["005"], subject=subjects["Romance"]),
            BookSubject(book=books["006"], subject=subjects["Romance"]),
            BookSubject(
                book=books["007"],
                subject=subjects["Science Fiction & Fantasy"],
            ),
            BookSubject(
                book=books["008"],
                subject=subjects["Science Fiction & Fantasy"],
            ),
        ]
        with subtransactions(session):
            session.add_all(book_subjects)

        book_languages = [
            BookLanguage(book=books["001"], language=languages["en-GB"]),
            BookLanguage(book=books["002"], language=languages["en-GB"]),
            BookLanguage(book=books["003"], language=languages["en-GB"]),
            BookLanguage(book=books["004"], language=languages["en-GB"]),
            BookLanguage(book=books["005"], language=languages["en-GB"]),
            BookLanguage(book=books["006"], language=languages["en-GB"]),
            BookLanguage(book=books["007"], language=languages["en-GB"]),
            BookLanguage(book=books["008"], language=languages["en-GB"]),
            BookLanguage(book=books["001"], language=languages["fr-FR"]),
            BookLanguage(book=books["002"], language=languages["fr-FR"]),
            BookLanguage(book=books["003"], language=languages["fr-FR"]),
            BookLanguage(book=books["004"], language=languages["fr-FR"]),
            BookLanguage(book=books["005"], language=languages["fr-FR"]),
            BookLanguage(book=books["006"], language=languages["fr-FR"]),
            BookLanguage(book=books["007"], language=languages["fr-FR"]),
            BookLanguage(book=books["008"], language=languages["fr-FR"]),
        ]
        with subtransactions(session):
            session.add_all(book_languages)

        book_shelves = [
            BookShelf(book=books["001"], shelf=shelves["Shelf A"]),
            BookShelf(book=books["001"], shelf=shelves["Shelf B"]),
            BookShelf(book=books["002"], shelf=shelves["Shelf A"]),
            BookShelf(book=books["002"], shelf=shelves["Shelf B"]),
            BookShelf(book=books["003"], shelf=shelves["Shelf A"]),
            BookShelf(book=books["004"], shelf=shelves["Shelf A"]),
            BookShelf(book=books["005"], shelf=shelves["Shelf A"]),
            BookShelf(book=books["006"], shelf=shelves["Shelf A"]),
            BookShelf(book=books["007"], shelf=shelves["Shelf A"]),
            BookShelf(book=books["008"], shelf=shelves["Shelf A"]),
        ]
        with subtransactions(session):
            session.add_all(book_shelves)

        # Dummy data
        if nsize > 1:
            nsamples = int(nsize)
            faker = Faker()
            print("Adding {} books".format(nsamples))

            for _ in range(nsamples):
                book = Book(
                    isbn=faker.isbn13(),
                    title=faker.sentence(),
                    description=faker.text(),
                    publisher=random.choice(list(publishers.values())),
                )
                session.add(book)
                author = Author(
                    name="{} .{} {}".format(
                        faker.first_name(),
                        faker.random_letter(),
                        faker.last_name(),
                    ),
                    date_of_birth=faker.date_object(),
                    city=random.choice(list(cities.values())),
                )
                session.add(author)
                book_author = BookAuthor(book=book, author=author)
                session.add(book_author)
                book_subject = BookSubject(
                    book=book,
                    subject=random.choice(list(subjects.values())),
                )
                session.add(book_subject)
                book_languge = BookLanguage(
                    book=book,
                    language=random.choice(list(languages.values())),
                )
                session.add(book_languge)
                book_shelf = BookShelf(
                    book=book,
                    shelf=random.choice(list(shelves.values())),
                )
                session.add(book_shelf)
            session.commit()
Beispiel #15
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    document = json.load(open(config))
    engine = pg_engine(
        database=document[0].get('database', document[0]['index']))
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    users = [
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
        Users(email='*****@*****.**'),
    ]

    hosts = [
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
        Hosts(email='*****@*****.**'),
    ]

    cities = [
        Cities(name='Manila',
               country=Countries(
                   name='Philippines',
                   country_code='PH',
               )),
        Cities(
            name='Lisbon',
            country=Countries(
                name='Portugal',
                country_code='PT',
            ),
        ),
        Cities(
            name='Havana',
            country=Countries(
                name='Cuba',
                country_code='CU',
            ),
        ),
        Cities(
            name='Copenhagen',
            country=Countries(
                name='Denmark',
                country_code='DK',
            ),
        ),
        Cities(
            name='London',
            country=Countries(
                name='United Kingdom',
                country_code='UK',
            ),
        ),
        Cities(
            name='Casablanca',
            country=Countries(
                name='Morocco',
                country_code='MA',
            ),
        ),
    ]

    places = [
        Places(
            host=hosts[0],
            city=cities[0],
            address='Quezon Boulevard',
        ),
        Places(
            host=hosts[1],
            city=cities[1],
            address='Castelo de São Jorge',
        ),
        Places(
            host=hosts[2],
            city=cities[2],
            address='Old Havana',
        ),
        Places(
            host=hosts[3],
            city=cities[3],
            address='Tivoli Gardens',
        ),
        Places(
            host=hosts[4],
            city=cities[4],
            address='Buckingham Palace',
        ),
        Places(
            host=hosts[5],
            city=cities[5],
            address='Medina',
        ),
    ]

    reviews = [
        Reviews(
            booking=Bookings(
                user=users[0],
                place=places[0],
                start_date=datetime.now() + timedelta(days=1),
                end_date=datetime.now() + timedelta(days=4),
                price_per_night=100,
                num_nights=4,
            ),
            rating=1,
            review_body='The rooms were left in a tolerable state',
        ),
        Reviews(
            booking=Bookings(
                user=users[1],
                place=places[1],
                start_date=datetime.now() + timedelta(days=2),
                end_date=datetime.now() + timedelta(days=4),
                price_per_night=150,
                num_nights=3,
            ),
            rating=2,
            review_body='I found my place wonderfully taken care of',
        ),
        Reviews(
            booking=Bookings(
                user=users[2],
                place=places[2],
                start_date=datetime.now() + timedelta(days=15),
                end_date=datetime.now() + timedelta(days=19),
                price_per_night=120,
                num_nights=4,
            ),
            rating=3,
            review_body='All of my house rules were respected',
        ),
        Reviews(
            booking=Bookings(
                user=users[3],
                place=places[3],
                start_date=datetime.now() + timedelta(days=2),
                end_date=datetime.now() + timedelta(days=7),
                price_per_night=300,
                num_nights=5,
            ),
            rating=4,
            review_body='Such a pleasure to host and welcome these guests',
        ),
        Reviews(
            booking=Bookings(
                user=users[4],
                place=places[4],
                start_date=datetime.now() + timedelta(days=1),
                end_date=datetime.now() + timedelta(days=10),
                price_per_night=800,
                num_nights=3,
            ),
            rating=5,
            review_body='We would be happy to host them again',
        ),
        Reviews(
            booking=Bookings(
                user=users[5],
                place=places[5],
                start_date=datetime.now() + timedelta(days=2),
                end_date=datetime.now() + timedelta(days=8),
                price_per_night=80,
                num_nights=10,
            ),
            rating=3,
            review_body='Please do not visit our town ever again!',
        ),
    ]

    with subtransactions(session):
        session.add_all(users)
        session.add_all(hosts)
        session.add_all(cities)
        session.add_all(places)
        session.add_all(reviews)
Beispiel #16
0
    def test_update_non_concurrent(self, data, book_cls):
        """Test sync update and then sync in non-concurrent mode."""
        document = {
            "index": "testdb",
            "nodes": {
                "table": "book",
                "columns": ["isbn", "title"]
            },
        }
        sync = Sync(document)
        sync.es.bulk(sync.index, sync.sync())
        sync.es.refresh("testdb")

        session = sync.session

        docs = search(sync.es, "testdb")

        assert docs == [
            {
                "_meta": {},
                "isbn": "abc",
                "title": "The Tiger Club"
            },
            {
                "_meta": {},
                "isbn": "def",
                "title": "The Lion Club"
            },
            {
                "_meta": {},
                "isbn": "ghi",
                "title": "The Rabbit Club"
            },
        ]

        with subtransactions(session):
            session.execute(book_cls.__table__.update().where(
                book_cls.__table__.c.isbn == "abc").values(title="Tiger Club"))

        sync.es.bulk(sync.index, sync.sync())
        sync.es.refresh("testdb")

        docs = search(sync.es, "testdb")

        assert docs == [
            {
                "_meta": {},
                "isbn": "abc",
                "title": "Tiger Club"
            },
            {
                "_meta": {},
                "isbn": "def",
                "title": "The Lion Club"
            },
            {
                "_meta": {},
                "isbn": "ghi",
                "title": "The Rabbit Club"
            },
        ]
        assert_resync_empty(sync, document.get("node", {}))
        sync.es.close()
Beispiel #17
0
 def poll_db():
     with subtransactions(session):
         session.execute(book_cls.__table__.update().where(
             book_cls.__table__.c.isbn == "abc").values(isbn="cba"))
         session.commit()
Beispiel #18
0
    def test_update_primary_key_non_concurrent(self, data, book_cls):
        """
        Test sync updates primary_key and then sync in non-concurrent mode.
        TODO: Note this test highlights a potential undesired bahaviour
        i.e we have a duplicate doc at a point in time.
        Note to self. I think this has been fixed. i.e we delete and then
        query ibsert
        """
        document = {
            "index": "testdb",
            "nodes": {
                "table": "book",
                "columns": ["isbn", "title"]
            },
        }
        sync = Sync(document)
        sync.es.bulk(sync.index, sync.sync())
        sync.es.refresh("testdb")

        docs = search(sync.es, "testdb")

        assert docs == [
            {
                "_meta": {},
                "isbn": "abc",
                "title": "The Tiger Club"
            },
            {
                "_meta": {},
                "isbn": "def",
                "title": "The Lion Club"
            },
            {
                "_meta": {},
                "isbn": "ghi",
                "title": "The Rabbit Club"
            },
        ]

        session = sync.session
        with subtransactions(session):
            session.execute(book_cls.__table__.update().where(
                book_cls.__table__.c.isbn == "abc").values(isbn="cba"))

        sync.es.bulk(sync.index, sync.sync())
        sync.es.refresh("testdb")

        docs = search(sync.es, "testdb")

        assert docs == [
            {
                "_meta": {},
                "isbn": "abc",
                "title": "The Tiger Club"
            },
            {
                "_meta": {},
                "isbn": "cba",
                "title": "The Tiger Club"
            },
            {
                "_meta": {},
                "isbn": "def",
                "title": "The Lion Club"
            },
            {
                "_meta": {},
                "isbn": "ghi",
                "title": "The Rabbit Club"
            },
        ]
        assert_resync_empty(sync, document.get("node", {}))
        sync.es.close()
Beispiel #19
0
    def data(self, sync, book_cls, publisher_cls):
        session = sync.session

        books = [
            book_cls(
                isbn="abc",
                title="The Tiger Club",
                description="Tigers are fierce creatures",
                publisher=publisher_cls(name="Tiger publishing"),
            ),
            book_cls(
                isbn="def",
                title="The Lion Club",
                description="Lion and the mouse",
                publisher=publisher_cls(name="Lion publishing"),
            ),
            book_cls(
                isbn="ghi",
                title="The Rabbit Club",
                description="Rabbits on the run",
                publisher=publisher_cls(name="Hop Bunny publishing"),
            ),
        ]

        with subtransactions(session):
            conn = session.connection().engine.connect().connection
            conn.set_isolation_level(
                psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
            cursor = conn.cursor()
            channel = sync.database
            cursor.execute(f"UNLISTEN {channel}")

        with subtransactions(session):
            session.add_all(books)

        sync.logical_slot_get_changes(
            f"{sync.database}_testdb",
            upto_nchanges=None,
        )

        yield books

        with subtransactions(session):
            conn = session.connection().engine.connect().connection
            conn.set_isolation_level(
                psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
            cursor = conn.cursor()
            channel = session.connection().engine.url.database
            cursor.execute(f"UNLISTEN {channel}")

        with subtransactions(session):
            sync.truncate_tables(
                [book_cls.__table__.name, publisher_cls.__table__.name])

        sync.logical_slot_get_changes(
            f"{sync.database}_testdb",
            upto_nchanges=None,
        )

        try:
            sync.es.teardown(index="testdb")
        except Exception:
            raise

        sync.redis.delete()
        session.connection().engine.connect().close()
        session.connection().engine.dispose()
        sync.es.close()
Beispiel #20
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    documents = json.load(open(config))
    engine = pg_engine(
        database=documents[0].get(
            'database',
            documents[0]['index'],
        )
    )
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    # Bootstrap
    categories = {
        'Category 1': Category(
            id=1,
            uid='c001',
            text='Colours',
        ),
        'Category 2': Category(
            id=2,
            uid='c002',
            text='Weather',
        ),
    }
    with subtransactions(session):
        session.add_all(categories.values())

    questions = {
        'Question 1': Question(
            id=1,
            uid='q001',
            category_id=1,
            category_uid='c001',
            text='What is your favorite color?',
        ),
        'Question 2': Question(
            id=2,
            uid='q002',
            category_id=2,
            category_uid='c002',
            text='Is it raining outside?',
        ),
    }
    with subtransactions(session):
        session.add_all(questions.values())

    answers = {
        'Answer 1': Answer(id=1, uid='a001', text='Red'),
        'Answer 2': Answer(id=2, uid='a002', text='Yes'),
        'Answer 3': Answer(id=3, uid='a003', text='Green'),
        'Answer 4': Answer(id=4, uid='a004', text='No'),
    }
    with subtransactions(session):
        session.add_all(answers.values())

    possible_answers = {
        'Possible Answer 1': PossibleAnswer(
            question_id=1,
            question_uid='q001',
            answer_id=1,
            answer_uid='a001',
        ),
        'Possible Answer 2': PossibleAnswer(
            question_id=1,
            question_uid='q001',
            answer_id=3,
            answer_uid='a003',
        ),
        'Possible Answer 3': PossibleAnswer(
            question_id=2,
            question_uid='q002',
            answer_id=2,
            answer_uid='a002',
        ),
        'Possible Answer 4': PossibleAnswer(
            question_id=2,
            question_uid='q002',
            answer_id=4,
            answer_uid='a004',
        ),
    }
    with subtransactions(session):
        session.add_all(possible_answers.values())

    real_answers = {
        'Real Answer 1': RealAnswer(
            question_id=1,
            question_uid='q001',
            answer_id=1,
            answer_uid='a001',
        ),
        'Real Answer 4': RealAnswer(
            question_id=2,
            question_uid='q002',
            answer_id=4,
            answer_uid='a004',
        ),
    }
    with subtransactions(session):
        session.add_all(real_answers.values())
Beispiel #21
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    documents = json.load(open(config))
    engine = pg_engine(
        database=documents[0].get('database', documents[0]['index'])
    )
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    product_categories = {
        'productCategory1': ProductCategory(id=1, name='productCategory1'),
        'productCategory2': ProductCategory(id=2, name='productCategory2'),
        'productCategory3': ProductCategory(id=3, name='productCategory3'),
        'productCategory4': ProductCategory(id=4, name='productCategory4')
    }
    with subtransactions(session):
        session.add_all(product_categories.values())

    products = [
        Product(id=1, name='product1', productCategory=product_categories['productCategory1']),
        Product(id=2, name='product2', productCategory=product_categories['productCategory1']),
        Product(id=3, name='product3', productCategory=product_categories['productCategory2']),
        Product(id=4, name='product4', productCategory=product_categories['productCategory2']),
        Product(id=5, name='product5', productCategory=product_categories['productCategory3']),
        Product(id=6, name='product6', productCategory=product_categories['productCategory3']),
        Product(id=7, name='product7', productCategory=product_categories['productCategory4']),
        Product(id=8, name='product8', productCategory=product_categories['productCategory4'])
    ]
    with subtransactions(session):
        session.add_all(products)

    services = {
        'saleService1': Service(id=1, name='saleService1'),
        'saleService2': Service(id=2, name='saleService2'),
        'rentService1': Service(id=3, name='rentService1'),
        'rentService2': Service(id=4, name='rentService2')
    }
    with subtransactions(session):
        session.add_all(services.values())

    service_items = [
        ServiceItem(id=1, name='saleService1 item', service=services['saleService1']),
        ServiceItem(id=2, name='saleService2 item', service=services['saleService2']),
        ServiceItem(id=3, name='rentService1 item', service=services['rentService1']),
        ServiceItem(id=4, name='rentService2 item', service=services['rentService2'])
    ]
    with subtransactions(session):
        session.add_all(service_items)

    sellers = {
        'seller1': Seller(id=1, name='seller1'),
        'seller2': Seller(id=2, name='seller2'),
        'seller3': Seller(id=3, name='seller3'),
        'seller4': Seller(id=4, name='seller4')
    }
    with subtransactions(session):
        session.add_all(sellers.values())

    renters = {
        'renter1': Renter(id=1, name='renter1'),
        'renter2': Renter(id=2, name='renter2'),
        'renter3': Renter(id=3, name='renter3'),
        'renter4': Renter(id=4, name='renter4')
    }
    with subtransactions(session):
        session.add_all(renters.values())

    service_product_categoryies = [
        ServiceProductCategory(
            productCategory=product_categories['productCategory1'],
            sellerService=services['saleService1'],
            seller=sellers['seller1'],
            renterService=services['rentService1'],
            renterOne=renters['renter1'],
            renterTwo=renters['renter2']
        ),
        ServiceProductCategory(
            productCategory=product_categories['productCategory1'],
            sellerService=services['saleService2'],
            seller=sellers['seller2'],
            renterService=services['rentService2'],
            renterOne=renters['renter3'],
            renterTwo=renters['renter4'],
        )
    ]
    with subtransactions(session):
        session.add_all(service_product_categoryies)
Beispiel #22
0
def main(config, nsize):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    document = json.load(open(config))
    engine = pg_engine(
        database=document[0].get('database', document[0]['index']))
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    # Bootstrap
    continents = {
        'Europe': Continent(name='Europe'),
        'North America': Continent(name='North America'),
    }
    with subtransactions(session):
        session.add_all(continents.values())

    countries = {
        'United Kingdom':
        Country(
            name='United Kingdom',
            continent=continents['Europe'],
        ),
        'France':
        Country(
            name='France',
            continent=continents['Europe'],
        ),
        'United States':
        Country(
            name='United States',
            continent=continents['North America'],
        ),
    }
    with subtransactions(session):
        session.add_all(countries.values())

    cities = {
        'London': City(name='London', country=countries['United Kingdom']),
        'Paris': City(name='Paris', country=countries['France']),
        'New York': City(name='New York', country=countries['United States']),
    }
    with subtransactions(session):
        session.add_all(cities.values())

    publishers = {
        'Oxford Press': Publisher(name='Oxford Press', is_active=True),
        'Penguin Books': Publisher(name='Penguin Books', is_active=False),
        'Pearson Press': Publisher(name='Pearson Press', is_active=True),
        'Reutgers Press': Publisher(name='Reutgers Press', is_active=False),
    }
    with subtransactions(session):
        session.add_all(publishers.values())

    authors = {
        'Stephen King':
        Author(
            name='Stephen King',
            date_of_birth=datetime.datetime(1947, 9, 21),
            city=cities['London'],
        ),
        'J. K. Rowling':
        Author(
            name='J. K. Rowling',
            date_of_birth=datetime.datetime(1965, 7, 31),
            city=cities['Paris'],
        ),
        'James Patterson':
        Author(
            name='James Patterson',
            date_of_birth=datetime.datetime(1947, 3, 22),
            city=cities['New York'],
        ),
        'Melinda Leigh':
        Author(
            name='Melinda Leigh',
            date_of_birth=datetime.datetime(1980, 1, 1),
            city=cities['Paris'],
        ),
        'Tolu Aina':
        Author(
            name='Tolu Aina',
            date_of_birth=datetime.datetime(1980, 5, 21),
            city=cities['London'],
        ),
    }
    with subtransactions(session):
        session.add_all(authors.values())

    subjects = {
        'Literature': Subject(name='Literature'),
        'Poetry': Subject(name='Poetry'),
        'Romance': Subject(name='Romance'),
        'Science Fiction & Fantasy': Subject(name='Science Fiction & Fantasy'),
        'Westerns': Subject(name='Westerns'),
    }
    with subtransactions(session):
        session.add_all(subjects.values())

    languages = {
        'en-GB': Language(code='en-GB'),
        'en-US': Language(code='en-US'),
        'de-DE': Language(code='de-DE'),
        'af-ZA': Language(code='af-ZA'),
        'es-ES': Language(code='es-ES'),
        'fr-FR': Language(code='fr-FR'),
        'it-IT': Language(code='it-IT'),
        'ja-JP': Language(code='ja-JP'),
    }
    with subtransactions(session):
        session.add_all(languages.values())

    shelves = {
        'Shelf A': Shelf(shelf='Shelf A'),
        'Shelf B': Shelf(shelf='Shelf B'),
    }
    with subtransactions(session):
        session.add_all(shelves.values())

    books = {
        '001':
        Book(
            isbn='001',
            title='It',
            description='Stephens Kings It',
            publisher=publishers['Oxford Press'],
            tags=['a', 'b', 'c'],
        ),
        '002':
        Book(
            isbn='002',
            title='The Body',
            description='Lodsdcsdrem ipsum dodscdslor sit amet',
            publisher=publishers['Oxford Press'],
            tags=['d', 'e', 'f'],
        ),
        '003':
        Book(
            isbn='003',
            title='Harry Potter and the Sorcerer\'s Stone',
            description='Harry Potter has never been',
            publisher=publishers['Penguin Books'],
            tags=['g', 'h', 'i'],
        ),
        '004':
        Book(
            isbn='004',
            title='Harry Potter and the Chamber of Secrets',
            description='The Dursleys were so mean and hideous that summer '
            'that all Harry Potter wanted was to get back to the '
            'Hogwarts School for Witchcraft and Wizardry',
            publisher=publishers['Penguin Books'],
            tags=['j', 'k', 'l'],
        ),
        '005':
        Book(
            isbn='005',
            title='The 17th Suspect',
            description='A series of shootings exposes San Francisco to a '
            'methodical yet unpredictable killer, and a reluctant '
            'woman decides to put her trust in Sergeant Lindsay '
            'Boxer',
            publisher=publishers['Pearson Press'],
            tags=['m', 'n', 'o'],
        ),
        '006':
        Book(
            isbn='006',
            title='The President Is Missing',
            description='The publishing event of 2018: Bill Clinton and James '
            'Patterson\'s The President Is Missing is a '
            'superlative thriller',
            publisher=publishers['Pearson Press'],
        ),
        '007':
        Book(
            isbn='007',
            title='Say You\'re Sorry',
            description='deserunt mollit anim id est laborum',
            publisher=publishers['Reutgers Press'],
        ),
        '008':
        Book(
            isbn='008',
            title='Bones Don\'t Lie',
            description='Lorem ipsum',
            publisher=publishers['Reutgers Press'],
        ),
    }
    with subtransactions(session):
        session.add_all(books.values())

    book_authors = [
        BookAuthor(book=books['001'], author=authors['Stephen King']),
        BookAuthor(book=books['002'], author=authors['Stephen King']),
        BookAuthor(book=books['003'], author=authors['J. K. Rowling']),
        BookAuthor(book=books['004'], author=authors['J. K. Rowling']),
        BookAuthor(book=books['005'], author=authors['James Patterson']),
        BookAuthor(book=books['006'], author=authors['James Patterson']),
        BookAuthor(book=books['007'], author=authors['Melinda Leigh']),
        BookAuthor(book=books['008'], author=authors['Melinda Leigh']),
        BookAuthor(book=books['007'], author=authors['Tolu Aina']),
        BookAuthor(book=books['008'], author=authors['Tolu Aina']),
    ]
    with subtransactions(session):
        session.add_all(book_authors)

    book_subjects = [
        BookSubject(book=books['001'], subject=subjects['Literature']),
        BookSubject(book=books['002'], subject=subjects['Literature']),
        BookSubject(book=books['003'], subject=subjects['Poetry']),
        BookSubject(book=books['004'], subject=subjects['Poetry']),
        BookSubject(book=books['005'], subject=subjects['Romance']),
        BookSubject(book=books['006'], subject=subjects['Romance']),
        BookSubject(
            book=books['007'],
            subject=subjects['Science Fiction & Fantasy'],
        ),
        BookSubject(
            book=books['008'],
            subject=subjects['Science Fiction & Fantasy'],
        ),
    ]
    with subtransactions(session):
        session.add_all(book_subjects)

    book_languages = [
        BookLanguage(book=books['001'], language=languages['en-GB']),
        BookLanguage(book=books['002'], language=languages['en-GB']),
        BookLanguage(book=books['003'], language=languages['en-GB']),
        BookLanguage(book=books['004'], language=languages['en-GB']),
        BookLanguage(book=books['005'], language=languages['en-GB']),
        BookLanguage(book=books['006'], language=languages['en-GB']),
        BookLanguage(book=books['007'], language=languages['en-GB']),
        BookLanguage(book=books['008'], language=languages['en-GB']),
        BookLanguage(book=books['001'], language=languages['fr-FR']),
        BookLanguage(book=books['002'], language=languages['fr-FR']),
        BookLanguage(book=books['003'], language=languages['fr-FR']),
        BookLanguage(book=books['004'], language=languages['fr-FR']),
        BookLanguage(book=books['005'], language=languages['fr-FR']),
        BookLanguage(book=books['006'], language=languages['fr-FR']),
        BookLanguage(book=books['007'], language=languages['fr-FR']),
        BookLanguage(book=books['008'], language=languages['fr-FR']),
    ]
    with subtransactions(session):
        session.add_all(book_languages)

    book_shelves = [
        BookShelf(book=books['001'], shelf=shelves['Shelf A']),
        BookShelf(book=books['001'], shelf=shelves['Shelf B']),
        BookShelf(book=books['002'], shelf=shelves['Shelf A']),
        BookShelf(book=books['002'], shelf=shelves['Shelf B']),
        BookShelf(book=books['003'], shelf=shelves['Shelf A']),
        BookShelf(book=books['004'], shelf=shelves['Shelf A']),
        BookShelf(book=books['005'], shelf=shelves['Shelf A']),
        BookShelf(book=books['006'], shelf=shelves['Shelf A']),
        BookShelf(book=books['007'], shelf=shelves['Shelf A']),
        BookShelf(book=books['008'], shelf=shelves['Shelf A']),
    ]
    with subtransactions(session):
        session.add_all(book_shelves)

    # Dummy data
    if nsize > 1:
        nsamples = int(nsize)
        faker = Faker()
        print('Adding {} books'.format(nsamples))

        for _ in range(nsamples):
            book = Book(
                isbn=faker.isbn13(),
                title=faker.sentence(),
                description=faker.text(),
                publisher=random.choice(list(publishers.values())),
            )
            session.add(book)
            author = Author(
                name='{} .{} {}'.format(
                    faker.first_name(),
                    faker.random_letter(),
                    faker.last_name(),
                ),
                date_of_birth=faker.date_object(),
                city=random.choice(list(cities.values())),
            )
            session.add(author)
            book_author = BookAuthor(book=book, author=author)
            session.add(book_author)
            book_subject = BookSubject(
                book=book,
                subject=random.choice(list(subjects.values())),
            )
            session.add(book_subject)
            book_languge = BookLanguage(
                book=book,
                language=random.choice(list(languages.values())),
            )
            session.add(book_languge)
            book_shelf = BookShelf(
                book=book,
                shelf=random.choice(list(shelves.values())),
            )
            session.add(book_shelf)
        session.commit()
Beispiel #23
0
    def test_insert_non_concurrent(self, data, book_cls):
        """Test sync insert and then sync in non-concurrent mode."""
        document = {
            'index': 'testdb',
            'nodes': {
                'table': 'book',
                'columns': ['isbn', 'title']
            }
        }
        sync = Sync(document)
        sync.sync()
        sync.es.refresh('testdb')

        session = sync.session

        docs = search(sync.es, 'testdb')

        assert docs == [
            {
                u'_meta': {},
                u'isbn': u'abc',
                u'title': u'The Tiger Club'
            },
            {
                u'_meta': {},
                u'isbn': u'def',
                u'title': u'The Lion Club'
            },
            {
                u'_meta': {},
                u'isbn': u'ghi',
                u'title': u'The Rabbit Club'
            }
        ]
        with subtransactions(session):
            session.execute(
                book_cls.__table__.insert().values(
                    isbn='xyz',
                    title='Encyclopedia'
                )
            )

        sync.sync()
        sync.es.refresh('testdb')

        docs = search(sync.es, 'testdb')

        assert docs == [
            {
                u'_meta': {},
                u'isbn': u'abc',
                u'title': u'The Tiger Club'
            },
            {
                u'_meta': {},
                u'isbn': u'def',
                u'title': u'The Lion Club'
            },
            {
                u'_meta': {},
                u'isbn': u'ghi',
                u'title': u'The Rabbit Club'
            },
            {
                u'_meta': {},
                u'isbn': u'xyz',
                u'title': u'Encyclopedia'
            }
        ]
        assert_resync_empty(sync, document.get('node', {}))
Beispiel #24
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    documents = json.load(open(config))
    engine = pg_engine(
        database=documents[0].get('database', documents[0]['index']))
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    parents = [
        Parent(id=1, name="Parent A"),
        Parent(id=2, name="Parent B"),
        Parent(id=3, name="Parent C"),
    ]
    with subtransactions(session):
        session.add_all(parents)

    surrogates = [
        Surrogate(id=1, name="Surrogate A Parent A", parent_id=1),
        Surrogate(id=2, name="Surrogate B Parent A", parent_id=1),
        Surrogate(id=3, name="Surrogate A Parent B", parent_id=2),
        Surrogate(id=4, name="Surrogate B Parent B", parent_id=2),
        Surrogate(id=5, name="Surrogate A Parent C", parent_id=3),
        Surrogate(id=6, name="Surrogate B Parent C", parent_id=3),
    ]
    with subtransactions(session):
        session.add_all(surrogates)

    children = [
        Child(id=1, name="Child A Surrogate A Parent A", surrogate_id=1),
        Child(id=2, name="Child B Surrogate B Parent A", surrogate_id=2),
        Child(id=3, name="Child C Surrogate A Parent B", surrogate_id=3),
        Child(id=4, name="Child D Surrogate B Parent B", surrogate_id=4),
        Child(id=5, name="Child E Surrogate A Parent C", surrogate_id=5),
        Child(id=6, name="Child F Surrogate B Parent C", surrogate_id=6),
    ]
    with subtransactions(session):
        session.add_all(children)

    grand_children = [
        GrandChild(id=1, name="Grand Child A Child A", child_id=1),
        GrandChild(id=2, name="Grand Child B Child A", child_id=1),
        GrandChild(id=3, name="Grand Child C Child B", child_id=2),
        GrandChild(id=4, name="Grand Child D Child B", child_id=2),
        GrandChild(id=5, name="Grand Child E Child C", child_id=3),
        GrandChild(id=6, name="Grand Child F Child C", child_id=3),
        GrandChild(id=7, name="Grand Child G Child D", child_id=4),
        GrandChild(id=8, name="Grand Child H Child D", child_id=4),
        GrandChild(id=9, name="Grand Child I Child E", child_id=5),
        GrandChild(id=10, name="Grand Child J Child E", child_id=5),
        GrandChild(id=11, name="Grand Child K Child F", child_id=6),
        GrandChild(id=12, name="Grand Child L Child F", child_id=6),
    ]
    with subtransactions(session):
        session.add_all(grand_children)

    great_grand_children = [
        GreatGrandChild(
            id=1,
            name="Great Grand Child A Child A",
            grand_child_id=1,
        ),
        GreatGrandChild(
            id=2,
            name="Great Grand Child B Child B",
            grand_child_id=2,
        ),
        GreatGrandChild(
            id=3,
            name="Great Grand Child C Child C",
            grand_child_id=3,
        ),
        GreatGrandChild(
            id=4,
            name="Great Grand Child D Child D",
            grand_child_id=4,
        ),
        GreatGrandChild(
            id=5,
            name="Great Grand Child E Child E",
            grand_child_id=5,
        ),
        GreatGrandChild(
            id=6,
            name="Great Grand Child F Child F",
            grand_child_id=6,
        ),
        GreatGrandChild(
            id=7,
            name="Great Grand Child G Child G",
            grand_child_id=7,
        ),
        GreatGrandChild(
            id=8,
            name="Great Grand Child H Child H",
            grand_child_id=8,
        ),
        GreatGrandChild(
            id=9,
            name="Great Grand Child I Child I",
            grand_child_id=9,
        ),
        GreatGrandChild(
            id=10,
            name="Great Grand Child J Child J",
            grand_child_id=10,
        ),
        GreatGrandChild(
            id=11,
            name="Great Grand Child K Child K",
            grand_child_id=11,
        ),
        GreatGrandChild(
            id=12,
            name="Great Grand Child L Child L",
            grand_child_id=12,
        ),
    ]
    with subtransactions(session):
        session.add_all(great_grand_children)
Beispiel #25
0
def main(config):

    config = get_config(config)
    teardown(drop_db=False, config=config)
    documents = json.load(open(config))
    engine = pg_engine(
        database=documents[0].get('database', documents[0]['index'])
    )
    Session = sessionmaker(bind=engine, autoflush=True)
    session = Session()

    species = [
        Specie(id=1, name='Protos'),
        Specie(id=2, name='Zerg'),
        Specie(id=3, name='Terran'),
    ]
    with subtransactions(session):
        session.add_all(species)

    units = [
        Unit(
            name='Archon',
            details='Created by merging two templar units, the archon is a powerful melee unit with a very durable force shield and a strong energy-based attack.',
            specie_id=1,
        ),
        Unit(
            name='Carrier',
            details='A powerful air unit. Carriers do not have their own attacks but create interceptors to fight for them.',
            specie_id=1,
        ),
        Unit(
            name='Colossus',
            details='The large quad-legged vehicle fires lasers in a splash pattern well-suited to destroying swarms of weaker units. This unit can also traverse differences in terrain height due to its long legs, and will appear to step over ledges and other obstacles due to the inverse kinematics system.',
            specie_id=1,
        ),
        Unit(
            name='Dark Templar',
            details='A permanently cloaked stealth warrior.',
            specie_id=1,
        ),
        Unit(
            name='High Templar',
            details='A physically fragile unit with strong psychic abilities.',
            specie_id=1,
        ),
        Unit(
            name='Zergling',
            details='Fast but weak melee attacker ideal for swarming attacks in large numbers.',
            specie_id=2,
        ),
        Unit(
            name='Larva',
            details='The core genus of the zerg, larvae can mutate into other zerg breeds.',
            specie_id=2,
        ),
        Unit(
            name='SCV',
            details='The builder and resource gatherer of the terran race. Its Repair ability can be set to "autocast".',
            specie_id=3,
        ),
        Unit(
            name='Marine',
            details='The basic terran infantry, able to upgrade hit points with a shield.',
            specie_id=3,
        ),
    ]
    with subtransactions(session):
        session.add_all(units)

    structures = [
        Structure(
            name='Assimilator',
            details='Allows probes to harvest vespene gas from geysers.',
            specie_id=1,
        ),
        Structure(
            name='Baneling Nest',
            details='Required for baneling production and researches baneling upgrades.',
            specie_id=2,
        ),
        Structure(
            name='Barracks',
            details=' Produces terran infantry units.',
            specie_id=3,
        ),
    ]
    with subtransactions(session):
        session.add_all(structures)