Skip to content

Commit

Permalink
Merge pull request #215 from 18F/fix-codes-endpoint-issues
Browse files Browse the repository at this point in the history
Fix codes endpoint issues
  • Loading branch information
Colin Craig authored Dec 16, 2016
2 parents 45a8da5 + e553632 commit 73549ea
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 39 deletions.
49 changes: 17 additions & 32 deletions crime_data/common/marshmallow_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ class Meta:
class NibrsWeaponTypeSchema(ma.ModelSchema):
class Meta:
model = models.NibrsWeaponType
exclude = ('weapons', )
exclude = ('weapon_id', 'weapons', )
ordered = True


class NibrsWeaponSchema(ma.ModelSchema):
Expand Down Expand Up @@ -181,9 +182,7 @@ class Meta:
class RefCitySchema(ma.ModelSchema):
class Meta:
model = models.RefCity
exclude = (
'city_id',
'state', )
exclude = ('state', )
ordered = True

state = ma.Nested(RefStateSchema)
Expand All @@ -192,15 +191,13 @@ class Meta:
class RefContinentSchema(ma.ModelSchema):
class Meta:
model = models.RefContinent
exclude = ('continent_id', )
ordered = True


class RefCountySchema(ma.ModelSchema):
class Meta:
model = models.RefCounty
exclude = (
'county_id',
'state',
'agencies',
'agency_associations', )
Expand All @@ -211,10 +208,7 @@ class Meta:

class RefCountrySchema(ma.ModelSchema):
class Meta:
model = models.RefCounty
exclude = (
'country_id',
'continent', )
model = models.RefCountry
ordered = True

state = ma.Nested(RefContinentSchema)
Expand All @@ -223,12 +217,17 @@ class Meta:
class RefMsaSchema(ma.ModelSchema):
class Meta:
model = models.RefMsa
exclude = ('msa_id', )
ordered = True

state = ma.Nested(RefContinentSchema)


class RefMetroDivision(ma.ModelSchema):
class Meta:
model = models.RefMetroDivision
ordered = True


class RefPopulationFamilySchema(ma.ModelSchema):
class Meta:
model = models.RefPopulationFamily
Expand All @@ -252,9 +251,7 @@ class Meta:
class RefAgencyTypeSchema(ma.ModelSchema):
class Meta:
model = models.RefAgencyType
exclude = (
'agency_type_id',
'default_pop_family_id', )
exclude = ('default_pop_family_id', )


class RefDepartmentSchema(ma.ModelSchema):
Expand All @@ -274,7 +271,6 @@ class Meta:
class RefTribeSchema(ma.ModelSchema):
class Meta:
model = models.RefTribe
exclude = ('tribe_id', )


class RefAgencyCountySchema(ma.ModelSchema):
Expand All @@ -291,7 +287,6 @@ class Meta:
class RefUniversitySchema(ma.ModelSchema):
class Meta:
model = models.RefUniversity
exclude = ('university_id', )


class RefAgencySchema(ma.ModelSchema, ArgumentsSchema):
Expand Down Expand Up @@ -413,9 +408,7 @@ class Meta:
class NibrsPropLossTypeSchema(ma.ModelSchema):
class Meta:
model = models.NibrsPropLossType
exclude = (
'prop_loss_id',
'property', )
exclude = ('property', )


class NibrsPropertySchema(ma.ModelSchema):
Expand All @@ -425,7 +418,7 @@ class Meta:
'incident',
'property_id', )

prop_loss = ma.Nested(NibrsPropLossTypeSchema)
prop_loss = ma.Nested(NibrsPropLossTypeSchema, exclude=('prop_loss_id', ))


class NibrsAgeSchema(ma.ModelSchema):
Expand Down Expand Up @@ -463,13 +456,6 @@ class Meta:
'victims', )


class NibrsWeaponTypeSchema(ma.ModelSchema):
class Meta:
model = models.NibrsWeaponType
exclude = ('weapon_id', )
ordered = True


class NibrsActivityTypeSchema(ma.ModelSchema):
class Meta:
model = models.NibrsActivityType
Expand Down Expand Up @@ -544,7 +530,7 @@ def check_age(self, out_data):
class NibrsArrestTypeSchema(ma.ModelSchema):
class Meta:
model = models.NibrsArrestType
exclude = ('arrest_type_id', )
exclude = ('arrest_type_id', 'arrestees', )
ordered = True


Expand Down Expand Up @@ -593,7 +579,7 @@ class Meta:
class NibrsAssignmentTypeSchema(ma.ModelSchema):
class Meta:
model = models.NibrsAssignmentType
exclude = ('assignment_type_id', )
exclude = ('assignment_type_id', 'victims', )
ordered = True


Expand All @@ -614,7 +600,7 @@ class Meta:
class NibrsCriminalActTypeSchema(ma.ModelSchema):
class Meta:
model = models.NibrsCriminalActType
exclude = ('criminal_act_id', )
exclude = ('criminal_act_id', 'criminal_acts', )
ordered = True


Expand All @@ -635,7 +621,7 @@ class Meta:
class NibrsRelationshipSchema(ma.ModelSchema):
class Meta:
model = models.NibrsRelationship
exclude = ('relationship_id', )
exclude = ('relationship_id', 'relationships', )
ordered = True


