Skip to content

Instantly share code, notes, and snippets.

View delbetu's full-sized avatar
🏠
Working from home

M. Bellucci delbetu

🏠
Working from home
View GitHub Profile
@delbetu
delbetu / db_nulls_check.rb
Last active March 29, 2024 14:47
Every attribute on a model with 'presence: true' shouldn't allow nulls on db. This script helps identify missing not null constraints. It looks for attributes with presence: true and checks these have NOT NULL on db/structure.sql
# frozen_string_literal: true
require "active_support/all"
def null_on_db?(table_name, col_name)
db_structure = File.read("db/structure.sql")
db_structure.match(/CREATE TABLE public.#{table_name} \((?<cols>.*?)\)/m) || {cols: ""} => { cols: attrs }
attrs.match?(/#{col_name}.*NOT NULL/)
end
@delbetu
delbetu / flaky_tests.md
Created January 6, 2023 17:09
Rails Flaky Tests Guide

Here are some of the things I've identified that can cause flaky tests.

Race conditions

A common type of race condition in tests is this: the test submits a form and then clicks a link on the subsequent page. BUT sometimes the page loads a little more slowly than at other times, and so the test driver clicks the link before the link actually shows up on the page. It's a race between the link showing up and the test trying to click the link. In the cases where the click "wins" the race, the test will fail because the link isn't available yet. Other kinds of race conditions exist too but that's a common case.

Leaked state

Let's imagine there's a test suite with just two tests in it, test A and test B. Both tests create a user in the database with the email address "[email protected]". But the difference between the two tests is that test A deletes the user at the end of the test, while test B leaves the user lying around in the database. If test A runs before test B then there's no problem because test A

@delbetu
delbetu / finance_lease_calculator_spec.rb
Last active May 4, 2020 19:01
Spec for finance lease
class LeaseLiability
attr_reader :year, :fixed_payment, :annual_increase, :discount_rate, :initial_direct_cost,
:cash_incentives
attr_accessor :beginning_balance
def initialize(
year:,
fixed_payment:,
annual_increase:,
discount_rate:,
@delbetu
delbetu / interseccion_arrays.rb
Last active February 21, 2020 22:13
Zype Questions
a = [1,2,3,4,5]
b = [2,4,5,9,8,7]
result = []
a.each do |ai|
result << b.find { |bj| bj == ai }
end
return result
Orden a.length * b.length
@delbetu
delbetu / Rails_Problem.md
Last active February 18, 2020 14:25
Pexels Qualification Test - Marcos Bellucci

Problem

Models

class Post < ApplicationRecord
    has_many :comments
end

class User < ApplicationRecord
  has_many :comments
end
@delbetu
delbetu / Docker.md
Last active November 28, 2019 19:39
Getting Dev.to up and running locally

Docker Cheat Sheet

docker

Create Dockerfile --> contiene el provisioning (todo lo que tiene que tener instalado) base image -> ruby:2.3.0 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

RUN mdkir /myapp WORKDIR /myapp

@delbetu
delbetu / wrapping_results.rb
Created September 19, 2019 14:50
Adding status to methods results
class ResultWrapper
attr_reader :value, :success?, :error_messages
def failure?
!success?
end
end
class SomeController
def create
result = process(params[:id])

Title

Development

  • Creating gems
  • Installing gems
  • Private repos - Gemfury
  • rbenv - manage installed ruby versions
  • bundler - manage gems based on gemfile

Ruby

  • Regexp
require 'thread'
params_for_job = Queue.new
workers = []
def perform_task(params_for_job)
while x = params_for_job.pop
print x + "\n"
end
return x
end
@delbetu
delbetu / mockingFetch.spec.js
Created July 7, 2019 16:02
Javascript Mocking fetch with jest
jest.mock('node-fetch');
import fetch, {Response} from 'node-fetch';
it('can mock fetch body', () => {
fetch.mockImplementation(() => {
return new Promise((resolve, reject)=> { resolve(
{
json: () => { return new Promise((resolve, reject) => resolve({ name: 'value' })) }
}
)})