Skip to content

Commit

Permalink
simplify existing test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
adwhit committed Nov 10, 2022
1 parent 28a013d commit 023babb
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 124 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Now we can use `diesel-derive-enum` to hook in our own enum:
// src/my_code.rs

#[derive(diesel_derive_enum::DbEnum)]
#[DieselTypePath = "crate::schema::sql_types::MyEnum"]
#[ExistingTypePath = "crate::schema::sql_types::MyEnum"]
pub enum MyEnum {
Foo,
Bar,
Expand Down Expand Up @@ -217,7 +217,7 @@ e.g. Postgres `INTEGER` maps to `diesel::types::Integer` maps to `i32`.
*For `postgres` only*, as of `diesel-2.0.0-rc.0`, diesel will create the 'dummy' internal enum type as part
of the schema generation process. This crate will attempt to locate this dummy type at the
the default path of `crate::schema::sql_types::{enum_name}`. This location can be overridden with the
`DieselTypePath` attribute.
`ExistingTypePath` attribute.

For `mysql` and `sqlite`, the intenal type is *not* automatically generated, so this macro will instead create it
with the default name `{enum_name}Mapping`. This name can be overridden with the `DieselType` attribute.
Expand Down
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,24 @@ use syn::*;
pub fn derive(input: TokenStream) -> TokenStream {
let input: DeriveInput = parse_macro_input!(input as DeriveInput);

let existing_mapping_path = type_from_attrs(&input.attrs, "ExistingTypePath");
let existing_mapping_path = val_from_attrs(&input.attrs, "ExistingTypePath");
if !cfg!(feature = "postgres") && existing_mapping_path.is_some() {
panic!("ExistingTypePath attribute only applies when the 'postgres' feature is enabled");
}

// we could allow a default value here but... I'm not very keen
// let existing_mapping_path = existing_mapping_path
// .unwrap_or_else(|| format!("crate::schema::sql_types::{}", input.ident));

let pg_internal_type = type_from_attrs(&input.attrs, "PgType");
let pg_internal_type = val_from_attrs(&input.attrs, "PgType");

if existing_mapping_path.is_some() && pg_internal_type.is_some() {
panic!("Cannot specify both `ExistingTypePath` and `PgType` attributes");
}

let pg_internal_type = pg_internal_type.unwrap_or(input.ident.to_string().to_snake_case());

let new_diesel_mapping = type_from_attrs(&input.attrs, "DieselType");
let new_diesel_mapping = val_from_attrs(&input.attrs, "DieselType");
if existing_mapping_path.is_some() && new_diesel_mapping.is_some() {
panic!("Cannot specify both `ExistingTypePath` and `DieselType` attributes");
}
Expand All @@ -61,7 +62,7 @@ pub fn derive(input: TokenStream) -> TokenStream {

// Maintain backwards compatibility by defaulting to snake case.
let case_style =
type_from_attrs(&input.attrs, "DbValueStyle").unwrap_or_else(|| "snake_case".to_string());
val_from_attrs(&input.attrs, "DbValueStyle").unwrap_or_else(|| "snake_case".to_string());
let case_style = CaseStyle::from_string(&case_style);

let existing_mapping_path = existing_mapping_path.map(|v| {
Expand Down Expand Up @@ -92,15 +93,18 @@ pub fn derive(input: TokenStream) -> TokenStream {
}
}

fn type_from_attrs(attrs: &[Attribute], attrname: &str) -> Option<String> {
fn val_from_attrs(attrs: &[Attribute], attrname: &str) -> Option<String> {
for attr in attrs {
if attr.path.is_ident(attrname) {
match attr.parse_meta().ok()? {
Meta::NameValue(MetaNameValue {
lit: Lit::Str(lit_str),
..
}) => return Some(lit_str.value()),
_ => return None,
_ => panic!(
"Attribute '{}' must have form: {} = \"value\"",
attrname, attrname
),
}
}
}
Expand Down Expand Up @@ -160,7 +164,7 @@ fn generate_derive_enum_impls(
let variants_db: Vec<String> = variants
.iter()
.map(|variant| {
type_from_attrs(&variant.attrs, "db_rename")
val_from_attrs(&variant.attrs, "db_rename")
.unwrap_or_else(|| stylize_value(&variant.ident.to_string(), case_style))
})
.collect();
Expand Down
22 changes: 1 addition & 21 deletions tests/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
use diesel::prelude::*;

use diesel_derive_enum::DbEnum;
use diesel::connection::SimpleConnection;

#[derive(Debug, PartialEq, DbEnum, Clone)]
#[cfg_attr(feature = "postgres", DieselTypePath = "MyEnumPgMapping")]
pub enum MyEnum {
Foo,
Bar,
BazQuxx,
}

#[cfg(feature = "postgres")]
#[derive(diesel::sql_types::SqlType, diesel::query_builder::QueryId)]
#[diesel(postgres_type(name = "my_enum"))]
pub struct MyEnumPgMapping;

#[cfg(feature = "postgres")]
table! {
use diesel::sql_types::Integer;
use super::MyEnumPgMapping;
test_simple {
id -> Integer,
my_enum -> MyEnumPgMapping,
}
}

#[cfg(any(feature = "mysql", feature = "sqlite"))]
table! {
use diesel::sql_types::Integer;
use super::MyEnumMapping;
Expand All @@ -44,7 +28,6 @@ pub struct Simple {

#[cfg(feature = "postgres")]
pub fn get_connection() -> PgConnection {
use diesel::connection::SimpleConnection;
let database_url =
::std::env::var("PG_TEST_DATABASE_URL").expect("Env var PG_TEST_DATABASE_URL not set");
let mut conn = PgConnection::establish(&database_url)
Expand Down Expand Up @@ -95,7 +78,6 @@ pub fn sample_data() -> Vec<Simple> {

#[cfg(feature = "postgres")]
pub fn create_table(conn: &mut PgConnection) {
use diesel::connection::SimpleConnection;
conn.batch_execute(
r#"
CREATE TYPE my_enum AS ENUM ('foo', 'bar', 'baz_quxx');
Expand All @@ -110,7 +92,6 @@ pub fn create_table(conn: &mut PgConnection) {

#[cfg(feature = "mysql")]
pub fn create_table(conn: &mut MysqlConnection) {
use diesel::connection::SimpleConnection;
conn.batch_execute(
r#"
CREATE TEMPORARY TABLE IF NOT EXISTS test_simple (
Expand All @@ -124,7 +105,6 @@ pub fn create_table(conn: &mut MysqlConnection) {

#[cfg(feature = "sqlite")]
pub fn create_table(conn: &mut SqliteConnection) {
use diesel::connection::SimpleConnection;
conn.batch_execute(
r#"
CREATE TABLE test_simple (
Expand Down
22 changes: 1 addition & 21 deletions tests/src/complex_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ table! {
}
}

#[derive(diesel::sql_types::SqlType, diesel::query_builder::QueryId)]
#[diesel(postgres_type(name = "server_status"))]
pub struct Server_status_pg;

#[cfg(feature = "postgres")]
table! {
use diesel::sql_types::*;
use super::Server_status_pg;
servers (id) {
id -> Integer,
user_id -> Integer,
status -> Server_status_pg,
}
}

#[cfg(not(feature = "postgres"))]
table! {
use diesel::sql_types::*;
use super::Server_status;
Expand All @@ -38,11 +22,7 @@ joinable!(servers -> users (user_id));
allow_tables_to_appear_in_same_query!(users, servers);

#[derive(diesel_derive_enum::DbEnum, Clone, Debug, PartialEq)]
#[cfg_attr(
any(feature = "mysql", feature = "sqlite"),
DieselType = "Server_status"
)]
#[cfg_attr(feature = "postgres", DieselTypePath = "Server_status_pg")]
#[DieselType = "Server_status"]
enum ServerStatus {
Started,
Stopped,
Expand Down
3 changes: 2 additions & 1 deletion tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod complex_join;
mod nullable;
#[cfg(feature = "postgres")]
mod pg_array;
mod rename;
#[cfg(feature = "postgres")]
mod pg_remote_type;
mod simple;
mod value_style;
28 changes: 9 additions & 19 deletions tests/src/nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,6 @@ use diesel::prelude::*;

use crate::common::*;

#[cfg(feature = "postgres")]
table! {
use diesel::sql_types::{Integer, Nullable};
use super::MyEnumPgMapping;
test_nullable {
id -> Integer,
my_enum -> Nullable<MyEnumPgMapping>,
}
}
#[cfg(not(feature = "postgres"))]
table! {
use diesel::sql_types::{Integer, Nullable};
use super::MyEnumMapping;
test_nullable {
id -> Integer,
my_enum -> Nullable<MyEnumMapping>,
}
}

#[derive(Insertable, Queryable, Identifiable, Debug, PartialEq)]
#[diesel(table_name = test_nullable)]
struct Nullable {
Expand All @@ -36,6 +17,15 @@ struct MaybeNullable {
my_enum: MyEnum,
}

table! {
use diesel::sql_types::{Integer, Nullable};
use super::MyEnumMapping;
test_nullable {
id -> Integer,
my_enum -> Nullable<MyEnumMapping>,
}
}

#[cfg(feature = "postgres")]
pub fn create_null_table(conn: &mut PgConnection) {
use diesel::connection::SimpleConnection;
Expand Down
4 changes: 2 additions & 2 deletions tests/src/pg_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ fn enum_query() {

table! {
use diesel::sql_types::{Integer, Array};
use super::MyEnumPgMapping;
use super::MyEnumMapping;
test_array {
id -> Integer,
my_enum_arr -> Array<MyEnumPgMapping>,
my_enum_arr -> Array<MyEnumMapping>,
}
}

Expand Down
20 changes: 1 addition & 19 deletions tests/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ use diesel::prelude::*;
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::common::get_connection;

#[derive(diesel::sql_types::SqlType, diesel::query_builder::QueryId)]
#[diesel(postgres_type(name = "Some_External_Type"))]
pub struct Some_Internal_Type_Pg;

#[derive(Debug, PartialEq, diesel_derive_enum::DbEnum)]
#[cfg_attr(
any(feature = "mysql", feature = "sqlite"),
DieselType = "Some_Internal_Type"
)]
#[cfg_attr(feature = "postgres", DieselTypePath = "Some_Internal_Type_Pg")]
#[DieselType = "Some_Internal_Type"]
pub enum SomeEnum {
#[db_rename = "mod"]
Mod,
Expand All @@ -22,16 +14,6 @@ pub enum SomeEnum {
WithASpace,
}

#[cfg(feature = "postgres")]
table! {
use diesel::sql_types::Integer;
use super::Some_Internal_Type_Pg;
test_rename {
id -> Integer,
renamed -> Some_Internal_Type_Pg,
}
}
#[cfg(any(feature = "mysql", feature = "sqlite"))]
table! {
use diesel::sql_types::Integer;
use super::Some_Internal_Type;
Expand Down
14 changes: 0 additions & 14 deletions tests/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,12 @@ fn sqlite_invalid_enum() {
// (but we won't actually bother round-tripping)

#[derive(Debug, PartialEq, diesel_derive_enum::DbEnum)]
#[cfg_attr(feature = "postgres", DieselTypePath = "MyEnumPgMapping")]
pub enum my_enum {
foo,
bar,
bazQuxx,
}

#[derive(diesel::sql_types::SqlType, diesel::query_builder::QueryId)]
#[diesel(postgres_type(name = "my_enum"))]
pub struct MyEnumPgMapping;
#[cfg(feature = "postgres")]
table! {
use diesel::sql_types::Integer;
use super::MyEnumPgMapping;
test_snakey {
id -> Integer,
my_enum -> MyEnumPgMapping,
}
}
#[cfg(not(feature = "postgres"))]
table! {
use diesel::sql_types::Integer;
use super::my_enumMapping;
Expand Down
19 changes: 1 addition & 18 deletions tests/src/value_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use diesel::prelude::*;
use crate::common::get_connection;

#[derive(Debug, PartialEq, diesel_derive_enum::DbEnum)]
#[cfg_attr(
any(feature = "mysql", feature = "sqlite"),
DieselType = "Stylized_Internal_Type"
)]
#[cfg_attr(feature = "postgres", DieselTypePath = "Stylized_Internal_Type_Pg")]
#[DieselType = "Stylized_Internal_Type"]
#[DbValueStyle = "PascalCase"]
pub enum StylizedEnum {
FirstVariant,
Expand All @@ -19,19 +15,6 @@ pub enum StylizedEnum {
cRaZy_FiFtH,
}

#[derive(diesel::sql_types::SqlType, diesel::query_builder::QueryId)]
#[diesel(postgres_type(name = "Stylized_External_Type"))]
pub struct Stylized_Internal_Type_Pg;
#[cfg(feature = "postgres")]
table! {
use diesel::sql_types::Integer;
use super::Stylized_Internal_Type_Pg;
test_value_style {
id -> Integer,
value -> Stylized_Internal_Type_Pg,
}
}
#[cfg(any(feature = "mysql", feature = "sqlite"))]
table! {
use diesel::sql_types::Integer;
use super::Stylized_Internal_Type;
Expand Down

0 comments on commit 023babb

Please sign in to comment.