Exemplo n.º 1
0
def create_app(db):
    schema = Schema(db)
    schema.load_schema()

    api = Api(schema)

    app = Flask(__name__)
    app.secret_key = 'keepitsecretkeepitsafe'
    app.config['api'] = api
    app.config['admin_api'] = AdminApi(schema)
    app.register_blueprint(api_bp)
    app.register_blueprint(client_bp)
    app.register_blueprint(admin_bp)
    app.register_blueprint(search_bp)
    app.register_blueprint(login_bp)

    init_login(app)

    @app.route("/")
    def index():
        return redirect(url_for('client.client_root'))

    integration_runner = IntegrationRunner(api, db)
    integration_runner.start()

    atexit.register(integration_runner.stop)

    return app
Exemplo n.º 2
0
class SchemaTest(unittest.TestCase):
    def setUp(self):
        client = MongoClient()
        client.drop_database('metaphor2_test_db')
        self.db = client.metaphor2_test_db
        self.schema = Schema(self.db)
        self.maxDiff = None

    def test_int_type(self):
        self.db.metaphor_schema.insert_one(
            {"specs": {
                "employee": {
                    "fields": {
                        "age": {
                            "type": "int"
                        }
                    },
                },
            }})
        self.schema.load_schema()
        self.assertEquals(1, len(self.schema.specs))
        self.assertEquals(
            "int", self.schema.specs['employee'].fields['age'].field_type)

        self.assertEquals([],
                          self.schema.validate_spec('employee', {'age': 12}))

        self.assertEquals([{
            'error': "Nonexistant field: 'name'"
        }], self.schema.validate_spec('employee', {'name': "12"}))

        self.assertEquals([{
            'error':
            "Invalid type: str for field 'age' of 'employee' (expected 'int')"
        }], self.schema.validate_spec('employee', {'age': "12"}))

    def test_str_type(self):
        self.db.metaphor_schema.insert_one(
            {"specs": {
                "employee": {
                    "fields": {
                        "name": {
                            "type": "str"
                        }
                    },
                },
            }})
        self.schema.load_schema()
        self.assertEquals(1, len(self.schema.specs))
        self.assertEquals(
            "str", self.schema.specs['employee'].fields['name'].field_type)

        self.assertEquals([],
                          self.schema.validate_spec('employee',
                                                    {'name': 'Bob'}))

        self.assertEquals([{
            'error': "Nonexistant field: 'age'"
        }], self.schema.validate_spec('employee', {'age': "12"}))

        self.assertEquals([{
            'error':
            "Invalid type: int for field 'name' of 'employee' (expected 'str')"
        }], self.schema.validate_spec('employee', {'name': 12}))

    def test_bool_type(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "admin": {
                            "type": "bool"
                        }
                    },
                },
            }
        })
        self.schema.load_schema()
        self.assertEquals(1, len(self.schema.specs))
        self.assertEquals(
            "bool", self.schema.specs['employee'].fields['admin'].field_type)

        self.assertEquals([],
                          self.schema.validate_spec('employee',
                                                    {'admin': True}))

        self.assertEquals([{
            'error': "Nonexistant field: 'age'"
        }], self.schema.validate_spec('employee', {'age': "12"}))

        self.assertEquals([{
            'error':
            "Invalid type: int for field 'admin' of 'employee' (expected 'bool')"
        }], self.schema.validate_spec('employee', {'admin': 12}))

    def test_float_type(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "salary": {
                            "type": "float"
                        }
                    },
                },
            }
        })
        self.schema.load_schema()
        self.assertEquals(1, len(self.schema.specs))
        self.assertEquals(
            "float", self.schema.specs['employee'].fields['salary'].field_type)

        self.assertEquals([],
                          self.schema.validate_spec('employee',
                                                    {'salary': 22.33}))
        self.assertEquals([],
                          self.schema.validate_spec('employee',
                                                    {'salary': 22}))

        self.assertEquals([{
            'error': "Nonexistant field: 'age'"
        }], self.schema.validate_spec('employee', {'age': "12"}))

    def test_datetime_type(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "created": {
                            "type": "datetime"
                        }
                    },
                },
            },
            "root": {
                "employees": {
                    "type": "collection",
                    "target_spec_name": "employee",
                },
            },
        })
        self.schema.load_schema()
        self.assertEquals(1, len(self.schema.specs))
        self.assertEquals(
            "datetime",
            self.schema.specs['employee'].fields['created'].field_type)

        self.assertEquals([],
                          self.schema.validate_spec(
                              'employee', {'created': "2021-12-21T12:30:40"}))

        self.assertEqual(
            [{
                'error': "Invalid type for field 'created' (expected 'str')"
            }], self.schema.validate_spec('employee', {'created': 12}))

        self.assertEquals([{
            'error':
            "Invalid date string for field 'created' (expected ISO format)"
        }], self.schema.validate_spec('employee', {'created': "March 12"}))

        self.schema.insert_resource('employee',
                                    {'created': "2021-12-31T12:30:20"},
                                    'employees')

        inserted = self.db.resource_employee.find_one()
        self.assertEqual(datetime(2021, 12, 31, 12, 30, 20),
                         inserted['created'])
