|
| 1 | +use crates_io_test_db::TestDatabase; |
| 2 | +use diesel::prelude::*; |
| 3 | +use diesel::sql_types::{Nullable, Text}; |
| 4 | +use diesel_async::RunQueryDsl; |
| 5 | +use std::fmt::Debug; |
| 6 | + |
| 7 | +#[tokio::test] |
| 8 | +async fn test_jsonb_output() { |
| 9 | + let test_db = TestDatabase::new(); |
| 10 | + let mut conn = test_db.async_connect().await; |
| 11 | + |
| 12 | + let mut check = async |num| { |
| 13 | + let query = format!("select semver_ord('{num}') as output"); |
| 14 | + |
| 15 | + #[derive(QueryableByName)] |
| 16 | + struct Row { |
| 17 | + #[diesel(sql_type = Nullable<Text>)] |
| 18 | + output: Option<String>, |
| 19 | + } |
| 20 | + |
| 21 | + diesel::sql_query(query) |
| 22 | + .get_result::<Row>(&mut conn) |
| 23 | + .await |
| 24 | + .unwrap() |
| 25 | + .output |
| 26 | + .unwrap_or_default() |
| 27 | + }; |
| 28 | + |
| 29 | + insta::assert_snapshot!(check("0.0.0").await, @r#"[0, 0, 0, {}]"#); |
| 30 | + insta::assert_snapshot!(check("1.0.0-alpha.1").await, @r#"[1, 0, 0, [true, "alpha", false, 1, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, ""]]"#); |
| 31 | + |
| 32 | + // see https://crates.io/crates/cursed-trying-to-break-cargo/1.0.0-0.HDTV-BluRay.1020p.YTSUB.L33TRip.mkv – thanks @Gankra! |
| 33 | + insta::assert_snapshot!(check("1.0.0-0.HDTV-BluRay.1020p.YTSUB.L33TRip.mkv").await, @r#"[1, 0, 0, [false, 0, true, "HDTV-BluRay", true, "1020p", true, "YTSUB", true, "L33TRip", true, "mkv", null, null, null, null, null, null, null, null, ""]]"#); |
| 34 | + |
| 35 | + // Invalid version string |
| 36 | + insta::assert_snapshot!(check("foo").await, @""); |
| 37 | + |
| 38 | + // Version string with a lot of prerelease identifiers |
| 39 | + insta::assert_snapshot!(check("1.2.3-1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.end").await, @r#"[1, 2, 3, [false, 1, false, 2, false, 3, false, 4, false, 5, false, 6, false, 7, false, 8, false, 9, false, 10, "11.12.13.14.15.16.17.end"]]"#); |
| 40 | +} |
| 41 | + |
| 42 | +/// This test checks that the `semver_ord` function orders versions correctly. |
| 43 | +/// |
| 44 | +/// The test data is a list of versions in a random order. The versions are then |
| 45 | +/// ordered by the `semver_ord` function and the result is compared to the |
| 46 | +/// expected order (see <https://semver.org/#spec-item-11>). |
| 47 | +/// |
| 48 | +/// The test data was imported from <https://github.com/dtolnay/semver/blob/1.0.26/tests/test_version.rs#L223-L242>. |
| 49 | +#[tokio::test] |
| 50 | +async fn test_spec_order() { |
| 51 | + let test_db = TestDatabase::new(); |
| 52 | + let mut conn = test_db.async_connect().await; |
| 53 | + |
| 54 | + #[derive(QueryableByName)] |
| 55 | + struct Row { |
| 56 | + #[diesel(sql_type = Text)] |
| 57 | + num: String, |
| 58 | + } |
| 59 | + |
| 60 | + impl Debug for Row { |
| 61 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 62 | + f.write_str(&self.num) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + let mut check = async |order| { |
| 67 | + let query = format!( |
| 68 | + r#" |
| 69 | + with nums as ( |
| 70 | + select unnest(array[ |
| 71 | + '1.0.0-beta', |
| 72 | + '1.0.0-alpha', |
| 73 | + '1.0.0-rc.1', |
| 74 | + '1.0.0', |
| 75 | + '1.0.0-beta.2', |
| 76 | + '1.0.0-alpha.1', |
| 77 | + '1.0.0-alpha.beta', |
| 78 | + '1.0.0-beta.11' |
| 79 | + ]) as num |
| 80 | + ) |
| 81 | + select num |
| 82 | + from nums |
| 83 | + order by semver_ord(num) {order}; |
| 84 | + "# |
| 85 | + ); |
| 86 | + |
| 87 | + diesel::sql_query(query) |
| 88 | + .load::<Row>(&mut conn) |
| 89 | + .await |
| 90 | + .unwrap() |
| 91 | + }; |
| 92 | + |
| 93 | + insta::assert_debug_snapshot!(check("asc").await, @r" |
| 94 | + [ |
| 95 | + 1.0.0-alpha, |
| 96 | + 1.0.0-alpha.1, |
| 97 | + 1.0.0-alpha.beta, |
| 98 | + 1.0.0-beta, |
| 99 | + 1.0.0-beta.2, |
| 100 | + 1.0.0-beta.11, |
| 101 | + 1.0.0-rc.1, |
| 102 | + 1.0.0, |
| 103 | + ] |
| 104 | + "); |
| 105 | + |
| 106 | + insta::assert_debug_snapshot!(check("desc").await, @r" |
| 107 | + [ |
| 108 | + 1.0.0, |
| 109 | + 1.0.0-rc.1, |
| 110 | + 1.0.0-beta.11, |
| 111 | + 1.0.0-beta.2, |
| 112 | + 1.0.0-beta, |
| 113 | + 1.0.0-alpha.beta, |
| 114 | + 1.0.0-alpha.1, |
| 115 | + 1.0.0-alpha, |
| 116 | + ] |
| 117 | + "); |
| 118 | +} |
0 commit comments