package com.couchbase.devxp.updownvote.test;

import com.couchbase.devxp.updownvote.Application;
import com.couchbase.devxp.updownvote.Presentation;
import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.QueryEnumerator;

import android.test.ApplicationTestCase;

/**
 * TODO those tests will fail to start with, your job is to implement the needed logic to make them work!
 *
 * Follow the TODOs throughout the project to make it work
 */
public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }

    public Application application;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        createApplication();
        application = getApplication();
    }

    @Override
    protected void tearDown() throws Exception {
        application.getDatabase().delete();
        super.tearDown();
    }

    public void testDatabaseConnection () {
        assertNotNull(application.getDatabase());
    }

    public void testCreatePresentation () {
        Presentation presentation = new Presentation(application.getDatabase());
        presentation.setTitle("I am a test");
        try {
            presentation.save();
        } catch (CouchbaseLiteException e) {
            fail();
        }
    }

    public void testFindAndUpdatePresentation () {
        String title = "I am a test";
        Presentation presentation = new Presentation(application.getDatabase());
        presentation.setTitle(title);
        try {
            presentation.save();
            QueryEnumerator presentations = Presentation.findAll(application.getDatabase()).run();
            Presentation loaded = Presentation.from(presentations.getRow(0).getDocument());
            assertEquals(loaded.getTitle(), title);
        } catch (CouchbaseLiteException e) {
            fail();
        }
    }
}
@@ -1424,16 +1424,58 @@
  test%22;%0A
+        String newTitle = %22I am updated%22;%0A
         
@@ -1819,24 +1819,183 @@
 ocument());%0A
+            loaded.setTitle(newTitle);%0A            loaded.save();%0A            Presentation loaded1 = Presentation.from(presentations.getRow(0).getDocument());%0A
             
@@ -2013,16 +2013,17 @@
 s(loaded
+1
 .getTitl
@@ -2027,17 +2027,20 @@
 itle(), 
-t
+newT
 itle);%0A 

fix test