Skip to content

Commit

Permalink
Use the circuits_sim to add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fhunleth committed Jul 18, 2023
1 parent 6c879a8 commit 080e221
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
10 changes: 10 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Config

# Simulate an SHT4X on i2c-1
config :circuits_sim,
config: [
{CircuitsSim.Device.SHT4X, bus_name: "i2c-1", address: 0x44, serial_number: 0x87654321}
]

# Enable simulated I2C as the default
config :circuits_i2c, default_backend: CircuitsSim.I2C.Backend
7 changes: 5 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ defmodule SHT4X.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:circuits_i2c, "~> 1.0"},
{:circuits_i2c, "~> 1.0 or ~> 2.0.0-pre.0"},
{:circuits_sim,
github: "elixir-circuits/circuits_sim",
sha: "d2efb89312335e91c4c6f8da6e3f53cba2b21512",
only: [:dev, :test]},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.28", only: :docs, runtime: false},
{:mix_test_watch, "~> 1.1", only: :dev, runtime: false},
{:cerlc, "~> 0.2.0"},
{:typed_struct, "~> 0.3.0"}
]
Expand Down
8 changes: 5 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
%{
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"cerlc": {:hex, :cerlc, "0.2.1", "cfe0880aa049ebcca079ca49578055aa48e7f2e9ed8ae08bd1f919d59015d03f", [:rebar3], [], "hexpm", "37f0d74a4277dcbaf64c7c47e9953dcc8060d26d86bb466ab2a86928e0181a7d"},
"circuits_i2c": {:hex, :circuits_i2c, "1.2.2", "1666bf1763fe60ab835462d13f04ca75b0de000a7d5b89e24d8f35a06ef6d097", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "a7fffcb1bf128e4112b0734796f26b0b4f2359e4c60e86d9718c31834e10a343"},
"circuits_gpio": {:hex, :circuits_gpio, "2.0.0-pre.0", "122a76a90f49cc2e83483157012fb7eff194ad80787567a2a6c1bd5133b989a7", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "454449f11527ff70da428765f725ef84263f5d68919dbb830a4868315ed0823e"},
"circuits_i2c": {:hex, :circuits_i2c, "2.0.0-pre.0", "3f8d8bdf609f21197d6b3ab2156372971a523ab7a1f3173325e7307d0040cd50", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "3233b7ae7b11216437910576def0b6fb6d0e2c9cc2ae38a3572d6535ebcbd8d1"},
"circuits_sim": {:git, "https://github.com/elixir-circuits/circuits_sim.git", "d2efb89312335e91c4c6f8da6e3f53cba2b21512", []},
"circuits_spi": {:hex, :circuits_spi, "2.0.0-pre.0", "51c12522f2dec1c3219e93caa95cc12315adbafe895b4fa4f096daa69ca8f912", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "154a8a9295f14aed0c7302bfb819572ec40482de62cd33b840f5e2c0f9a005c6"},
"credo": {:hex, :credo, "1.7.0", "6119bee47272e85995598ee04f2ebbed3e947678dee048d10b5feca139435f75", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "6839fcf63d1f0d1c0f450abc8564a57c43d644077ab96f2934563e68b8a769d7"},
"dialyxir": {:hex, :dialyxir, "1.3.0", "fd1672f0922b7648ff9ce7b1b26fcf0ef56dda964a459892ad15f6b4410b5284", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "00b2a4bcd6aa8db9dcb0b38c1225b7277dca9bc370b6438715667071a304696f"},
"earmark_parser": {:hex, :earmark_parser, "1.4.33", "3c3fd9673bb5dcc9edc28dd90f50c87ce506d1f71b70e3de69aa8154bc695d44", [:mix], [], "hexpm", "2d526833729b59b9fdb85785078697c72ac5e5066350663e5be6a1182da61b8f"},
"elixir_make": {:hex, :elixir_make, "0.7.7", "7128c60c2476019ed978210c245badf08b03dbec4f24d05790ef791da11aa17c", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bc19fff950fad52bbe5f211b12db9ec82c6b34a9647da0c2224b8b8464c7e6c"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.30.2", "7a3e63ddb387746925bbbbcf6e9cb00e43c757cc60359a2b40059aea573e3e57", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "5ba8cb61d069012f16b50e575b0e3e6cf4083935f7444fab0d92c9314ce86bb6"},
"ex_doc": {:hex, :ex_doc, "0.30.3", "bfca4d340e3b95f2eb26e72e4890da83e2b3a5c5b0e52607333bf5017284b063", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "fbc8702046c1d25edf79de376297e608ac78cdc3a29f075484773ad1718918b6"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
"mix_test_watch": {:hex, :mix_test_watch, "1.1.0", "330bb91c8ed271fe408c42d07e0773340a7938d8a0d281d57a14243eae9dc8c3", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "52b6b1c476cbb70fd899ca5394506482f12e5f6b0d6acff9df95c7f1e0812ec3"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"typed_struct": {:hex, :typed_struct, "0.3.0", "939789e3c1dca39d7170c87f729127469d1315dcf99fee8e152bb774b17e7ff7", [:mix], [], "hexpm", "c50bd5c3a61fe4e198a8504f939be3d3c85903b382bde4865579bc23111d1b6d"},
}
37 changes: 37 additions & 0 deletions test/sht4x_test.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
defmodule SHT4XTest do
use ExUnit.Case
doctest SHT4X

alias CircuitsSim.Device.SHT4X, as: SHT4XSim

@i2c_bus "i2c-1"
@i2c_address 0x44

test "reading the temperature and humidity via the simulator" do
sht_pid = start_supervised!(SHT4X)

SHT4XSim.set_temperature_c(@i2c_bus, @i2c_address, 11.1)
SHT4XSim.set_humidity_rh(@i2c_bus, @i2c_address, 33.3)

measurement = SHT4X.get_sample(sht_pid)
assert_in_delta measurement.humidity_rh, 33.3, 0.1
assert_in_delta measurement.temperature_c, 11.1, 0.1
end

test "reading the simulated serial number" do
sht_pid = start_supervised!(SHT4X)

# See config.exs for where the serial number is set in the simulator
assert SHT4X.serial_number(sht_pid) == {:ok, 0x87654321}
end

defp add_degree(measurement) do
%{measurement | temperature_c: measurement.temperature_c + 1}
end

test "compensation callback gets called" do
sht_pid = start_supervised!({SHT4X, compensation_callback: &add_degree/1})

SHT4XSim.set_temperature_c(@i2c_bus, @i2c_address, 20)

measurement = SHT4X.get_sample(sht_pid)
assert_in_delta measurement.temperature_c, 21.0, 0.1
end
end

0 comments on commit 080e221

Please sign in to comment.