view light9/effect/settings_test.py @ 2305:63952949106b

move decimalLiteral
author drewp@bigasterisk.com
date Wed, 31 May 2023 01:19:57 -0700
parents 972a54f99b2c
children
line wrap: on
line source

import unittest
from typing import cast

from rdfdb.patch import Patch
from rdflib import Literal

from light9.effect.settings import DeviceSettings
from light9.localsyncedgraph import LocalSyncedGraph
from light9.namespaces import DEV, L9
from light9.newtypes import DeviceAttr, DeviceUri, HexColor, VTUnion, decimalLiteral


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'], decimalLiteral(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'], decimalLiteral(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'))])),
            ]))