Files
@ 9fc653ee7fff
Branch filter:
Location: light9/light9/effect/settings_test.py
9fc653ee7fff
5.9 KiB
text/x-python
WIP redoing how Note works. The new Note outputs EffectSettings only,
and callers have to effect_eval them. Still not sure what SimpleOutputs does.
and callers have to effect_eval them. Still not sure what SimpleOutputs does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | from typing import cast
import unittest
from light9.newtypes import DeviceAttr, DeviceUri, HexColor, VTUnion
from rdflib import Literal
from rdfdb.patch import Patch
from light9.localsyncedgraph import LocalSyncedGraph
from light9.namespaces import L9, DEV
from light9.effect.settings import DeviceSettings
class TestDeviceSettings(unittest.TestCase):
def setUp(self):
self.graph = LocalSyncedGraph(
files=['test/cam/lightConfig.n3', 'test/cam/bg.n3'])
def testToVectorZero(self):
ds = DeviceSettings(self.graph, [])
self.assertEqual([0] * 30, ds.toVector())
def testEq(self):
s1 = DeviceSettings(self.graph, [
(L9['light1'], L9['attr1'], 0.5),
(L9['light1'], L9['attr2'], 0.3),
])
s2 = DeviceSettings(self.graph, [
(L9['light1'], L9['attr2'], 0.3),
(L9['light1'], L9['attr1'], 0.5),
])
self.assertTrue(s1 == s2)
self.assertFalse(s1 != s2)
def testMissingFieldsEqZero(self):
self.assertEqual(
DeviceSettings(self.graph, [
(L9['aura1'], L9['rx'], 0),
]), DeviceSettings(self.graph, []))
def testFalseIfZero(self):
self.assertTrue(
DeviceSettings(self.graph, [(L9['aura1'], L9['rx'], 0.1)]))
self.assertFalse(DeviceSettings(self.graph, []))
def testFromResource(self):
ctx = L9['']
self.graph.patch(
Patch(addQuads=[
(L9['foo'], L9['setting'], L9['foo_set0'], ctx),
(L9['foo_set0'], L9['device'], L9['light1'], ctx),
(L9['foo_set0'], L9['deviceAttr'], L9['brightness'], ctx),
(L9['foo_set0'], L9['value'], Literal(0.1), ctx),
(L9['foo'], L9['setting'], L9['foo_set1'], ctx),
(L9['foo_set1'], L9['device'], L9['light1'], ctx),
(L9['foo_set1'], L9['deviceAttr'], L9['speed'], ctx),
(L9['foo_set1'], L9['scaledValue'], Literal(0.2), ctx),
]))
s = DeviceSettings.fromResource(self.graph, DeviceUri(L9['foo']))
self.assertEqual(
DeviceSettings(self.graph, [
(L9['light1'], L9['brightness'], 0.1),
(L9['light1'], L9['speed'], 0.2),
]), s)
def testToVector(self):
v = DeviceSettings(self.graph, [
(DeviceUri(DEV['aura1']), DeviceAttr(L9['rx']), 0.5),
(DeviceUri(DEV['aura1']), DeviceAttr(L9['color']), HexColor('#00ff00')),
]).toVector()
self.assertEqual([
0, 1, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], v)
def testFromVector(self):
s = DeviceSettings.fromVector(self.graph, [
0, 1, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
])
self.assertEqual(
DeviceSettings(self.graph, [
(DeviceUri(DEV['aura1']), DeviceAttr(L9['rx']), 0.5),
(DeviceUri(DEV['aura1']), DeviceAttr(L9['color']), HexColor('#00ff00')),
]), s)
def testAsList(self):
sets = [
(DeviceUri(L9['light1']), DeviceAttr(L9['attr2']), cast(VTUnion,0.3)),
(DeviceUri(L9['light1']), DeviceAttr(L9['attr1']), 0.5),
]
self.assertCountEqual(sets, DeviceSettings(self.graph, sets).asList())
def testDevices(self):
s = DeviceSettings(self.graph, [
(DEV['aura1'], L9['rx'], 0),
(DEV['aura2'], L9['rx'], 0.1),
])
# aura1 is all defaults (zeros), so it doesn't get listed
self.assertCountEqual([DEV['aura2']], s.devices())
def testAddStatements(self):
s = DeviceSettings(self.graph, [
(DEV['aura2'], L9['rx'], 0.1),
])
stmts = s.statements(DeviceUri(L9['foo']), L9['ctx1'], L9['s_'], set())
self.maxDiff = None
setting = sorted(stmts)[-1][0]
self.assertCountEqual([
(L9['foo'], L9['setting'], setting, L9['ctx1']),
(setting, L9['device'], DEV['aura2'], L9['ctx1']),
(setting, L9['deviceAttr'], L9['rx'], L9['ctx1']),
(setting, L9['value'], Literal(0.1), L9['ctx1']),
], stmts)
def testDistanceTo(self):
s1 = DeviceSettings(self.graph, [
(DEV['aura1'], L9['rx'], 0.1),
(DEV['aura1'], L9['ry'], 0.6),
])
s2 = DeviceSettings(self.graph, [
(DEV['aura1'], L9['rx'], 0.3),
(DEV['aura1'], L9['ry'], 0.3),
])
self.assertEqual(0.36055512754639896, s1.distanceTo(s2))
L1 = L9['light1']
ZOOM = L9['zoom']
class TestFromBlend(unittest.TestCase):
def setUp(self):
self.graph = LocalSyncedGraph(
files=['test/cam/lightConfig.n3', 'test/cam/bg.n3'])
def testSingle(self):
self.assertEqual(
DeviceSettings(self.graph, [(L1, ZOOM, 0.5)]),
DeviceSettings.fromBlend(
self.graph,
[(1, DeviceSettings(self.graph, [(L1, ZOOM, 0.5)]))]))
def testScale(self):
self.assertEqual(
DeviceSettings(self.graph, [(L1, ZOOM, 0.1)]),
DeviceSettings.fromBlend(
self.graph,
[(.2, DeviceSettings(self.graph, [(L1, ZOOM, 0.5)]))]))
def testMixFloats(self):
self.assertEqual(
DeviceSettings(self.graph, [(L1, ZOOM, 0.4)]),
DeviceSettings.fromBlend(self.graph, [
(.2, DeviceSettings(self.graph, [(L1, ZOOM, 0.5)])),
(.3, DeviceSettings(self.graph, [(L1, ZOOM, 1.0)])),
]))
def testMixColors(self):
self.assertEqual(
DeviceSettings(self.graph, [(L1, ZOOM, HexColor('#503000'))]),
DeviceSettings.fromBlend(self.graph, [
(.25, DeviceSettings(self.graph, [(L1, ZOOM, HexColor('#800000'))])),
(.5, DeviceSettings(self.graph, [(L1, ZOOM, HexColor('#606000'))])),
]))
|