Beispiel #1
0
    def test_item_size_computation(self):
        from ddbmock.database.item import Item

        item1 = Item()
        item2 = Item(ITEM_TYPE)

        # cache init
        self.assertIsNone(item1.size)
        self.assertIsNone(item2.size)

        # compute size
        s1 = item1.get_size()
        s2 = item2.get_size()

        self.assertEqual(0, s1)
        self.assertEqual(114, s2)

        # check cache
        self.assertEqual(s1, item1.size)
        self.assertEqual(s2, item2.size)

        # any call to "update item" invalidates the cache
        item1.apply_actions({})
        item2.apply_actions({})

        self.assertIsNone(item1.size)
        self.assertIsNone(item2.size)
Beispiel #2
0
    def test_item_unit_computation(self):
        # full chain test (~functional)
        from ddbmock.database.item import Item

        item1 = Item(ITEM_BIG)
        item2 = Item(ITEM_TYPE)

        self.assertEqual(123, item1.get_size().as_units())  # If I tell you the '123' is not on purpose, you won't believe me, will you ? Especially when Pi is involved
        self.assertEqual(1, item2.get_size().as_units())
Beispiel #3
0
    def test_expected_field_exists_value(self):
        from ddbmock.database.item import Item
        from ddbmock.errors import ConditionalCheckFailedException

        # ok
        item = Item({FIELDNAME: VALUE_S})
        item.assert_match_expected(
            {FIELDNAME: {
                'Exists': True,
                'Value': VALUE_S
            }})

        # exitence mismatch
        item = Item({})
        self.assertRaises(ConditionalCheckFailedException,
                          item.assert_match_expected,
                          {FIELDNAME: {
                              'Exists': True,
                              'Value': VALUE_S
                          }})

        # type mismatch
        item = Item({FIELDNAME: VALUE_S})
        self.assertRaises(ConditionalCheckFailedException,
                          item.assert_match_expected,
                          {FIELDNAME: {
                              'Exists': True,
                              'Value': VALUE_N
                          }})
Beispiel #4
0
    def test_item_size_computation(self):
        from ddbmock.database.item import Item

        item1 = Item()
        item2 = Item(ITEM_TYPE)

        # cache init
        self.assertIsNone(item1.size)
        self.assertIsNone(item2.size)

        # compute size
        s1 = item1.get_size()
        s2 = item2.get_size()

        self.assertEqual(0, s1)
        self.assertEqual(114, s2)

        # check cache
        self.assertEqual(s1, item1.size)
        self.assertEqual(s2, item2.size)

        # any call to "update item" invalidates the cache
        item1.apply_actions({})
        item2.apply_actions({})

        self.assertIsNone(item1.size)
        self.assertIsNone(item2.size)
Beispiel #5
0
    def test_expected_field_does_not_exist(self):
        from ddbmock.database.item import Item
        from ddbmock.errors import ConditionalCheckFailedException

        # ok
        item = Item({})
        item.assert_match_expected({
           FIELDNAME: {u'Exists': False}
        })

        # mismatch
        item = Item({FIELDNAME: VALUE_SS})
        self.assertRaises(ConditionalCheckFailedException,
                          item.assert_match_expected,
                          {FIELDNAME: {u'Exists': False}})
Beispiel #6
0
    def test_expected_field_exists_value(self):
        from ddbmock.database.item import Item
        from ddbmock.errors import ConditionalCheckFailedException

        # ok
        item = Item({FIELDNAME: VALUE_S})
        item.assert_match_expected({
           FIELDNAME: {'Exists': True, 'Value': VALUE_S}
        })

        # exitence mismatch
        item = Item({})
        self.assertRaises(ConditionalCheckFailedException,
                          item.assert_match_expected,
                          {FIELDNAME: {'Exists': True, 'Value': VALUE_S}})

        # type mismatch
        item = Item({FIELDNAME: VALUE_S})
        self.assertRaises(ConditionalCheckFailedException,
                          item.assert_match_expected,
                          {FIELDNAME: {'Exists': True, 'Value': VALUE_N}})
Beispiel #7
0
    def test_size_computation(self):
        from ddbmock.database.item import Item

        item = Item(ITEM_TYPE)

        self.assertEqual(0, item.get_field_size(u"toto"))
        self.assertEqual(8, item.get_field_size(u"N"))
        self.assertEqual(15, item.get_field_size(u"S"))
        self.assertEqual(12, item.get_field_size(u"B"))
        self.assertEqual(16, item.get_field_size(u"NS"))
        self.assertEqual(30, item.get_field_size(u"SS"))
        self.assertEqual(24, item.get_field_size(u"BS"))
