Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing joins with MockDatabase #447

Closed
billy1624 opened this issue Jan 13, 2022 · 4 comments · Fixed by #455
Closed

Testing joins with MockDatabase #447

billy1624 opened this issue Jan 13, 2022 · 4 comments · Fixed by #455
Milestone

Comments

@billy1624
Copy link
Member

billy1624 commented Jan 13, 2022

Is it possible to somehow have a function that generates such a map given two Entity structs? Like iterating all columns adding A_ to the first and B_ to the second

Originally posted by @cemoktra in #443 (reply in thread)


Just as a proposal:

fn entity_to_prefixed_btreemap<T>(model: T::Model, prefix: &str) -> BTreeMap<String, sea_orm::Value>
    where
        T: EntityTrait,
{
    let mut mapped = BTreeMap::new();
    for column in T::Column::iter() {
        mapped.insert(format!("{}{}", prefix, column.as_str()), model.get(column));
    }
    mapped
}

and then:

fn mock_join<T1, T2>(model1: T1::Model, model2: T2::Model) -> BTreeMap<String, sea_orm::Value>
    where
        T1: EntityTrait,
        T2: EntityTrait,        
{
    let mut map = entity_to_prefixed_btreemap::<T1>(model1, "A_");
    map.extend(entity_to_prefixed_btreemap::<T2>(model2, "B_"));
    map
}

Originally posted by @cemoktra in #443 (reply in thread)

@cemoktra
Copy link
Contributor

cemoktra commented Jan 13, 2022

Note that this BTreeMap has owned String as key, so it might be also useful to implement IntoMockRow for BTreeMap<String, Value>.

And the above can be optimized to be generic only over ModelTrait

But maybe an alternative would be to have IntoMockRow implemented for (Model1, Model2) as external interface:

fn into_mock_row(self) -> sea_orm::MockRow {
    mock_join::<M1::Entity, M2::Entity>(self.0, self.1)
}

@cemoktra
Copy link
Contributor

Putting it all together:

pub struct MockJoin<M1, M2>(M1, M2);

impl<M1, M2> IntoMockRow for MockJoin<M1, M2>
where
    M1: ModelTrait,
    M2: ModelTrait,
{
    fn into_mock_row(self) -> sea_orm::MockRow {
        let mut mapped_join = BTreeMap::new();

        for column in <<M1 as sea_orm::ModelTrait>::Entity as EntityTrait>::Column::iter() {
            mapped_join.insert(format!("A_{}", column.as_str()), self.0.get(column));
        }
        for column in <<M2 as sea_orm::ModelTrait>::Entity as EntityTrait>::Column::iter() {
            mapped_join.insert(format!("B_{}", column.as_str()), self.1.get(column));
        }

        mapped_join
            .iter()
            .map(|(key, value)| (key.as_str(), value.to_owned()))
            .collect::<BTreeMap<&str, sea_orm::Value>>()
            .into_mock_row()
    }
}

@billy1624
Copy link
Member Author

And perhaps...

impl<M, N> IntoMockRow for (M, N)
where
    M: ModelTrait,
    N: ModelTrait,
{
    fn into_mock_row(self) -> MockRow { ... }
}
impl IntoMockRow for (BTreeMap<&str, Value>, BTreeMap<&str, Value>) {
    fn into_mock_row(self) -> MockRow { ... }
}

@cemoktra
Copy link
Contributor

Yeah exactly

@billy1624 billy1624 linked a pull request Jan 21, 2022 that will close this issue
@billy1624 billy1624 added this to the 0.6.x milestone Jan 25, 2022
tyt2y3 added a commit that referenced this issue Feb 6, 2022
* Easy joins with MockDatabase #447

* fix MR

* add unit test

* Add test cases

* Cargo fmt

Co-authored-by: Bastian Schubert <[email protected]>
Co-authored-by: Billy Chan <[email protected]>
Co-authored-by: Chris Tsang <[email protected]>
tyt2y3 added a commit that referenced this issue Feb 6, 2022
Easy joins with MockDatabase #447 (#455)
tyt2y3 added a commit that referenced this issue Mar 6, 2022
* Add Poem example

