unlogic

unlogic

I have a Phoenix application that is provides a standard REST API. I currently have two models and am looking at unit-testing. One of my controller’s unit tests is passing fine, but the other is showing some off behaviour in the update and create tests.

  • Update with valid data isn’t updating the data and returns {"errors":{"comment":["can't be blank"],"control":["can't be blank"],"value":["can't be blank"]}}
  • Update with invalid data isn’t erroring and returning the entry
  • Create with valid data is doing what the invalid case should do and returning 422 with {"errors":{"comment":["can't be blank"],"control":["can't be blank"],"value":["can't be blank"]}}

These are generated tests, as are those for the other controller which are working fine. I’d be OK with this if the controller wasn’t working as expected, but when running the queries manually from a REST client, the application behaves as expected in all three cases.

Here’s the

Note that I have dropped the ["data"] part of the response. This is the case for the other controller too, where the tests are passing fine.

Showing Posts 1 to 5

al2o3cr

al2o3cr

Can you post the controller code? The link labeled “Controller” above is actually a schema module.

I have zero evidence to support this, but my suspicion is that the controller doesn’t agree with the test about what key to use in the params.

unlogic

unlogic OP

Sorry, that was the wrong paste

So if I understand your comment correctly, it’s not using the correct primary key? I’m just very confused about why these three test cases aren’t working as expected, but they are the same as the other model/controller I have set up.

thanks for the help so far though

al2o3cr

al2o3cr

  def create(conn, job_control_params) do

Try dumping out what you’re getting in job_control_params here when you run the tests. Does it match what JobControls.create_job_control/1 expects?

I don’t believe it will: create_job_control is called from fixture(:job_control) with parameters shaped like

%{comment: ..., control: ..., value: ...}

but in the tests this code:

    test "renders job_control when data is valid", %{conn: conn} do
      conn = post(conn, Routes.job_control_path(conn, :create), job_control: @create_attrs)

will pass (unless use FarmqWeb, :controller is doing something nonstandard) parameters shaped like:

%{"job_control" => %{"comment" => ..., "control" => ..., "value" => ...}}

cast will ignore parameters it doesn’t understand, so it will filter out ALL the input here.

A typical idiom in controller actions looks like:

  def create(conn, %{"job_control" => job_control_params}) do
    with {:ok, %JobControl{} = job_control} <- JobControls.create_job_control(job_control_params) do

...


  def update(conn, %{"id" => id, "job_control" => job_control_params}) do
    job_control = JobControls.get_job_control!(id)

    with {:ok, %JobControl{} = job_control} <- JobControls.update_job_control(job_control, job_control_params) do
unlogic

unlogic OP

I forgot to mention that I updated the original comment with the controller code. The controller code is the same for this controller as well as the other one. I also think that I didn’t edit the create function either.

So I think that the update function is as expected. I printed the contents of job_control_params and it’s what I’d expect it to be

I’d be less confused if my other controller/tests didn’t pass and if using the API via a client wasn’t working as expected

unlogic

unlogic OP

Found the issue. You were absolutely correct in your assumption about the controller being odd. For whatever reason, in the erroring controller I had

def update(conn, job_control_params) do
instead of
def update(conn, %{"job_control" => job_control_params}) do

Same for the create function. Not sure why it was like this but I’ve fixed it now and the tests pass.

Thanks for the help!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New
adr
Hello all. I have a question as to best practices for code organization when implementing a liveview with a lot of responsibilities. Sup...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews