MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / test_enum

Function test_enum

crates/pg/src/encoder.rs:240–294  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

238
239 #[tokio::test]
240 async fn test_enum() {
241 let some = AlgebraicType::option(AlgebraicType::I64);
242 let schema = ProductType::from([some.clone(), some]);
243 let value = product![
244 AlgebraicValue::sum(0, AlgebraicValue::I64(1)), // Some(1)
245 AlgebraicValue::sum(1, AlgebraicValue::unit()), // None
246 ];
247
248 let row = run(schema, value).await;
249 assert_eq!(row, "\0\0\0\u{b}{\"some\": 1}\0\0\0\u{c}{\"none\": {}}");
250
251 let result = AlgebraicType::result(AlgebraicType::I64, AlgebraicType::String);
252 let schema = ProductType::from([result.clone(), result.clone()]);
253 let value = product![
254 AlgebraicValue::sum(0, AlgebraicValue::I64(1)), // Ok(1)
255 AlgebraicValue::sum(1, AlgebraicValue::String("error".into())), // Err("error")
256 ];
257 let row = run(schema, value).await;
258 assert_eq!(row, "\0\0\0\t{\"ok\": 1}\0\0\0\u{10}{\"err\": \"error\"}");
259
260 let color = AlgebraicType::Sum([SumTypeVariant::new_named(AlgebraicType::I64, "Gray")].into());
261 let nested = AlgebraicType::option(color.clone());
262 let schema = ProductType::from([color, nested]);
263 // {"Gray": 1}, {"some": {"Gray": 2}}
264 let value = product![
265 AlgebraicValue::sum(0, AlgebraicValue::I64(1)), // Gray(1)
266 AlgebraicValue::sum(0, AlgebraicValue::sum(0, AlgebraicValue::I64(2))), // Some(Gray(2))
267 ];
268 let row = run(schema.clone(), value.clone()).await;
269 assert_eq!(row, "\0\0\0\u{b}{\"Gray\": 1}\0\0\0\u{15}{\"some\": {\"Gray\": 2}}");
270
271 // Now nested product
272 let product = AlgebraicType::product([("x", AlgebraicType::Product(schema)), ("y", AlgebraicType::String)]);
273 let schema = ProductType::from([product.clone()]);
274 let value = product![AlgebraicValue::product(vec![
275 value.into(),
276 AlgebraicValue::String("a".into()),
277 ])];
278 let row = run(schema, value).await;
279 assert_eq!(
280 row,
281 "\0\0\0G{\"x\": {\"col_0\": {\"Gray\": 1}, \"col_1\": {\"some\": {\"Gray\": 2}}}, \"y\": \"a\"}"
282 );
283
284 // Now a simple enum
285 let names = AlgebraicType::simple_enum(["A", "B", "C"].into_iter());
286 let schema = ProductType::from([names.clone(), names.clone(), names]);
287 let value = product![
288 AlgebraicValue::enum_simple(0), // A
289 AlgebraicValue::enum_simple(1), // B
290 AlgebraicValue::enum_simple(2), // C
291 ];
292 let row = run(schema, value).await;
293 assert_eq!(row, "\0\0\0\u{1}A\0\0\0\u{1}B\0\0\0\u{1}C");
294 }
295
296 #[tokio::test]
297 async fn test_special_types() {

Callers

nothing calls this directly

Calls 8

SumFunction · 0.85
productFunction · 0.85
runFunction · 0.70
optionFunction · 0.50
resultFunction · 0.50
ProductFunction · 0.50
cloneMethod · 0.45
into_iterMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…