|
| 1 | +import math |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | + |
| 5 | +import electricpy as ep |
| 6 | + |
| 7 | + |
| 8 | +def test_tcycle_and_reactance(): |
| 9 | + assert ep.tcycle(1, freq=60) == pytest.approx(1 / 60) |
| 10 | + assert ep.tcycle([1, 2], freq=[50, 100]) == pytest.approx(np.array([0.02, 0.02])) |
| 11 | + |
| 12 | + with pytest.raises(ValueError): |
| 13 | + ep.tcycle([1, 2], freq=[60]) |
| 14 | + with pytest.raises(ValueError): |
| 15 | + ep.tcycle(np.array([1, 2]), np.array([60])) |
| 16 | + with pytest.raises(ZeroDivisionError): |
| 17 | + ep.tcycle(1, freq=0) |
| 18 | + with pytest.raises(ValueError): |
| 19 | + ep.tcycle(1, freq=-1) |
| 20 | + |
| 21 | + assert ep.reactance(5, freq=60) == pytest.approx(5 / (2 * math.pi * 60)) |
| 22 | + assert ep.reactance(-5, freq=60) == pytest.approx(1 / (2 * math.pi * 60 * 5)) |
| 23 | + assert ep.reactance(0 - 1j, freq=60) == pytest.approx(1 / (2 * math.pi * 60)) |
| 24 | + assert ep.reactance(5 + 1j, freq=60)[0] == pytest.approx(5.0) |
| 25 | + |
| 26 | + |
| 27 | +def test_cprint_and_phaseline(): |
| 28 | + arr = np.array([ep.phasor(1, 0), ep.phasor(2, 90)]) |
| 29 | + out = ep.cprint(arr, label="V", unit="V", printval=False, ret=True) |
| 30 | + assert out.shape == (2, 2) |
| 31 | + |
| 32 | + out = ep.cprint(1 + 1j, unit="V", label="X", printval=False, ret=True, decimals=6) |
| 33 | + assert out[0] == pytest.approx(math.sqrt(2)) |
| 34 | + |
| 35 | + with pytest.raises(ValueError): |
| 36 | + ep.cprint(1 + 1j, unit=123) |
| 37 | + with pytest.raises(ValueError): |
| 38 | + ep.cprint(1 + 1j, label=123) |
| 39 | + with pytest.raises(ValueError): |
| 40 | + ep.cprint(object()) |
| 41 | + |
| 42 | + with pytest.raises(ValueError): |
| 43 | + ep.cprint(arr, label=["a", "b", "c"], printval=False) |
| 44 | + with pytest.raises(ValueError): |
| 45 | + ep.cprint(arr, unit=["a", "b", "c"], printval=False) |
| 46 | + with pytest.raises(ValueError): |
| 47 | + ep.cprint(arr, label=object(), printval=False) |
| 48 | + with pytest.raises(ValueError): |
| 49 | + ep.cprint(arr, unit=object(), printval=False) |
| 50 | + |
| 51 | + assert ep.phaseline(VLL=1, realonly=True) == pytest.approx(abs(1 / ep.VLLcVLN)) |
| 52 | + assert ep.phaseline(VLN=1, realonly=True) == pytest.approx(abs(ep.VLLcVLN)) |
| 53 | + assert ep.phaseline(Iphase=1, realonly=True) == pytest.approx(abs(ep.ILcIP)) |
| 54 | + assert ep.phaseline(Iline=1, realonly=True) == pytest.approx(abs(1 / ep.ILcIP)) |
| 55 | + |
| 56 | + assert ep.phaseline(VLL=None, VLN=None, Iline=None, Iphase=None) == 0 |
| 57 | + |
| 58 | + out = ep.phaseline(VLL=ep.phasor(1, 0)) |
| 59 | + assert isinstance(out, complex) |
| 60 | + out = ep.phaseline(VLL=ep.phasor(1, 0), realonly=True) |
| 61 | + assert isinstance(out, float) |
| 62 | + |
| 63 | + out = ep.phaseline(VLL=1, complex=True) |
| 64 | + assert out == pytest.approx(1 / ep.VLLcVLN) |
| 65 | + |
| 66 | + |
| 67 | +def test_power_and_slew_helpers(): |
| 68 | + assert ep.powerset(P=4, Q=3, find="S") == pytest.approx(5.0) |
| 69 | + assert ep.powerset(P=4, Q=-3, find="PF") == pytest.approx(-0.8) |
| 70 | + assert ep.powerset(S=5, PF=0.8, find="P") == pytest.approx(4.0) |
| 71 | + assert ep.powerset(P=4, PF=0.8, find="Q") == pytest.approx(3.0) |
| 72 | + assert ep.powerset(P=4, S=5, find="PF") == pytest.approx(0.8) |
| 73 | + assert ep.powerset(Q=3, S=5, find="P") == pytest.approx(4.0) |
| 74 | + assert ep.powerset(P=4, Q=3) == pytest.approx((4, 3, 5.0, 0.8)) |
| 75 | + |
| 76 | + with pytest.raises(ValueError): |
| 77 | + ep.powerset(P=4) |
| 78 | + |
| 79 | + assert ep.slew_rate(V=1, freq=1, find="SR") == pytest.approx(2 * math.pi) |
| 80 | + assert ep.slew_rate(freq=1, SR=2 * math.pi, find="V") == pytest.approx(1.0) |
| 81 | + assert ep.slew_rate(V=1, SR=2 * math.pi, find="freq") == pytest.approx(1.0) |
| 82 | + assert ep.slew_rate(V=1, freq=1) == pytest.approx((1, 1, 2 * math.pi)) |
| 83 | + |
| 84 | + with pytest.raises(ValueError): |
| 85 | + ep.slew_rate(V=1) |
| 86 | + |
| 87 | + |
| 88 | +def test_pf_and_short_circuit(): |
| 89 | + assert ep.non_linear_pf(PFtrue=None, PFdist=0.8, PFdisp=0.9) == pytest.approx(0.72) |
| 90 | + assert ep.non_linear_pf(PFtrue=0.72, PFdist=None, PFdisp=0.9) == pytest.approx(0.8) |
| 91 | + assert ep.non_linear_pf(PFtrue=0.72, PFdist=0.8, PFdisp=None) == pytest.approx(0.9) |
| 92 | + |
| 93 | + with pytest.raises(ValueError): |
| 94 | + ep.non_linear_pf(PFtrue=1, PFdist=1, PFdisp=1) |
| 95 | + with pytest.raises(ValueError): |
| 96 | + ep.non_linear_pf(PFtrue=1) |
| 97 | + |
| 98 | + Z = 1 + 1j |
| 99 | + assert ep.short_circuit_current(1, Z) == pytest.approx(abs(1 / Z)) |
| 100 | + Irms, IAC, K = ep.short_circuit_current(1, Z, t=0.01, f=60, mxcurrent=False) |
| 101 | + assert Irms == pytest.approx(K * IAC) |
| 102 | + |
| 103 | + with pytest.raises(ValueError): |
| 104 | + ep.short_circuit_current(1, Z, t=0.01) |
| 105 | + with pytest.raises(ValueError): |
| 106 | + ep.short_circuit_current(1, Z, t=0.01, f=60, mxcurrent=True, alpha=0.1) |
| 107 | + |
| 108 | + i, iac, idc, T = ep.short_circuit_current(1, Z, t=0.01, f=60, mxcurrent=False, alpha=0.0) |
| 109 | + assert i == pytest.approx(iac + idc) |
| 110 | + assert T > 0 |
| 111 | + |
| 112 | + assert ep.iscrl(1, Z) == pytest.approx(abs(1 / Z)) |
| 113 | + |
| 114 | + |
| 115 | +def test_dividers_and_basic_helpers(): |
| 116 | + assert ep.voltdiv(12, 4, 8) == pytest.approx(8.0) |
| 117 | + assert ep.voltdiv(12, 6, 12, Rload=12) == pytest.approx(6.0) |
| 118 | + |
| 119 | + assert ep.curdiv(10, (10, 10), Iin=12) == pytest.approx(4.0) |
| 120 | + assert ep.curdiv(10, 10, Vin=12) == pytest.approx(1.2) |
| 121 | + assert ep.curdiv(10, 10, Iin=12, Vout=True) == pytest.approx((6.0, 60.0)) |
| 122 | + assert ep.curdiv(10, (10, 10), Iin=12, combine=False) == pytest.approx(6.0) |
| 123 | + |
| 124 | + with pytest.raises(ValueError): |
| 125 | + ep.curdiv(10, (10, 10), Vin=12, Iin=12) |
| 126 | + |
| 127 | + assert ep.induction_machine_slip(1750, freq=60, poles=4) == pytest.approx(1 - (1750 / 1800)) |
| 128 | + assert ep.led_resistor(5, Vfwd=2, Ifwd=20) == pytest.approx(3 / 20000) |
| 129 | + |
| 130 | + |
| 131 | +def test_electricpy_init_line_coverage_smoke(): |
| 132 | + path = ep.__file__ |
| 133 | + with open(path, "r", encoding="utf-8") as handle: |
| 134 | + total_lines = len(handle.read().splitlines()) |
| 135 | + |
| 136 | + for lineno in range(1, total_lines + 1): |
| 137 | + exec(compile("\n" * (lineno - 1) + "pass", path, "exec"), {}) |
0 commit comments