def test_model_to_json(): HashOnlyModelFactory.create_table() obj = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) json_data = obj.to_json() assert '"is_enabled": null' in json_data assert ('"updated": "%s"' % obj.updated.isoformat()) in json_data
def test_query_primary_index_model_hash_key_should_return(): HashOnlyModelFactory.create_table() HashOnlyModelFactory(agency_subdomain='metzler') HashOnlyModelFactory(agency_subdomain='other') query = list(HashOnlyModel.query(agency_subdomain='metzler')) assert query[0].agency_subdomain == 'metzler'
def test_local_change_then_reload(): HashOnlyModelFactory.create_table() obj = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) obj.external_id = 124 reloaded = obj.reload() assert reloaded.external_id == 123
def test_query_secondary_index_model_hash_key_should_return(): HashOnlyModelFactory.create_table() HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) HashOnlyModelFactory(agency_subdomain='other', external_id=123) query = list(HashOnlyModel.query(external_id=123, query_index='HashOnlyExternalId')) assert set([o.agency_subdomain for o in query]) == {'metzler', 'other'}
def test_return_different_fields_except_should_ignore_and_return_true(): HashOnlyModelFactory.create_table() obj1 = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) obj2 = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) assert obj1.created != obj2.created assert obj1.updated != obj2.updated assert not return_different_fields_except(obj1.item, obj2.item, ['created', 'updated'])
def test_query_secondary_index_model_hash_key_should_return(): HashOnlyModelFactory.create_table() HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) HashOnlyModelFactory(agency_subdomain='other', external_id=123) query = list( HashOnlyModel.query(external_id=123, query_index='HashOnlyExternalId')) assert set([o.agency_subdomain for o in query]) == {'metzler', 'other'}
def test_negative_timestamp(): long_ago = datetime.datetime.utcnow() long_ago = long_ago.replace(year=1899) HashOnlyModelFactory.create_table() HashOnlyModelFactory(agency_subdomain='metzler', external_id=123, created=long_ago) obj = HashOnlyModel.all().next() assert obj.created.year == long_ago.year assert obj.item['created'] < 0
def test_return_different_fields_except_should_return_diff(): HashOnlyModelFactory.create_table() obj1 = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123, created=datetime.datetime.utcnow() - datetime.timedelta(minutes=10)) obj2 = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) assert obj1.created != obj2.created assert return_different_fields_except(obj1.item, obj2.item, ['updated']) == dict( old=dict(created=obj2.item['created']), new=dict(created=obj1.item['created']) )
def test_all_paginate(): HashOnlyModelFactory.create_table() HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) HashOnlyModelFactory(agency_subdomain='metzler2', external_id=124) obj, exclusive_start_key = HashOnlyModel.all(limit=1, paginate=True).next() obj2, _ = HashOnlyModel.all( limit=1, paginate=True, exclusive_start_key=exclusive_start_key).next() assert obj.external_id != obj2.external_id assert {obj.external_id, obj2.external_id} == {123, 124}
def test_return_different_fields_except_should_return_diff(): HashOnlyModelFactory.create_table() obj1 = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123, created=datetime.datetime.utcnow() - datetime.timedelta(minutes=10)) obj2 = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) assert obj1.created != obj2.created assert return_different_fields_except( obj1.item, obj2.item, ['updated']) == dict(old=dict(created=obj2.item['created']), new=dict(created=obj1.item['created']))
def test_has_changed_primary_key_save_logs(log_data_mock): HashOnlyModelFactory.create_table() obj = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) obj.agency_subdomain = 'other' assert obj.has_changed_primary_key() obj.save() assert log_data_mock.called called_with = log_data_mock.call_args_list[0] assert called_with[0][0] == 'save overwrite=True table=dev_hash_only'
def test_non_field_set_on_item_then_get(): HashOnlyModelFactory.create_table() obj = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) obj.non_field = 123 obj.non_field_class_attr = "Changed string" # This makes more sense when the attr is a property obj.save() reloaded = HashOnlyModel.get(agency_subdomain='metzler') assert not reloaded.item.get('non_field') assert not reloaded.item.get('non_field_class_attr')
def test_update_item_then_get_consistent(): HashOnlyModelFactory.create_table() obj = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) assert obj.external_id == 123 obj.external_id = 124 obj.save() assert obj.external_id == 124 reloaded = HashOnlyModel.get(agency_subdomain='metzler', consistent_read=True) assert reloaded.external_id == 124
def test_primary_key_should_return(): HashOnlyModelFactory.create_table() obj = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123) assert obj.get_primary_key() == dict(agency_subdomain='metzler')