More than you ever wanted to know
def greet(person: str) -> str:
return f"Hello, {person}!"
def test_greet_func():
person = "Elon"
assert greet(person) == "Hello, Elon!"
from datetime import datetime
class TimeProvider:
@staticmethod
def current_time():
return datetime.now()
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
def test_counter():
counter = Counter()
# first time
counter.increment()
assert counter.count == 1
# second time
counter.increment()
assert counter.count == 2
account = %Account{}
|> Account.changeset(%{username: "rand 12823"})
|> L3DB.Repo.insert!
result = %Device{}
|> Device.changeset(%{@valid_attrs | account_id: account.id})
|> L3DB.Repo.insert
assert {:ok, device} = result
assert Ecto.UUID.cast(device.uid) == {:ok, device.uid}
assert device.token =~ ~r([\w_-]{28})
daily_reward = %{
stack_value: %Cost{coins: 2},
interval: :timer.hours(1) |> div(:timer.seconds(1)),
max_stacks: 10}
# 0
date_ref = (now - div(daily_reward.interval, 2))
{timestamp, reward} = DailyReward.compute(daily_reward, date_ref)
assert timestamp == date_ref
assert reward == %{coins: 0}
# 3
date_ref = (now - daily_reward.interval * 3 - 100)
{timestamp, reward} = DailyReward.compute(daily_reward, date_ref)
assert_in_delta now - timestamp, 100, 2
assert reward == %{coins: 3 * daily_reward.stack_value.coins}
# > 10
date_ref = (now - daily_reward.interval * 11)
{timestamp, reward} = DailyReward.compute(daily_reward, date_ref)
assert_in_delta now - timestamp, 0, 2
assert reward == %{coins: daily_reward.max_stacks * daily_reward.stack_value.coins}
https://stackoverflow.com/questions/90002
https://www.youtube.com/watch?v=hXnS_Xjwk2Y