Example #1
0
    def test_get(db_rc: RestClient) -> None:
        """Run some test queries."""
        databases = db_rc.request_seq("GET", "/databases/names")
        print(databases)

        for db in databases["databases"]:
            db_request_body = {"database": db}
            collections = db_rc.request_seq("GET", "/collections/names",
                                            db_request_body)
            print(collections)
            for coll in collections["collections"]:
                coll_request_body = {"database": db, "collection": coll}
                histograms = db_rc.request_seq(
                    "GET", "/collections/histograms/names", coll_request_body)
                print(histograms)
                for histo_name in histograms["histograms"]:
                    histo_request_body = {
                        "database": db,
                        "collection": coll,
                        "name": histo_name,
                    }
                    histo = db_rc.request_seq("GET", "/histogram",
                                              histo_request_body)
                    print(histo)
                filelist = db_rc.request_seq("GET", "/files/names",
                                             coll_request_body)
                print(filelist)

        db_rc.close()
Example #2
0
    def test_post_files(db_rc: RestClient) -> None:
        """Failure-test role authorization."""
        post_body = {
            "database": "test_histograms",
            "collection": "collection_name",
            "files": ["test.txt"],
        }
        with pytest.raises(requests.exceptions.HTTPError) as e:
            db_rc.request_seq("POST", "/files/names", post_body)
            assert e.response.status_code == 403  # Forbidden Error

        db_rc.close()
Example #3
0
    def test_post_histo(db_rc: RestClient) -> None:
        """Failure-test role authorization."""
        post_body = {
            "database": "test_histograms",
            "collection": "TEST",
            "histogram": {
                "Anything": True
            },
        }
        with pytest.raises(requests.exceptions.HTTPError) as e:
            db_rc.request_seq("POST", "/histogram", post_body)
            assert e.response.status_code == 403  # Forbidden Error

        db_rc.close()
Example #4
0
def index(
    paths: List[str],
    blacklist: List[str],
    rest_client_args: RestClientArgs,
    site: str,
    indexer_flags: IndexerFlags,
) -> List[str]:
    """Index paths, excluding any matching the blacklist.

    Return all child paths nested under any directories.
    """
    if not isinstance(paths, list):
        raise TypeError(f"`paths` object is not list {paths}")
    if not paths:
        return []

    # Filter
    paths = file_utils.sorted_unique_filepaths(list_of_filepaths=paths)
    paths = [p for p in paths if not path_in_blacklist(p, blacklist)]

    # Prep
    fc_rc = RestClient(
        rest_client_args["url"],
        token=rest_client_args["token"],
        timeout=rest_client_args["timeout"],
        retries=rest_client_args["retries"],
    )
    manager = MetadataManager(
        site,
        basic_only=indexer_flags["basic_only"],
        iceprodv2_rc_token=indexer_flags["iceprodv2_rc_token"],
        iceprodv1_db_pass=indexer_flags["iceprodv1_db_pass"],
    )

    # Index
    child_paths = asyncio.get_event_loop().run_until_complete(
        index_paths(paths, manager, fc_rc, indexer_flags["patch"],
                    indexer_flags["dryrun"]))

    fc_rc.close()
    return child_paths
