Refactoring and adding tests for Phoenix - Part 10

Phoenix Elixir stutorial

Other articles in the series

  • Making our UI user friendly - Part 15
  • Deploying Phoenix VueJS application using Docker - Part 14
  • Fixing failing Elixir tests - Part 13
  • Adding new features to Order Management - Part 12
  • Add Order Management - Part 11
  • Refactoring and adding tests for Phoenix - Part 10
  • Refactoring VueJS with Typescript- Part 9
  • Add Customer Management - Part 8
  • Writing tests with Jest and Typescript - Part 7
  • Adding Vuex to Vue using Typescript - Part 6
  • Building our Homepage view component - Part 5
  • Add Multi-language support to Vue Typescript - Part 4
  • Generate Vue Typescript Application - Part 3
  • Setting up Models with Ecto and Adding Routing . Part 2
  • Setting up Your Phoenix Application - Part 1
  • Tutorial Series for building a VueJS (Typescript) and Phoenix(Elixir) Shop Management Application - Part 0

  • The back-end related code for this post is available here.

    In this post, we will refactor our Phoenix codebase and fix tests. So our step would be to adopt a consistent naming for database tables and column names. Currently we have camel case and snake case for our table column names. We will adopt the snake case style for our column names. The first step to achieve this would be to create a migration and then rename the required columns in that migration.

    mix ecto.gen.migration rename_fields_snake_case

    This will create a file in the priv/repo/migrations/ folder. Lets make the following changes there.

    defmodule Ms.Repo.Migrations.RenameFieldsSnakeCase do
      use Ecto.Migration
      def change do
        rename table("orders"), :creationDate, to: :creation_date
        rename table("order_items"), :unitPrice, to: :unit_price
        rename table("deliveries"), :orderitem, to: :order_item

    The change function will be executed when we apply the migration. The changes are self-explanatory. Now we need to rename all occurrences of unitPrices to unit_price and so on in all files. This is a simple search and replace operation. This results in changes in schema files inside ms/ directory. For example the ms/order_management/order_item.ex file becomes

    defmodule Ms.OrderManagement.OrderItem do
      use Ecto.Schema
      import Ecto.Changeset
      schema "order_items" do
        field :amount, :integer
        field :unit_price, :float
        field :product, :id
        field :order, :id
      @doc false
      def changeset(order_item, attrs) do
        |> cast(attrs, [:amount, :unit_price])
        |> validate_required([:amount, :unit_price])

    This will also change some tests. But by running tests using,

    MIX_ENV=test mix test

    we can confirm that all tests are passing. MIX_ENV is an environment telling mix about the running context. For example to drop the test database from ecto, we can use

    MIX_ENV=test mix ecto.drop

    If we set MIX_ENV=prod, it will drop the production database.

    Now lets fix another inconsistency in our naming conventions. All management modules like customer_management, order_management end with an _management, except inventory. In order to ensure consistency we will rename inventory to inventory_management. This is also pretty simple by replacing all occurrences of Inventory with InventoryManagement. Then rename inventory folder to inventory_management and we are done. Just like in previous case, it will also change some tests. By running tests again, we can confirm that things are working correctly.

    In next post, we will add order handling functionality.