Exemplo n.º 3
0
class SchemaTest(unittest.TestCase):
    def setUp(self):
        client = MongoClient()
        client.drop_database('metaphor2_test_db')
        self.db = client.metaphor2_test_db
        self.schema = Schema(self.db)
        self.maxDiff = None

    def test_load_basic_spec(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "name": {
                            "type": "str"
                        },
                        "age": {
                            "type": "int"
                        }
                    },
                },
            }
        })
        self.schema.load_schema()
        self.assertEquals(1, len(self.schema.specs))
        self.assertEquals(
            "str", self.schema.specs['employee'].fields['name'].field_type)
        self.assertEquals(
            "int", self.schema.specs['employee'].fields['age'].field_type)

    def test_load_basic_link_with_reverse_link(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "name": {
                            "type": "str"
                        },
                    },
                },
                "department": {
                    "fields": {
                        "manager": {
                            "type": "link",
                            "target_spec_name": "employee",
                        },
                    },
                },
            }
        })
        self.schema.load_schema()
        self.assertEquals(2, len(self.schema.specs))
        self.assertEquals(
            "str", self.schema.specs['employee'].fields['name'].field_type)
        self.assertEquals(
            "link",
            self.schema.specs['department'].fields['manager'].field_type)
        self.assertEquals(
            "employee",
            self.schema.specs['department'].fields['manager'].target_spec_name)

        self.assertEquals(
            "reverse_link", self.schema.specs['employee'].
            fields['link_department_manager'].field_type)
        self.assertEquals(
            "department", self.schema.specs['employee'].
            fields['link_department_manager'].target_spec_name)

    def test_load_collection_with_parent_link(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "name": {
                            "type": "str"
                        },
                    },
                },
                "department": {
                    "fields": {
                        "employees": {
                            "type": "collection",
                            "target_spec_name": "employee",
                        },
                    },
                },
            },
        })
        self.schema.load_schema()
        self.assertEquals(2, len(self.schema.specs))
        self.assertEquals(
            "str", self.schema.specs['employee'].fields['name'].field_type)
        self.assertEquals(
            "collection",
            self.schema.specs['department'].fields['employees'].field_type)
        self.assertEquals(
            "employee", self.schema.specs['department'].fields['employees'].
            target_spec_name)

        self.assertEquals(
            "parent_collection", self.schema.specs['employee'].
            fields['parent_department_employees'].field_type)
        self.assertEquals(
            "department", self.schema.specs['employee'].
            fields['parent_department_employees'].target_spec_name)

    def test_load_link_collection_with_reverse_link(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "name": {
                            "type": "str"
                        },
                    },
                },
                "department": {
                    "fields": {
                        "parttimers": {
                            "type": "linkcollection",
                            "target_spec_name": "employee",
                        },
                    },
                },
            }
        })
        self.schema.load_schema()
        self.assertEquals(2, len(self.schema.specs))
        self.assertEquals(
            "str", self.schema.specs['employee'].fields['name'].field_type)
        self.assertEquals(
            "linkcollection",
            self.schema.specs['department'].fields['parttimers'].field_type)
        self.assertEquals(
            "employee", self.schema.specs['department'].fields['parttimers'].
            target_spec_name)

        self.assertEquals(
            "reverse_link_collection", self.schema.specs['employee'].
            fields['link_department_parttimers'].field_type)
        self.assertEquals(
            "department", self.schema.specs['employee'].
            fields['link_department_parttimers'].target_spec_name)

    def test_save_resource_encode_id(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "name": {
                            "type": "str"
                        },
                    },
                },
            }
        })
        self.schema.load_schema()
        employee_id = self.schema.insert_resource('employee', {'name': 'Bob'},
                                                  'employees')
        new_resource = self.db.resource_employee.find_one()
        self.assertEquals(ObjectId(employee_id[2:]),
                          self.schema.decodeid(employee_id))
        self.assertEquals(
            {
                '_id': self.schema.decodeid(employee_id),
                '_grants': [],
                '_canonical_url': '/employees/%s' % employee_id,
                'name': 'Bob',
                '_parent_canonical_url': '/',
                '_parent_field_name': 'employees',
                '_parent_id': None,
                '_parent_type': 'root',
            }, new_resource)
        self.assertEquals('ID%s' % (new_resource['_id'], ), employee_id)

    def test_update_field(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "name": {
                            "type": "str"
                        },
                    },
                },
            }
        })
        self.schema.load_schema()
        employee_id = self.schema.insert_resource('employee', {'name': 'Bob'},
                                                  'employees')
        self.schema.update_resource_fields('employee', employee_id,
                                           {'name': 'Ned'})
        reload_employee = self.db.resource_employee.find_one(
            {'_id': self.schema.decodeid(employee_id)})
        self.assertEquals('Ned', reload_employee['name'])

    def test_roots(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "name": {
                            "type": "str"
                        },
                    },
                },
                "department": {
                    "fields": {
                        "manager": {
                            "type": "link",
                            "target_spec_name": "employee",
                        },
                    },
                },
            },
            "root": {
                "employees": {
                    "type": "collection",
                    "target_spec_name": "employee",
                },
                "departments": {
                    "type": "collection",
                    "target_spec_name": "department",
                }
            },
        })
        self.schema.load_schema()
        self.assertEquals(2, len(self.schema.specs))
        self.assertEquals(2, len(self.schema.root.fields))
        self.assertEquals('collection',
                          self.schema.root.fields['employees'].field_type)
        self.assertEquals(
            'employee', self.schema.root.fields['employees'].target_spec_name)
        self.assertEquals('collection',
                          self.schema.root.fields['departments'].field_type)
        self.assertEquals(
            'department',
            self.schema.root.fields['departments'].target_spec_name)

    def test_canonical_url(self):
        self.db.metaphor_schema.insert_one({
            "specs": {
                "employee": {
                    "fields": {
                        "name": {
                            "type": "str"
                        },
                        "age": {
                            "type": "int"
                        },
                        "division": {
                            "type": "link",
                            "target_spec_name": "division",
                        },
                    },
                },
                "division": {
                    "fields": {
                        "name": {
                            "type": "str",
                        },
                        "yearly_sales": {
                            "type": "int",
                        },
                        "sections": {
                            "type": "collection",
                            "target_spec_name": "section",
                        }
                    },
                },
                "section": {
                    "fields": {
                        "name": {
                            "type": "str",
                        },
                    },
                },
            },
            "root": {
                "employees": {
                    "type": "collection",
                    "target_spec_name": "employee",
                },
                "divisions": {
                    "type": "collection",
                    "target_spec_name": "division",
                }
            },
        })
        self.schema.load_schema()

        division_id_1 = self.schema.insert_resource('division', {
            'name': 'sales',
            'yearly_sales': 100
        }, 'divisions')

        self.assertEquals(
            {
                '_id': self.schema.decodeid(division_id_1),
                '_grants': [],
                '_canonical_url': '/divisions/%s' % division_id_1,
                '_parent_id': None,
                '_parent_type': 'root',
                '_parent_field_name': 'divisions',
                '_parent_canonical_url': '/',
                'name': 'sales',
                'yearly_sales': 100,
            }, self.db['resource_division'].find_one(
                {'_id': self.schema.decodeid(division_id_1)}))

        section_id_1 = self.schema.insert_resource(
            'section', {'name': 'appropriation'},
            parent_type='division',
            parent_id=division_id_1,
            parent_field_name='sections')

        self.assertEquals(
            {
                '_id': self.schema.decodeid(division_id_1),
                '_grants': [],
                '_canonical_url': '/divisions/%s' % division_id_1,
                '_parent_id': None,
                '_parent_type': 'root',
                '_parent_field_name': 'divisions',
                '_parent_canonical_url': '/',
                'name': 'sales',
                'yearly_sales': 100,
            }, self.db['resource_division'].find_one(
                {'_id': self.schema.decodeid(division_id_1)}))

        self.assertEquals(
            {
                '_id':
                self.schema.decodeid(section_id_1),
                '_grants': [],
                '_canonical_url':
                '/divisions/%s/sections/%s' % (division_id_1, section_id_1),
                '_parent_id':
                self.schema.decodeid(division_id_1),
                '_parent_type':
                'division',
                '_parent_field_name':
                'sections',
                '_parent_canonical_url':
                '/divisions/%s' % division_id_1,
                'name':
                'appropriation',
            }, self.db['resource_section'].find_one(
                {'_id': self.schema.decodeid(section_id_1)}))

    def test_calc_infer_type(self):
        spec = self.schema.add_spec('employees')
        self.schema.add_field(spec, 'name', 'str')
        calc_field = self.schema.add_calc(spec, 'current_name', 'self.name')

        self.assertEquals('str', calc_field.infer_type().field_type)
        self.assertTrue(calc_field.is_primitive())
        self.assertFalse(calc_field.is_collection())

    def test_calc_infer_type_collection(self):
        spec = self.schema.add_spec('employees')
        buddy_spec = self.schema.add_spec('buddy')
        self.schema.add_field(spec, 'buddies', 'collection', 'buddy')
        calc_field = self.schema.add_calc(spec, 'all_buddies', 'self.buddies')

        self.assertEquals(buddy_spec, calc_field.infer_type())
        self.assertFalse(calc_field.is_primitive())
        self.assertTrue(calc_field.is_collection())

    def test_delete_linkcollection_entry(self):
        pass

    def test_parse_fields_test(self):
        # add parsing and validation for field types
        pass

    def test_initialize_schema(self):
        self.schema.create_initial_schema()

        schema = self.db.metaphor_schema.find_one()
        self.assertEqual(
            {
                "groups": {
                    "target_spec_name": "group",
                    "type": "collection"
                },
                "users": {
                    "target_spec_name": "user",
                    "type": "collection"
                }
            }, schema['root'])
        self.assertEqual(
            {
                'grant': {
                    'fields': {
                        'type': {
                            'type': 'str'
                        },
                        'url': {
                            'type': 'str'
                        }
                    }
                },
                'group': {
                    'fields': {
                        'grants': {
                            'target_spec_name': 'grant',
                            'type': 'collection'
                        },
                        'name': {
                            'type': 'str'
                        }
                    }
                },
                'user': {
                    'fields': {
                        'admin': {
                            'type': 'bool'
                        },
                        'create_grants': {
                            'calc_str':
                            "self.groups.grants[type='create'].url",
                            'deps': [
                                'grant.type', 'grant.url', 'group.grants',
                                'user.groups'
                            ],
                            'type':
                            'calc'
                        },
                        'delete_grants': {
                            'calc_str':
                            "self.groups.grants[type='delete'].url",
                            'deps': [
                                'grant.type', 'grant.url', 'group.grants',
                                'user.groups'
                            ],
                            'type':
                            'calc'
                        },
                        'groups': {
                            'target_spec_name': 'group',
                            'type': 'linkcollection'
                        },
                        'password': {
                            'type': 'str'
                        },
                        'put_grants': {
                            'calc_str':
                            "self.groups.grants[type='put'].url",
                            'deps': [
                                'grant.type', 'grant.url', 'group.grants',
                                'user.groups'
                            ],
                            'type':
                            'calc'
                        },
                        'read_grants': {
                            'calc_str':
                            "self.groups.grants[type='read'].url",
                            'deps': [
                                'grant.type', 'grant.url', 'group.grants',
                                'user.groups'
                            ],
                            'type':
                            'calc'
                        },
                        'update_grants': {
                            'calc_str':
                            "self.groups.grants[type='update'].url",
                            'deps': [
                                'grant.type', 'grant.url', 'group.grants',
                                'user.groups'
                            ],
                            'type':
                            'calc'
                        },
                        'username': {
                            'type': 'str'
                        }
                    }
                }
            }, schema['specs'])

    def test_load_calcs_by_dependency(self):
        self.schema.create_spec('employee')
        self.schema.create_field('employee', 'name', 'str')

        self.schema.create_spec('branch')
        self.schema.create_field('branch', 'income', 'int')
        self.schema.create_field('branch', 'employees', 'collection',
                                 'employee')

        self.schema.create_spec('section')
        self.schema.create_field('section', 'branch', 'link', 'branch')

        self.schema.load_schema()

        self.schema.create_field(
            'employee', 'average_section_income', 'calc', None,
            'average(self.parent_branch_employees.income)')
        self.schema.create_field('branch', 'section', 'calc', None,
                                 'self.link_section_branch')
        self.schema.create_field('section', 'employees', 'calc', None,
                                 'self.branch.employees')

        expected = {
            "employee": {
                "fields": {
                    "name": {
                        "type": "str"
                    },
                    "average_section_income": {
                        "type": "calc",
                        "calc_str":
                        "average(self.parent_branch_employees.income)",
                        "deps": ["branch.income"],
                    },
                },
            },
            "branch": {
                "fields": {
                    "income": {
                        "type": "int",
                    },
                    "employees": {
                        "type": "collection",
                        "target_spec_name": "employee",
                    },
                    "section": {
                        "type": "calc",
                        "calc_str": "self.link_section_branch",
                        "deps": ["section.branch"],
                    },
                },
            },
            "section": {
                "fields": {
                    "branch": {
                        "type": "link",
                        "target_spec_name": "branch",
                    },
                    "employees": {
                        "type": "calc",
                        "calc_str": "self.branch.employees",
                        "deps": ["branch.employees", "section.branch"],
                    },
                },
            },
        }

        self.assertEqual(expected, self.db.metaphor_schema.find_one()['specs'])

        self.schema.load_schema()
        self.assertEquals(3, len(self.schema.specs))

    def test_load_calcs_by_dependency_almost_circular(self):
        self.schema.create_spec('primary')
        self.schema.create_field('primary', 'name', 'str')
        self.schema.create_field('primary',
                                 'calced_name',
                                 'calc',
                                 calc_str="self.name + 'a'")

        self.schema.create_spec('secondary')
        self.schema.create_field('secondary', 'name', 'str')
        self.schema.create_field('secondary',
                                 'calced_name',
                                 'calc',
                                 calc_str="self.name + 'b'")

        self.schema.create_field('primary', 'secondary', 'link', 'secondary')
        self.schema.create_field('secondary', 'primary', 'link', 'primary')

        self.schema.create_field('primary',
                                 'secondary_name',
                                 'calc',
                                 calc_str="self.secondary.calced_name")
        self.schema.create_field('secondary',
                                 'primary_name',
                                 'calc',
                                 calc_str="self.primary.calced_name")

        self.schema.load_schema()
        self.assertEquals(2, len(self.schema.specs))
