---
# System prepended metadata

title: '<center><i class="fa fa-edit"></i> Smart Dispenser: `members_addtional_Card` Subfunction Testing </center>'
tags: [Internship]

---

# <center><i class="fa fa-edit"></i> Smart Dispenser: `members_addtional_Card` Subfunction Testing </center>
###### tags: `Internship`
:::info
**Goal:**
- [x] Test run the current code
- [x] Make changes to current code accordingly

**Resources:**
[PyQt](https://hackmd.io/Ibk_VuuWRXOjkION4wk3tg)
[newXinXingPyQtStructure](https://drive.google.com/drive/u/0/folders/1Ahr8zkZgUv8ujauLKGz1ZSzkZq0M9_mx)
[CodiMD](https://hackmd.io/c/codimd-documentation/%2F%40codimd%2Fextra-supported-syntax#Flow-Chart)
[冠辰參考的筆記](https://www.wongwonggoods.com/page/4/?s=pyqt&ct_post_type=post%3Apage&ct_product_price=1)
:::

---
:::spoiler **Expand Catalog**
[TOC]
:::

---
### Figures
*Figure 1. Open a new collection members_additional_card*
![](https://i.imgur.com/HIZq4rs.jpg)

*Figure 2. Flowchart*
![](https://i.imgur.com/KeSi6dk.jpg)

---
### Error: `check()` and `Membersdata(DB, Collection, Search={})`
The `check()` function utilizes the`Membersdata`function in 冠辰's `XinxingQt.py`.

Regardless of whether or not the inputed School ID is correct, the system output would always be error.

Tried debugging but it did not show where the problem was.

Approached 冠辰 with the problem:
* The database was closed over the weekend but is now working. Tried again but still produced the same error.
* Code was working on 冠辰's end but not mine -> concluded the issue was not the code but something with my system.
*  Added these two lines of code to the end of `XinxingQt.py`:
```
result = Membersdata("xinxing_dispenser", "members_data_test",{"SchoolID" : "10950005"})
print(result)
```
*  Received and UnboundLocalError:

![](https://i.imgur.com/FplSFEV.jpg)

* 冠辰 then changed the code of his `Membersdata` function and sent it over to me as follows:

```
def Membersdata(DB, Collection, Search={}):
    global mongo_url_01,mongo_url_02
    conn = MongoClient(mongo_url_01) 
    db = conn[DB]
    collection = db[Collection]
    cursor=collection.find(Search)
    data=[d for d in cursor]
    if data==[]:
        return False
    else:
        return data
```
* Received a ServerSelectionTimeoutError:

![](https://i.imgur.com/t2WN9Lk.jpg)

* 冠辰 tested the code out in Visual Studio Code and received the same error. However, when running the code through Spyder, it was functioning properly.
* 冠辰 then changed his pymongo to version `3.12.0` and produced the proper output.
* I followed suit and the function is now working properly:

![](https://i.imgur.com/1KMb9yD.png)

### Changes to Code
**All 3 Check Functions**
* Changed the try-except to an if-else statement because the original `Membersdata` function and the newly updated version of it already incorporates the try-except.
    * This error should have been caught in the process of creating the code and understanding `XinxingQt.py` but I was too caught up in why the database was not working.
* `Membersdata` function returns false if data is not found and returns the data if it is found so I changed all three check functions to correpond with the return values.

![](https://i.imgur.com/IVyOdJB.png)

**`collectionMAC`**
* Changed `collectionMac` to `members_addtional_card_test` to help decipher the test run collection and the actual collection when it is put into use in the future.

![](https://i.imgur.com/XvRC1ye.png)

**Register Function**
* Realized that the set up in the flowchart (as seen in Figure 2.) is that when an invalid School ID is entered, then the popup would prompt the user an option to register.
* Thus, it is uneccessary to check if the user is already registered because:
    * An invalid school ID will always give an unregistered response.
    * The way registration works is that if you swipe your card at a smart dispenser, the data will be logged into the system automatically.
    * No extra steps needed for registration.
* Added `pop.destroy()` at the end of this function to close the popup window for the user.

![](https://i.imgur.com/ECL8SRX.png)