Example #1
0
class DataStructureElementFixture(FixtureInterface):
    data_structure = None
    data_structure_element_root = None
    data_structure_element_child = None

    def insert_data(self, user="******"):
        self.generate_data_structure_element(user)

    def generate_data_structure_element(self, user):
        self.data_structure = MockDataStructure(user="******",
                                                template="template",
                                                name="name").save()
        self.data_structure_element_child = DataStructureElement(
            user=user,
            tag="child",
            value="value",
            data_structure=self.data_structure)
        self.data_structure_element_child.save()

        self.data_structure_element_root = DataStructureElement(
            user=user,
            tag="root",
            children=[self.data_structure_element_child.id],
            data_structure=self.data_structure,
        )
        self.data_structure_element_root.save()
Example #2
0
 def test_data_structure_element_get_by_xpath_return_data_if_found(
         self, mock_get):
     # Arrange
     mock_data_structure_element = DataStructureElement(
         tag="tag", value="value", options={"xpath.xml": "value_xpath"})
     mock_get.return_value = mock_data_structure_element
     # Act
     result = DataStructureElement.get_by_xpath("value_xpath")
     # Assert
     self.assertEqual(result, mock_data_structure_element)
Example #3
0
 def test_data_structure_element_get_all_return_collection_of_data(
         self, mock_list):
     # Arrange
     mock_data_structure_element_1 = DataStructureElement("tag", "value")
     mock_data_structure_element_2 = DataStructureElement("tag", "value")
     mock_list.return_value = [
         mock_data_structure_element_1, mock_data_structure_element_2
     ]
     # Act
     result = data_structure_element_api.get_all()
     # Assert
     self.assertTrue(
         all(isinstance(item, DataStructureElement) for item in result))
    def setUp(self):
        self.fixtures = DataStructureElementFixtures()
        self.fixtures.insert_data(user=self.fixtures.default_owner_with_perm)

        self.users = {
            "anon": AnonymousUser(),
            "user": self.fixtures.other_user_with_perm,
            "owner": self.fixtures.default_owner_with_perm,
            "superuser": self.fixtures.superuser,
        }

        self.mock_data_structure_element = DataStructureElement(
            id=ObjectId(),
            user=str(self.users["owner"].id),
            tag="mock_tag",
            value="mock_value",
            data_structure=self.fixtures.data_structure,
        )
        request_factory = RequestFactory()
        self.request = request_factory.get(
            reverse("core_parser_app_data_structure_element_value"))
        self.request.GET = {
            "id":
            str(self.fixtures.data_structure_element_collection["root"].id)
        }
Example #5
0
    def _generate_data_structure_element(
        self, element_id, user=None, options=None, children=None, data_structure=None
    ):
        """Return a DataStructureElement with the given parameters

        Args:
            element_id:
            user:
            options:
            children:
            data_structure:

        Returns:
        """
        children = [] if children is None else children
        options = {} if options is None else options
        user = self.default_user_id if not user else str(user.id)

        return DataStructureElement(
            user=user,
            tag="tag_%s" % element_id,
            value="value_%s" % element_id,
            options=options,
            children=children,
            data_structure=data_structure,
        ).save()
Example #6
0
def get_by_id(data_structure_element_id):
    """ Return DataStructureElement object with the given id

        Args:
            data_structure_element_id:

        Returns: DataStructureElement object
    """
    return DataStructureElement.get_by_id(data_structure_element_id)
Example #7
0
    def generate_data_structure_element(self, user):
        self.data_structure = MockDataStructure(user="******",
                                                template="template",
                                                name="name").save()
        self.data_structure_element_child = DataStructureElement(
            user=user,
            tag="child",
            value="value",
            data_structure=self.data_structure)
        self.data_structure_element_child.save()

        self.data_structure_element_root = DataStructureElement(
            user=user,
            tag="root",
            children=[self.data_structure_element_child.id],
            data_structure=self.data_structure,
        )
        self.data_structure_element_root.save()
Example #8
0
 def test_data_structure_element_get_by_id_return_data_if_found(
         self, mock_get):
     # Arrange
     mock_data_structure_element = DataStructureElement("tag", "value")
     mock_get.return_value = mock_data_structure_element
     # Act
     result = data_structure_element_api.get_by_id(1)
     # Assert
     self.assertIsInstance(result, DataStructureElement)
Example #9
0
def get_all_by_child_id(child_id):
    """ Get Data structure element object which contains the given child id in its children

    Args:
        child_id:

    Returns:

    """
    return DataStructureElement.get_all_by_child_id(child_id)
Example #10
0
 def test_data_structure_element_get_all_by_id_return_data_if_found(
         self, mock_get):
     # Arrange
     mock_data_structure_element = DataStructureElement("tag", "value")
     mock_get.return_value = [mock_data_structure_element]
     # Act
     result = data_structure_element_api.get_all_by_child_id(1)
     # Assert
     self.assertTrue(
         all(isinstance(item, DataStructureElement) for item in result))
def _set_data_structure_fields(data_structure):
    """ Set DataStructureTypeElement fields.

    Returns:
        DataStructureTypeElement with assigned fields.

    """
    data_structure.path = "dummy.path"
    data_structure.data_structure = DataStructureElement()

    return data_structure
Example #12
0
    def test_data_structure_element_upsert_return_data_structure_element(
            self, mock_save):
        # Arrange
        mock_data_structure_element = DataStructureElement("tag", "value")
        mock_save.return_value = mock_data_structure_element

        # Act
        result = data_structure_element_api.upsert(mock_data_structure_element)

        # Assert
        self.assertIsInstance(result, DataStructureElement)
