Skip to content

Conversation

@tuesmiddt
Copy link
Contributor

@tuesmiddt tuesmiddt commented Jul 8, 2018

Fixes #98
Fixes #58

  • Remove some unnecessary helper methods
  • Refactor assessments methods to use only 1 db query where possible
  • Refactored some tests

@coveralls
Copy link

coveralls commented Jul 8, 2018

Pull Request Test Coverage Report for Build 943

  • 8 of 8 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 99.413%

Totals Coverage Status
Change from base Build 932: -0.02%
Covered Lines: 508
Relevant Lines: 511

💛 - Coveralls

Copy link
Member

@indocomsoft indocomsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, just some styling issues

Repo.all(Assessment)
end

def all_assessments(assessment_type) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should wait until my PR #101 is merged cos I added max_xp virtual field as well as query to fetch that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure lets do that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you wanna add the max_xp field here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh i'm thinking of removing these methods. nothing's using them right now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha yes. I don't think anyone would miss it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm thinking if it's possible to refactor Assessments context such that we can remove the assessments_test.exs altogether and getting coverage just from the controller tests. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do away with a lot of them but we still need the functions that build questions/assessments

def all_assessments(assessment_type) do
Repo.all(from(a in Assessment, where: a.type == ^assessment_type))
Assessment
|> where([a], a.type == ^assessment_type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|> where(type: ^assessment_type)

# TODO: Refactor to be done on SQL instead of in-memory
Enum.filter(assessment_with_type, &(&1.is_published and Timex.before?(&1.open_at, now)))
Assessment
|> where([a], a.is_published)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing, use the shorter where wherever possible

Enum.filter(assessment_with_type, &(&1.is_published and Timex.before?(&1.open_at, now)))
Assessment
|> where([a], a.is_published)
|> where([a], a.open_at <= from_now(1, "second"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not 0 second lol

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump ^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh just wanted to give a little buffer, although I guess it makes no difference either way

end

def create_question_for_assessment(params, assessment_id)
when is_binary(assessment_id) or is_number(assessment_id) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, should be is_integer(assessment_id) instead of is_number

assessment =
Assessment
|> where([assessment], assessment.id == ^assessment_id)
|> join(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferable to use one letter bindings, i.e.
|> join(:left, [a], q in Question, q.assessment_id == a.id)

questions = assessment.questions
assessment =
Assessment
|> where([assessment], assessment.id == ^assessment_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the short where

result = Enum.map(Assessments.all_assessments(), & &1.id)
assert result == assessments
end
alias Cadet.Assessments.{Question, Assessment, AssessmentType}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OCD but prefer alphabetical order {Assessment, AssessmentType, Question}

@tuesmiddt
Copy link
Contributor Author

Made amendments as requested. Will wait on your controller before checking and merging again.

@indocomsoft
Copy link
Member

Lel might wanna rebase/merge this on/with `master cos I'm pretty sure there'll be quite a number of conflicts @tuesmiddt

Copy link
Member

@indocomsoft indocomsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some refactor might be required.

Also wondering if we should do from_now(0, "second") instead of 1

Repo.all(Assessment)
end

def all_assessments(assessment_type) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you wanna add the max_xp field here?

assessment_with_type = Repo.all(from(a in Assessment, where: a.type == ^assessment_type))
# TODO: Refactor to be done on SQL instead of in-memory
Enum.filter(assessment_with_type, &(&1.is_published and Timex.before?(&1.open_at, now)))
Assessment
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing w.r.t. max_xp field

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm thinking of removing this method since assessments_controller is implemented and doesn't use it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I don't think anyone's gonna miss it.

Assessment
|> where(is_published: true)
|> where(type: ^assessment_type)
|> where([a], a.open_at <= from_now(1, "second"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not from_now(0, "second")? 😂

idem for the other from_now's

|> preload([a, q], questions: q)
|> Repo.one()

params_with_assessment_id = Map.put_new(params, :assessment_id, assessment.id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No check if assessment == nil? Might be better to use with

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or refactor into 2 functions:

def create_question_for_assessment(params, assessment_id) when is_ecto_id(assessment_id)
def create_question_for_assessment(params, assessment = %Assessment{})

Copy link
Member

@indocomsoft indocomsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some small changes needed

simple_update(
Assessment,
id,
using: &Assessment.changeset/2,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[_, q] lel

def publish_assessment(id) do
simple_update(
Assessment,
id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|> join(:left, [a], q in assoc(a, :questions))

Copy link
Member

@indocomsoft indocomsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, will merge once Travis passes

@indocomsoft indocomsoft merged commit 42391b9 into master Jul 16, 2018
@indocomsoft indocomsoft deleted the refactor-assessments-context branch July 16, 2018 13:52
indocomsoft pushed a commit that referenced this pull request Aug 17, 2018
* Refactor assessments context to execute single queries where possible

* Refactor some assessments tests

* Wups

* Respond to comments

* Remove some unused functions

* Fix `create_question_for_assessment`

* Minor fixes
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 this pull request may close these issues.

4 participants