Skip to content

Commit

Permalink
first steps towards tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlue committed Feb 23, 2024
1 parent 5f156fd commit 220d76e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 35 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ import biobricks as bb
bb.assets('tox21') # get the paths for the 'tox21' brick
```

To list the bricks currently available visit [github.com/biobricks-ai](https://github.com/biobricks-ai)
To list the bricks currently available visit [github.com/biobricks-ai](https://github.com/biobricks-ai)

# TODO
1. eventually need to special case (or remove) the testing biobricks token
5 changes: 3 additions & 2 deletions biobricks/brick.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import subprocess, functools, os, re, datetime, json
from subprocess import run, DEVNULL
import types, pyarrow.parquet as pq, re
import types
from pathlib import Path
from .config import bblib, token
from .logger import logger
import os, urllib.request as request, functools, dvc.api, shutil
import os, urllib.request as request, functools, shutil
# import dvc.api
from urllib.parse import urlparse
import sys
from .checks import check_url_available, check_token, check_symlink_permission, check_safe_git_repo
Expand Down
20 changes: 11 additions & 9 deletions biobricks/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from importlib import metadata

from .logger import logger
from .config import read_config, write_config, init_bblib
from .config import biobricks_config_path, read_config, write_config, init_bblib
from .checks import check_token
from .brick import Brick
from .local_bb import LocalBB
Expand All @@ -22,20 +22,22 @@ class Sect:
@click.option("--bblib", default=None, type=click.Path(), help="path to store bricks")
@click.option("--token", default=None, help="biobricks.ai/token auth token")
@click.option("--overwrite", default=False, help="overwrite existing config?")
def configure(bblib, token, overwrite):
@click.option("--interactive", default=True, help="run configure interactively?")
def configure(bblib, token, overwrite, interactive):

if not interactive:
config = { "BBLIB": f"{bblib}", "TOKEN": token }
write_config(config)
init_bblib()
return

path = Path.home().joinpath(".biobricks")
path = biobricks_config_path()
config = read_config()

# CHECK IF CONFIG WILL OVERWRITE EXISTING
msg = click.style("WARNING: overwrite existing config?", fg="red")
if path.exists() and not overwrite and not click.confirm(msg):
sys.exit(0)

# EMAIL PROMPT
email = click.prompt("Choose Email", type=click.Path())
click.echo("\nRegister and log in to: https://biobricks.ai\nThen copy your token from https://biobricks.ai/token\n")
token = click.prompt("Copy token from https://biobricks.ai/token here")

# VALIDATE TOKEN
while not check_token(token, silent=True):
Expand All @@ -50,7 +52,7 @@ def configure(bblib, token, overwrite):
bblib = click.prompt("Choose path to store bricks", type=click.Path())

# write configuration
config = { "BBLIB": bblib, "TOKEN": token, "EMAIL": email }
config = { "BBLIB": bblib, "TOKEN": token }
write_config(config)

# initialize bblib
Expand Down
11 changes: 9 additions & 2 deletions biobricks/config.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import os, warnings, json, urllib
from subprocess import run, DEVNULL, STDOUT
from pathlib import Path
import shutil

def biobricks_config_path():
return Path.home().joinpath(".biobricks")

def read_config():
path = Path.home().joinpath(".biobricks")
path = biobricks_config_path()
if not path.exists(): return {}
return json.loads(path.read_text())

def write_config(config):
path = Path.home().joinpath(".biobricks")
path = biobricks_config_path()
path.write_text(json.dumps(config))

def init_bblib() -> None:
breakpoint()
bbpath = Path(read_config()['BBLIB'])
os.makedirs(bbpath, exist_ok=True)
os.makedirs(bbpath / "cache", exist_ok=True)
if not shutil.which('git'):
raise Exception('Program "git" is not installed. Please install "git"')
run("git init", cwd=bbpath, stdout=DEVNULL, stderr=STDOUT, shell=True)
return bblib()

Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- remove pyarrow dependency
- remove git from local_bb
8 changes: 7 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Create an ubuntu docker image with pip, python, and git
# build with `docker build -t ubuntu-pip-python-git .`
# create a container from the image that gets destroyed after use
# `docker run --rm -it -v ./:/biobricks ubuntu-pip-python-git /bin/bash`
# `docker run --rm -it -v ./:/home/appuser/biobricks ubuntu-pip-python-git /bin/bash`

FROM python:3.9-slim

# Install system dependencies including Git
RUN apt-get update && \
apt-get install -y --no-install-recommends git && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Install pipx
RUN python -m pip install --upgrade pip && \
pip install pipx && \
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dvc==2.45.1
pytest-cov==4.0.0
click>=8.1.3
GitPython==3.1.37
requests>=2.0.0,<3.0.0
cloup>=3.0.0,<4.0.0
54 changes: 36 additions & 18 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
# test_cli.py
import pytest
from biobricks.cli import register_on_biobricks
import unittest
from unittest.mock import patch, MagicMock
from pathlib import Path
from biobricks import Brick, cli
from biobricks.config import write_config, init_bblib
import tempfile

# Using requests_mock to mock API responses
@pytest.fixture
def mock_request(requests_mock):
mock_url = 'https://biobricks.ai/api/register'
mock_response = {
'auth_numbers': '12345',
'auth_url': 'https://biobricks.ai/auth'
}
requests_mock.post(mock_url, json=mock_response, status_code=200)
return requests_mock # This return isn't necessary but can be useful if you want to add more specifics in the fixture.
def configure(bblib, token, overwrite, interactive):

if not interactive:
config = { "BBLIB": f"{bblib}", "TOKEN": token }
write_config(config)
init_bblib()
return

def test_register_on_biobricks(mock_request): # This should be the fixture name.
result = register_on_biobricks('[email protected]')
class TestBrickResolve(unittest.TestCase):

tempdir = tempfile.TemporaryDirectory()

assert result['message'] == "Authentication needed."
assert result['auth_numbers'] == '12345'
assert result['auth_url'] == 'https://biobricks.ai/auth'
def setUp(self):
bblib = Path(f"{TestBrickResolve.tempdir.name}/biobricks")
bblib.mkdir(exist_ok=True,parents=True)
configure(f"{bblib}", "VQF6Q2U-NKktZ31ioVYa9w", None, None)

def tearDown(self) -> None:
return super().tearDown()

# test basic install of the hello-brick
@patch('biobricks.bblib')
def test_install_hello_brick():
from biobricks.brick import Brick
brick = Brick.Resolve("hello-brick")
brick.install()
assert brick.path().exists()
assert (brick.path() / "hello.txt").exists()
assert (brick.path() / "hello.txt").read_text() == "hello world\n"

if __name__ == '__main__':
unittest.main()

0 comments on commit 220d76e

Please sign in to comment.