Example #13
0
    def generate_data_structure_element(self):
        """ Insert three elements in mock database with different tag and value

        two of them have same children
        two of then have same options

        Returns:

        """
        # children build
        data_structure_element_child_1 = DataStructureElement(
            'tag_child_1', 'value_child_1').save()
        data_structure_element_child_2 = DataStructureElement(
            'tag_child_2', 'value_child_2').save()
        children_1 = [
            data_structure_element_child_1, data_structure_element_child_2
        ]
        self.data_structure_element_child_id_1 = data_structure_element_child_1.id
        self.data_structure_element_child_3 = DataStructureElement(
            'tag_child_3', 'value_child_3').save()
        data_structure_element_child_4 = DataStructureElement(
            'tag_child_4', 'value_child_4').save()
        self.children_2 = [
            self.data_structure_element_child_3, data_structure_element_child_4
        ]

        # option build
        data_structure_element_option_1 = {
            "opt_1_1": "value_opt_1",
            "opt_1_2": "value_opt_2"
        }
        data_structure_element_option_2 = {
            "opt_2_1": "value_opt_1",
            "opt_2_2": "value_opt_2"
        }

        # insert two complete data structure element
        data_structure_element_1 = DataStructureElement(
            'tag1', 'value1', data_structure_element_option_1,
            children_1).save()
        data_structure_element_2 = DataStructureElement(
            'tag2', 'value2', data_structure_element_option_2,
            self.children_2).save()
        data_structure_element_3 = DataStructureElement(
            'tag3', 'value3', data_structure_element_option_2,
            self.children_2).save()

        # build a collection of data structure element
        self.data_structure_element_collection = [
            data_structure_element_child_1, data_structure_element_child_2,
            self.data_structure_element_child_3,
            data_structure_element_child_4, data_structure_element_1,
            data_structure_element_2, data_structure_element_3
        ]
    def setUp(self):
        self.fixtures = DataStructureElementFixtures()
        self.fixtures.insert_data(user=self.fixtures.default_owner_with_perm)

        self.users = {
            "anon": AnonymousUser(),
            "user": self.fixtures.other_user_with_perm,
            "owner": self.fixtures.default_owner_with_perm,
            "superuser": self.fixtures.superuser,
        }

        self.mock_data_structure_element = DataStructureElement(
            user=str(self.users["owner"].id),
            tag="mock_tag",
            value="mock_value",
            data_structure=self.fixtures.data_structure,
        ).save()
        self.mock_request = Mock(spec=HttpRequest)
Example #15
0
    def test_data_structure_element_upsert_return_data_structure_element(
            self, mock_save):
        # Arrange
        mock_user = create_mock_user("1", is_superuser=True)
        mock_request = Mock(spec=HttpRequest)
        mock_request.user = mock_user

        mock_data_structure_element = DataStructureElement(
            user="******",
            tag="tag",
            value="value",
            data_structure=MockDataStructure())
        mock_save.return_value = mock_data_structure_element

        # Act
        result = data_structure_element_api.upsert(mock_data_structure_element,
                                                   mock_request)

        # Assert
        self.assertIsInstance(result, DataStructureElement)
Example #16
0
def get_all():
    """ List all DataStructureElement

        Returns: DataStructureElement collection
    """
    return DataStructureElement.get_all()
Example #17
0
    def generate_data_structure_element(self):
        """ Insert data structure elements on multiple levels in mock database

        Example::

            Root -> 1 -> 1_1 -> 1_1_1
                             -> 1_1_2 -> 1_1_2_1
                             -> 1_1_3
                 -> 2

            Root2 -> test


        Returns:

        """
        # children build

        self.data_structure_element_1_1_2_1 = DataStructureElement(
            'tag_1_1_2_1', 'value_1_1_2_1').save()

        self.data_structure_element_1_1_1 = DataStructureElement(
            'tag_1_1_1', 'value_1_1_1').save()
        self.data_structure_element_1_1_2 = DataStructureElement(
            'tag_1_1_2',
            'value_1_1_2',
            children=[self.data_structure_element_1_1_2_1]).save()
        self.data_structure_element_1_1_3 = DataStructureElement(
            'tag_1_1_3', 'value_1_1_3').save()

        self.data_structure_element_1_1 = DataStructureElement(
            'tag_1_1',
            'value_1_1',
            children=[
                self.data_structure_element_1_1_1,
                self.data_structure_element_1_1_2,
                self.data_structure_element_1_1_3
            ]).save()

        self.data_structure_element_1 = DataStructureElement(
            'tag_1', 'value_1',
            children=[self.data_structure_element_1_1]).save()

        self.data_structure_element_2 = DataStructureElement(
            'tag_2', 'value_2').save()

        self.data_structure_element_root = DataStructureElement(
            'root',
            'root',
            children=[
                self.data_structure_element_1, self.data_structure_element_2
            ]).save()

        self.data_structure_element_test = DataStructureElement(
            'test', 'test').save()

        self.data_structure_element_root_2 = DataStructureElement(
            'root2', 'root2',
            children=[self.data_structure_element_test]).save()

        # build a collection of data structure element
        self.data_structure_element_collection = [
            self.data_structure_element_root, self.data_structure_element_1,
            self.data_structure_element_2, self.data_structure_element_1_1,
            self.data_structure_element_1_1_1,
            self.data_structure_element_1_1_2,
            self.data_structure_element_1_1_3,
            self.data_structure_element_1_1_2_1,
            self.data_structure_element_root_2,
            self.data_structure_element_test
        ]