Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #259

Merged
merged 18 commits into from
Sep 26, 2020
Merged
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: 3 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,33 @@ There are handy DSL that allows write content in the typesafe way.
- All app content should be placed into `com.github.braillesystems.learnbraille.res` package.
- Use `DslTest.kt` file as DSL tutorial.

Symbols that are not from particular alphabet and does not exist on classical american keyboard should be treated as special and added via `enum class`.
Information correctness should be checked in compile time or in app initialization runtime as much as possible. If some additional info is need, do not hardcode it. Just make request to the new DSL feature via github issues.

Adding rules, prevent lambda of capturing context that will be invalid next time fragment entered, so use `Fragment.getString` outside of lambdas.

#### Adding course

1. Create lessons by `lessons` delegate.
2. Create course in `CourseBuilder` and add lessons to it.

Always use `com.github.braillesystems.learnbraille.res.content` value to get materials, they are indexed here in proper way.

#### Adding deck

1. Add new deck tag to `DeckTags`.
2. Map tag to deck's predicate in `DecksBuilder`.
3. Map deck's tag to user-visible string in `deckTagToName`.

#### Adding materials

1. Create materials by one of delegates: `markers` or `symbols`.
2. Add created materials to the `contens` (`materials` delegate).
3. Add to `inputSymbolPrintRules` and `showSymbolPrintRules`, or to `inputMarkerPrintRules` and `showMarkerPrintRules`.

Symbols that are not from particular alphabet and does not exist on classical american keyboard should be treated as special and be added via `enum class`.

New materials can be marked as known by default in `knownMaterials` (`known` delegate).

## Database

