Category: Uncategorized

SQLite and Transaction Management : I wasted 2 hours of my time

SQLite and Transaction Management : I wasted 2 hours of my time

Have you ever interacted with SQlite and wondered why your query is not returning all records in a given table. Well , I have and I almost gave up. It’s such a silly mistake or rather lack of concentration on my part. I wanted to get location data collected at intervals of 1 minute , cache them on SQLite and send them to some server. As simple as that sounds , it turned out to be such a Pain.

This was what was wrong : I was running a query to get all records and within the loop , I was running another Query to get the userID.

Then when I decided to get the userID first and assign it to a variable them use it on the second query which was getting all location records , it worked.

 

Turn On / Turn Off your Phone’s Flash Light

Turn On / Turn Off your Phone’s Flash Light

To be very honest I didn’t know where the Flash Button was , I noticed it was after you turn on the flash option on your Camera Settings. So I decided to make a button which when clicked turned on/off the flash light depending on the status. I also thought it’ll be interesting if you could send an SMS to your phone with some command i.e “Turn On” and the phone does just that.

Prequisites

  1. Basic UI Design (By basic I mean drag and drop )
  2. Basic Java Knowledge

XML Layout

Open your Android Studio and create a blank project , you can name it whatever you want. Add two buttons on the xml layout . Here is how I did mine , I could have simply used one button but for simplicity I had two buttons

The flashlight is provided by the Android Camera API , it has interfaces that allows you to have your own implementation of how the camera should work. We are required to ask for permission before we are able to initiate such hardware requests. Be sure to declare your permissions at the Android Manifest File. The new versions of Android requires you do the same while on Run-time. Put up the following in your Manifest File.

Also on your Activity ask for the permissions.

He’s the fun part now, add listeners to the two buttons you created.

Have functions which when called turns on the flash light and switches it off depending on the context.

To maintain the state of the buttons , you can make use of Visibility Option in Android’s UI Elements. For instance if the user clicks the “Turn On” button it makes the button disappear and show the “Turn Off” and vice-versa.

Viola , you are now able to Turn On and Off your Phone’s Flash Light. Get the Code on my Github here : https://github.com/brianosoro/FlashLight-App

Comment / Critic / Share

@brayanosoro

brianosoro.com

MongoDB CRUD & Mocha Testing Part 3

MongoDB CRUD & Mocha Testing Part 3

Hi guys, in this tutorial we will continue leaning MongoDB CRUD operations in details, and learn how to write their test cases using Mocha. In our previous tutorial, we learnt how to write our first test where we inserted a user and checked in our MongoDB. If you have not yet gone through it, please do as this tutorial series will be a continuation.

As I had mentioned earlier on, this tutorial series will be a beginner guide, and most of the content will be covered in our Node Js tutorials. Then everything will now start making sense, but if you need any help or clarification, please leave a comment or question below.

MongoDB CRUD & Mocha Testing Part 3

In our previous tutorial, we wrote our Create test, and we created a User Model, and used the model to insert data in our MongoDB. In this tutorial, we are going to try deleting our User, that we had created earlier and confirm using the test we are also going to write.

Remember, we had our node-mongo projects we had created earlier, open it as we are going to continue working on it. We are now going to open our test folder and create a new file there, to try and do delete operations.

Please make sure you have Installed and configure and Mongo DB, and that you are able to access it either via the command line or RoboMongo.

For those who may get errors here, please leave questions, comments or even your error screenshots and I will be glad to help you out. Now, inside our test folder, create a file and call it deleting_test.js. This will be where we will be writing the delete test. If you do not understand most of this things, don’t worry as we will cover them in our next Node Js tutorials.

Delete User Test

After creating the file, add the following code snippet inside:

I had explained a few things in our previous tutorial, which you could look at if you had skipped it. This tutorial will shed some light about a couple of things I will not repeat in this tutorial. Now, the first test, which is the beforeEach creates a new User every time we are running a test. This allows us to delete that created User in the second test, which is the ‘it’ that follows it.

We use this mongoose method to search for a User from our MongoDB:

This searches in our database, for all Users with the name ‘Allan’ and then Removes it. This means that we have removed or deleted whatever User we get, but we need to confirm if we have actually deleted the User first. We write a simple assert which searches again for the user after deleting them to make sure the result is empty or null using the following code snippet:

The test should pass, as we created a User called Allan then used the findOneAndRemove to delete the User with the name Allan and then searching to see if we get any result. The result is a null, meaning we have actually managed to delete our User from the database.