Exemplo n.º 4
0
class LRParseTest(unittest.TestCase):
    def setUp(self):
        self.maxDiff = None
        client = MongoClient()
        client.drop_database('metaphor2_test_db')
        self.db = client.metaphor2_test_db
        self.schema = Schema(self.db)

        self.db.metaphor_schema.insert_one({
            "specs" : {
                "employee" : {
                    "fields" : {
                        "name" : {
                            "type" : "str"
                        },
                        "pseudoname" : {
                            "type" : "str"
                        },
                        "age": {
                            "type": "int"
                        },
                        "division": {
                            "type": "link",
                            "target_spec_name": "division",
                        },
                    },
                },
                "division": {
                    "fields": {
                        "name": {
                            "type": "str",
                        },
                        "yearly_sales": {
                            "type": "int",
                        },
                        "sections": {
                            "type": "collection",
                            "target_spec_name": "section",
                        },
                        "parttimers": {
                            "type": "linkcollection",
                            "target_spec_name": "employee",
                        },
                        "partners": {
                            "type": "collection",
                            "target_spec_name": "employee",
                        },
                    },
                },
                "section": {
                    "fields": {
                        "name": {
                            "type": "str",
                        },
                    },
                },
            },
            "root": {
                "employees": {
                    "type": "collection",
                    "target_spec_name": "employee",
                },
                "divisions": {
                    "type": "collection",
                    "target_spec_name": "division",
                }
            },
        })
        self.schema.load_schema()

    def test_basic(self):
        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 41}, 'employees')
        division_id = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 10}, 'divisions')

        self.schema.update_resource_fields('employee', employee_id, {'division': division_id})

        tree = parse("self.division.yearly_sales", self.schema.specs['employee'])

        self.assertEquals(self.schema.specs['division'].fields['yearly_sales'], tree.infer_type())
        self.assertFalse(tree.is_collection())

        self.assertEquals(10, tree.calculate(employee_id))

    def test_even_basicer(self):
        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 41}, 'employees')

        tree = parse("self.age", self.schema.specs['employee'])
        self.assertEquals(self.schema.specs['employee'].fields['age'], tree.infer_type())
        self.assertFalse(tree.is_collection())

        self.assertEquals(41, tree.calculate(employee_id))

    def test_basic_link_follow(self):
        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 41}, 'employees')
        division_id = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 10}, 'divisions')

        self.schema.update_resource_fields('employee', employee_id, {'division': division_id})

        tree = parse("self.division", self.schema.specs['employee'])
        self.assertEquals(self.schema.specs['division'], tree.infer_type())
        self.assertFalse(tree.is_collection())

        calculated = tree.calculate(employee_id)
        self.assertEquals({
            '_id': self.schema.decodeid(division_id),
            '_grants': [],
            '_canonical_url': '/divisions/%s' % division_id,
            'name': 'sales',
            'yearly_sales': 10,
            '_parent_canonical_url': '/',
            '_parent_field_name': 'divisions',
            '_parent_id': None,
            '_parent_type': 'root',
        }, calculated)

    def test_aggregate_filtered(self):
        tree = parse("sum(employees.division[name='sales'].yearly_sales)", self.schema.specs['employee'])

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')
        division_id_2 = self.schema.insert_resource('division', {'name': 'marketting', 'yearly_sales': 20}, 'divisions')

        self.schema.update_resource_fields('employee', employee_id_1, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_2, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_3, {'division': division_id_2})

        result = tree.calculate(employee_id_1)
        self.assertEquals(100, result)

    def test_list(self):
        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')
        division_id_2 = self.schema.insert_resource('division', {'name': 'marketting', 'yearly_sales': 20}, 'divisions')

        self.schema.update_resource_fields('employee', employee_id_1, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_2, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_3, {'division': division_id_2})

        tree = parse("employees.division", self.schema.specs['employee'])

        # size and offset to be added
        result = tree.calculate(employee_id_1)

        # just making up for a lack of ordering
        division_1 = [r for r in result if r['name'] == 'sales'][0]
        division_2 = [r for r in result if r['name'] == 'marketting'][0]

        self.assertEquals('sales', division_1['name'])
        self.assertEquals('marketting', division_2['name'])

        self.assertEquals(division_id_1, self.schema.encodeid(division_1['_id']))
        self.assertEquals(division_id_2, self.schema.encodeid(division_2['_id']))

    def test_reverse_list(self):
        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')
        division_id_2 = self.schema.insert_resource('division', {'name': 'marketting', 'yearly_sales': 20}, 'divisions')

        self.schema.update_resource_fields('employee', employee_id_1, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_2, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_3, {'division': division_id_2})

        tree = parse("self.division.link_employee_division", self.schema.specs['employee'])
        result = tree.calculate(employee_id_1)

        self.assertEquals("ned", result[0]['name'])
        self.assertEquals("bob", result[1]['name'])

        self.assertEquals(self.schema.decodeid(employee_id_1), result[0]['_id'])
        self.assertEquals(self.schema.decodeid(employee_id_2), result[1]['_id'])

    def test_spec_hier_error(self):
        employee_id = self.schema.insert_resource('employee', {'name': 'sailor'}, 'employees')
        division_id = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 10}, 'divisions')

        self.schema.update_resource_fields('employee', employee_id, {'division': division_id})

        tree = parse("self.name", self.schema.specs['employee'])
        aggregation, spec, is_aggregate = tree.aggregation(employee_id)
        # unsure how this guy fits in exactly

        self.assertEquals([
            {'$match': {'_id': self.schema.decodeid(employee_id)}},
            {'$project': {'name': True}}
        ], aggregation)


    def test_nonexistant_field_in_calc(self):
        try:
            tree = parse("self.nonexistant", self.schema.specs['employee'])
            self.fail("should have thrown")
        except SyntaxError as e:
            self.assertEquals("No such field nonexistant in employee", str(e))

    def test_validate_condition_nofield(self):
        try:
            tree = parse("employees[total_nonexistant>100]", self.schema.specs['employee'])
            tree.validate()
            self.fail("should have thrown")
        except SyntaxError as e:
            self.assertEquals("Resource employee has no field total_nonexistant", str(e))

    def test_validate_const_type(self):
        try:
            tree = parse("employees[age>'str']", self.schema.specs['employee'])
            tree.validate()
            self.fail("should have thrown")
        except SyntaxError as e:
            self.assertEquals('Cannot compare "int > str"', str(e))

    def test_validate_collection(self):
        try:
            tree = parse("employees.nonexistant", self.schema.specs['employee'])
            self.fail("should have thrown")
        except SyntaxError as e:
            self.assertEquals('No such field nonexistant in employee', str(e))

    def test_validate_filtered_collection(self):
        try:
            tree = parse("employees[age>21].nonexistant", self.schema.specs['employee'])
            self.fail("should have thrown")
        except Exception as e:
            self.assertEquals('No such field nonexistant in employee', str(e))

    def test_validate_ternary(self):
        try:
            tree = parse("max(employees.age) < 50 -> 'young' : 14", self.schema.specs['employee'])
            tree.validate()
            self.fail("should have thrown")
        except SyntaxError as e:
            self.assertEquals('Both sides of ternary must return same type', str(e))

    def test_validate_resource_ternary(self):
        try:
            tree = parse("max(employees.age) < 50 -> employees.age : divisions.sections", self.schema.specs['employee'])
            tree.validate()
            self.fail("should have thrown")
        except SyntaxError as e:
            self.assertEquals('Both sides of ternary must return same type (int != section)', str(e))

    def test_nonexistant_root_collection(self):
        try:
            parse("nonexistant", self.schema.specs['employee'])
            self.fail("should have thrown")
        except SyntaxError as e:
            self.assertEquals("Cannot parse expression: nonexistant", str(e))

    def test_aggregation(self):
        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 41}, 'employees')
        division_id = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 10}, 'divisions')
        self.schema.update_resource_fields('employee', employee_id, {'division': division_id})
        tree = parse("employees[age>40].division[name='sales'].yearly_sales", self.schema.specs['employee'])

        aggregation, spec, is_aggregate = tree.aggregation(employee_id)
        self.assertEquals([
            {'$match': {'$and': [{'_parent_field_name': 'employees'},
                                 {'_parent_canonical_url': '/'}]}},
            {'$match': {'age': {'$gt': 40}}},
            {'$lookup': {'as': '_field_division',
                        'foreignField': '_id',
                        'from': 'resource_division',
                        'localField': 'division'}},
            {'$group': {'_id': '$_field_division'}},
            {'$unwind': '$_id'},
            {'$replaceRoot': {'newRoot': '$_id'}},
            {'$match': {'name': {'$eq': 'sales'}}},
            {'$project': {'yearly_sales': True}}], aggregation)
        self.assertEquals(self.schema.specs['division'].fields['yearly_sales'],
                          spec)

    def test_conditions_multiple(self):
        employee_spec = self.schema.specs['employee']
        self.schema.add_field(employee_spec, 'salary', 'int')
        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 41, 'salary': 100}, 'employees')
        tree = parse("employees[age>40 & salary>99].name", self.schema.specs['employee'])

        aggregation, spec, is_aggregate = tree.aggregation(employee_id)
        self.assertEquals([
            {'$match': {'$and': [{'_parent_field_name': 'employees'},
                                 {'_parent_canonical_url': '/'}]}},
            {'$match': {'$and' : [ {'age': {'$gt': 40}}, {'salary': {'$gt': 99}}]}},
            {'$project': {'name': True}}], aggregation)
        self.assertEquals(self.schema.specs['employee'].fields['name'],
                          spec)

    def test_conditions_multiple_or(self):
        employee_spec = self.schema.specs['employee']
        self.schema.add_field(employee_spec, 'salary', 'int')
        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 41, 'salary': 100}, 'employees')
        tree = parse("employees[age>40 | salary>99].name", self.schema.specs['employee'])

        aggregation, spec, is_aggregate = tree.aggregation(employee_id)
        self.assertEquals([
            {'$match': {'$and': [{'_parent_field_name': 'employees'},
                                 {'_parent_canonical_url': '/'}]}},
            {'$match': {'$or' : [ {'age': {'$gt': 40}}, {'salary': {'$gt': 99}}]}},
            {'$project': {'name': True}}], aggregation)
        self.assertEquals(self.schema.specs['employee'].fields['name'],
                          spec)

    def test_aggregation_self(self):
        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 41}, 'employees')
        division_id = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 10}, 'divisions')

        tree = parse("self.division[name='sales'].yearly_sales", self.schema.specs['employee'])

        aggregation, spec, is_aggregate = tree.aggregation(employee_id)
        self.assertEquals([
            {'$match': {'_id': self.schema.decodeid(employee_id)}},
            {'$lookup': {'as': '_field_division',
                        'foreignField': '_id',
                        'from': 'resource_division',
                        'localField': 'division'}},
            {'$group': {'_id': '$_field_division'}},
            {'$unwind': '$_id'},
            {'$replaceRoot': {'newRoot': '$_id'}},
            {'$match': {'name': {'$eq': 'sales'}}},
            {'$project': {'yearly_sales': True}}], aggregation)
        self.assertEquals(self.schema.specs['division'].fields['yearly_sales'],
                          spec)


    def test_aggregates(self):
        # entities[name=self.other[resolve='me',first=True]]
        # parents[name='ned'].entities[averagePay>average(self.children[self.type='boss'].pay)]
        pass

    def test_calc_operators(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "int")
        employee_spec.fields["tax"] = Field("tax", "int")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10, 'tax': 2}, 'employees')

        tree = parse("self.salary + self.tax", employee_spec)
        self.assertEquals(12, tree.calculate(employee_id_1))

        tree = parse("self.salary - self.tax", employee_spec)
        self.assertEquals(8, tree.calculate(employee_id_1))

        tree = parse("self.salary * self.tax", employee_spec)
        self.assertEquals(20, tree.calculate(employee_id_1))

        tree = parse("self.salary / self.tax", employee_spec)
        self.assertEquals(5, tree.calculate(employee_id_1))

        tree = parse("self.salary < self.tax", employee_spec)
        self.assertTrue(False is tree.calculate(employee_id_1))

        tree = parse("self.salary > self.tax", employee_spec)
        self.assertTrue(True is tree.calculate(employee_id_1))

        tree = parse("self.salary = self.tax", employee_spec)
        self.assertTrue(False is tree.calculate(employee_id_1))

        tree = parse("self.salary <= self.tax", employee_spec)
        self.assertTrue(False is tree.calculate(employee_id_1))

        tree = parse("self.salary >= self.tax", employee_spec)
        self.assertTrue(True is tree.calculate(employee_id_1))

    def test_calc_nones(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "int")
        employee_spec.fields["tax"] = Field("tax", "int")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10, 'tax': None}, 'employees')

        tree = parse("self.salary + self.tax", employee_spec)
        self.assertEquals(10, tree.calculate(employee_id_1))

        tree = parse("self.salary - self.tax", employee_spec)
        self.assertEquals(10, tree.calculate(employee_id_1))

        tree = parse("self.salary * self.tax", employee_spec)
        self.assertEquals(None, tree.calculate(employee_id_1))

        # Going with None instead of NaN for now
        tree = parse("self.salary / self.tax", employee_spec)
        self.assertEquals(None, tree.calculate(employee_id_1))

    def test_function_call_param_list(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "float")
        employee_spec.fields["tax"] = Field("tax", "float")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10.6, 'tax': 2.4}, 'employees')

        tree = parse("round(self.salary + self.tax, 2)", employee_spec)
        self.assertEquals(13, tree.calculate(employee_id_1))

    def test_function_basic(self):
        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned'}, 'employees')

        employee_spec = self.schema.specs['employee']
        tree = parse("round(14.14)", employee_spec)
        self.assertEquals(14, tree.calculate(employee_id_1))

    def test_function_call_param_list_multiple_calcs(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "float")
        employee_spec.fields["tax"] = Field("tax", "float")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10.6, 'tax': 2.4}, 'employees')

        tree = parse("round(self.salary) + round(self.tax)", employee_spec)
        self.assertEquals(13, tree.calculate(employee_id_1))

    def test_function_within_a_function(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "float")
        employee_spec.fields["tax"] = Field("tax", "float")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10.12345}, 'employees')

        tree = parse("round(round(self.salary, 4), 3)", employee_spec)
        self.assertEquals(10.123, tree.calculate(employee_id_1))

    def test_ternary_condition(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "float")
        employee_spec.fields["tax"] = Field("tax", "float")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10.6, 'tax': 2.4}, 'employees')

        tree = parse("self.salary < 2 -> 5 : 10", employee_spec)
        self.assertEquals(10, tree.calculate(employee_id_1))

        tree = parse("self.salary > 10 -> 5 : 10", employee_spec)
        self.assertEquals(5, tree.calculate(employee_id_1))

        tree = parse("(self.salary + 5) > 15 -> 5 : 10", employee_spec)
        self.assertEquals(5, tree.calculate(employee_id_1))

    def test_ternary_condition_rhs(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "float")
        employee_spec.fields["tax"] = Field("tax", "float")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10.6, 'tax': 2.4}, 'employees')

        tree = parse("self.salary > self.tax -> 'greater' : 'wrong'", employee_spec)
        self.assertEquals('greater', tree.calculate(employee_id_1))

        tree = parse("self.salary < (self.tax + 10) -> 'less' : 'wrong'", employee_spec)
        self.assertEquals('less', tree.calculate(employee_id_1))

    def test_ternary_condition_resource(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "float")
        employee_spec.fields["tax"] = Field("tax", "float")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10.6, 'tax': 2.4}, 'employees')

        tree = parse("self.salary > 10 -> self.salary : 0", employee_spec)
        self.assertEquals(10.6, tree.calculate(employee_id_1))

        tree = parse("self.salary < 10 -> 0 : self.tax", employee_spec)
        self.assertEquals(2.4, tree.calculate(employee_id_1))

    def test_ternary_resource_plus_const(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "float")
        employee_spec.fields["tax"] = Field("tax", "float")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10.6, 'tax': 2.4}, 'employees')

        tree = parse("self.salary > 10 -> 11 : self.salary", employee_spec)
        self.assertEquals(11, tree.calculate(employee_id_1))

        tree = parse("self.salary < 10 -> self.tax: 12", employee_spec)
        self.assertEquals(12, tree.calculate(employee_id_1))

    def test_switch(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary_range"] = Field("salary_range", "str")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary_range': 'upper'}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'salary_range': 'lower'}, 'employees')

        tree = parse("self.salary_range -> ('upper': 20.0, 'lower': 40.0)", employee_spec)
        self.assertEquals(20.0, tree.calculate(employee_id_1))
        self.assertEquals(40.0, tree.calculate(employee_id_2))

    def test_switch_longer_list(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary_range"] = Field("salary_range", "str")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary_range': 'upper'}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'salary_range': 'lower'}, 'employees')

        tree = parse("self.salary_range -> ('upper': 20.0, 'lower': 40.0, 'middling': 30.0)", employee_spec)
        self.assertEquals(20.0, tree.calculate(employee_id_1))
        self.assertEquals(40.0, tree.calculate(employee_id_2))

    def test_switch_basic(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary_range"] = Field("salary_range", "str")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary_range': 'upper'}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'salary_range': 'lower'}, 'employees')

        tree = parse("self.salary_range -> ('upper': 20.0)", employee_spec)
        self.assertEquals(20.0, tree.calculate(employee_id_1))
        self.assertEquals(None, tree.calculate(employee_id_2))

    def test_switch_field_refs(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["upper_salary"] = Field("salary", "float")
        employee_spec.fields["lower_salary"] = Field("salary", "float")
        employee_spec.fields["salary_range"] = Field("salary_range", "str")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary_range': 'upper', 'upper_salary': 50000, 'lower_salary': 40000}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'salary_range': 'lower', 'upper_salary': 30000, 'lower_salary': 20000}, 'employees')

        tree = parse("self.salary_range -> ('upper': self.upper_salary, 'lower': self.lower_salary)", employee_spec)
        self.assertEquals(50000, tree.calculate(employee_id_1))
        self.assertEquals(20000, tree.calculate(employee_id_2))

    def test_switch_calcs(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["upper_salary_level"] = Field("salary", "float")
        employee_spec.fields["lower_salary_level"] = Field("salary", "float")
        employee_spec.fields["salary"] = Field("salary", "float")
        employee_spec.fields["salary_range"] = Field("salary_range", "str")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary_range': 'upper', 'salary': 50000, 'upper_salary_level': 0.4, 'lower_salary_level': 0.2}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'salary_range': 'lower', 'salary': 30000, 'upper_salary_level': 0.5, 'lower_salary_level': 0.3}, 'employees')

        tree = parse("self.salary_range -> ('upper': (self.salary * self.upper_salary_level), 'lower': (self.salary * self.lower_salary_level))", employee_spec)
        self.assertEquals(20000, tree.calculate(employee_id_1))
        self.assertEquals(9000, tree.calculate(employee_id_2))

    def test_math_functions(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "float")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 20}, 'employees')
        self.schema.insert_resource('employee', {'name': 'bob', 'salary': 10}, 'employees')
        self.schema.insert_resource('employee', {'name': 'bil', 'salary': 30}, 'employees')

        # max
        tree = parse("max(employees.salary)", employee_spec)
        self.assertEquals(30, tree.calculate(employee_id_1))

        # min
        tree = parse("min(employees.salary)", employee_spec)
        self.assertEquals(10, tree.calculate(employee_id_1))

        # avg
        tree = parse("average(employees.salary)", employee_spec)
        self.assertEquals(20, tree.calculate(employee_id_1))

        # sum
        tree = parse("sum(employees.salary)", employee_spec)
        self.assertEquals(60, tree.calculate(employee_id_1))

    def test_extra_math(self):
        employee_spec = self.schema.specs['employee']
        employee_spec.fields["salary"] = Field("salary", "float")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'salary': 20.1234}, 'employees')
        self.schema.insert_resource('employee', {'name': 'ned', 'salary': 10.5678}, 'employees')
        self.schema.insert_resource('employee', {'name': 'bil', 'salary': 30.7777}, 'employees')

        tree = parse("round(sum(employees.salary), 2) + round(max(employees.salary))", employee_spec)
        self.assertEquals(92.47, tree.calculate(employee_id_1))


        tree = parse("round(sum(employees[name='ned'].salary), 2) + round(max(employees.salary))", employee_spec)
        self.assertEquals(61.69, tree.calculate(employee_id_1))

        tree = parse("round(sum(employees[name='ned'].salary), 2) + round(max(employees[name='ned'].salary))", employee_spec)
        self.assertEquals(50.69, tree.calculate(employee_id_1))

        # filter nones
        # filter generic aggregates (filter(aggregate, name='paul', age>20))
        # space out range
        # cap (ceil, floor) aggregates
        # min max range

    def test_return_type(self):
        employee_spec = self.schema.specs['employee']
        division_spec = self.schema.specs['division']

        tree = parse("employees[age>40].division[name='sales'].yearly_sales", employee_spec)
        self.assertEquals(division_spec.fields['yearly_sales'], tree.infer_type())
        self.assertTrue(tree.is_collection())

        tree = parse("employees[age>40].division[name='sales']", employee_spec)
        # it's a link spec
        self.assertEquals(division_spec, tree.infer_type())
        self.assertTrue(tree.is_collection())

        tree = parse("self.division", employee_spec)
        self.assertEquals(division_spec, tree.infer_type())
        self.assertFalse(tree.is_collection())

    def test_root_collection_aggregates(self):
        tree = parse("employees.division", self.schema.specs['division'])

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')
        division_id_2 = self.schema.insert_resource('division', {'name': 'marketting', 'yearly_sales': 20}, 'divisions')

        self.schema.update_resource_fields('employee', employee_id_1, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_2, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_3, {'division': division_id_2})

        result = tree.calculate(division_id_1)
        self.assertEquals(2, len(result))

    def test_calculate_toplevel_rootresourceref(self):
        tree = parse("employees[name='bob']", self.schema.specs['division'])

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        result = tree.calculate(division_id_1)
        self.assertEquals(1, len(result))
        self.assertEquals('bob', result[0]['name'])

        # work with either resource type as starting point
        result = tree.calculate(employee_id_1)
        self.assertEquals(1, len(result))
        self.assertEquals('bob', result[0]['name'])

    def test_parent_link(self):
        tree = parse("self.parent_division_sections", self.schema.specs['section'])

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')
        division_id_2 = self.schema.insert_resource('division', {'name': 'marketting', 'yearly_sales': 20}, 'divisions')

        section_id_1 = self.schema.insert_resource('section', {'name': 'appropriation'}, parent_type='division', parent_id=division_id_1, parent_field_name='sections')

        result = tree.calculate(section_id_1)
        self.assertEquals('sales', result['name'])

    def test_parse_url(self):
        tree = parse("employees", self.schema.root)

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')
        division_id_2 = self.schema.insert_resource('division', {'name': 'marketting', 'yearly_sales': 20}, 'divisions')

        self.schema.update_resource_fields('employee', employee_id_1, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_2, {'division': division_id_1})
        self.schema.update_resource_fields('employee', employee_id_3, {'division': division_id_2})

        result = tree.calculate(employee_id_1)
        self.assertEquals(3, len(result))

    def test_linkcollection(self):
        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')
        division_id_2 = self.schema.insert_resource('division', {'name': 'marketting', 'yearly_sales': 20}, 'divisions')

        tree = parse('self.parttimers', self.schema.specs['division'])
        self.assertEquals([], tree.calculate(division_id_1))

        self.schema.create_linkcollection_entry('division', division_id_1, 'parttimers', employee_id_1)

        self.assertEquals([{
            '_id': self.schema.decodeid(employee_id_1),
            '_grants': [],
            '_canonical_url': '/employees/%s' % employee_id_1,
            '_parent_canonical_url': '/',
            '_parent_field_name': 'employees',
            '_parent_id': None,
            '_parent_type': 'root',
            'age': 41,
            'name': 'ned'}], tree.calculate(division_id_1))

    def test_linkcollection_filter(self):
        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')

        self.schema.create_linkcollection_entry('division', division_id_1, 'parttimers', employee_id_1)
        self.schema.create_linkcollection_entry('division', division_id_1, 'parttimers', employee_id_2)
        self.schema.create_linkcollection_entry('division', division_id_1, 'parttimers', employee_id_3)

        tree = parse('self.parttimers[age>30]', self.schema.specs['division'])

        self.assertEquals([
            {'_id': self.schema.decodeid(employee_id_1),
             '_grants': [],
             '_canonical_url': '/employees/%s' % employee_id_1,
             '_parent_canonical_url': '/',
             '_parent_field_name': 'employees',
             '_parent_id': None,
             '_parent_type': 'root',
             'age': 41,
             'name': 'ned'},
            {'_id': self.schema.decodeid(employee_id_2),
             '_grants': [],
             '_canonical_url': '/employees/%s' % employee_id_2,
             '_parent_canonical_url': '/',
             '_parent_field_name': 'employees',
             '_parent_id': None,
             '_parent_type': 'root',
             'age': 31,
             'name': 'bob'},
            ], tree.calculate(division_id_1))


    def test_linkcollection_reverse_aggregation(self):
        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')

        self.schema.create_linkcollection_entry('division', division_id_1, 'parttimers', employee_id_1)
        self.schema.create_linkcollection_entry('division', division_id_1, 'parttimers', employee_id_2)
        self.schema.create_linkcollection_entry('division', division_id_1, 'parttimers', employee_id_3)

        tree = parse('self.parttimers.age', self.schema.specs['division'])

        # testing reverse aggregation when a resource in the "middle" of the calc is added/removed/updated
        # Note: the first aggregation is the "tracker" aggregation
        self.assertEqual([
            [
                {'$lookup': {'as': '_field_parttimers',
                'foreignField': 'parttimers._id',
                'from': 'resource_division',
                'localField': '_id'}},
                {'$group': {'_id': '$_field_parttimers'}},
                {'$unwind': '$_id'},
                {'$replaceRoot': {'newRoot': '$_id'}}],
            [
                {'$match': {'_id': self.schema.decodeid(employee_id_2)}},
                {'$lookup': {'as': '_field_parttimers',
                                'foreignField': 'parttimers._id',
                                'from': 'resource_division',
                                'localField': '_id'}},
                {'$group': {'_id': '$_field_parttimers'}},
                {'$unwind': '$_id'},
                {'$replaceRoot': {'newRoot': '$_id'}}
            ],
        ], tree.build_reverse_aggregations(self.schema.specs['employee'], employee_id_2, 'division', 'parttimers_age'))

    def test_dependencies(self):
        employee_spec = self.schema.specs['employee']
        division_spec = self.schema.specs['division']

        tree = parse("employees[age>40].division[name='sales'].yearly_sales", employee_spec)
        self.assertEquals(
            tree.get_resource_dependencies(),
            {
                'root.employees',
                'employee.division',
                'employee.age',
                'division.name',
                'division.yearly_sales',
            })

        tree = parse("self.age", employee_spec)
        self.assertEquals(
            tree.get_resource_dependencies(),
            {
                'employee.age',
            })

        tree = parse("self.parttimers", division_spec)
        self.assertEquals(
            tree.get_resource_dependencies(),
            {
                'division.parttimers',
            })

        tree = parse("self.division.parttimers", employee_spec)
        self.assertEquals(
            tree.get_resource_dependencies(),
            {
                'employee.division',
                'division.parttimers',
            })

        # referenced calc
        self.schema.add_calc(employee_spec, 'my_division', 'self.division.link_employee_division')
        tree = parse("self.my_division", employee_spec)
        self.assertEquals(
            tree.get_resource_dependencies(),
            {
                'employee.my_division',
            })

        # reverse link
        tree = parse("self.division.link_employee_division.name", employee_spec)
        self.assertEquals(
            tree.get_resource_dependencies(),
            {
                'employee.division',
                'employee.name',
            })

    def test_gte(self):
        employee_spec = self.schema.specs['employee']
        division_spec = self.schema.specs['division']

        tree = parse("employees[age>=40]", employee_spec)

        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 40}, 'employees')

        calculated = tree.calculate(employee_id)
        self.assertEquals([{
            '_id': self.schema.decodeid(employee_id),
            '_grants': [],
            '_canonical_url': '/employees/%s' % employee_id,
            'name': 'sailor',
            'age': 40,
            '_parent_canonical_url': '/',
            '_parent_field_name': 'employees',
            '_parent_id': None,
            '_parent_type': 'root',
        }], calculated)

    def test_lte(self):
        employee_spec = self.schema.specs['employee']
        division_spec = self.schema.specs['division']

        tree = parse("employees[age<=40]", employee_spec)

        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 40}, 'employees')

        calculated = tree.calculate(employee_id)
        self.assertEquals([{
            '_id': self.schema.decodeid(employee_id),
            '_grants': [],
            '_canonical_url': '/employees/%s' % employee_id,
            'name': 'sailor',
            'age': 40,
            '_parent_canonical_url': '/',
            '_parent_field_name': 'employees',
            '_parent_id': None,
            '_parent_type': 'root',
        }], calculated)

    def test_search_filter(self):
        employee_spec = self.schema.specs['employee']

        tree = parse_filter("age<=40", employee_spec)

        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 40}, 'employees')

        self.assertEqual({'age': {'$lte': 40}}, tree.condition_aggregation(employee_spec, employee_id))

    def test_search_filter_multiple(self):
        employee_spec = self.schema.specs['employee']

        tree = parse_filter("age<=40 & name='sailor'", employee_spec)

        employee_id = self.schema.insert_resource(
            'employee', {'name': 'sailor', 'age': 40}, 'employees')

        self.assertEqual(
            {'$and': [{'age': {'$lte': 40}}, {'name': {'$eq': 'sailor'}}]},
            tree.condition_aggregation(employee_spec, employee_id))

    def test_search_filter_commas_are_nice(self):
        employee_spec = self.schema.specs['employee']

        tree = parse_filter("age<=40, name='sailor'", employee_spec)

        employee_id = self.schema.insert_resource(
            'employee', {'name': 'sailor', 'age': 40}, 'employees')

        self.assertEqual(
            {'$or': [{'age': {'$lte': 40}}, {'name': {'$eq': 'sailor'}}]},
            tree.condition_aggregation(employee_spec, employee_id))

    def test_search_filter_like_string(self):
        employee_spec = self.schema.specs['employee']

        tree = parse_filter("name~'sam'", employee_spec)

        self.assertEqual(
            {'name': {'$options': 'i', '$regex': 'sam'}},
            tree.condition_aggregation(employee_spec, None))

    def test_search_filter_like_string_or(self):
        employee_spec = self.schema.specs['employee']

        tree = parse_filter("name~'sam',name~'bob'", employee_spec)

        self.assertEqual(
            {'$or': [{'name': {'$options': 'i', '$regex': 'sam'}},
                     {'name': {'$options': 'i', '$regex': 'bob'}}]},
            tree.condition_aggregation(employee_spec, None))

    def test_search_filter_commas_and_or(self):
        employee_spec = self.schema.specs['employee']

        tree = parse_filter("age<=40, name='sailor' | name='weaver'", employee_spec)

        employee_id = self.schema.insert_resource(
            'employee', {'name': 'sailor', 'age': 40}, 'employees')

        self.assertEqual(
            {'$or': [{'$or': [{'age': {'$lte': 40}}, {'name': {'$eq': 'sailor'}}]},
                     {'name': {'$eq': 'weaver'}}]},
            tree.condition_aggregation(employee_spec, employee_id))

    def test_search_filter_resource_ref(self):
        employee_spec = self.schema.specs['employee']

        tree = parse("name=self.pseudoname", employee_spec)

        employee_id = self.schema.insert_resource(
            'employee', {'name': 'sailor', 'pseudoname': 'bob'}, 'employees')

        self.assertEqual(
            {'name': {'$eq': 'bob'}},
            tree.condition_aggregation(employee_spec, employee_id))

    def test_resources_in_different_collections(self):
        tree = parse("employees", self.schema.root)

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'age': 41}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'age': 31}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'fred', 'age': 21}, 'employees')

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'yearly_sales': 100}, 'divisions')

        employee_id_4 = self.schema.insert_resource('employee', {'name': 'pete', 'age': 60}, 'partners', 'division', division_id_1)

        result = tree.calculate(employee_id_1)
        self.assertEquals(3, len(result))

        employee_spec = self.schema.specs['employee']
        tree = parse("max(employees.age)", employee_spec)
        self.assertEquals(41, tree.calculate(employee_id_1))

    def test_validate_root(self):
        try:
            tree = parse("schmemployees", self.schema.root)
            tree.validate()
            self.fail("should have thrown")
        except SyntaxError as se:
            self.assertEqual("Cannot parse expression: schmemployees", str(se))

    def test_validate_condition(self):
        try:
            tree = parse("employees[nope>21]", self.schema.root)
            tree.validate()
            self.fail("should have thrown")
        except SyntaxError as se:
            self.assertEqual("Resource employee has no field nope", str(se))

    def test_calc_result(self):
        employee_spec = self.schema.specs['employee']
        division_spec = self.schema.specs['division']

        tree = parse("max(employees[age>=40].age) + 15", employee_spec)

        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 40}, 'employees')

        calculated = tree.calculate(employee_id)
        self.assertEqual(55, calculated)

    def test_basic_calc_result(self):
        employee_spec = self.schema.specs['employee']
        division_spec = self.schema.specs['division']

        tree = parse("10 + (15 / 3)", employee_spec)

        employee_id = self.schema.insert_resource('employee', {'name': 'sailor', 'age': 40}, 'employees')

        calculated = tree.calculate(employee_id)
        self.assertEqual(15, calculated)

    def test_calc_datetime(self):
        employee_spec = self.schema.specs['employee']
        division_spec = self.schema.specs['division']

        employee_spec.fields["created"] = Field("created", "datetime")

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'created': "2021-12-01"}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'created': "2021-12-01"}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'bil', 'created': "2021-12-01"}, 'employees')

        tree = parse("self.created - days(2)", employee_spec)

        calculated = tree.calculate(employee_id_1)
        self.assertEqual(datetime(2021, 11, 29), calculated)

        tree = parse("self.created - hours(2)", employee_spec)

        calculated = tree.calculate(employee_id_1)
        self.assertEqual(datetime(2021, 11, 30, 22), calculated)

        tree = parse("self.created - minutes(2)", employee_spec)

        calculated = tree.calculate(employee_id_1)
        self.assertEqual(datetime(2021, 11, 30, 23, 58), calculated)


    def test_calc_datetime_comparison(self):
        employee_spec = self.schema.specs['employee']
        division_spec = self.schema.specs['division']

        employee_spec.fields["created"] = Field("created", "datetime")
        division_spec.fields["cutoff"] = Field("cutoff", "datetime")

        division_id_1 = self.schema.insert_resource('division', {'name': 'sales', 'cutoff': "2021-12-12"}, 'divisions')

        employee_id_1 = self.schema.insert_resource('employee', {'name': 'ned', 'created': "2021-12-01", "division": division_id_1}, 'employees')
        employee_id_2 = self.schema.insert_resource('employee', {'name': 'bob', 'created': "2021-12-14", "division": division_id_1}, 'employees')
        employee_id_3 = self.schema.insert_resource('employee', {'name': 'bil', 'created': "2021-12-24", "division": division_id_1}, 'employees')

        tree = parse("self.link_employee_division[created>self.cutoff]", division_spec)

        calculated = tree.calculate(division_id_1)
        self.assertEqual(['bob', 'bil'], [e['name'] for e in calculated])

    def test_add_reverse_links(self):
        section_spec = self.schema.specs['section']

        tree = parse('self.name + (self.parent_division_sections.name)', section_spec)

    def test_replace_whitespace_with_spaces(self):
        section_spec = self.schema.specs['section']

        tree = parse('self.name + \t(\nself.parent_division_sections.name\n)', section_spec)
