Пример #1
0
    def test_build_with_trait_inheritance(self):
        # this is testing inheritance of traits, not traits plus doc inheritance
        factory = Factory(self.collection)
        factory.trait("timestamped", {
            "created": lambda doc: datetime(2001, 1, 1, 1, 1, 1)
        })

        factory.trait("versioned", {
            "v": 3
        }, parent="timestamped")

        factory.document("car", {
            "wheels": 4
        }, traits=["versioned"])

        factory.document("mazda", {
            "make": lambda doc: "Mazda"
        }, parent="car")

        expected = {
            "wheels": 4,
            "make": "Mazda",
            "created": datetime(2001, 1, 1, 1, 1, 1),
            "v": 3
        }

        self.assertDictEqual(expected, factory.build("mazda"))
Пример #2
0
    def test_build_with_only_traits(self):
        factory = Factory(self.collection)
        factory.trait("timestamped", {
            "created": lambda doc: datetime(2001, 1, 1, 1, 1, 1)
        })
        factory.trait("versioned", {
            "v": 3
        })
        factory.document("car", traits=["timestamped", "versioned"])

        expected = {
            "created": datetime(2001, 1, 1, 1, 1, 1),
            "v": 3
        }

        self.assertDictEqual(expected, factory.build("car"))
Пример #3
0
 def test_fragments_with_only_traits(self):
     factory = Factory(self.collection)
     factory.trait('versioned', {"v": 5})
     factory.fragment("alert_prefs", traits=['versioned'])
     factory.document("user", {
         "first_name": 'John',
         "last_name": 'Smith',
         "age": 32,
         "alerts": factory.embed("alert_prefs")
     })
     self.assertDictEqual(factory.build("user"), {
         "first_name": "John",
         "last_name": "Smith",
         "age": 32,
         "alerts": {
             "v": 5
         }
     })
Пример #4
0
    def test_build_default_with_traits(self):
        factory = Factory(self.collection)
        factory.trait("timestamped", {
            "created": lambda doc: datetime(2001, 1, 1, 1, 1, 1)
        })
        factory.trait("versioned", {
            "v": 3
        })
        factory.default({
            "wheels": 4,
            "make": lambda doc: "Mazda"
        }, traits=["timestamped", "versioned"])

        expected = {
            "wheels": 4,
            "make": "Mazda",
            "created": datetime(2001, 1, 1, 1, 1, 1),
            "v": 3
        }

        self.assertDictEqual(expected, factory.build())
Пример #5
0
 def test_embed_fragment_with_inline_trait(self):
     factory = Factory(self.collection)
     factory.trait('versioned', {"v": 5})
     factory.trait('sms', {"sms": True})
     factory.fragment("alert_prefs", {
         "emails": True
     }, traits=['versioned'])
     factory.document("user", {
         "first_name": 'John',
         "last_name": 'Smith',
         "age": 32,
         "alerts": factory.embed("alert_prefs", traits=["sms"])
     })
     self.assertDictEqual(factory.build("user"), {
         "first_name": "John",
         "last_name": "Smith",
         "age": 32,
         "alerts": {
             "emails": True,
             "v": 5,
             "sms": True
         }
     })