Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
uoy-faculty-spec-helpers / lib / faculty / fluent_module_helper.rb
Size: Mime:
# frozen_string_literal: true

require_relative 'database_helper'
require_relative 'fluent_helper'

module Faculty
  # Spec helper module for creating module records for spec testing
  module FluentModuleHelper
    # Class for tracking state of a fluent module specification
    class FluentModuleRecord < FluentHelper
      def initialize(**params)
        super()
        maybe_insert dbt.sits.department, code: params[:department_code] if params.key? :department_code
        @module = insert(dbt.sits.module_occurrence, params)
      end

      def with_period(**params)
        maybe_add_period(**period_defaults, **params)
        insert dbt.sits.module_period, params, **period_defaults
        self
      end

      private

      def maybe_add_period(**params)
        check_fields = { academic_year: params[:academic_year], period_code: params[:period_code] }
        extra_fields = { period_key: "#{params[:academic_year]}-#{params[:period_code]}",
                         period_dates: Sequel::Postgres::PGRange.from_range(Date.new(2022)..Date.new(2022, 2)) }
        insert(dbt.sits.period, check_fields.merge(extra_fields)) unless dbt.sits.period.include?(**check_fields)
      end

      def period_defaults
        { module_occurrence_id: @module[:module_occurrence_id], academic_year: @module[:start_academic_year],
          mav_key: @module[:mav_key], period_code: 'T1' }
      end
    end

    def sits_module(...)
      FluentModuleRecord.new(...)
    end
  end
end