Beispiel #8
0
    def test_item_unit_computation(self):
        # full chain test (~functional)
        from ddbmock.database.item import Item

        item1 = Item(ITEM_BIG)
        item2 = Item(ITEM_TYPE)

        self.assertEqual(
            123,
            item1.get_size().as_units()
        )  # If I tell you the '123' is not on purpose, you won't believe me, will you ? Especially when Pi is involved
        self.assertEqual(1, item2.get_size().as_units())
Beispiel #9
0
    def test_size_computation(self):
        from ddbmock.database.item import Item

        item = Item(ITEM_TYPE)

        self.assertEqual(0, item.get_field_size(u"toto"))
        self.assertEqual(8, item.get_field_size(u"N"))
        self.assertEqual(15, item.get_field_size(u"S"))
        self.assertEqual(12, item.get_field_size(u"B"))
        self.assertEqual(16, item.get_field_size(u"NS"))
        self.assertEqual(30, item.get_field_size(u"SS"))
        self.assertEqual(24, item.get_field_size(u"BS"))
Beispiel #10
0
    def test_expected_field_does_not_exist(self):
        from ddbmock.database.item import Item
        from ddbmock.errors import ConditionalCheckFailedException

        # ok
        item = Item({})
        item.assert_match_expected({FIELDNAME: {u'Exists': False}})

        # mismatch
        item = Item({FIELDNAME: VALUE_SS})
        self.assertRaises(ConditionalCheckFailedException,
                          item.assert_match_expected,
                          {FIELDNAME: {
                              u'Exists': False
                          }})
Beispiel #11
0
    def test_action_put(self):
        from ddbmock.database.item import Item

        item = Item({})
        item._apply_action(FIELDNAME, {"Action": "PUT", "Value": VALUE_S})
        self.assertEqual([(FIELDNAME, VALUE_S)], item.items())
Beispiel #12
0
    def test_action_type_mismatch(self):
        from ddbmock.database.item import Item
        from ddbmock.errors import ValidationException

        # delete from set, set of same type
        item = Item({
            FIELDNAME: VALUE_SS,
        })
        self.assertRaises(ValidationException,
                          item._apply_action,
                          FIELDNAME, {"Action": "DELETE", "Value": VALUE_S})
        self.assertEqual([(FIELDNAME, VALUE_SS)], item.items())
        self.assertRaises(ValidationException,
                          item._apply_action,
                          FIELDNAME, {"Action": "DELETE", "Value": VALUE_NS})
        self.assertEqual([(FIELDNAME, VALUE_SS)], item.items())

        # delete value from scalar field (fail)
        item = Item({
            FIELDNAME: VALUE_S,
        })
        self.assertRaises(ValidationException,
                          item._apply_action,
                          FIELDNAME, {"Action": "DELETE", "Value": VALUE_S})
        self.assertEqual([(FIELDNAME, VALUE_S)], item.items())

        # add to scalar non number field
        item = Item({FIELDNAME: VALUE_S})
        self.assertRaises(ValidationException,
                          item._apply_action,
                          FIELDNAME, {"Action": "ADD", "Value": VALUE_S})
        self.assertEqual([(FIELDNAME, VALUE_S)], item.items())

        # add to set of different type
        item = Item({FIELDNAME: VALUE_SS})
        self.assertRaises(ValidationException,
                          item._apply_action,
                          FIELDNAME, {"Action": "ADD", "Value": VALUE_NS})
        self.assertEqual([(FIELDNAME, VALUE_SS)], item.items())

        # add non number to non existing field
        item = Item({})
        self.assertRaises(ValidationException,
                          item._apply_action,
                          FIELDNAME, {"Action": "ADD", "Value": VALUE_S})
        self.assertEqual([], item.items())
Beispiel #13
0
    def test_action_add(self):
        from ddbmock.database.item import Item

        # add value to int field
        item = Item({FIELDNAME: VALUE_N})
        item._apply_action(FIELDNAME, {"Action": "ADD", "Value": VALUE_N})
        self.assertEqual([(FIELDNAME, VALUE_N_X2)], item.items())

        # push value to any set
        item = Item({FIELDNAME: VALUE_SS})
        item._apply_action(FIELDNAME, {"Action": "ADD", "Value": VALUE_SS_DEL})
        self.assertEqual([(FIELDNAME, VALUE_SS_RES)], item.items())

        # field does not exist
        item = Item({})
        item._apply_action(FIELDNAME, {"Action": "ADD", "Value": VALUE_NS})
        self.assertEqual([(FIELDNAME, VALUE_NS)], item.items())
