Files
Mario Montoya 82d5a4f6c0 Implement SpacetimeType for Result<T, E> (#3790)
# Description of Changes

Closes #3673 

*NOTE*: C++ part will be in another PR

# Expected complexity level and risk

2

Adding a new type touch everywhere

# Testing

- [x] Adding smoke and unit test

---------

Signed-off-by: Ryan <r.ekhoff@clockworklabs.io>
Co-authored-by: rekhoff <r.ekhoff@clockworklabs.io>
Co-authored-by: Phoebe Goldman <phoebe@goldman-tribe.org>
Co-authored-by: Jason Larabie <jason@clockworklabs.io>
2026-01-08 15:50:18 +00:00

175 lines
5.9 KiB
Python

from .. import Smoketest
class SqlFormat(Smoketest):
MODULE_CODE = """
use spacetimedb::sats::{i256, u256};
use spacetimedb::{ConnectionId, Identity, ReducerContext, Table, Timestamp, TimeDuration, SpacetimeType, Uuid};
#[derive(Copy, Clone)]
#[spacetimedb::table(name = t_ints)]
pub struct TInts {
i8: i8,
i16: i16,
i32: i32,
i64: i64,
i128: i128,
i256: i256,
}
#[spacetimedb::table(name = t_ints_tuple)]
pub struct TIntsTuple {
tuple: TInts,
}
#[derive(Copy, Clone)]
#[spacetimedb::table(name = t_uints)]
pub struct TUints {
u8: u8,
u16: u16,
u32: u32,
u64: u64,
u128: u128,
u256: u256,
}
#[spacetimedb::table(name = t_uints_tuple)]
pub struct TUintsTuple {
tuple: TUints,
}
#[derive(Clone)]
#[spacetimedb::table(name = t_others)]
pub struct TOthers {
bool: bool,
f32: f32,
f64: f64,
str: String,
bytes: Vec<u8>,
identity: Identity,
connection_id: ConnectionId,
timestamp: Timestamp,
duration: TimeDuration,
uuid: Uuid,
}
#[spacetimedb::table(name = t_others_tuple)]
pub struct TOthersTuple {
tuple: TOthers
}
#[derive(SpacetimeType, Debug, Clone, Copy)]
pub enum Action {
Inactive,
Active,
}
#[derive(Clone)]
#[spacetimedb::table(name = t_enums)]
pub struct TEnums {
bool_opt: Option<bool>,
bool_result: Result<bool, String>,
action: Action,
}
#[spacetimedb::table(name = t_enums_tuple)]
pub struct TEnumsTuple {
tuple: TEnums,
}
#[spacetimedb::reducer]
pub fn test(ctx: &ReducerContext) {
let tuple = TInts {
i8: -25,
i16: -3224,
i32: -23443,
i64: -2344353,
i128: -234434897853,
i256: (-234434897853i128).into(),
};
ctx.db.t_ints().insert(tuple);
ctx.db.t_ints_tuple().insert(TIntsTuple { tuple });
let tuple = TUints {
u8: 105,
u16: 1050,
u32: 83892,
u64: 48937498,
u128: 4378528978889,
u256: 4378528978889u128.into(),
};
ctx.db.t_uints().insert(tuple);
ctx.db.t_uints_tuple().insert(TUintsTuple { tuple });
let tuple = TOthers {
bool: true,
f32: 594806.58906,
f64: -3454353.345389043278459,
str: "This is spacetimedb".to_string(),
bytes: vec!(1, 2, 3, 4, 5, 6, 7),
identity: Identity::ONE,
connection_id: ConnectionId::ZERO,
timestamp: Timestamp::UNIX_EPOCH,
duration: TimeDuration::ZERO,
uuid: Uuid::NIL,
};
ctx.db.t_others().insert(tuple.clone());
ctx.db.t_others_tuple().insert(TOthersTuple { tuple });
let tuple = TEnums {
bool_opt: Some(true),
bool_result: Ok(false),
action: Action::Active,
};
ctx.db.t_enums().insert(tuple.clone());
ctx.db.t_enums_tuple().insert(TEnumsTuple { tuple });
}
"""
def test_sql_format(self):
"""This test is designed to test the format of the output of sql queries"""
self.call("test")
self.assertSql("SELECT * FROM t_ints", """\
i8 | i16 | i32 | i64 | i128 | i256
-----+-------+--------+----------+---------------+---------------
-25 | -3224 | -23443 | -2344353 | -234434897853 | -234434897853
""")
self.assertSql("SELECT * FROM t_ints_tuple", """\
tuple
---------------------------------------------------------------------------------------------------
(i8 = -25, i16 = -3224, i32 = -23443, i64 = -2344353, i128 = -234434897853, i256 = -234434897853)
""")
self.assertSql("SELECT * FROM t_uints", """\
u8 | u16 | u32 | u64 | u128 | u256
-----+------+-------+----------+---------------+---------------
105 | 1050 | 83892 | 48937498 | 4378528978889 | 4378528978889
""")
self.assertSql("SELECT * FROM t_uints_tuple", """\
tuple
-------------------------------------------------------------------------------------------------
(u8 = 105, u16 = 1050, u32 = 83892, u64 = 48937498, u128 = 4378528978889, u256 = 4378528978889)
""")
self.assertSql("SELECT * FROM t_others", """\
bool | f32 | f64 | str | bytes | identity | connection_id | timestamp | duration | uuid
------+-----------+--------------------+-----------------------+------------------+--------------------------------------------------------------------+------------------------------------+---------------------------+-----------+----------------------------------------
true | 594806.56 | -3454353.345389043 | "This is spacetimedb" | 0x01020304050607 | 0x0000000000000000000000000000000000000000000000000000000000000001 | 0x00000000000000000000000000000000 | 1970-01-01T00:00:00+00:00 | +0.000000 | "00000000-0000-0000-0000-000000000000"
""")
self.assertSql("SELECT * FROM t_others_tuple", """\
tuple
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(bool = true, f32 = 594806.56, f64 = -3454353.345389043, str = "This is spacetimedb", bytes = 0x01020304050607, identity = 0x0000000000000000000000000000000000000000000000000000000000000001, connection_id = 0x00000000000000000000000000000000, timestamp = 1970-01-01T00:00:00+00:00, duration = +0.000000, uuid = "00000000-0000-0000-0000-000000000000")
""")
self.assertSql("SELECT * FROM t_enums", """\
bool_opt | bool_result | action
---------------+--------------+---------------
(some = true) | (ok = false) | (Active = ())
""")
self.assertSql("SELECT * FROM t_enums_tuple", """\
tuple
--------------------------------------------------------------------------------
(bool_opt = (some = true), bool_result = (ok = false), action = (Active = ()))
""")