@@ -15,33 +15,67 @@ defmodule AHT20Test do
15
15
:ok
16
16
end
17
17
18
- test "start_link" do
19
- config = [ i2c_bus: "i2c-1" , i2c_address: 0x38 ]
20
- assert { :ok , _ } = AHT20 . start_link ( config )
21
- end
18
+ describe "without worker" do
19
+ test "start" do
20
+ config = [ i2c_bus: "i2c-1" , i2c_address: 0x38 ]
21
+ assert { :ok , _ } = AHT20 . start ( config )
22
+ end
23
+
24
+ test "read_data" do
25
+ AHT20.MockI2C
26
+ |> Mox . expect ( :read , 1 , fn _ref , _address , _data , [ ] -> { :ok , << 28 , 38 , 154 , 118 , 66 , 231 , 118 >> } end )
22
27
23
- test "read_data" do
24
- AHT20.MockI2C
25
- |> Mox . expect ( :read , 1 , fn _ref , _address , _data , [ ] -> { :ok , << 28 , 38 , 154 , 118 , 66 , 231 , 118 >> } end )
28
+ sensor = % AHT20.Sensor { i2c_address: 0x38 , i2c_bus: "i2c-1" , i2c_ref: make_ref ( ) }
29
+ assert { :ok , data } = AHT20 . read_data ( sensor )
26
30
27
- assert { :ok , _pid } = AHT20 . start_link ( i2c_bus: "i2c-1" , i2c_address: 0x38 )
28
- assert { :ok , data } = AHT20 . read_data ( )
31
+ assert data == % AHT20.Measurement {
32
+ raw_humidity: 158_119 ,
33
+ raw_temperature: 410_343 ,
34
+ relative_humidity: 15.079402923583984 ,
35
+ temperature_c: 28.26671600341797 ,
36
+ temperature_f: 82.88008880615234
37
+ }
38
+ end
29
39
30
- assert data == % AHT20.Measurement {
31
- raw_humidity: 158_119 ,
32
- raw_temperature: 410_343 ,
33
- relative_humidity: 15.079402923583984 ,
34
- temperature_c: 28.26671600341797 ,
35
- temperature_f: 82.88008880615234
36
- }
40
+ test "read_state" do
41
+ AHT20.MockI2C
42
+ |> Mox . expect ( :write_read , 1 , fn _ref , _address , _data , _bytes_to_read , [ ] -> { :ok , << 0b00011100 >> } end )
43
+
44
+ sensor = % AHT20.Sensor { i2c_address: 0x38 , i2c_bus: "i2c-1" , i2c_ref: make_ref ( ) }
45
+ assert { :ok , state } = AHT20 . read_state ( sensor )
46
+ assert state == % AHT20.State { busy: false , calibrated: true , mode: :nor }
47
+ end
37
48
end
38
49
39
- test "read_state" do
40
- AHT20.MockI2C
41
- |> Mox . expect ( :write_read , 1 , fn _ref , _address , _data , _bytes_to_read , [ ] -> { :ok , << 0b00011100 >> } end )
50
+ describe "with worker" do
51
+ test "start_link" do
52
+ config = [ i2c_bus: "i2c-1" , i2c_address: 0x38 ]
53
+ assert { :ok , _ } = AHT20 . start_link ( config )
54
+ end
55
+
56
+ test "read_data" do
57
+ AHT20.MockI2C
58
+ |> Mox . expect ( :read , 1 , fn _ref , _address , _data , [ ] -> { :ok , << 28 , 38 , 154 , 118 , 66 , 231 , 118 >> } end )
59
+
60
+ assert { :ok , pid } = AHT20 . start_link ( i2c_bus: "i2c-1" , i2c_address: 0x38 )
61
+ assert { :ok , data } = AHT20 . read_data ( pid )
62
+
63
+ assert data == % AHT20.Measurement {
64
+ raw_humidity: 158_119 ,
65
+ raw_temperature: 410_343 ,
66
+ relative_humidity: 15.079402923583984 ,
67
+ temperature_c: 28.26671600341797 ,
68
+ temperature_f: 82.88008880615234
69
+ }
70
+ end
71
+
72
+ test "read_state" do
73
+ AHT20.MockI2C
74
+ |> Mox . expect ( :write_read , 1 , fn _ref , _address , _data , _bytes_to_read , [ ] -> { :ok , << 0b00011100 >> } end )
42
75
43
- AHT20 . start_link ( i2c_bus: "i2c-1" , i2c_address: 0x38 )
44
- assert { :ok , state } = AHT20 . read_state ( )
45
- assert state == % AHT20.State { busy: false , calibrated: true , mode: :nor }
76
+ assert { :ok , pid } = AHT20 . start_link ( i2c_bus: "i2c-1" , i2c_address: 0x38 )
77
+ assert { :ok , state } = AHT20 . read_state ( pid )
78
+ assert state == % AHT20.State { busy: false , calibrated: true , mode: :nor }
79
+ end
46
80
end
47
81
end
0 commit comments