lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

tests.rs (7595B)


      1 use crate::{
      2     RADROOTS_REPLICA_TRANSFER_VERSION, RadrootsReplicaFarmSelector, RadrootsReplicaSyncRequest,
      3     radroots_replica_sync_all,
      4 };
      5 use radroots_events::farm::{RadrootsGeoJsonPoint, RadrootsGeoJsonPolygon};
      6 use radroots_events::kinds::{KIND_FARM, KIND_LIST_SET_GENERIC, KIND_PLOT, KIND_PROFILE};
      7 use radroots_replica_db::{
      8     farm, farm_gcs_location, farm_member, farm_member_claim, farm_tag, gcs_location, migrations,
      9     nostr_profile, plot, plot_gcs_location, plot_tag,
     10 };
     11 use radroots_replica_db_schema::farm::IFarmFields;
     12 use radroots_replica_db_schema::farm_gcs_location::IFarmGcsLocationFields;
     13 use radroots_replica_db_schema::farm_member::IFarmMemberFields;
     14 use radroots_replica_db_schema::farm_member_claim::IFarmMemberClaimFields;
     15 use radroots_replica_db_schema::farm_tag::IFarmTagFields;
     16 use radroots_replica_db_schema::gcs_location::IGcsLocationFields;
     17 use radroots_replica_db_schema::nostr_profile::INostrProfileFields;
     18 use radroots_replica_db_schema::plot::IPlotFields;
     19 use radroots_replica_db_schema::plot_gcs_location::IPlotGcsLocationFields;
     20 use radroots_replica_db_schema::plot_tag::IPlotTagFields;
     21 use radroots_sql_core::SqliteExecutor;
     22 use radroots_sql_core::error::SqlError;
     23 use radroots_types::types::IError;
     24 use std::panic;
     25 
     26 #[cfg_attr(coverage_nightly, coverage(off))]
     27 fn unwrap_sql<T>(result: Result<T, IError<SqlError>>, label: &str) -> T {
     28     result
     29         .map_err(|err| format!("{label}: {}", err.err))
     30         .unwrap()
     31 }
     32 
     33 #[test]
     34 fn sync_all_emits_expected_order() {
     35     let exec = SqliteExecutor::open_memory().expect("exec");
     36     migrations::run_all_up(&exec).expect("migrations");
     37 
     38     let farm_pubkey = "f".repeat(64);
     39     let farm_fields = IFarmFields {
     40         d_tag: "AAAAAAAAAAAAAAAAAAAAAA".to_string(),
     41         pubkey: farm_pubkey.clone(),
     42         name: "Green Farm".to_string(),
     43         about: Some("About".to_string()),
     44         website: None,
     45         picture: None,
     46         banner: None,
     47         location_primary: None,
     48         location_city: None,
     49         location_region: None,
     50         location_country: None,
     51     };
     52     let farm_row = unwrap_sql(farm::create(&exec, &farm_fields), "farm").result;
     53 
     54     let gcs_point = RadrootsGeoJsonPoint {
     55         r#type: "Point".to_string(),
     56         coordinates: [-122.4, 37.7],
     57     };
     58     let gcs_polygon = RadrootsGeoJsonPolygon {
     59         r#type: "Polygon".to_string(),
     60         coordinates: vec![vec![
     61             [-122.4, 37.7],
     62             [-122.4, 37.701],
     63             [-122.401, 37.701],
     64             [-122.4, 37.7],
     65         ]],
     66     };
     67     let gcs_fields = IGcsLocationFields {
     68         d_tag: "AAAAAAAAAAAAAAAAAAAAAQ".to_string(),
     69         lat: 37.7,
     70         lng: -122.4,
     71         geohash: "9q8yy".to_string(),
     72         point: serde_json::to_string(&gcs_point).expect("point"),
     73         polygon: serde_json::to_string(&gcs_polygon).expect("polygon"),
     74         accuracy: None,
     75         altitude: None,
     76         tag_0: None,
     77         label: None,
     78         area: None,
     79         elevation: None,
     80         soil: None,
     81         climate: None,
     82         gc_id: None,
     83         gc_name: None,
     84         gc_admin1_id: None,
     85         gc_admin1_name: None,
     86         gc_country_id: None,
     87         gc_country_name: None,
     88     };
     89     let gcs_row = unwrap_sql(gcs_location::create(&exec, &gcs_fields), "gcs").result;
     90 
     91     let farm_gcs_fields = IFarmGcsLocationFields {
     92         farm_id: farm_row.id.clone(),
     93         gcs_location_id: gcs_row.id.clone(),
     94         role: "primary".to_string(),
     95     };
     96     let _ = unwrap_sql(
     97         farm_gcs_location::create(&exec, &farm_gcs_fields),
     98         "farm_gcs",
     99     );
    100 
    101     let plot_fields = IPlotFields {
    102         d_tag: "AAAAAAAAAAAAAAAAAAAAAw".to_string(),
    103         farm_id: farm_row.id.clone(),
    104         name: "Plot A".to_string(),
    105         about: None,
    106         location_primary: None,
    107         location_city: None,
    108         location_region: None,
    109         location_country: None,
    110     };
    111     let plot_row = unwrap_sql(plot::create(&exec, &plot_fields), "plot").result;
    112 
    113     let plot_gcs_fields = IPlotGcsLocationFields {
    114         plot_id: plot_row.id.clone(),
    115         gcs_location_id: gcs_row.id.clone(),
    116         role: "primary".to_string(),
    117     };
    118     let _ = unwrap_sql(
    119         plot_gcs_location::create(&exec, &plot_gcs_fields),
    120         "plot_gcs",
    121     );
    122 
    123     let _ = unwrap_sql(
    124         farm_tag::create(
    125             &exec,
    126             &IFarmTagFields {
    127                 farm_id: farm_row.id.clone(),
    128                 tag: "coffee".to_string(),
    129             },
    130         ),
    131         "farm_tag",
    132     );
    133 
    134     let _ = unwrap_sql(
    135         plot_tag::create(
    136             &exec,
    137             &IPlotTagFields {
    138                 plot_id: plot_row.id.clone(),
    139                 tag: "orchard".to_string(),
    140             },
    141         ),
    142         "plot_tag",
    143     );
    144 
    145     let owner_pubkey = "8".repeat(64);
    146     let _ = unwrap_sql(
    147         farm_member::create(
    148             &exec,
    149             &IFarmMemberFields {
    150                 farm_id: farm_row.id.clone(),
    151                 member_pubkey: owner_pubkey.clone(),
    152                 role: "owner".to_string(),
    153             },
    154         ),
    155         "farm_member",
    156     );
    157 
    158     let _ = unwrap_sql(
    159         farm_member_claim::create(
    160             &exec,
    161             &IFarmMemberClaimFields {
    162                 member_pubkey: owner_pubkey.clone(),
    163                 farm_pubkey: farm_pubkey.clone(),
    164             },
    165         ),
    166         "farm_member_claim",
    167     );
    168 
    169     let _ = unwrap_sql(
    170         nostr_profile::create(
    171             &exec,
    172             &INostrProfileFields {
    173                 public_key: farm_pubkey.clone(),
    174                 profile_type: "farm".to_string(),
    175                 name: "Farm Profile".to_string(),
    176                 display_name: None,
    177                 about: None,
    178                 website: None,
    179                 picture: None,
    180                 banner: None,
    181                 nip05: None,
    182                 lud06: None,
    183                 lud16: None,
    184             },
    185         ),
    186         "farm_profile",
    187     );
    188 
    189     let _ = unwrap_sql(
    190         nostr_profile::create(
    191             &exec,
    192             &INostrProfileFields {
    193                 public_key: owner_pubkey.clone(),
    194                 profile_type: "individual".to_string(),
    195                 name: "Owner".to_string(),
    196                 display_name: None,
    197                 about: None,
    198                 website: None,
    199                 picture: None,
    200                 banner: None,
    201                 nip05: None,
    202                 lud06: None,
    203                 lud16: None,
    204             },
    205         ),
    206         "owner_profile",
    207     );
    208 
    209     let request = RadrootsReplicaSyncRequest {
    210         farm: RadrootsReplicaFarmSelector {
    211             id: Some(farm_row.id.clone()),
    212             d_tag: None,
    213             pubkey: None,
    214         },
    215         options: None,
    216     };
    217     let bundle = radroots_replica_sync_all(&exec, &request).expect("sync");
    218 
    219     assert_eq!(bundle.version, RADROOTS_REPLICA_TRANSFER_VERSION);
    220     assert_eq!(bundle.events.len(), 9);
    221     let kinds = bundle
    222         .events
    223         .iter()
    224         .map(|event| event.kind)
    225         .collect::<Vec<_>>();
    226     assert_eq!(kinds[0], KIND_PROFILE);
    227     assert_eq!(kinds[1], KIND_PROFILE);
    228     assert_eq!(kinds[2], KIND_FARM);
    229     assert_eq!(kinds[3], KIND_PLOT);
    230     assert!(
    231         kinds[4..8]
    232             .iter()
    233             .all(|kind| *kind == KIND_LIST_SET_GENERIC)
    234     );
    235     assert_eq!(kinds[8], KIND_LIST_SET_GENERIC);
    236 }
    237 
    238 #[test]
    239 fn unwrap_sql_panics_on_error() {
    240     let result = panic::catch_unwind(|| {
    241         let err = IError::from(SqlError::InvalidArgument("bad".to_string()));
    242         let _ = unwrap_sql::<()>(Err(err), "unwrap");
    243     });
    244     assert!(result.is_err());
    245 }