Example #5
0
    def test_histo(db_rc: RestClient) -> None:  # pylint: disable=R0914
        """Run posts with updating."""

        def assert_get(histo: Histogram) -> None:
            get_body = {
                "database": "test_histograms",
                "collection": "TEST",
                "name": histo["name"],
            }
            get_resp = db_rc.request_seq("GET", "/histogram", get_body)
            assert get_resp["histogram"] == histo
            assert get_resp["history"]

        histograms = TestDBServerProdRole._create_new_histograms()
        # use first histogram for updating values in all histograms
        new_bin_values = histograms[0]["bin_values"]  # value will be incremented
        new_overflow = histograms[0]["overflow"]  # value will be incremented
        new_underflow = histograms[0]["underflow"]  # value will be incremented
        new_nan_count = histograms[0]["nan_count"]  # value will be incremented

        # Test!
        for orignial_histo in histograms:
            # 1. POST with no update flag
            post_body_1 = {
                "database": "test_histograms",
                "collection": "TEST",
                "histogram": orignial_histo,
            }
            post_resp_1 = db_rc.request_seq("POST", "/histogram", post_body_1)
            assert post_resp_1["history"]
            assert post_resp_1["histogram"] == orignial_histo
            assert not post_resp_1["updated"]

            # GET
            assert_get(orignial_histo)

            # 2. POST again with no update flag
            post_body_2 = {
                "database": "test_histograms",
                "collection": "TEST",
                "histogram": orignial_histo,
            }
            with pytest.raises(requests.exceptions.HTTPError) as e:
                _ = db_rc.request_seq("POST", "/histogram", post_body_2)
                assert e.response.status_code == 409  # Conflict Error

            # GET
            assert_get(orignial_histo)

            # 3. POST with update
            newer_histo = copy.deepcopy(orignial_histo)
            newer_histo["bin_values"] = new_bin_values
            newer_histo["overflow"] = new_overflow
            newer_histo["underflow"] = new_underflow
            newer_histo["nan_count"] = new_nan_count
            post_body_3 = {
                "database": "test_histograms",
                "collection": "TEST",
                "histogram": newer_histo,
                "update": True,
            }
            post_resp_3 = db_rc.request_seq("POST", "/histogram", post_body_3)
            assert post_resp_3["histogram"] == TestDBServerProdRole._get_updated_histo(
                orignial_histo, newer_histo
            )
            assert post_resp_3["updated"]
            assert len(post_resp_3["history"]) == 2

            # GET
            assert_get(
                TestDBServerProdRole._get_updated_histo(orignial_histo, newer_histo)
            )

        db_rc.close()
Example #6
0
    def test_file(db_rc: RestClient) -> None:
        """Run some test posts."""
        collection_name = f"TEST-{uuid.uuid4().hex}"

        def assert_get(_files: List[str]) -> None:
            get_body = {"database": "test_histograms", "collection": collection_name}
            get_resp = db_rc.request_seq("GET", "/files/names", get_body)
            assert get_resp["files"] == _files
            assert get_resp["history"]

        # 1. POST with no update flag
        files = TestDBServerProdRole._create_new_files()
        post_body_1 = {
            "database": "test_histograms",
            "collection": collection_name,
            "files": files,
        }
        post_resp_1 = db_rc.request_seq("POST", "/files/names", post_body_1)
        assert post_resp_1["files"] == files
        assert post_resp_1["history"]

        # GET
        assert_get(files)

        # 2. POST again with no update flag
        post_body_2 = {
            "database": "test_histograms",
            "collection": collection_name,
            "files": files,
        }
        with pytest.raises(requests.exceptions.HTTPError) as e:
            _ = db_rc.request_seq("POST", "/files/names", post_body_2)
            assert e.response.status_code == 409  # Conflict Error

        # GET
        assert_get(files)

        # 3. POST with update but no new files
        post_body_3 = {
            "database": "test_histograms",
            "collection": collection_name,
            "files": files,
            "update": True,
        }
        post_resp_3 = db_rc.request_seq("POST", "/files/names", post_body_3)
        assert post_resp_3["files"] == files
        assert len(post_resp_3["history"]) == 2

        # GET
        assert_get(files)

        # 4. POST with update flag and new files
        new_files = TestDBServerProdRole._create_new_files()
        post_body_4 = {
            "database": "test_histograms",
            "collection": collection_name,
            "files": new_files,
            "update": True,
        }
        post_resp_4 = db_rc.request_seq("POST", "/files/names", post_body_4)
        assert post_resp_4["files"] == sorted(set(files) | set(new_files))
        assert len(post_resp_4["history"]) == 3

        # GET
        assert_get(sorted(set(files) | set(new_files)))  # set-add files

        db_rc.close()
Example #7
0
def test_01_init() -> None:
    """Test `__init__()` & `close()`."""
    rpc = RestClient("http://test", "passkey")
    rpc.close()