Skip to content

Commit afac310

Browse files
committed
amend auto_migration smoketest to test changing a sum type
1 parent 96d3f9f commit afac310

1 file changed

Lines changed: 55 additions & 15 deletions

File tree

smoketests/tests/auto_migration.py

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44

55

66
class AddTableAutoMigration(Smoketest):
7-
MODULE_CODE = """
7+
MODULE_CODE_INIT = """
88
use spacetimedb::{log, ReducerContext, Table, SpacetimeType};
9+
use PersonKind::*;
910
1011
#[spacetimedb::table(name = person, public)]
1112
pub struct Person {
1213
name: String,
14+
kind: PersonKind,
1315
}
1416
1517
#[spacetimedb::reducer]
16-
pub fn add_person(ctx: &ReducerContext, name: String) {
17-
ctx.db.person().insert(Person { name });
18+
pub fn add_person(ctx: &ReducerContext, name: String, kind: String) {
19+
let kind = kind_from_string(kind);
20+
ctx.db.person().insert(Person { name, kind });
1821
}
1922
2023
#[spacetimedb::reducer]
2124
pub fn print_persons(ctx: &ReducerContext, prefix: String) {
2225
for person in ctx.db.person().iter() {
23-
log::info!("{}: {}", prefix, person.name);
26+
let kind = kind_to_string(person.kind);
27+
log::info!("{prefix}: {} - {kind}", person.name);
2428
}
2529
}
2630
@@ -39,11 +43,47 @@ class AddTableAutoMigration(Smoketest):
3943
4044
#[spacetimedb::client_visibility_filter]
4145
const PERSON_VISIBLE: spacetimedb::Filter = spacetimedb::Filter::Sql("SELECT * FROM person");
46+
"""
47+
48+
MODULE_CODE = MODULE_CODE_INIT + """
49+
#[derive(SpacetimeType, Clone, Copy, PartialEq, Eq)]
50+
pub enum PersonKind {
51+
Student,
52+
}
53+
54+
fn kind_from_string(_: String) -> PersonKind {
55+
Student
56+
}
57+
58+
fn kind_to_string(Student: PersonKind) -> &'static str {
59+
"Student"
60+
}
4261
"""
4362

4463
MODULE_CODE_UPDATED = (
4564
MODULE_CODE
4665
+ """
66+
#[derive(SpacetimeType, Clone, Copy, PartialEq, Eq)]
67+
pub enum PersonKind {
68+
Student,
69+
Professor,
70+
}
71+
72+
fn kind_from_string(kind: String) -> PersonKind {
73+
match &*kind {
74+
"Student" => Student,
75+
"Professor" => Professor,
76+
_ => panic!(),
77+
}
78+
}
79+
80+
fn kind_to_string(kind: PersonKind) -> &'static str {
81+
match kind {
82+
Student => "Student",
83+
Professor => "Professor",
84+
}
85+
}
86+
4787
#[spacetimedb::table(name = book, public)]
4888
pub struct Book {
4989
isbn: String,
@@ -89,14 +129,14 @@ def test_add_table_auto_migration(self):
89129
logging.info("Initial publish complete")
90130
# initial module code is already published by test framework
91131

92-
self.call("add_person", "Robert")
93-
self.call("add_person", "Julie")
94-
self.call("add_person", "Samantha")
132+
self.call("add_person", "Robert", "Student")
133+
self.call("add_person", "Julie", "Student")
134+
self.call("add_person", "Samantha", "Student")
95135
self.call("print_persons", "BEFORE")
96136
logs = self.logs(100)
97-
self.assertIn("BEFORE: Samantha", logs)
98-
self.assertIn("BEFORE: Julie", logs)
99-
self.assertIn("BEFORE: Robert", logs)
137+
self.assertIn("BEFORE: Samantha - Student", logs)
138+
self.assertIn("BEFORE: Julie - Student", logs)
139+
self.assertIn("BEFORE: Robert - Student", logs)
100140

101141
logging.info(
102142
"Initial operations complete, updating module without clear",
@@ -120,16 +160,16 @@ def test_add_table_auto_migration(self):
120160

121161
self.logs(100)
122162

123-
self.call("add_person", "Husserl")
163+
self.call("add_person", "Husserl", "Professor")
124164
self.call("add_book", "1234567890")
125165
self.call("print_persons", "AFTER_PERSON")
126166
self.call("print_books", "AFTER_BOOK")
127167

128168
logs = self.logs(100)
129-
self.assertIn("AFTER_PERSON: Samantha", logs)
130-
self.assertIn("AFTER_PERSON: Julie", logs)
131-
self.assertIn("AFTER_PERSON: Robert", logs)
132-
self.assertIn("AFTER_PERSON: Husserl", logs)
169+
self.assertIn("AFTER_PERSON: Samantha - Student", logs)
170+
self.assertIn("AFTER_PERSON: Julie - Student", logs)
171+
self.assertIn("AFTER_PERSON: Robert - Student", logs)
172+
self.assertIn("AFTER_PERSON: Husserl - Professor", logs)
133173
self.assertIn("AFTER_BOOK: 1234567890", logs)
134174

135175

0 commit comments

Comments
 (0)