ModelAdapter { @NonNull public Long save(@NonNull ModelClass model) { Long id = getModelId(model); if (isModelExists(id)) { update(model, id); } else { id = insert(model, id); } ModelChangeNotifier.get().notifyModelChanged(model, ChangeAction.SAVE); return id; } ModelAdapter(@NonNull TableInfo tableInfo); @NonNull TableInfo getTableInfo(); @NonNull ModelCache<ModelClass> getModelCache(); @Nullable ModelClass load(@NonNull Class<ModelClass> type, long id); @NonNull Long save(@NonNull ModelClass model); void delete(@NonNull ModelClass model); void loadFromCursor(@NonNull ModelClass model, @NonNull Cursor cursor); @Nullable Long getModelId(@NonNull ModelClass model); static final int INSERT_FAILED; }
@Test public void testIdValueAfterSave() { TestModel model = new TestModel(); assertNull(model.id); testModelModelAdapter.save(model); assertEquals(1L, model.id, 0); assertEquals(1, getCount()); }
@Test public void testPredefinedIdValueAfterSave() { TestModel model = new TestModel(100L); assertEquals(100L, model.id, 0); testModelModelAdapter.save(model); assertEquals(100L, model.id, 0); }
@Test public void testSave() { TestModel model = new TestModel(); testModelModelAdapter.save(model); assertEquals(1, getCount()); }