Esempio n. 1
0
def test_derex_mongodb(start_mongodb):
    from derex.runner.mongodb import list_databases
    from derex.runner.cli.mongodb import copy_mongodb
    from derex.runner.cli.mongodb import drop_mongodb
    import derex.runner.mongodb

    reload(derex.runner.mongodb)
    MONGODB_CLIENT = derex.runner.mongodb.MONGODB_CLIENT

    test_db_name = f"derex_test_db_{uuid.uuid4().hex[:20]}"
    test_db_copy_name = f"derex_test_db_copy_{uuid.uuid4().hex[:20]}"
    random_value = uuid.uuid4().hex[:20]
    test_data = {"data": random_value}

    MONGODB_CLIENT[test_db_name]["test_collection"].insert_one(test_data)
    assert test_db_name in [database["name"] for database in list_databases()]

    runner.invoke(copy_mongodb,
                  f"{test_db_name} {test_db_copy_name}",
                  input="y")
    assert test_db_copy_name in [
        database["name"] for database in list_databases()
    ]
    assert MONGODB_CLIENT[test_db_copy_name]["test_collection"].find_one(
        test_data)

    runner.invoke(drop_mongodb, test_db_name, input="y")
    runner.invoke(drop_mongodb, test_db_copy_name, input="y")
    assert test_db_name not in [
        database["name"] for database in list_databases()
    ]
    assert test_db_copy_name not in [
        database["name"] for database in list_databases()
    ]
Esempio n. 2
0
def mongodb(context: click.core.Context):
    """Commands to operate on the mongodb database"""
    if context.invoked_subcommand is None:
        click.echo(mongodb.get_help(context))
        if isinstance(context.obj, Project):
            from derex.runner.mongodb import list_databases

            click.echo()
            project = context.obj
            for db in list_databases():
                if db["name"] == project.mongodb_db_name:
                    click.echo(
                        f'Current MongoDB databases for project "{project.name}"'
                    )
                    console = get_rich_console()
                    table = get_rich_table("Database",
                                           "Tables",
                                           "Django users",
                                           show_lines=True)
                    table.add_row(
                        db["name"],
                        str(db["sizeOnDisk"]),
                        str(db["empty"]),
                    )
                    console.print(table)
                    break
            else:
                click.echo(
                    f'No MongoDB database "{project.mongodb_db_name}" found for project "{project.name}"'
                )
Esempio n. 3
0
def mongodb(context: click.core.Context):
    """Commands to operate on the mongodb database"""
    if context.invoked_subcommand is None:
        from derex.runner.mongodb import list_databases

        click.echo(mongodb.get_help(context))
        databases = [(database["name"], database["sizeOnDisk"],
                      database["empty"]) for database in list_databases()]
        if isinstance(context.obj, Project):
            project = context.obj
            database = [
                database for database in databases
                if database[0] == project.mongodb_db_name
            ]
            click.echo()
            if database:
                databases = database
                click.echo(
                    f'Current MongoDB databases for project "{project.name}"')
            else:
                click.echo(
                    f'No MongoDB database "{project.mongodb_db_name}" found for project "{project.name}"'
                )
        click.echo()
        click.echo(tabulate(databases, headers=["Database", "Size", "Empty"]))
Esempio n. 4
0
def list_mongodb(project: Optional[Project], db_name: str):
    """List all mongodb databases"""
    from derex.runner.mongodb import list_databases

    databases = [(database["name"], database["sizeOnDisk"], database["empty"])
                 for database in list_databases()]
    click.echo(tabulate(databases, headers=["Database", "Size", "Empty"]))
    return 0
Esempio n. 5
0
def list_databases_cmd(project: Optional[Project], db_name: str):
    """List all MongoDB databases"""
    from derex.runner.mongodb import list_databases

    console = get_rich_console()
    table = get_rich_table("Database",
                           "Size (bytes)",
                           "Empty",
                           show_lines=True)
    for database in list_databases():
        table.add_row(database["name"], str(database["sizeOnDisk"]),
                      str(database["empty"]))
    console.print(table)
    return 0