Expand All @@ -649,7 +635,6 @@ class Meta:
class OffenseClassificationSchema(ma.ModelSchema):
class Meta:
model = models.OffenseClassification
exclude = ('classification_id', )
ordered = True


Expand Down
6 changes: 3 additions & 3 deletions crime_data/resources/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
'nibrs_weapon_type': models.NibrsWeaponType,
'offense_classification': models.OffenseClassification,
'ref_agency_type': models.RefAgencyType,
'ref_city': models.RefCity,
'ref_continent': models.RefContinent,
'ref_county': models.RefCounty,
'ref_country': models.RefCountry,
'ref_metro_division': models.RefMetroDivision,
'ref_msa': models.RefMsa,
'ref_population_group': models.RefPopulationGroup,
'ref_race': models.RefRace,
Expand Down Expand Up @@ -80,10 +80,10 @@
'nibrs_weapon_type': marshmallow_schemas.NibrsWeaponTypeSchema,
'offense_classification': marshmallow_schemas.OffenseClassificationSchema,
'ref_agency_type': marshmallow_schemas.RefAgencyTypeSchema,
'ref_city': marshmallow_schemas.RefCitySchema,
'ref_continent': marshmallow_schemas.RefContinentSchema,
'ref_county': marshmallow_schemas.RefCountySchema,
'ref_country': marshmallow_schemas.RefCountrySchema,
'ref_metro_division': marshmallow_schemas.RefMetroDivision,
'ref_msa': marshmallow_schemas.RefMsaSchema,
'ref_population_group': marshmallow_schemas.RefPopulationGroupSchema,
'ref_race': marshmallow_schemas.RefRaceSchema,
Expand Down Expand Up @@ -120,4 +120,4 @@ def get(self, args, code_table, output=None):
if output == 'csv':
return self.as_csv_response(codes, code_table, args)
else:
return self.with_metadata(codes, args)
return jsonify(self.schema.dump(codes).data)
66 changes: 62 additions & 4 deletions tests/functional/test_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,76 @@


class TestCodesIndex:
"""Test the /codes URL endpoint"""

def test_codes_index_exists(self, testapp):
res = testapp.get('/codes')
assert res.status_code == 200


class TestCodesEndpoint:
@pytest.mark.parametrize("table", CODE_MODELS)
def test_codes_endpoint_exists(self, testapp, table):
"""Test the /codes/* methods"""

@pytest.mark.parametrize('table,id_col', [
('asr_age_range', 'age_range_code'),
('asr_ethnicity', 'ethnicity_code'),
('asr_offense', 'offense_code'),
('asr_offense_category', 'offense_cat_code'),
('crime_type', 'crime_type_id'),
('nibrs_activity_type', 'activity_type_code'),
('nibrs_age', 'age_code'),
('nibrs_arrest_type', 'arrest_type_code'),
('nibrs_assignment_type', 'assignment_type_code'),
('nibrs_bias_list', 'bias_code'),
('nibrs_circumstance', 'circumstances_code'),
('nibrs_criminal_act_type', 'criminal_act_code'),
('nibrs_cleared_except', 'cleared_except_code'),
('nibrs_drug_measure_type', 'drug_measure_code'),
('nibrs_ethnicity', 'ethnicity_code'),
('nibrs_injury', 'injury_code'),
('nibrs_justifiable_force', 'justifiable_force_code'),
('nibrs_location_type', 'location_code'),
('nibrs_offense_type', 'offense_code'),
('nibrs_prop_desc_type', 'prop_desc_code'),
('nibrs_prop_loss_type', 'prop_loss_id'),
('nibrs_relationship', 'relationship_code'),
('nibrs_suspected_drug_type', 'suspected_drug_code'),
('nibrs_using_list', 'suspect_using_code'),
('nibrs_victim_type', 'victim_type_code'),
('nibrs_weapon_type', 'weapon_code'),
('offense_classification', 'classification_id'),
('ref_agency_type', 'agency_type_id'),
('ref_continent', 'continent_id'),
('ref_county', 'county_id'),
('ref_country', 'country_id'),
('ref_metro_division', 'metro_div_id'),
('ref_msa', 'msa_id'),
('ref_population_group', 'population_group_code'),
('ref_race', 'race_code'),
('ref_state', 'state_code'),
('ref_tribe', 'tribe_id'),
('ref_university', 'university_id'),
('reta_offense_category', 'offense_category_id'),
('supp_larceny_type', 'larceny_type_code'),
('supp_property_type', 'prop_type_code')
])
def test_codes_endpoint_exists(self, testapp, table, id_col):
res = testapp.get('/codes/{0}'.format(table))
assert res.status_code == 200
assert id_col in res.json[0]

@pytest.mark.parametrize("table", CODE_MODELS)
def test_codes_endpoint_csv(self, testapp, table):
res = testapp.get('/codes/{0}.csv'.format(table))
assert res.status_code == 200


@pytest.mark.parametrize('table,key', [
('nibrs_arrest_type', 'arrestees'),
('nibrs_assignment_type', 'victims'),
('nibrs_criminal_act_type', 'criminal_acts'),
('nibrs_relationship', 'relationships'),
('nibrs_weapon_type', 'weapons')
])
def test_codes_dont_have_backwards_associations(self, testapp, table, key):
res = testapp.get('/codes/{0}'.format(table))
assert res.status_code == 200
assert key not in res.json[0]

0 comments on commit 73549ea

Please sign in to comment.