kdb-connection 0.1.0

kDB connection.
Documentation
use serde_with::skip_serializing_none;

use super::KRQLQuery;

#[derive(Clone, KRQLQuery)]
#[krql(tag = "action")]
pub enum Dataset
{
    /// Create a new collection of datasets
    #[serde(rename = "create collection")]
    CreateCollection
    {
        collection_uri: String
    },
    /// Create a new collection of datasets
    #[serde(rename = "delete collection")]
    DeleteCollection
    {
        collection_uri: String
    },
    /// A string to print by the kDB Server
    #[serde(rename = "insert dataset")]
    #[allow(clippy::enum_variant_names)]
    InsertDataset
    {
        collection_uri: String,
        frame_type_uri: String,
        geometry: crate::Value,
        properties: crate::ValueHash,
        dataset_uri: Option<String>,
    },
    /// Get all the datasets from a collection
    #[serde(rename = "get all datasets")]
    GetAllDatasets
    {
        collection_uri: String
    },
}

#[cfg(test)]
mod tests
{
    use crate::{Connection as _, ValueHash, value::ValueArray};

    use super::*;

    #[tokio::test(flavor = "current_thread")]
    async fn test_http_dataset_query()
    {
        let mut store = crate::test::create_store(
            crate::test::create_store_configuration()
                .set_web_port(8888)
                .load_extension("kDBDatasets"),
        );
        store.start().unwrap();
        let c = crate::http::Connection::new("http://localhost:8888");
        crate::test::wait_for_connection(&c).await;

        // Create a dataset
        let f = c
            .execute_query(Dataset::InsertDataset {
                collection_uri: "http://askco.re/graph#private_datasets".into(),
                frame_type_uri: "http://askco.re/sensing#point_cloud".into(),
                geometry: crate::Value::from_uri_value(
                    "http://www.opengis.net/ont/geosparql#Geometry",
                    "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))",
                ),
                properties: crate::value_hash!(
                    "http://askco.re/sensing#point_density" =>
                        crate::Value::from_uri_value("http://askco.re/datatype#quantityDecimal", "10 point/m^2")),
                dataset_uri: Some("http://example.org/dataset/0".into()),
            })
            .unwrap();

        let r = f.await.unwrap();
        assert_eq!(r.metadata.error, String::new());
        assert!(r.metadata.success);

        // Get all the datasets
        let f = c
            .execute_query(Dataset::GetAllDatasets {
                collection_uri: "http://askco.re/graph#private_datasets".into(),
            })
            .unwrap();
        let r = f.await.unwrap();
        assert_eq!(r.metadata.error, String::new());
        assert!(r.metadata.success);
        assert_eq!(r.results.bindings.len(), 1);
        let datasets = r.results.bindings[0].get("data").unwrap();
        let datasets: ValueArray = datasets.to_owned().try_into().unwrap();
        assert_eq!(datasets.len(), 1);
        let dataset_0 = datasets.first().unwrap();
        let dataset_0: ValueHash = dataset_0.to_owned().try_into().unwrap();

        crate::test::validate_dataset_0(dataset_0);
    }
}