This is a sample screenshot of my tests passing, after running the tests using the Terminal in my Webstorm

We have managed to delete the User, and you can check in MongoDB just to confirm. Lets try to find a User first, then try to update the User we were searching for and writing their tests.

Finding or Searching User Test

We need to create a file and name it finding_test.js inside out test folder. After creating the file, add the following code snippet inside:

We need to create a new User first, because in our connection.js  we have a beforeEach which empty’s our User before we run any test. Creating a new User is something we can be able to do easily now. After creating a User called ‘Allan’, we use a mongoose method findOne to search for the User.

The result will return everything about the User, so we need to use result.name to tell our assert that whatever we want to compare is the value of the name returned and the value we have after the === signs. This will check to see if the User with the name Allan exists. If they exist, the test will pass:

As you can see above, my search test has passed. Now lets try to edit or update some User Data we have in our Database now.

Updating or Editing User Test

We need to create a file and name it updating_test.js inside out test folder. After creating the file, add the following code snippet inside:

We are first creating a User, with name Allan and weight of 10. Then we need to maybe edit the name alone, and leave the weight as 10 still. We use a mongoose method findOneAndUpdate to search for the user with the name ‘Allan’ and then update the name to ‘Juma‘. We then search for ‘Allan’ to make sure we get null, because we have edited the name from Allan to Juma. If you run the test, this should give you a test passed!

The above screenshot shows my results, for all the tests we have written! We have now managed to write all tests successfully.

Project Source Code on Github

Some of you may have encountered a few errors while trying to follow this tutorial series, I have made the source code for the project available via Github. You can download or clone the project and go through it.

This is the project Github link

The project should run just fine on your local machine, but you would still require to make sure MongoDB is running first.

Summary

This closes this mini tutorial series, we will continue with Android Development as you guys continue to learn and understand whatever we have been learning about MongoDB. We will then get started with Node Js! Please leave a comment, suggestion or a question below if you get stuck, and I will be glad to help you. You could try running your tests with different users and see what results you will get. Make sure Mongo is running, either from your Terminal or as a service. Remember to keep sharing!

Happy Coding!

MongoDB CRUD & Mocha Testing Part 2

MongoDB CRUD & Mocha Testing Part 2

Hi guys, in this tutorial we will start leaning MongoDB CRUD operations in details, and learn how to write their test cases using Mocha. In our previous tutorial, we learnt how to write our first simple test. If you have not yet gone through it, please do as this tutorial series will be a continuation.

In this tutorial, we will start with learning a few other new things, then proceed with our CRUD learning process. As I had mentioned earlier on, this tutorial series will be a beginner guide, and most of the content will be covered in our Node Js tutorials. Then everything will now start making sense, but if you need any help or clarification, please leave a comment or question below.

MongoDB CRUD & Mocha Testing Part 2

In our previous tutorial, we wrote our first Test, and I had explained what are models, and what they do. In this tutorial, we are going to create a simple model, and then use the model to learn how to write tests for MongoDB CRUD operations.

Remember, we had our node-mongo projects we had created earlier, open it as we are going to continue working on it. I will create a simple model for you guys first, then explain what it means. We are going to create another folder inside our node-mongo folder, and name it models. We are going to create a User Model.

Please make sure you have Installed and configure and Mongo DB, and that you are able to access it either via the command line or RoboMongo.

For those who may get errors here, please leave questions, comments or even your error screenshots and I will be glad to help you out. Now, inside our model folder, create a file and call it User.js. This will be our User Model file, there we are going to create our User Schema and Model and the export it. If you do not understand most of this things, don’t worry as we will cover them in our next Node Js tutorials.

Create a User Model

Now, we need to create a User Model. This will allow us to save and do operations on our User from our MongoDB using Mongoose. Let me show you a simple User Model. In our previous tutorial, we had a started writing our first test. We had a test folder. Now create a new folder and name it models. We are going to use this folder to create our User Model file inside. After you create a blank folder, create a file and name it user.js and paste the following code:

We have now created a User Model, with name and weight values. So whenever we need to use our User Model, we need to pass the name and the weight, then save, read, update or delete from our MongoDB. In this previous tutorial, we covered Introduction to Mongo and a few other requirements. You can go through it to get refreshed first.

Now, let me explain a few things. We require mongoose first. Mongoose is a Node package that we use with Node Js to carry out operations with MongoDB easily. To use mongoose, you need to install it first. Open Terminal/CMD and type the following:

