|
| 1 | +from .. import Smoketest #, random_string |
| 2 | +# import subprocess |
| 3 | + |
| 4 | +# class FailInitialPublish(Smoketest): |
| 5 | +# AUTOPUBLISH = False |
| 6 | + |
| 7 | +# MODULE_CODE_BROKEN = """ |
| 8 | +# use spacetimedb::{SpacetimeType, ViewContext}; |
| 9 | + |
| 10 | +# #[derive(SpacetimeType)] |
| 11 | +# pub enum NotProductType { |
| 12 | +# A, |
| 13 | +# B, |
| 14 | +# C, |
| 15 | +# } |
| 16 | + |
| 17 | +# pub fn my_view(_: &ViewContext) -> Option<NotProductType> { |
| 18 | +# None |
| 19 | +# } |
| 20 | +# """ |
| 21 | + |
| 22 | +# MODULE_CODE_FIXED = """ |
| 23 | +# use spacetimedb::{SpacetimeType, ViewContext}; |
| 24 | + |
| 25 | +# #[derive(SpacetimeType)] |
| 26 | +# pub enum ProductType { |
| 27 | +# id: u64, |
| 28 | +# } |
| 29 | + |
| 30 | +# pub fn my_view(_: &ViewContext) -> Option<ProductType> { |
| 31 | +# None |
| 32 | +# } |
| 33 | +# """ |
| 34 | + |
| 35 | +# def test_fail_initial_publish(self): |
| 36 | +# """This tests that publishing an invalid module does not leave a broken entry in the control DB.""" |
| 37 | + |
| 38 | +# name = random_string() |
| 39 | + |
| 40 | +# self.write_module_code(self.MODULE_CODE_BROKEN) |
| 41 | + |
| 42 | +# with self.assertRaises(Exception): |
| 43 | +# self.publish_module(name) |
| 44 | + |
| 45 | +# describe_output = self.spacetime("describe", "--json", name, full_output = True, check = False) |
| 46 | + |
| 47 | +# with self.assertRaises(subprocess.CalledProcessError): |
| 48 | +# describe_output.check_returncode() |
| 49 | + |
| 50 | +# self.assertIn("Error: No such database.", describe_output.stderr) |
| 51 | + |
| 52 | +# # We can publish a fixed module under the same database name. |
| 53 | +# # This used to be broken; |
| 54 | +# # the failed initial publish would leave the control database in a bad state. |
| 55 | +# self.write_module_code(self.MODULE_CODE_FIXED) |
| 56 | + |
| 57 | +# self.publish_module(name, clear = False) |
| 58 | +# describe_output = self.spacetime("describe", "--json", name) |
| 59 | + |
| 60 | +# self.assertIn("my_view", [line.strip() for line in describe_output.splitlines()]) |
| 61 | + |
| 62 | +# # Publishing the broken code again fails, but the database still exists afterwards, |
| 63 | +# # with the previous version of the module code. |
| 64 | +# self.write_module_code(self.MODULE_CODE_BROKEN) |
| 65 | + |
| 66 | +# with self.assertRaises(Exception): |
| 67 | +# self.publish_module(name, clear = False) |
| 68 | + |
| 69 | +# describe_output = self.spacetime("describe", "--json", name) |
| 70 | +# self.assertIn("my_view", [line.strip() for line in describe_output.splitlines()]) |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +class Views(Smoketest): |
| 75 | + MODULE_CODE = """ |
| 76 | +use spacetimedb::ViewContext; |
| 77 | +
|
| 78 | +#[derive(Copy, Clone)] |
| 79 | +#[spacetimedb::table(name = player_state)] |
| 80 | +pub struct PlayerState { |
| 81 | + #[primary_key] |
| 82 | + id: u64, |
| 83 | + #[index(btree)] |
| 84 | + level: u64, |
| 85 | +} |
| 86 | +
|
| 87 | +#[spacetimedb::view(public)] |
| 88 | +pub fn player(ctx: &ViewContext, id: u64) -> Option<PlayerState> { |
| 89 | + ctx.db.player_state().id().find(id) |
| 90 | +} |
| 91 | +""" |
| 92 | + |
| 93 | + def assertSql(self, sql, expected): |
| 94 | + self.maxDiff = None |
| 95 | + sql_out = self.spacetime("sql", self.database_identity, sql) |
| 96 | + sql_out = "\n".join([line.rstrip() for line in sql_out.splitlines()]) |
| 97 | + expected = "\n".join([line.rstrip() for line in expected.splitlines()]) |
| 98 | + self.assertMultiLineEqual(sql_out, expected) |
| 99 | + |
| 100 | + def test_st_view_tables(self): |
| 101 | + """This test asserts that views populate the st_view_* system tables""" |
| 102 | + |
| 103 | + self.assertSql("SELECT * FROM st_view", """\ |
| 104 | + view_id | view_name | table_id | is_public | is_anonymous |
| 105 | +---------+-----------+---------------+-----------+-------------- |
| 106 | + 4096 | "player" | (some = 4097) | true | false |
| 107 | +""") |
| 108 | + |
| 109 | + self.assertSql("SELECT * FROM st_view_param", """\ |
| 110 | +view_id | param_pos | param_name | param_type |
| 111 | +--------+-----------+------------+------------ |
| 112 | +""") |
| 113 | + |
| 114 | + self.assertSql("SELECT * FROM st_view_column", """\ |
| 115 | +view_id | col_pos | col_name | col_type |
| 116 | +--------+---------+----------+---------- |
| 117 | +""") |
0 commit comments