Coder Social home page Coder Social logo

schema_assertions's Introduction

SchemaAssertions

ExUnit assertions for Ecto schemas.

Work in progress.

Installation

If available in Hex, the package can be installed by adding schema_assertions to your list of dependencies in mix.exs:

def deps do
  [
    {:schema_assertions, "~> 0.1"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/schema_assertions.

Other useful schema-related libraries

License

This code is available under the Apache 2.0 license. See also license. Based on code from Epi Viewpoint.

schema_assertions's People

Contributors

cbortz avatar eahanson avatar sax avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

cbortz merihkepek

schema_assertions's Issues

doesn't allow asserting belongs to with custom source

Given a schema:

defmodule Group do
  schema "groups" do
    belongs_to :org, Org, source: :organization_id
  end
end

The following test will fail:

test "schema" do
  assert_belongs_to Group, :org, Org
end

...with the message:

 Expected Group to belong to
   :org
 via
   .Org

 org_id does not exist in table

This is true that org_id is not a field. It's an alias for organization_id, but it would be nice if I could specify a source in the assertion.


I did some sleuthing and looked at the schema reflection and saw that the :owner_key is set to :org_id by Ecto. It doesn't seem like a bug because everything works as you would expect in the actual code.

However that reflection doesn't give us access to the source. I did find the reflection :field_source which looks something like:

Group.__schema__(:field_source, :org_id) # returns :organization_id

Checking the reflection of the field source for :org_id gives us the value :organization_id which is exactly what we're looking for. I wonder if we could leverage this in connection with an optional :source option in the assert_belongs_to call such that when it is provided we check for a field source alias here:

%Ecto.Association.BelongsTo{queryable: ^assoc_module, owner_key: owner_key} ->
fieldset = module.__schema__(:source) |> SchemaAssertions.Database.fieldset()
if Keyword.has_key?(fieldset, owner_key),
do: :ok,
else: {:error, "#{owner_key} does not exist in table"}

What do y'all think? If you like this idea, I can open a PR for it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.