SeaQL / SeaQL/sea-orm

Axum mock example doesn't work for unit tests

Open
#2,504 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Area:mock Category:enhancement
Dominant language
Rust
Stars
9.9k
Forks
735
Avg merge
6h 36m
Merged PRs (30d)
8

Description

Description

Due to https://github.com/SeaQL/sea-orm/issues/836 and https://github.com/SeaQL/sea-orm/issues/438, I see that you implemented https://github.com/SeaQL/sea-orm/pull/890 which is a rather hacky way to perform mock tests with (in my case) Axum, as also other users have already reported.

The examples provided only have integration tests, for which the hack works, however for unit tests, we would need to test every member of the workspace individually, as cargo will only compile one version of the sea orm crate.

Steps to Reproduce

Let's take for example, https://github.com/SeaQL/sea-orm/tree/75f7d4fa57aaeaeee88ca43dda99ed602cdcc930/examples/axum_example :

If I were to add to service/src/mutation.rs

@@ -51,3 +51,11 @@ impl Mutation {
         Post::delete_many().exec(db).await
     }
 }
+
+#[test]
+#[cfg(test)]
+fn test() {
+    use sea_orm::MockDatabase;
+
+    let db = MockDatabase::new(sea_orm::DatabaseBackend::MySql);
+}

and test with cargo test --all --features axum-example-service/mock we get the error

Expected Behavior

Test runs successfully

Actual Behavior
error[E0277]: the trait bound `DatabaseConnection: Clone` is not satisfied
  --> api/src/lib.rs:74:5
   |
71 | #[derive(Clone)]
   |          ----- in this derive macro expansion
...
74 |     conn: DatabaseConnection,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `DatabaseConnection`
   |
   = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
Reproduces How Often

Always reproducible

Workarounds

Test each package of a workspace individually or test all packages except the ones with mock and test these ones separately, which is not great

Reproducible Example

Example provided above as it is very simple

Versions

sea-orm-axum-example v0.1.0 (/tmp/sea-orm/examples/axum_example)
└── axum-example-api v0.1.0 (/tmp/sea-orm/examples/axum_example/api)
    ├── axum-example-service v0.1.0 (/tmp/sea-orm/examples/axum_example/service)
    │   ├── entity v0.1.0 (/tmp/sea-orm/examples/axum_example/entity)
    │   │   ├── sea-orm v1.1.5 (/tmp/sea-orm)
    │   │   │   ├── sea-orm-macros v1.1.5 (proc-macro) (/tmp/sea-orm/sea-orm-macros)
    │   │   │   │   ├── sea-bae v0.2.1 (proc-macro)
    │   │   │   ├── sea-query v0.32.2
    │   │   │   │   ├── sea-query-derive v0.4.2 (proc-macro)
    │   │   │   ├── sea-query-binder v0.7.0
    │   │   │   │   ├── sea-query v0.32.2 (*)
    │   └── sea-orm v1.1.5 (/tmp/sea-orm) (*)
    ├── entity v0.1.0 (/tmp/sea-orm/examples/axum_example/entity) (*)
    ├── migration v0.1.0 (/tmp/sea-orm/examples/axum_example/migration)
    │   └── sea-orm-migration v1.1.5 (/tmp/sea-orm/sea-orm-migration)
    │       ├── sea-orm v1.1.5 (/tmp/sea-orm) (*)
    │       ├── sea-orm-cli v1.1.5 (/tmp/sea-orm/sea-orm-cli)
    │       │   ├── sea-schema v0.16.1
    │       │   │   ├── sea-query v0.32.2 (*)
    │       │   │   └── sea-schema-derive v0.3.0 (proc-macro)
    │       ├── sea-schema v0.16.1 (*)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with examples/axum_example/service/src/mutation.rs and reproduce the failure using cargo test --all --features axum-example-service/mock. Compare the workspace package dependencies and the DatabaseConnection Clone error in api/src/lib.rs; done means the unit test compiles and runs successfully with the workspace test command.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
databases, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.