Exemplo n.º 5
0
class ApiTest(unittest.TestCase):
    def setUp(self):
        self.maxDiff = None
        client = MongoClient()
        client.drop_database('metaphor2_test_db')
        self.db = client.metaphor2_test_db
        self.schema = Schema(self.db)

        self.db.metaphor_schema.insert_one({
            "specs" : {
                "employee" : {
                    "fields" : {
                        "name" : {
                            "type" : "str"
                        },
                        "age": {
                            "type": "int"
                        },
                        "division": {
                            "type": "link",
                            "target_spec_name": "division",
                        },
                        "division_link": {
                            "type": "calc",
                            "calc_str": "self.division",
                        },
                        "parttime_division_name": {
                            "type": "calc",
                            "calc_str": "self.name + (self.parent_section_parttimers.parent_division_sections.name)",
                        },
                    },
                },
                "division": {
                    "fields": {
                        "name": {
                            "type": "str",
                        },
                        "yearly_sales": {
                            "type": "int",
                        },
                        "sections": {
                            "type": "orderedcollection",
                            "target_spec_name": "section",
                        },
                        "primary_sections": {
                            "type": "calc",
                            "calc_str": "self.sections[name='primary']",
                        },
                        "average_section_total": {
                            "type": "calc",
                            "calc_str": "average(self.sections.section_total)",
                        },
                        "average_bracket_calc": {
                            "type": "calc",
                            "calc_str": "average(self.sections.section_total) + ((10 + sum(self.sections.section_total)) / 5)",
                        },
                        "older_employees": {
                            "type": "calc",
                            "calc_str": "self.link_employee_division[age>40]",
                        },
                    },
                },
                "section": {
                    "fields": {
                        "name": {
                            "type": "str",
                        },
                        "section_total": {
                            "type": "int",
                        },
                        "division_name": {
                            "type": "calc",
                            "calc_str": "self.parent_division_sections.name",
                        },
                        "distance_from_average": {
                            "type": "calc",
                            "calc_str": "self.section_total - average(self.parent_division_sections.sections.section_total)",
                        },
                        "parttimers": {
                            "type": "orderedcollection",
                            "target_spec_name": "employee",
                        },
                    },
                },
            },
            "root": {
                "employees": {
                    "type": "collection",
                    "target_spec_name": "employee",
                },
                "divisions": {
                    "type": "collection",
                    "target_spec_name": "division",
                }
            },
        })
        self.schema.load_schema()

        self.api = Api(self.schema)

    def test_calc_results(self):
        employee_id_1 = self.api.post('employees', {'name': 'ned', 'age': 41})
        employee_id_2 = self.api.post('employees', {'name': 'bob', 'age': 31})
        employee_id_3 = self.api.post('employees', {'name': 'fred', 'age': 21})

        division_id_1 = self.api.post('divisions', {'name': 'sales', 'yearly_sales': 100})

        self.api.patch('employees/%s' % employee_id_1, {'division': division_id_1})
        self.api.patch('employees/%s' % employee_id_2, {'division': division_id_1})

        section_id_1 = self.api.post('/divisions/%s/sections' % division_id_1, {'name': 'primary', 'section_total': 120})
        section_id_2 = self.api.post('/divisions/%s/sections' % division_id_1, {'name': 'secondary', 'section_total': 90})

        # test simple type
        section_1 = self.api.get('/divisions/%s/sections/%s' % (division_id_1, section_id_1))

        self.assertEquals('sales', section_1['division_name'])

        # test resource type
        division_1 = self.api.get('/divisions/%s' % division_id_1)
        self.assertEquals({
            '_meta': {'is_collection': False, 'spec': {'name': 'division'}},
            'id': division_id_1,
            'link_employee_division': '/divisions/%s/link_employee_division' % division_id_1,
            'name': 'sales',
            'average_section_total': 105.0,
            'average_bracket_calc': 149.0,
            'older_employees': '/divisions/%s/older_employees' % division_id_1,
            'primary_sections': '/divisions/%s/primary_sections' % division_id_1,
            'sections': '/divisions/%s/sections' % division_id_1,
            'self': '/divisions/%s' % division_id_1,
            'yearly_sales': 100}, division_1)

        # test calculated resource collection endpoint
        older_employees = self.api.get('/divisions/%s/older_employees' % division_id_1)['results']
        self.assertEquals(1, len(older_employees))

    def test_calc_link_1(self):
        employee_id_1 = self.api.post('employees', {'name': 'ned', 'age': 41})

    def test_calc_link(self):
        division_id_1 = self.api.post('divisions', {'name': 'sales', 'yearly_sales': 100})
        employee_id_1 = self.api.post('employees', {'name': 'ned', 'age': 41, 'division': division_id_1})

        self.assertEqual({
            '_meta': {'is_collection': False, 'spec': {'name': 'employee'}},
            'age': 41,
            'division': '/divisions/%s' % division_id_1,
            'division_link': '/divisions/%s' % division_id_1,
            'id': employee_id_1,
            'name': 'ned',
            'parent_section_parttimers': None,
            'parttime_division_name': None,
            'self': '/employees/%s' % employee_id_1}
            , self.api.get('/employees/%s' % employee_id_1))

    def test_calc_parent_links(self):
        division_id_1 = self.api.post('divisions', {'name': 'sales', 'yearly_sales': 100})
        section_1 = self.api.post('/divisions/%s/sections' % (division_id_1,), {'name': 'hr'})
        parttimer_1 = self.api.post('/divisions/%s/sections/%s/parttimers' % (division_id_1, section_1), {'name': 'bob'})

        parttimer = self.api.get('/divisions/%s/sections/%s/parttimers/%s' % (division_id_1, section_1, parttimer_1))

        self.assertEqual('bobsales', parttimer['parttime_division_name'])