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(accessor = t_ints)] pub struct TInts { i8: i8, i16: i16, i32: i32, i64: i64, i128: i128, i256: i256, } #[spacetimedb::table(accessor = t_ints_tuple)] pub struct TIntsTuple { tuple: TInts, } #[derive(Copy, Clone)] #[spacetimedb::table(accessor = t_uints)] pub struct TUints { u8: u8, u16: u16, u32: u32, u64: u64, u128: u128, u256: u256, } #[spacetimedb::table(accessor = t_uints_tuple)] pub struct TUintsTuple { tuple: TUints, } #[derive(Clone)] #[spacetimedb::table(accessor = t_others)] pub struct TOthers { bool: bool, f32: f32, f64: f64, str: String, bytes: Vec, identity: Identity, connection_id: ConnectionId, timestamp: Timestamp, duration: TimeDuration, uuid: Uuid, } #[spacetimedb::table(accessor = t_others_tuple)] pub struct TOthersTuple { tuple: TOthers } #[derive(SpacetimeType, Debug, Clone, Copy)] pub enum Action { Inactive, Active, } #[derive(Clone)] #[spacetimedb::table(accessor = t_enums)] pub struct TEnums { bool_opt: Option, bool_result: Result, action: Action, } #[spacetimedb::table(accessor = 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", """\ i_8 | i_16 | i_32 | i_64 | i_128 | i_256 -----+-------+--------+----------+---------------+--------------- -25 | -3224 | -23443 | -2344353 | -234434897853 | -234434897853 """) self.assertSql("SELECT * FROM t_ints_tuple", """\ tuple --------------------------------------------------------------------------------------------------------- (i_8 = -25, i_16 = -3224, i_32 = -23443, i_64 = -2344353, i_128 = -234434897853, i_256 = -234434897853) """) self.assertSql("SELECT * FROM t_uints", """\ u_8 | u_16 | u_32 | u_64 | u_128 | u_256 -----+------+-------+----------+---------------+--------------- 105 | 1050 | 83892 | 48937498 | 4378528978889 | 4378528978889 """) self.assertSql("SELECT * FROM t_uints_tuple", """\ tuple ------------------------------------------------------------------------------------------------------- (u_8 = 105, u_16 = 1050, u_32 = 83892, u_64 = 48937498, u_128 = 4378528978889, u_256 = 4378528978889) """) self.assertSql("SELECT * FROM t_others", """\ bool | f_32 | f_64 | 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, f_32 = 594806.56, f_64 = -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 = ())) """)