This install mongoose in a folder named node_modules, and add’s it to our package.json file

The node_module folder is created automatically, when you install a package using npm. The folder is used to store our modules which we require in our application. You do not therefore need to edit the contents of this file.

Then we create a Schema instance using mongoose. We use the Schema Instance to create a new UserSchema, as shown in this code snippet below:

Now we use our UserSchema to create a user model. This is done using this code snippet:

Our MongoDB will now use this User model to create a collection called user  using the UserSchema. A collections is basically a table in an sql database. Now, I explained that Mongo is a NoSQL database, meaning we don’t have tables.

We have now created a model, but we need to use it i.e make it accessible outside the User Model file. We use the following code snippet:

The module.export makes our user model accessible anywhere we need it in our application. Now lets proceed to use our model and do some insert or create operations.

Create a MongoDB Connection File

Next, we are going to create a file named connection.js which will make sure the mongo connection is working even before we start writing tests. This is necessary as we need the connection with Mongo first, to be able to run the test.

I explained why we need mongoose earlier on. Now we have a strange line highlighted below:

Just include it but I will explain it in a later tutorial, so that you are not confused. Now, we have a before which simply means that whatever is inside it needs to be executed before anything else. This is what we actually need, because we want to ensure our database connection is working first, then continue with whatever we are doing. To create a connection with our MongoDB, we use the following line: (This assumes you are running MongoDB on your local machine)

We then need to log some message on the console, this could be either a succesfull connection or an error that we might get. If the connection is successful, we log Connected to Mongo but if we get an error, we log the value of error!

Now, we have a beforeEach after our connection is working. This is shown by the following code:

This simply empty’s our database, before each test can run. This is important to make every test runs like a new one, like a fresh test.

If you run this without running mongod first, you might run into an error!

To run this and see if it works, just hit the following command on your terminal:

Look at our console, you should see our Connected to Mongo message now. This means that our connection file is running or working as expected. Now, we are going to write a simple create test.

Saving or Create User Test

We have managed to create a User Model and a simple connection file to our MongoDB. Now, we need to try to add some data to our User and then seeing if the test will actually pass. Open your test folder and create a new file and call it saving_test.js. Add the following code to this file:

In our previous tutorial, we used assert to basically tell if whatever we were trying to test was passing or returning true. We therefore require it inside this file so that we can use it. Then, we require the User Model, which we created earlier, and exported it to make it available. To require it, we need to pass its path, where the file is located in our project.

Then we write our test. We start with the describe and then an it. The describe can have many ‘it’ inside as we had mentioned earlier. We are only having on test, which will try to save our user. The user we are trying to save is a variable we are creating as shown below:

We then try to save the user, by creating a new user in our MongoDB. This might look confusing at first, but as we continue learning you will now start getting some tips, ideas and even start understanding the whole idea. We then assert to see if the user we have saved has been saved. We use user.isNew === false which checks to see if the user we have saved is a new User.

The test will pass by asserting that the user we have saved is not new, meaning we have saved the User already in our MongoDB.

Save the file, and then try to run the project in your terminal or CMD for Windows Users using this command:

I am using Webstorm, this is how it looks like after running the test command:

My tests run successfully and show 2 passing without an error. If you use Terminal to see if your data was saved, this is what you will see on your end, whatever we saved earlier:

You can also use Robomongo to see the data that we saved after running the test.  This is how Robomongo will show you the saved user:

Summary

We have managed to set up a few things and also write our first test, which actually saves data in our MongoDB and then asserts our test. We have been able to see our created User now in MongoDB using the Terminal or Robomongo. In our next tutorial, we are going to continue writing tests and confirming if they are actually working by comparing with the data that we get in our MongoDB. Please leave a comment, suggestion or a question below if you get stuck, and I will be glad to help you. You could try running your tests with different users and see what results you will get. Make sure Mongo is running, either from your Terminal or as a service. Awesome!

Happy Coding!

MongoDB CRUD & Mocha Testing Part 1

MongoDB CRUD & Mocha Testing Part 1

Hi guys, in this tutorial we will cover MongoDB in details, and learn how to write simple test cases using Mocha. This will be the first in a short series that we will cover using MongoDB in details. After this tutorial, you guys will be comfortable using Mongo to do simple operations. First, you need to make sure you have both Mongo and Node Js installed before you continue with this tutorial series. I assume you have the two installed, and if you get an error please leave a question below and I will be glad to help you out.

