fix market history overriding

pull/2311/head
Ayrat Badykov 5 years ago
parent 2a9b34b70e
commit 32243b0c4e
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 19
      apps/explorer/lib/explorer/market/history/source/crypto_compare.ex
  2. 7
      apps/explorer/lib/explorer/market/market.ex
  3. 54
      apps/explorer/test/explorer/market/history/source/crypto_compare_test.exs
  4. 19
      apps/explorer/test/explorer/market/market_test.exs

@ -48,13 +48,18 @@ defmodule Explorer.Market.History.Source.CryptoCompare do
defp format_data(data) do defp format_data(data) do
json = Jason.decode!(data) json = Jason.decode!(data)
for item <- json["Data"] do items =
%{ for item <- json["Data"] do
closing_price: Decimal.new(to_string(item["close"])), %{
date: date(item["time"]), closing_price: Decimal.new(to_string(item["close"])),
opening_price: Decimal.new(to_string(item["open"])) date: date(item["time"]),
} opening_price: Decimal.new(to_string(item["open"]))
end }
end
Enum.reject(items, fn item ->
Decimal.equal?(item.closing_price, 0) && Decimal.equal?(item.opening_price, 0)
end)
end end
@spec history_url(non_neg_integer()) :: String.t() @spec history_url(non_neg_integer()) :: String.t()

@ -40,7 +40,12 @@ defmodule Explorer.Market do
@doc false @doc false
def bulk_insert_history(records) do def bulk_insert_history(records) do
Repo.insert_all(MarketHistory, records, on_conflict: :replace_all, conflict_target: [:date]) records_without_zeroes =
Enum.reject(records, fn item ->
Decimal.equal?(item.closing_price, 0) && Decimal.equal?(item.opening_price, 0)
end)
Repo.insert_all(MarketHistory, records_without_zeroes, on_conflict: :replace_all, conflict_target: [:date])
end end
def add_price(%{symbol: symbol} = token) do def add_price(%{symbol: symbol} = token) do

@ -86,5 +86,59 @@ defmodule Explorer.Market.History.Source.CryptoCompareTest do
assert :error == CryptoCompare.fetch_history(3) assert :error == CryptoCompare.fetch_history(3)
end end
test "rejects empty prices", %{bypass: bypass} do
json = """
{
"Response": "Success",
"Type": 100,
"Aggregated": false,
"Data": [
{
"time": 1524528000,
"close": 0,
"high": 9741.91,
"low": 8957.68,
"open": 0,
"volumefrom": 136352.05,
"volumeto": 1276464750.74
},
{
"time": 1524614400,
"close": 0,
"high": 9765.23,
"low": 8757.06,
"open": 0,
"volumefrom": 192797.41,
"volumeto": 1779806222.98
},
{
"time": 1524700800,
"close": 8804.32,
"high": 8965.84,
"low": 8669.38,
"open": 8873.57,
"volumefrom": 74704.5,
"volumeto": 661168891
}
],
"TimeTo": 1524700800,
"TimeFrom": 1523836800,
"FirstValueInArray": true,
"ConversionType": {
"type": "direct",
"conversionSymbol": ""
}
}
"""
Bypass.expect(bypass, fn conn -> Conn.resp(conn, 200, json) end)
expected = [
%{closing_price: Decimal.from_float(8804.32), date: ~D[2018-04-26], opening_price: Decimal.from_float(8873.57)}
]
assert {:ok, expected} == CryptoCompare.fetch_history(3)
end
end end
end end

@ -72,6 +72,25 @@ defmodule Explorer.MarketTest do
assert missing_records == %{} assert missing_records == %{}
end end
test "doesn't replace existing records with zeros" do
date = ~D[2018-04-01]
{:ok, old_record} =
Repo.insert(%MarketHistory{date: date, closing_price: Decimal.new(1), opening_price: Decimal.new(1)})
new_record = %{
date: date,
closing_price: Decimal.new(0),
opening_price: Decimal.new(0)
}
Market.bulk_insert_history([new_record])
fetched_record = Repo.get_by(MarketHistory, date: date)
assert fetched_record.closing_price == old_record.closing_price
assert fetched_record.opening_price == old_record.opening_price
end
test "overrides existing records on date conflict" do test "overrides existing records on date conflict" do
date = ~D[2018-04-01] date = ~D[2018-04-01]
Repo.insert(%MarketHistory{date: date}) Repo.insert(%MarketHistory{date: date})

Loading…
Cancel
Save