* Name conflict of foreign key constraints when two entities have more than one foreign keys (#417)

* fix: name conflict of foreign key constraints when two entities have more than one fk

* test: update test case's foreign keys

* feat: override default name of foreign key constraint

* Add `max_lifetime` connection option

* Remove `r#` prefix from column name when field has a raw identifier

* Fix FromQueryResult when Result is redefined

Macros should only ever use absolute import paths.
Shorthands can clash (as they have in my case).

* Support the use of chrono::DateTime<Utc> in sea-orm

Add documentation for this

Temporarily use a fork to include new Sea-query code

Add tests for DateTimeUtc

Solve Github actions error by add the code to create a table

Assertion accuracy tests

Rectify incorrect format

* Refactoring

`ColumnDef` with default value

Cargo fmt

Update docs

Fixup

Support `DateTimeLocal`

Update docs

Codegen write db timestamp column as `DateTimeUtc` type

Update dependency

Merge branch 'sea-query/add-new-column-ref' into pr/429

feat: apply alias on `ColumnRef::SchemaTableColumn`

* Update sea-query dependency

* Fully quantity `std::result::Result` in proc_maros

* Migration (#335)

* Refactor `ConnectionTrait`

* Refactoring

* Build index & foreign key statements

* Fix imports

* Fixup

* Rocket example with migration

* async-std compatible with the tokio 1.0 runtime

* Use reexported dependency

* Compile without selecting any db backend

* Updating sea-orm-cli dep

* sea-orm-cli migrate commands

* cargo fmt

* Test [cli]

* Refactoring

* Clap app name should be "sea-orm-cli"

* Correctly capture MIGRATION_DIR

* Rename README

* Add `sea-orm-cli migrate init` command

* Update README

* Try restructured sea-query dependency (SeaQL/sea-schema#41)

* Set `DATABASE_URL` environment variable

* Fix sea-schema dependency

* Codegen `column_name` proc_macro attribute (#433)

* feat: codegen `column_name` proc_macro attribute

* test: codegen `column_name`

* Temporary fix

* Fix sea-orm dependency

* Insert Default - Inserting `ActiveModel` with all attributes `NotSet` (#432)

* feat: apply alias on `ColumnRef::SchemaTableColumn`

* build: update sea-query dependency

* feat: insert default

* Use sea-query 0.21

Co-authored-by: Billy Chan <[email protected]>

* Easy joins with MockDatabase #447 (#455)

* Easy joins with MockDatabase #447

* fix MR

* add unit test

* Add test cases

* Cargo fmt

Co-authored-by: Bastian Schubert <[email protected]>
Co-authored-by: Billy Chan <[email protected]>
Co-authored-by: Chris Tsang <[email protected]>

* cargo fmt

* Revert "Insert Default - Inserting `ActiveModel` with all attributes `NotSet` (#432)"

This reverts commit 33a87d7.

* Prepare release

* sea-orm-codegen 0.6.0

* sea-orm-cli 0.6.0

* sea-orm-macros 0.6.0

* 0.6.0

* Optimize GitHub actions

* Update examples sea-orm version

Update example sea-schema version

Update [cli] sea-schema version

* Fix [cli] cargo publish failed

* Update CHANGELOG

* Use sea-schema 0.5.1

* use sea_schema::migration::prelude

* Use caret requirements

* Fix build error

* Examples with migration (#509)

* Update examples sea-orm version

* Update example sea-schema version

* Update [cli] sea-schema version

* Fix [cli] cargo publish failed

* Update CHANGELOG

* Edit rocket example

* Poem example with migration

* Axum example with migration

* Refactoring

* Actix4 example with migration

* Actix example with migration

* Use sea_schema::migration::prelude

Co-authored-by: Sunli <[email protected]>
Co-authored-by: Billy Chan <[email protected]>
Co-authored-by: Billy Chan <[email protected]>
Co-authored-by: Szepesi Tibor <[email protected]>
Co-authored-by: Tom Hacohen <[email protected]>
Co-authored-by: Charles Chege <[email protected]>
Co-authored-by: Bastian <[email protected]>
Co-authored-by: Bastian Schubert <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants