Skip to content
This repository was archived by the owner on May 4, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion declex-test/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

<application
android:label="@string/app_name"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:name=".model.localdbmodels.ExpensesApplication_">

<activity android:name=".action.ActionMainActivity_"/>
<activity android:name=".injection.property.PropertyInjectionActivity_"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2016 DSpot Sp. z o.o
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dspot.declex.test.model.localdbmodels;

import com.activeandroid.app.Application;
import com.dspot.declex.annotation.UseLocalDB;

import org.androidannotations.annotations.EApplication;


@UseLocalDB
@EApplication
public class ExpensesApplication extends Application {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.dspot.declex.test.model.localdbmodels.model;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.dspot.declex.annotation.LocalDBModel;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;

import java.util.Locale;

@LocalDBModel
public class Expense extends Model {
@NotEmpty
@Column
String description = "";

@Column
String comment = "";

@NotEmpty
@Column
float amount;

@NotEmpty
@Column
String date = "";

@NotEmpty
@Column
String time = "";

@Column
long user_id;

public String getAmount() {
if (amount == 0) return "";

return String.format(Locale.US, "%.2f", amount);
}

public String datetime() {
return date + " " + time;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.dspot.declex.test.model.localdbmodels;

import com.activeandroid.Model;
import com.activeandroid.query.Delete;
import com.activeandroid.query.From;
import com.dspot.declex.annotation.LocalDBModel;
import com.dspot.declex.test.model.localdbmodels.model.Expense_;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.mockito.Mockito.*;

import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.*;
import java.lang.NoSuchMethodException;

@RunWith(RobolectricTestRunner.class)
@Config(
manifest = "app/src/main/AndroidManifest.xml",
sdk = 25
)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "org.powermock.*"})
@PrepareForTest({Expense_.class, LocalDBModel.class, From.class})
public class TestLocalDbModels {

@Rule
public PowerMockRule rule = new PowerMockRule();

@Before
public void setUp() throws Exception {

}

@Test
public void testMockLocalDBModelsAnnotation() {
LocalDBModel annotation = mock(LocalDBModel.class);

when(annotation.hasTable()).thenReturn(true);
when(annotation.custom()).thenReturn(true);
when(annotation.ignorePut()).thenReturn(false);
when(annotation.table()).thenReturn("expenses");

assertFalse(annotation.ignorePut());
assertTrue(annotation.hasTable());
assertEquals("expenses", annotation.table());

verify(annotation).ignorePut();
verify(annotation).hasTable();
verify(annotation).table();
}

@Test
public void testMockClassFrom() {
List<Model> models = new ArrayList<>();

From exeQuery = mock(From.class);
when(exeQuery.execute()).thenReturn(models);
exeQuery.execute();
verify(exeQuery).execute();

From exeQueryDelete = mock(new Delete().from(Expense_.class).getClass());
when(exeQueryDelete.execute()).thenReturn(models);
exeQueryDelete.execute();
verify(exeQueryDelete).execute();
}

@Test
public void testCreateQuery() {
Map<String, Object> args = new HashMap();
args.put("query", "amount>34.23");
args.put("orderBy", "user_id ASC");

String query = Expense_.getLocalDBModelQueryDefault();
if (args.containsKey("query")) query = (String) args.get("query");

String orderBy = Expense_.getLocalDBModelQueryDefault();
if (args.containsKey("orderBy")) orderBy = (String) args.get("orderBy");

assertEquals("amount>34.23", query);
assertEquals("user_id ASC", orderBy);
}

@Test
public void testCreateSelectQuery() {
AtomicBoolean condition = new AtomicBoolean(false);

Map<String, Object> args = new HashMap();
args.put("query", "select user_id from Expense");

String query = Expense_.getLocalDBModelQueryDefault();
if (args.containsKey("query")) query = (String) args.get("query");
if (query.toLowerCase().trim().startsWith("select ")) {
condition.set(true);
}

assertTrue(condition.get());
assertEquals("select user_id from Expense", query);
}

@Test
public void testCreateDeleteQuery() {
AtomicBoolean condition = new AtomicBoolean(false);

Map<String, Object> args = new HashMap();
args.put("query", "delete where user_id=1");

String query = Expense_.getLocalDBModelQueryDefault();
if (args.containsKey("query")) query = (String) args.get("query");
if (query.toLowerCase().trim().startsWith("delete ")) {
condition.set(true);
query = query.substring(7);
}

assertTrue(condition.get());
assertEquals("where user_id=1", query);
}
}