Beispiel #14
0
    def test_action_delete(self):
        from ddbmock.database.item import Item

        # field does not exist (just ignores)
        item = Item({})
        item._apply_action(FIELDNAME, {"Action": "DELETE", "Value": VALUE_S})
        self.assertEqual([], item.items())

        # delete scalar value
        item = Item({
            FIELDNAME: VALUE_S,
        })
        item._apply_action(FIELDNAME, {"Action": "DELETE"})
        self.assertEqual([], item.items())

        # delete set from set
        item = Item({
            FIELDNAME: VALUE_SS,
        })
        item._apply_action(FIELDNAME, {"Action": "DELETE", "Value": VALUE_SS_DEL})
        self.assertEqual([(FIELDNAME, VALUE_SS_SUR)], item.items())

        # delete set all from set
        item = Item({
            FIELDNAME: VALUE_SS,
        })
        item._apply_action(FIELDNAME, {"Action": "DELETE", "Value": VALUE_SS})
        self.assertEqual([], item.items())
Beispiel #15
0
    def test_action_put(self):
        from ddbmock.database.item import Item

        item = Item({})
        item._apply_action(FIELDNAME, {"Action": "PUT", "Value": VALUE_S})
        self.assertEqual([(FIELDNAME, VALUE_S)], item.items())
Beispiel #16
0
    def test_action_delete(self):
        from ddbmock.database.item import Item

        # field does not exist (just ignores)
        item = Item({})
        item._apply_action(FIELDNAME, {"Action": "DELETE", "Value": VALUE_S})
        self.assertEqual([], item.items())

        # delete scalar value
        item = Item({
            FIELDNAME: VALUE_S,
        })
        item._apply_action(FIELDNAME, {"Action": "DELETE"})
        self.assertEqual([], item.items())

        # delete set from set
        item = Item({
            FIELDNAME: VALUE_SS,
        })
        item._apply_action(FIELDNAME, {
            "Action": "DELETE",
            "Value": VALUE_SS_DEL
        })
        self.assertEqual([(FIELDNAME, VALUE_SS_SUR)], item.items())

        # delete set all from set
        item = Item({
            FIELDNAME: VALUE_SS,
        })
        item._apply_action(FIELDNAME, {"Action": "DELETE", "Value": VALUE_SS})
        self.assertEqual([], item.items())
Beispiel #17
0
    def test_action_add(self):
        from ddbmock.database.item import Item

        # add value to int field
        item = Item({FIELDNAME: VALUE_N})
        item._apply_action(FIELDNAME, {"Action": "ADD", "Value": VALUE_N})
        self.assertEqual([(FIELDNAME, VALUE_N_X2)], item.items())

        # push value to any set
        item = Item({FIELDNAME: VALUE_SS})
        item._apply_action(FIELDNAME, {"Action": "ADD", "Value": VALUE_SS_DEL})
        self.assertEqual([(FIELDNAME, VALUE_SS_RES)], item.items())

        # field does not exist
        item = Item({})
        item._apply_action(FIELDNAME, {"Action": "ADD", "Value": VALUE_NS})
        self.assertEqual([(FIELDNAME, VALUE_NS)], item.items())

        # add string set to non existing field
        item = Item({})
        item._apply_action(FIELDNAME, {"Action": "ADD", "Value": VALUE_SS})
        self.assertEqual([(FIELDNAME, VALUE_SS)], item.items())
Beispiel #18
0
    def test_action_type_mismatch(self):
        from ddbmock.database.item import Item
        from ddbmock.errors import ValidationException

        # delete from set, set of same type
        item = Item({
            FIELDNAME: VALUE_SS,
        })
        self.assertRaises(ValidationException, item._apply_action, FIELDNAME, {
            "Action": "DELETE",
            "Value": VALUE_S
        })
        self.assertEqual([(FIELDNAME, VALUE_SS)], item.items())
        self.assertRaises(ValidationException, item._apply_action, FIELDNAME, {
            "Action": "DELETE",
            "Value": VALUE_NS
        })
        self.assertEqual([(FIELDNAME, VALUE_SS)], item.items())

        # delete value from scalar field (fail)
        item = Item({
            FIELDNAME: VALUE_S,
        })
        self.assertRaises(ValidationException, item._apply_action, FIELDNAME, {
            "Action": "DELETE",
            "Value": VALUE_S
        })
        self.assertEqual([(FIELDNAME, VALUE_S)], item.items())

        # add to scalar non number field
        item = Item({FIELDNAME: VALUE_S})
        self.assertRaises(ValidationException, item._apply_action, FIELDNAME, {
            "Action": "ADD",
            "Value": VALUE_S
        })
        self.assertEqual([(FIELDNAME, VALUE_S)], item.items())

        # add to set of different type
        item = Item({FIELDNAME: VALUE_SS})
        self.assertRaises(ValidationException, item._apply_action, FIELDNAME, {
            "Action": "ADD",
            "Value": VALUE_NS
        })
        self.assertEqual([(FIELDNAME, VALUE_SS)], item.items())

        # add non number to non existing field
        item = Item({})
        self.assertRaises(ValidationException, item._apply_action, FIELDNAME, {
            "Action": "ADD",
            "Value": VALUE_S
        })
        self.assertEqual([], item.items())