Skip to content

Commit

Permalink
Fix some passing tests, add a failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
kazimuth committed Oct 14, 2024
1 parent 5f7c823 commit 5541cec
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
28 changes: 28 additions & 0 deletions crates/lib/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,34 @@ mod tests {
#[cfg(feature = "serde")]
mod serde {
use super::*;
use crate::sats::{algebraic_value::de::ValueDeserializer, de::Deserialize, Typespace};
use crate::ser::serde::SerializeWrapper;
use crate::WithTypespace;

proptest! {
/// Tests the round-trip used when using the `spacetime subscribe`
/// CLI command.
/// Somewhat confusingly, this is distinct from the ser-de path
/// in `test_serde_roundtrip`.
#[test]
fn test_wrapper_roundtrip(val: u128) {
let addr = Address::from_u128(val);
let wrapped = SerializeWrapper::new(&addr);

let ser = serde_json::to_string(&wrapped).unwrap();
let empty = Typespace::default();
let address_ty = Address::get_type();
let address_ty = WithTypespace::new(&empty, &address_ty);
let row = serde_json::from_str::<serde_json::Value>(&ser[..])?;
let de = ::serde::de::DeserializeSeed::deserialize(
crate::de::serde::SeedWrapper(
address_ty
),
row)?;
let de = Address::deserialize(ValueDeserializer::new(de)).unwrap();
prop_assert_eq!(addr, de);
}
}

proptest! {
#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/tests/snapshots/serde__json_mappings-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ expression: "de_json({\n \"foo\": 5654,\n \"bar\": [1, 15, 44],\n \"baz
none = (),
),
identity = (
__identity__ = 0x0000000000000000000000000000000000000000000000000000000000000000,
__identity__ = 0,
),
)
2 changes: 1 addition & 1 deletion crates/lib/tests/snapshots/serde__json_mappings.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ expression: "de_json({\n \"foo\": 42,\n \"bar\": \"404040FFFF0A48656C6C6F\
some = 3.141592653589793,
),
identity = (
__identity__ = 0x0000000000000000000000000000000000000000000000000000000000000000,
__identity__ = 0,
),
)
51 changes: 51 additions & 0 deletions smoketests/tests/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,54 @@ def test_set_email(self):
self.assertEqual(extract_field(output, "IDENTITY"), identity)
self.assertEqual(extract_field(output, "EMAIL").lower(), email.lower())


class IdentityFormatting(Smoketest):
MODULE_CODE = """
use log::info;
use spacetimedb::{Address, Identity, ReducerContext, Table};
#[spacetimedb::table(name = connected_client)]
pub struct ConnectedClient {
identity: Identity,
address: Address,
}
#[spacetimedb::reducer(client_connected)]
fn on_connect(ctx: &ReducerContext) {
ctx.db.connected_client().insert(ConnectedClient {
identity: ctx.sender,
address: ctx.address.expect("sender address unset"),
});
}
#[spacetimedb::reducer(client_disconnected)]
fn on_disconnect(ctx: &ReducerContext) {
let sender_identity = &ctx.sender;
let sender_address = ctx.address.as_ref().expect("sender address unset");
let match_client = |row: &ConnectedClient| {
&row.identity == sender_identity && &row.address == sender_address
};
if let Some(client) = ctx.db.connected_client().iter().find(match_client) {
ctx.db.connected_client().delete(client);
}
}
#[spacetimedb::reducer]
fn print_num_connected(ctx: &ReducerContext) {
let n = ctx.db.connected_client().count();
info!("CONNECTED CLIENTS: {n}")
}
"""

def test_identity_formatting(self):
"""Tests formatting of Identity."""

# Start two subscribers
self.subscribe("SELECT * FROM connected_client", n=2)
self.subscribe("SELECT * FROM connected_client", n=2)

# Assert that we have two clients + the reducer call
self.call("print_num_connected")
logs = self.logs(10)
self.assertEqual("CONNECTED CLIENTS: 3", logs.pop())

0 comments on commit 5541cec

Please sign in to comment.