MongoDB CRUD & Mocha Testing Part 1

In our previous tutorial, I mentioned that Mocha is a testing framework we use to perform tests within our application to make sure everything works correctly. Now lets start by creating a simple project. Create a folder named mongo-mocha, anywhere in your local machine. Make sure you have mongodb installed and running, if you have any issues please leave a question below and I will be glad to help you out. Next, you need to open your folder using a text editor of your choice, but i prefer Webstorm.

The whole code that we will be working on in this tutorial will be available for download from my github page, which i will provide the necessary link. So don’t worry, but all the code snippets we will be working on also will be available in the tutorials as we progress.

Now open your folder using whatever text editor you have. In this tutorial, I will mostly cover Mocha and writing a simple test using it. In the next tutorial, we will now get to work and start writing CRUD examples, and their Mocha tests. Now inside our folder, create a file named test.js. Note we use use the word test to show this file will contain our tests that we will be writing using Mocha. We will be writing Javascript codes, hence the extension .js. Don’t worry if you have never worked with Javascript before, I will be guiding you throughout this tutorial to make sure you are not lost.

Next we will start using npm, which stands for Node Package Manager. We will learn how to install packages using it. Inside your mongo-mocha folder, open your git command line, or cmd for windows users and Terminal for Linux and Mac users.

If you are working using Webstorm, you can use the Terminal inbuilt within the IDE. See the screenshot below:

Next we are going to install Mocha, so that we can use it to write tests. But first we need to create a file called package.json. Now this file is very important in our Node Apps, as we use this to manage our app dependencies. We will be learning what are dependencies in details when we start creating REST APIs using Node. Now how do we create this pakage.json file? Inside the Terminal, write the following:

This command is used to create a package.json for a new project that you create. Follow all the steps that you are prompted to fill to the end. The screenshot below shows how i have managed to create my package.json

After this, just press enter, then inside your folder you will see a package.json file created. We now have two files created inside our test mongo-mocha folder. This are the package.json and the test.js, as shown below

As you can see, all those details that we filled when creating the package.json is what is created inside the file. Next we are going to install Mocha, which is a dependency in this case. Simple, go to the same cmd or terminal and write the following:

This is used to add mocha as a dependency to our package.json. This should take a few seconds or minutes, depending on your internet connection. This is how your package.json should look like:

As you can see, a new dependencies is created. I will skip explaining in this series, but when we will be learning Node Js I will cover all this. Now, you may have noticed a strange folder, node_modules  and probably you are wondering what the hell this means. Don’t worry, this is where our dependencies are saved. Whenever we add a package, its downloaded and saved in this folder.

Now inside our test.js, we add the following code:

Whenever we want to use a package inside our Node project, we need to require it first. We do this by assigning it a variable first. This is shown by the above code. I will explain this in details as we proceed.

Writing our First Test

Next we need to start writing our test. Let me first write a simple test, then explain what this means. This is simple test:

So what does this mean? We have a describe, which has description and a callback function. The description is ‘My First Test’, which ideally explains what this test does. Then inside our callback function, we have it, which takes the same two parameters. The it contains the test that we are actually writting. We can have many it inside one describe, each running a different test. Then we have the assert,  which contains the actual test that we want to carry out. Simple. As you can see, we have a simple test to assert that two numbers 2 and 3 add up to 5, which we use three equal signs to do the comparison. This turns out to be true, hence this test should pass.

Remember our package.json file, locate the following line of code

and add the following

Whatever we have done above is tell our application that we are using Mocha to do the test, which is the dependency we installed earlier on. Now our test.js file should look like this:

Try to run the file now using

You should get the following result now (This is how it looks like on my Terminal):

As you can see above, i am getting 1 passing and you can see the description as well as the test that has passed. This is true because 2 + 3 = 5, now lets change it to something that is not correct like 2 + 3 = 4 and re run our test again.

When you re run the test again, you will see huge red errors!

The test failed because it could not assert that 2 + 3 = 4, which is wrong. The test will then fail, and show us the error. Now that you have an idea on writing a simple test, we will start writing tests for our MongoDB CRUD.

Summary

This is a simple lesson that has introduced us to test, and even managed to write a simple test. In the next tutorial, we will start writing tests for our Mongo CRUD operations! Remember to keep on subscribing and drop a question, comment or even an additional note and I will be glad to help you!

Happy Coding!