Skip to content

Commit

Permalink
Merge pull request #14 from cehbrecht/work-on-tests
Browse files Browse the repository at this point in the history
Work on tests
  • Loading branch information
cehbrecht authored Apr 18, 2024
2 parents 439951b + c069386 commit da47b3d
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 68 deletions.
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ dependencies:
- jsonschema-with-format
- dataclasses-json
# tests
- pytest
- pytest
- sqlite
48 changes: 0 additions & 48 deletions piddiplatsch/pidmaker.py

This file was deleted.

10 changes: 10 additions & 0 deletions piddiplatsch/pidmaker/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .rest import RESTClient
from .dummy import DummyClient


def PidMaker(dry_run=True):
if dry_run:
client = DummyClient()
else:
client = RESTClient()
return client
6 changes: 6 additions & 0 deletions piddiplatsch/pidmaker/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class BaseClient:
def register_handle(self, handle, record):
raise NotImplementedError

def check_if_handle_exists(self, handle):
raise NotImplementedError
49 changes: 49 additions & 0 deletions piddiplatsch/pidmaker/dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from .base import BaseClient

import sqlite3
import json

import logging

LOGGER = logging.getLogger("piddiplatsch")

conn = sqlite3.connect(":memory:")
cursor = conn.cursor()


def table_exists(table_name):
cursor.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table_name,)
)
return cursor.fetchone() is not None


if not table_exists("data"):
cursor.execute(
"""CREATE TABLE data (
id TEXT PRIMARY KEY,
json_string TEXT
)"""
)


class DummyClient(BaseClient):

def register_handle(self, handle, record):
LOGGER.info(f"handle: {handle}")
LOGGER.info(f"record: {record}")

data = json.dumps(record)

cursor.execute(
"""INSERT INTO data (id, json_string) VALUES (?, ?)""",
(handle, data),
)
conn.commit()

def check_if_handle_exists(self, handle):
if not handle:
return False
elif "invalid" in handle:
return False
return True
14 changes: 14 additions & 0 deletions piddiplatsch/pidmaker/rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .base import BaseClient

import pyhandle


class RESTClient(BaseClient):
def __init__(self):
self.client = pyhandle.handleclient.PyHandleClient("rest")

def register_handle(self, handle, record):
self.client.register_handle(handle, record)

def check_if_handle_exists(self, handle):
return self.client.check_if_handle_exists(handle)
62 changes: 43 additions & 19 deletions tests/storm/locustfile.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
from locust import FastHttpUser, between, task
from locust import FastHttpUser, between, task, tag

import json

TOPIC = "pids"
RABBITMQ_URL = "http://localhost:15672"


# API endpoint for publishing messages to a queue
# https://www.freekb.net/Article?id=3108
PUBLISH_URL = "/api/exchanges/%2f/pids/publish"
PUBLISH_URL = f"/api/exchanges/%2f/{TOPIC}/publish"


class RabbitMQUser(FastHttpUser):
host = "http://localhost:15672"
host = RABBITMQ_URL
wait_time = between(1, 5)

@task
def send_cmip6_message(self):
# Define JSON message to be sent
message = {
"aggregation_level": "FILE",
"file_name": "temp.nc",
"file_size": 18028171468,
"file_version": 1,
"checksum": "312cb59854334b340d4d046310d028d08e0e71cfaa31ac031b383e1008594d42",
"checksum_method": "SHA256",
"is_part_of": "hdl:21.14100/37528ff1-2653-3c50-b670-7c00ba96fd6e",
"url_original_data": "http://vesg.ipsl.upmc.fr/thredds/fileServer/cmip6/temp.nc",
"url_replica": "http://esgf3.dkrz.de/thredds/fileServer/cmip6/temp.nc",
}

def send_message(self, routing_key, message):
data = json.dumps(message)

# Message payload
payload = {
"properties": {},
"routing_key": "cmip6.test",
"routing_key": routing_key,
"payload": data,
"payload_encoding": "string",
}
Expand All @@ -49,3 +38,38 @@ def send_cmip6_message(self):
response.failure(
f"'routed' had an unexpected value: {response.js['routed']}"
)

@tag("cmip6")
@task(10)
def send_cmip6_message(self):
# Define JSON message to be sent
message = {
"aggregation_level": "FILE",
"file_name": "temp.nc",
"file_size": 18028171468,
"file_version": 1,
"checksum": "312cb59854334b340d4d046310d028d08e0e71cfaa31ac031b383e1008594d42",
"checksum_method": "SHA256",
"is_part_of": "hdl:21.14100/37528ff1-2653-3c50-b670-7c00ba96fd6e",
"url_original_data": "http://vesg.ipsl.upmc.fr/thredds/fileServer/cmip6/temp.nc",
"url_replica": "http://esgf3.dkrz.de/thredds/fileServer/cmip6/temp.nc",
}

self.send_message("cmip6.test", message)

@tag("wdcc")
@task
def send_wdcc_message(self):
# Define JSON message to be sent
message = {
# "handle": "hdl:21.14106/test_abc_1",
"url_landing_page": "https://www.wdc-climate.de/ui/entry?acronym=temperature",
"is_part_of": "hdl:21.14106/test_temperature",
"publisher": "WDCC at DKRZ",
"aggregation_level": "dataset",
"title": "temperature",
"entry_id": "2426195",
"please_allow_datasets_without_parents": False,
}

self.send_message("wdcc.test", message)

0 comments on commit da47b3d

Please sign in to comment.