Database scheme is described [here](https://github.com/braille-systems/learn-braille/blob/master/database.md).
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException


/**
* Tests data serialization first of all.
*/
@RunWith(AndroidJUnit4::class)
class LearnBrailleDatabaseTest {

Expand Down Expand Up @@ -87,15 +83,15 @@ class LearnBrailleDatabaseTest {
id = 3,
data = ShowDots(
text = "Перед Вами полное шеститочие",
dots = BrailleDots(F, F, F, F, F, F)
brailleDots = BrailleDots(F, F, F, F, F, F)
),
lessonId = 1, courseId = 1
),
Step(
id = 4,
data = InputDots(
text = "Введите все шесть точек",
dots = BrailleDots(F, F, F, F, F, F)
brailleDots = BrailleDots(F, F, F, F, F, F)
),
lessonId = 2, courseId = 1
),
Expand Down Expand Up @@ -157,30 +153,30 @@ class LearnBrailleDatabaseTest {

@Test
fun testUsers() = runBlocking {
assertEquals("default", db.userDao.getUser(1)!!.login)
assertEquals("default", db.userDao.user(1)!!.login)
}

@Test
fun testMaterials() = runBlocking {
val data = db.materialDao.getMaterial(1)!!.data
val data = db.materialDao.material(1)!!.data
require(data is Symbol)
assertEquals(BrailleDots(F, E, E, E, E, E), data.brailleDots)
}

@Test
fun testDecks() = runBlocking {
assertEquals("Ru letters", db.deckDao.getDeck(1)!!.tag)
assertEquals("Ru letters", db.deckDao.deck(1)!!.tag)
}

@Test
fun testCourses() = runBlocking {
assertEquals("Super course", db.courseDao.getCourse(1)!!.name)
assertEquals("Super course", db.courseDao.course(1)!!.name)
}

@Test
fun testSteps() = runBlocking {
for ((i, step) in steps.withIndex()) {
val fromDb = db.stepDao.getStep(i + 1L)!!
val fromDb = db.stepDao.step(i + 1L)!!
assertEquals(step, fromDb)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class ActionsRepositoryTest {

@Test
fun getAll() = runBlocking {
assertEquals(actions.toList(), repo.getActionsFrom(Days(100)))
assertEquals(actions.toList(), repo.actionsFrom(Days(100)))
}

@Test
fun getSince() = runBlocking {
assertEquals(listOf(actions.first()), repo.actionsFrom(Days(25)))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package com.github.braillesystems.learnbraille.data.repository

import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.github.braillesystems.learnbraille.data.db.LearnBrailleDatabase
import com.github.braillesystems.learnbraille.data.entities.*
import com.github.braillesystems.learnbraille.data.entities.BrailleDot.E
import com.github.braillesystems.learnbraille.data.entities.BrailleDot.F
import com.github.braillesystems.learnbraille.res.MarkerType
import com.github.braillesystems.learnbraille.res.SymbolType
import com.github.braillesystems.learnbraille.utils.unreachable
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException

@RunWith(AndroidJUnit4::class)
class MaterialsRepositoryTest {

private lateinit var db: LearnBrailleDatabase
private lateinit var repo: MaterialsRepository

private val users = listOf(
User(
login = "default",
name = "John Smith"
)
)

private val materials = listOf(
Material(
1,
Symbol(
char = 'А',
brailleDots = BrailleDots(F, E, E, E, E, E),
type = SymbolType.ru
)
),
Material(
2,
MarkerSymbol(
type = MarkerType.GreekCapital,
brailleDots = BrailleDots(F, F, F, E, E, E)
)
),
Material(
3,
Symbol(
char = 'B',
brailleDots = BrailleDots(F, F, F, E, E, F),
type = SymbolType.digit
)
)
)

private val knownMaterials = listOf(
KnownMaterial(1, 2),
KnownMaterial(1, 3)
)

private val decks = listOf(
Deck(
id = 1,
tag = "Ru letters"
),
Deck(
id = 2,
tag = "Another useless deck"
)
)

private val cards = listOf(
Card(
deckId = 1,
materialId = 1
),
Card(
deckId = 2,
materialId = 1
),
Card(
deckId = 1,
materialId = 2
)
)

private val courses = listOf(
Course(
id = 1,
name = "Super course",
description = "Oh, it's so good"
)
)

private val lessons = listOf(
Lesson(
id = 1,
name = "First",
description = "First First First",
courseId = 1
),
Lesson(
id = 2,
name = "Last",
description = "Last Last Last",
courseId = 1
)
)

private val steps = listOf(
Step(
id = 1,
data = FirstInfo("FirstInfo"),
lessonId = 1, courseId = 1
)
)

private val annotations = listOf(
StepAnnotation(id = 1, name = "a1"),
StepAnnotation(id = 2, name = "a2")
)

private val stepAnnotations = listOf(
StepHasAnnotation(
courseId = 1,
lessonId = 3,
stepId = 2,
annotationId = 1
)
)

@Before
fun createDB() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
db = Room
.inMemoryDatabaseBuilder(context, LearnBrailleDatabase::class.java)
.allowMainThreadQueries()
.build().apply {
runBlocking {
userDao.insert(users)
materialDao.insert(materials)
knownMaterialDao.insert(knownMaterials)
deckDao.insert(decks)
cardDao.insert(cards)
courseDao.insert(courses)
lessonDao.insert(lessons)
stepDao.insert(steps)
stepAnnotationDao.insert(annotations)
stepHasAnnotationDao.insert(stepAnnotations)
}
}

repo = MaterialsRepositoryImpl(
db.deckDao, db.cardDao,
object : PreferenceRepository {
override val buzzEnabled: Boolean
get() = unreachable
override val toastsEnabled: Boolean
get() = unreachable
override val golubinaBookStepsEnabled: Boolean
get() = unreachable
override val slateStylusStepsEnabled: Boolean
get() = unreachable
override val traverseDotsInEnumerationOrder: Boolean
get() = unreachable
override val inputOnFlyCheck: Boolean
get() = unreachable
override val additionalAnnouncementsEnabled: Boolean
get() = unreachable
override val practiceUseOnlyKnownMaterials: Boolean
get() = true
override val extendedAccessibilityEnabled: Boolean
get() = unreachable
override val additionalQrCodeButtonEnabled: Boolean
get() = unreachable
override val currentUserId: DBid
get() = 1

override suspend fun getCurrentUser(): User = users.first()
}
)
}

@After
@Throws(IOException::class)
fun closeDB() {
db.close()
}

@Test
fun randomMaterialFromDeck() = runBlocking {
val material = repo.randomMaterialFromDeck(2)
assertTrue(material in materials.filter { Card(2, it.id) in cards })
}

@Test
fun randomKnownMaterialFromDeck() = runBlocking {
val material = repo.randomKnownMaterialFromDeck(1)
assertTrue(material in materials.filter { Card(1, it.id) in cards })
assertTrue(material!!.id in knownMaterials.map { it.materialId })
}

@Test
fun allMaterialsFromDeck() = runBlocking {
assertEquals(
materials.filter { Card(2, it.id) in cards },
repo.allMaterialsFromDeck(2)
)
}

@Test
fun allDecks() = runBlocking {
assertEquals(
decks,
repo.allDecks()
)
}

@Test
fun availableDecks() = runBlocking {
assertEquals(listOf(decks.first()), repo.availableDecks())
}

@Test
fun allDecksWithAvailability() = runBlocking {
assertEquals(
listOf(
DeckWithAvailability(decks[0], true),
DeckWithAvailability(decks[1], false)
),
repo.allDecksWithAvailability()
)
}
}

This file was deleted.

Loading