Skip to content

Commit 74abffe

Browse files
author
Bennett Hardwick
committed
Improve tests
1 parent 2fc828a commit 74abffe

15 files changed

+102
-23
lines changed

cryptonamo-derive/src/settings/builder.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ impl SettingsBuilder {
169169
}
170170
}
171171

172+
if field_name == "sk" {
173+
let has_partition_key_attr = field
174+
.attrs
175+
.iter()
176+
.find(|x| x.path().is_ident("sort_key"))
177+
.is_some();
178+
179+
if !has_partition_key_attr {
180+
return Err(syn::Error::new_spanned(
181+
field,
182+
format!("field named 'sk' must be annotated with #[sort_key]"),
183+
));
184+
}
185+
}
186+
172187
// Parse the meta for the field
173188
for attr in &field.attrs {
174189
if attr.path().is_ident("sort_key") {

tests/compile_tests.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
1-
#[test]
2-
fn ui_tests() {
3-
let t = trybuild::TestCases::new();
1+
use std::path::Path;
42

5-
t.compile_fail("tests/ui/compound-index-missing-field.rs");
6-
t.compile_fail("tests/ui/compound-index-missing-config.rs");
7-
t.compile_fail("tests/ui/compound-index-too-many-fields.rs");
8-
t.compile_fail("tests/ui/index-unsupported.rs");
9-
t.compile_fail("tests/ui/compound-index-unsupported.rs");
10-
t.compile_fail("tests/ui/using-pk-instead-of-pk-sk.rs");
11-
t.compile_fail("tests/ui/invalid-field-name.rs");
12-
t.compile_fail("tests/ui/pk-field-no-partition.rs");
13-
t.compile_fail("tests/ui/pk-field-wrong-partition.rs");
3+
macro_rules! run_tests {
4+
(fail => {$($f:expr),*}, pass => {$($p:expr),*}) => {
5+
#[test]
6+
fn ui_tests() {
7+
let t = trybuild::TestCases::new();
8+
let base = Path::new("tests");
149

15-
t.pass("tests/ui/pk-field-on-struct.rs");
16-
t.pass("tests/ui/pass.rs");
10+
$(
11+
t.compile_fail(base.join($f));
12+
)*
13+
14+
$(
15+
t.pass(base.join($p));
16+
)*
17+
}
18+
}
19+
}
20+
21+
run_tests! {
22+
fail => {
23+
"./ui/compound-index-missing-config.rs",
24+
"./ui/compound-index-missing-field.rs",
25+
"./ui/compound-index-too-many-fields.rs",
26+
"./ui/compound-index-unsupported.rs",
27+
"./ui/index-unsupported.rs",
28+
"./ui/invalid-field-name.rs",
29+
"./ui/pk-field-no-partition.rs",
30+
"./ui/pk-field-wrong-partition.rs",
31+
"./ui/sk-field-no-sort.rs",
32+
"./ui/sk-field-wrong-sort.rs",
33+
"./ui/using-pk-instead-of-pk-sk.rs"
34+
},
35+
36+
pass => {
37+
"./ui/pass.rs",
38+
"./ui/pk-field-on-struct.rs"
39+
}
1740
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Compound attribute was specified but no query options were. Specify how this field should be queried with the attribute #[cryptonamo(query = <option>, compound = "email#name")]
2-
--> tests/ui/compound-index-missing-config.rs:5:41
2+
--> tests/./ui/compound-index-missing-config.rs:5:41
33
|
44
5 | #[cryptonamo(compound = "email#name")]
55
| ^

tests/ui/compound-index-missing-field.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Not all fields were annotated with the #[cryptonamo(compound)] attribute. Missing fields: name
2-
--> tests/ui/compound-index-missing-field.rs:3:10
2+
--> tests/./ui/compound-index-missing-field.rs:3:10
33
|
44
3 | #[derive(Encryptable)]
55
| ^^^^^^^^^^^
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Compound index 'email#name' does not include current field 'age'.
2-
--> tests/ui/compound-index-too-many-fields.rs:9:35
2+
--> tests/./ui/compound-index-too-many-fields.rs:9:35
33
|
44
9 | #[cryptonamo(query = "exact", compound = "email#name")]
55
| ^^^^^^^^^^^^^^^^^^^^^^^
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Unsupported index type: blah
2-
--> tests/ui/compound-index-unsupported.rs:8:26
2+
--> tests/./ui/compound-index-unsupported.rs:8:26
33
|
44
8 | #[cryptonamo(query = "blah", compound = "email#name")]
55
| ^^^^^^

tests/ui/index-unsupported.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Unsupported index type: blah
2-
--> tests/ui/index-unsupported.rs:5:26
2+
--> tests/./ui/index-unsupported.rs:5:26
33
|
44
5 | #[cryptonamo(query = "blah")]
55
| ^^^^^^

tests/ui/invalid-field-name.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Invalid field '__name': fields must not be prefixed with __
2-
--> tests/ui/invalid-field-name.rs:8:5
2+
--> tests/./ui/invalid-field-name.rs:8:5
33
|
44
8 | __name: String
55
| ^^^^^^^^^^^^^^
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: field named 'pk' must be annotated with #[partition_key]
2-
--> tests/ui/pk-field-no-partition.rs:5:5
2+
--> tests/./ui/pk-field-no-partition.rs:5:5
33
|
44
5 | pk: String,
55
| ^^^^^^^^^^

tests/ui/pk-field-wrong-partition.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: field 'email' cannot be used as partition key as struct contains field named 'pk' which must be used
2-
--> tests/ui/pk-field-wrong-partition.rs:5:5
2+
--> tests/./ui/pk-field-wrong-partition.rs:5:5
33
|
44
5 | / #[partition_key]
55
6 | | #[cryptonamo(query = "exact", compound = "email#name")]

0 commit comments

Comments
 (0)